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