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