1option(BUILD_TESTING off) 2 3include(CMakeParseArguments) 4enable_testing() 5 6if(NOT TARGET check) 7 add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -C ${CMAKE_CFG_INTDIR}) 8endif() 9 10if(NOT TARGET tests) 11 add_custom_target(tests COMMENT "Build all tests.") 12 add_dependencies(check tests) 13endif() 14 15function(bcm_mark_as_test) 16 foreach(TEST_TARGET ${ARGN}) 17 if (NOT BUILD_TESTING) 18 set_target_properties(${TEST_TARGET} 19 PROPERTIES EXCLUDE_FROM_ALL TRUE 20 ) 21 endif() 22 add_dependencies(tests ${TEST_TARGET}) 23 endforeach() 24endfunction(bcm_mark_as_test) 25 26function(bcm_add_test) 27 set(options COMPILE_ONLY WILL_FAIL) 28 set(oneValueArgs NAME) 29 set(multiValueArgs SOURCES CONTENT) 30 31 cmake_parse_arguments(PARSE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 32 33 # TODO: Check if name exists 34 35 set(SOURCES ${PARSE_SOURCES}) 36 if(PARSE_CONTENT) 37 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/generated-${PARSE_NAME}.cpp "${PARSE_CONTENT}") 38 set(SOURCES ${CMAKE_CURRENT_BINARY_DIR}/generated-${PARSE_NAME}.cpp) 39 endif() 40 41 if(PARSE_COMPILE_ONLY) 42 if(PARSE_WILL_FAIL) 43 add_library(_${PARSE_NAME}_TEST_FAIL STATIC EXCLUDE_FROM_ALL ${SOURCES}) 44 add_custom_target(${PARSE_NAME} 45 COMMAND ${CMAKE_COMMAND} --build . --target _${PARSE_NAME}_TEST_FAIL --config $<CONFIGURATION> 46 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 47 add_test(NAME ${PARSE_NAME} 48 COMMAND ${CMAKE_COMMAND} --build . --target _${PARSE_NAME}_TEST_FAIL --config $<CONFIGURATION> 49 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 50 set_tests_properties(${PARSE_NAME} PROPERTIES WILL_FAIL TRUE) 51 else() 52 add_library(${PARSE_NAME} STATIC ${SOURCES}) 53 bcm_mark_as_test(${PARSE_NAME}) 54 endif() 55 else() 56 add_executable (${PARSE_NAME} ${SOURCES}) 57 bcm_mark_as_test(${PARSE_NAME}) 58 if(WIN32) 59 add_test(NAME ${PARSE_NAME} WORKING_DIRECTORY ${LIBRARY_OUTPUT_PATH} COMMAND ${PARSE_NAME}${CMAKE_EXECUTABLE_SUFFIX}) 60 else() 61 add_test(NAME ${PARSE_NAME} COMMAND ${PARSE_NAME}) 62 endif() 63 if(WILL_FAIL) 64 set_tests_properties(${PARSE_NAME} PROPERTIES WILL_FAIL TRUE) 65 endif() 66 endif() 67endfunction(bcm_add_test) 68 69function(bcm_test_header) 70 set(options STATIC) 71 set(oneValueArgs NAME HEADER) 72 set(multiValueArgs) 73 74 cmake_parse_arguments(PARSE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 75 76 if(PARSE_STATIC) 77 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/header-main-include-${PARSE_NAME}.cpp 78 "#include <${PARSE_HEADER}>\nint main() {}\n" 79 ) 80 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/header-static-include-${PARSE_NAME}.cpp 81 "#include <${PARSE_HEADER}>\n" 82 ) 83 bcm_add_test(NAME header-static-include-${PARSE_NAME} SOURCES 84 ${CMAKE_CURRENT_BINARY_DIR}/header-main-include-${PARSE_NAME}.cpp 85 ${CMAKE_CURRENT_BINARY_DIR}/header-static-include-${PARSE_NAME}.cpp 86 ) 87 else() 88 bcm_add_test(NAME header-include-${PARSE_NAME} CONTENT 89 "#include <${PARSE_HEADER}>\nint main() {}\n" 90 ) 91 endif() 92endfunction(bcm_test_header) 93