create_test.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Taken from amazon-freertos repository
  2. #function to create the test executable
  3. function(create_test test_name
  4. test_src
  5. link_list
  6. dep_list
  7. include_list)
  8. set(mocks_dir "${CMAKE_CURRENT_BINARY_DIR}/mocks")
  9. include (CTest)
  10. get_filename_component(test_src_absolute ${test_src} ABSOLUTE)
  11. add_custom_command(OUTPUT ${test_name}_runner.c
  12. COMMAND ruby
  13. ${CMOCK_DIR}/vendor/unity/auto/generate_test_runner.rb
  14. ${MODULE_ROOT_DIR}/tools/cmock/project.yml
  15. ${test_src_absolute}
  16. ${test_name}_runner.c
  17. DEPENDS ${test_src}
  18. )
  19. add_executable(${test_name} ${test_src} ${test_name}_runner.c)
  20. set_target_properties(${test_name} PROPERTIES
  21. COMPILE_FLAG "-O0 -ggdb"
  22. RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/tests"
  23. INSTALL_RPATH_USE_LINK_PATH TRUE
  24. LINK_FLAGS " \
  25. -Wl,-rpath,${CMAKE_BINARY_DIR}/lib \
  26. -Wl,-rpath,${CMAKE_CURRENT_BINARY_DIR}/lib"
  27. )
  28. target_include_directories(${test_name} PUBLIC
  29. ${mocks_dir}
  30. ${include_list}
  31. )
  32. target_link_directories(${test_name} PUBLIC
  33. ${CMAKE_CURRENT_BINARY_DIR}
  34. )
  35. # link all libraries sent through parameters
  36. foreach(link IN LISTS link_list)
  37. target_link_libraries(${test_name} ${link})
  38. endforeach()
  39. # add dependency to all the dep_list parameter
  40. foreach(dependency IN LISTS dep_list)
  41. add_dependencies(${test_name} ${dependency})
  42. target_link_libraries(${test_name} ${dependency})
  43. endforeach()
  44. target_link_libraries(${test_name} -lgcov unity)
  45. target_link_directories(${test_name} PUBLIC
  46. ${CMAKE_CURRENT_BINARY_DIR}/lib
  47. )
  48. add_test(NAME ${test_name}
  49. COMMAND ${CMAKE_BINARY_DIR}/bin/tests/${test_name}
  50. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  51. )
  52. endfunction()
  53. # Run the C preprocessor on target files.
  54. # Takes a CMAKE list of arguments to pass to the C compiler
  55. function(preprocess_mock_list mock_name file_list compiler_args)
  56. set_property(GLOBAL PROPERTY ${mock_name}_processed TRUE)
  57. foreach (target_file IN LISTS file_list)
  58. # Has to be TARGET ALL so the file is pre-processed before CMOCK
  59. # is executed on the file.
  60. add_custom_command(OUTPUT ${target_file}.backup
  61. COMMAND scp ${target_file} ${target_file}.backup
  62. VERBATIM COMMAND ${CMAKE_C_COMPILER} -E ${compiler_args} ${target_file} > ${target_file}.out
  63. )
  64. add_custom_target(pre_${mock_name}
  65. COMMAND mv ${target_file}.out ${target_file}
  66. DEPENDS ${target_file}.backup
  67. )
  68. endforeach()
  69. # Clean up temporary files that were created.
  70. # First we test to see if the backup file still exists. If it does we revert
  71. # the change made to the original file.
  72. foreach (target_file IN LISTS file_list)
  73. add_custom_command(TARGET ${mock_name}
  74. POST_BUILD
  75. COMMAND test ! -e ${target_file}.backup || mv ${target_file}.backup ${target_file}
  76. )
  77. endforeach()
  78. endfunction()
  79. # Generates a mock library based on a module's header file
  80. # places the generated source file in the build directory
  81. # @param mock_name: name of the target name
  82. # @param mock_list list of header files to mock
  83. # @param cmock_config configuration file of the cmock framework
  84. # @param mock_include_list include list for the target
  85. # @param mock_define_list special definitions to control compilation
  86. function(create_mock_list mock_name
  87. mock_list
  88. cmock_config
  89. mock_include_list
  90. mock_define_list)
  91. set(mocks_dir "${CMAKE_CURRENT_BINARY_DIR}/mocks")
  92. add_library(${mock_name} SHARED)
  93. foreach (mock_file IN LISTS mock_list)
  94. get_filename_component(mock_file_abs
  95. ${mock_file}
  96. ABSOLUTE
  97. )
  98. get_filename_component(mock_file_name
  99. ${mock_file}
  100. NAME_WLE
  101. )
  102. get_filename_component(mock_file_dir
  103. ${mock_file}
  104. DIRECTORY
  105. )
  106. add_custom_command (
  107. OUTPUT ${mocks_dir}/mock_${mock_file_name}.c
  108. COMMAND ruby
  109. ${CMOCK_DIR}/lib/cmock.rb
  110. -o${cmock_config} ${mock_file_abs}
  111. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  112. )
  113. target_sources(${mock_name} PUBLIC
  114. ${mocks_dir}/mock_${mock_file_name}.c
  115. )
  116. target_include_directories(${mock_name} PUBLIC
  117. ${mock_file_dir}
  118. )
  119. endforeach()
  120. target_include_directories(${mock_name} PUBLIC
  121. ${mocks_dir}
  122. ${mock_include_list}
  123. )
  124. set_target_properties(${mock_name} PROPERTIES
  125. LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib
  126. POSITION_INDEPENDENT_CODE ON
  127. )
  128. target_compile_definitions(${mock_name} PUBLIC
  129. ${mock_define_list}
  130. )
  131. target_link_libraries(${mock_name} cmock unity)
  132. endfunction()
  133. function(create_real_library target
  134. src_file
  135. real_include_list
  136. mock_name)
  137. add_library(${target} STATIC
  138. ${src_file}
  139. )
  140. target_include_directories(${target} PUBLIC
  141. ${real_include_list}
  142. )
  143. set_target_properties(${target} PROPERTIES
  144. COMPILE_FLAGS "-Wextra -Wpedantic \
  145. -fprofile-arcs -ftest-coverage -fprofile-generate \
  146. -Wno-unused-but-set-variable"
  147. LINK_FLAGS "-fprofile-arcs -ftest-coverage \
  148. -fprofile-generate "
  149. ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib
  150. )
  151. if(NOT(mock_name STREQUAL ""))
  152. add_dependencies(${target} ${mock_name})
  153. target_link_libraries(${target}
  154. -l${mock_name}
  155. -lgcov
  156. )
  157. endif()
  158. endfunction()