|
1 | | -cmake_minimum_required(VERSION 3.9) |
| 1 | +cmake_minimum_required(VERSION 3.20) |
2 | 2 |
|
3 | 3 | enable_language(CXX) |
4 | 4 |
|
| 5 | +option(BUILD_CUDA_TESTS "Include tests for use of the printf library with CUDA host-side and device-side code" OFF) |
| 6 | + |
5 | 7 | set(test_targets autotest) |
6 | 8 | if (NOT ALIAS_STANDARD_FUNCTION_NAMES) |
7 | 9 | list(APPEND test_targets test_suite) |
| 10 | + set(targets_needing_config_h test_suite) |
8 | 11 | endif() |
9 | 12 |
|
| 13 | + |
10 | 14 | option(TEST_WITH_NON_STANDARD_FORMAT_STRINGS "Include tests using non-standard-compliant format strings?" ON) |
11 | 15 | # ... don't worry, we'll suppress the compiler warnings for those. |
12 | 16 |
|
| 17 | +if(BUILD_CUDA_TESTS) |
| 18 | + enable_language(CUDA) |
| 19 | + list(APPEND CMAKE_CUDA_FLAGS "--extended-lambda --expt-relaxed-constexpr -Xcudafe --display_error_number") |
| 20 | + cmake_policy(SET CMP0104 OLD) |
| 21 | + include(FindCUDA/select_compute_arch) |
| 22 | + list(APPEND cuda_test_targets cuda_test_suite_host cuda_test_suite_device) |
| 23 | + if((NOT DEFINED CUDA_ARCH_FLAGS) OR ("${CUDA_ARCH_FLAGS}" STREQUAL "")) |
| 24 | + cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS_1 Auto) |
| 25 | + set(CUDA_ARCH_FLAGS ${CUDA_ARCH_FLAGS} CACHE STRING "CUDA -gencode parameters") |
| 26 | + string(REPLACE ";" " " CUDA_ARCH_FLAGS_STR "${CUDA_ARCH_FLAGS}") |
| 27 | + else() |
| 28 | + set(CUDA_ARCH_FLAGS_STR "${CUDA_ARCH_FLAGS}") |
| 29 | + endif() |
| 30 | + set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${CUDA_ARCH_FLAGS_STR}") |
| 31 | + |
| 32 | + foreach(tgt ${cuda_test_targets}) |
| 33 | + string(REPLACE "cuda_" "cuda/" source_file_prefix ${tgt}) |
| 34 | + add_executable(${tgt} "${source_file_prefix}.cu") |
| 35 | + target_include_directories(${tgt} PRIVATE "$<BUILD_INTERFACE:${GENERATED_INCLUDE_DIR}>") |
| 36 | + set_target_properties( |
| 37 | + ${tgt} |
| 38 | + PROPERTIES |
| 39 | + CUDA_STANDARD 11 |
| 40 | + CUDA_STANDARD_REQUIRED YES |
| 41 | + CUDA_EXTENSIONS NO |
| 42 | + ) |
| 43 | + if (TEST_WITH_NON_STANDARD_FORMAT_STRINGS) |
| 44 | + target_compile_definitions(${tgt} PRIVATE TEST_WITH_NON_STANDARD_FORMAT_STRINGS) |
| 45 | + endif() |
| 46 | + add_test(NAME "${PROJECT_NAME}.${tgt}" COMMAND "${tgt}") |
| 47 | + endforeach() |
| 48 | +endif() |
| 49 | + |
| 50 | +if(BUILD_CUDA_TESTS) |
| 51 | + if (NOT ALIAS_STANDARD_FUNCTION_NAMES) |
| 52 | + target_link_libraries(cuda_test_suite_device printf_cuda) |
| 53 | + set_target_properties(cuda_test_suite_device PROPERTIES CUDA_SEPARABLE_COMPILATION YES) |
| 54 | + list(APPEND targets_needing_config_h cuda_test_suite_host) |
| 55 | +# target_compile_options(cuda_test_suite_device PRIVATE -G -g) |
| 56 | + endif() |
| 57 | +endif() |
| 58 | + |
13 | 59 | foreach(tgt ${test_targets}) |
14 | 60 | add_executable(${tgt} "${tgt}.cpp") |
15 | 61 | set_target_properties( |
@@ -75,20 +121,21 @@ foreach(tgt ${test_targets}) |
75 | 121 | target_compile_options(${tgt} PRIVATE -ffat-lto-objects) |
76 | 122 | endif() |
77 | 123 | endif() |
78 | | - |
79 | 124 | endforeach() |
80 | 125 |
|
81 | 126 | if (NOT ALIAS_STANDARD_FUNCTION_NAMES) |
82 | | - # These two lines are necessary, since the test suite does not actually use the |
83 | | - # compiled library - it includes the library's source .c file; and that means we |
84 | | - # need to include the generated config.h file. |
85 | | - target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H) |
86 | | - target_include_directories( |
87 | | - test_suite |
88 | | - PRIVATE |
89 | | - "${GENERATED_INCLUDE_DIR}" |
90 | | - "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../src/>" |
91 | | - ) |
| 127 | + # These following is necessary, since thee targets applying printf_config.h |
| 128 | + # do not actually use the compiled library - they includes the library's source .c file; |
| 129 | + # so we need to make sure it's accessible to them |
| 130 | + foreach(tgt ${targets_needing_config_h}) |
| 131 | + target_compile_definitions(test_suite PRIVATE PRINTF_INCLUDE_CONFIG_H) |
| 132 | + target_include_directories( |
| 133 | + ${tgt} |
| 134 | + PRIVATE |
| 135 | + "${GENERATED_INCLUDE_DIR}" |
| 136 | + "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../src/>" |
| 137 | + ) |
| 138 | + endforeach() |
92 | 139 | add_test( |
93 | 140 | NAME "${PROJECT_NAME}.test_suite" |
94 | 141 | COMMAND "test_suite" # ${TEST_RUNNER_PARAMS} |
|
0 commit comments