|
| 1 | +# This file defines macros which can be used to setup |
| 2 | +# new cmake project tests without introducing excessive boilerplate. |
| 3 | + |
| 4 | +# declare_add_subdirectory_test(<name of test>): |
| 5 | +# Use when the test depends on ldserverapi via add_subdirectory. |
| 6 | + |
| 7 | +# declare_find_package_test(<test name>): |
| 8 | +# Use when the test depends on ldserverapi via find_package. |
| 9 | + |
| 10 | +# add_build_step(<test name>): |
| 11 | +# By default, the declare_* macros result in a test where "cmake -DSOMEVARIABLE=WHATEVER .." |
| 12 | +# (the cmake configure step) is invoked. This may be sufficient for a particular test, |
| 13 | +# for example testing that the configure step fails. |
| 14 | +# If the test should also invoke "cmake --build .", use this macro. |
| 15 | + |
| 16 | +# require_configure_failure(<test name>): |
| 17 | +# Asserts that the cmake configure step should fail. For example, this would |
| 18 | +# happen if a required version of a dependency couldn't be satisfied with find_package. |
| 19 | + |
| 20 | +# require_build_failure(<test name>): |
| 21 | +# Asserts that the cmake build step should fail. |
| 22 | + |
| 23 | +macro(declare_add_subdirectory_test name) |
| 24 | + set(test_prefix ${name}) |
| 25 | + |
| 26 | + add_test( |
| 27 | + NAME ${test_prefix}_configure |
| 28 | + COMMAND |
| 29 | + ${CMAKE_COMMAND} |
| 30 | + # Since project/CMakeLists.txt is going to call add_subdirectory(), it needs to know where |
| 31 | + # the SDK's project is (which is actually a couple directories above this particular file; not normally the case.) |
| 32 | + # The variable name is arbitrary. |
| 33 | + -DLDSERVERAPI_SOURCE_DIR=${PROJECT_SOURCE_DIR} |
| 34 | + # Do not setup all of the SDK's testing machinery, which would normally happen when calling add_subdirectory. |
| 35 | + -DBUILD_TESTING=OFF |
| 36 | + # Forward variables from the SDK project to the test project, if set. |
| 37 | + $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}> |
| 38 | + $<$<BOOL:${CURL_LIBRARY}>:-DCURL_LIBRARY=${CURL_LIBRARY}> |
| 39 | + $<$<BOOL:${CURL_INCLUDE_DIR}>:-DCURL_INCLUDE_DIR=${CURL_INCLUDE_DIR}> |
| 40 | + $<$<BOOL:${PCRE_LIBRARY}>:-DPCRE_LIBRARY=${PCRE_LIBRARY}> |
| 41 | + $<$<BOOL:${PCRE_INCLUDE_DIR}>:-DPCRE_INCLUDE_DIR=${PCRE_INCLUDE_DIR}> |
| 42 | + ${CMAKE_CURRENT_SOURCE_DIR}/project |
| 43 | + ) |
| 44 | + |
| 45 | + set_tests_properties(${test_prefix}_configure |
| 46 | + PROPERTIES |
| 47 | + FIXTURES_SETUP ${test_prefix} |
| 48 | + # Forward along the CC and CXX environment variables, because clang11 CI build uses them. |
| 49 | + ENVIRONMENT "CC=${CMAKE_C_COMPILER};CXX=${CMAKE_CXX_COMPILER}" |
| 50 | + ) |
| 51 | +endmacro() |
| 52 | + |
| 53 | +macro(require_configure_failure name) |
| 54 | + set_tests_properties(${name}_configure PROPERTIES WILL_FAIL TRUE) |
| 55 | +endmacro() |
| 56 | + |
| 57 | +macro(require_build_failure name) |
| 58 | + set_tests_properties(${name}_build PROPERTIES WILL_FAIL TRUE) |
| 59 | +endmacro() |
| 60 | + |
| 61 | +macro(add_build_step name) |
| 62 | + # Setup a 'test' to perform the cmake build step. |
| 63 | + add_test( |
| 64 | + NAME ${name}_build |
| 65 | + COMMAND ${CMAKE_COMMAND} --build . |
| 66 | + ) |
| 67 | + |
| 68 | + set_tests_properties(${name}_build |
| 69 | + PROPERTIES |
| 70 | + FIXTURES_REQUIRED ${name} |
| 71 | + ) |
| 72 | +endmacro() |
| 73 | + |
| 74 | +macro(declare_find_package_test name) |
| 75 | + # This test assumes that the SDK has been installed at CMAKE_INSTALL_PREFIX. |
| 76 | + set(test_prefix ${name}) |
| 77 | + |
| 78 | + add_test( |
| 79 | + NAME ${test_prefix}_configure |
| 80 | + COMMAND |
| 81 | + ${CMAKE_COMMAND} |
| 82 | + # Since project/CMakeLists.txt uses find_package(), it needs to know where to find |
| 83 | + # ldserverapiConfig.cmake. That can be found where the SDK is installed, which is CMAKE_INSTALL_PREFIX. |
| 84 | + -DCMAKE_PREFIX_PATH=${CMAKE_INSTALL_PREFIX} |
| 85 | + # Forward variables from the SDK project to the test project, if set. |
| 86 | + $<$<BOOL:${CMAKE_GENERATOR_PLATFORM}>:-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}> |
| 87 | + $<$<BOOL:${CURL_LIBRARY}>:-DCURL_LIBRARY=${CURL_LIBRARY}> |
| 88 | + $<$<BOOL:${CURL_INCLUDE_DIR}>:-DCURL_INCLUDE_DIR=${CURL_INCLUDE_DIR}> |
| 89 | + $<$<BOOL:${PCRE_LIBRARY}>:-DPCRE_LIBRARY=${PCRE_LIBRARY}> |
| 90 | + $<$<BOOL:${PCRE_INCLUDE_DIR}>:-DPCRE_INCLUDE_DIR=${PCRE_INCLUDE_DIR}> |
| 91 | + ${CMAKE_CURRENT_SOURCE_DIR}/project |
| 92 | + ) |
| 93 | + |
| 94 | + set_tests_properties(${test_prefix}_configure |
| 95 | + PROPERTIES |
| 96 | + FIXTURES_SETUP ${test_prefix} |
| 97 | + # Forward along the CC and CXX environment variables, because clang11 CI build uses them. |
| 98 | + ENVIRONMENT "CC=${CMAKE_C_COMPILER};CXX=${CMAKE_CXX_COMPILER}" |
| 99 | + ) |
| 100 | +endmacro() |
0 commit comments