|
| 1 | +# ——— Helper function to add & register tests ————————————————————————— |
| 2 | +function(ppc_add_test test_name test_src USE_FLAG) |
| 3 | + if(${USE_FLAG}) |
| 4 | + add_executable(${test_name} "${PROJECT_SOURCE_DIR}/${test_src}") |
| 5 | + enable_testing() |
| 6 | + add_test(NAME ${test_name} COMMAND ${test_name}) |
| 7 | + install(TARGETS ${test_name} RUNTIME DESTINATION bin) |
| 8 | + endif() |
| 9 | +endfunction() |
| 10 | + |
| 11 | +# Function to configure tests |
| 12 | +function(add_tests test_flag exec_target subdir) |
| 13 | + if(${test_flag}) |
| 14 | + # Gather all source files under tests/<subdir> |
| 15 | + file(GLOB_RECURSE src_files |
| 16 | + "${TEST_DIR}/${subdir}/*.cpp" |
| 17 | + "${TEST_DIR}/${subdir}/*.cxx" |
| 18 | + "${TEST_DIR}/${subdir}/*.cc" |
| 19 | + ) |
| 20 | + target_sources(${exec_target} PRIVATE ${src_files}) |
| 21 | + list(APPEND TEST_EXECUTABLES ${exec_target}) |
| 22 | + set(TEST_EXECUTABLES "${TEST_EXECUTABLES}" PARENT_SCOPE) |
| 23 | + endif() |
| 24 | +endfunction() |
| 25 | + |
| 26 | +# ============================================================================ |
| 27 | +# Function: setup_implementation |
| 28 | +# - NAME: implementation sub‐directory name (e.g. “mpi”) |
| 29 | +# - PROJ_NAME: project base name |
| 30 | +# - BASE_DIR: root source directory |
| 31 | +# - TESTS: list of test executables to link against |
| 32 | +# ============================================================================ |
| 33 | +function(setup_implementation) |
| 34 | + # parse named args: NAME, PROJ_NAME, BASE_DIR; multi‐value: TESTS |
| 35 | + cmake_parse_arguments( |
| 36 | + SETUP |
| 37 | + "" # no plain options |
| 38 | + "NAME;PROJ_NAME;BASE_DIR" |
| 39 | + "TESTS" |
| 40 | + ${ARGN} |
| 41 | + ) |
| 42 | + |
| 43 | + # skip if impl dir doesn't exist |
| 44 | + set(IMP_DIR "${SETUP_BASE_DIR}/${SETUP_NAME}") |
| 45 | + if(NOT EXISTS "${IMP_DIR}") |
| 46 | + return() |
| 47 | + endif() |
| 48 | + message(STATUS " -- ${SETUP_NAME}") |
| 49 | + |
| 50 | + # collect sources |
| 51 | + file(GLOB_RECURSE CPP_SOURCES "${IMP_DIR}/src/*.cpp") |
| 52 | + file(GLOB_RECURSE ALL_SOURCES |
| 53 | + "${IMP_DIR}/include/*.h" |
| 54 | + "${IMP_DIR}/include/*.hpp" |
| 55 | + "${IMP_DIR}/src/*.cpp" |
| 56 | + ) |
| 57 | + |
| 58 | + # create library (STATIC if .cpp exist, otherwise INTERFACE) |
| 59 | + set(LIB_NAME "${SETUP_PROJ_NAME}_${SETUP_NAME}") |
| 60 | + if(CPP_SOURCES) |
| 61 | + add_library(${LIB_NAME} STATIC ${ALL_SOURCES}) |
| 62 | + else() |
| 63 | + add_library(${LIB_NAME} INTERFACE ${ALL_SOURCES}) |
| 64 | + endif() |
| 65 | + |
| 66 | + # link core module |
| 67 | + target_link_libraries(${LIB_NAME} PUBLIC core_module_lib) |
| 68 | + |
| 69 | + # and link into each enabled test executable |
| 70 | + foreach(test_exec ${SETUP_TESTS}) |
| 71 | + target_link_libraries(${test_exec} PUBLIC ${LIB_NAME}) |
| 72 | + endforeach() |
| 73 | +endfunction() |
| 74 | + |
| 75 | +# Function to configure each subproject |
| 76 | +function(ppc_configure_subproject SUBDIR) |
| 77 | + # Module-specific compile-time definitions |
| 78 | + add_compile_definitions( |
| 79 | + PPC_SETTINGS_${SUBDIR}="${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/settings.json" |
| 80 | + PPC_ID_${SUBDIR}="${SUBDIR}" |
| 81 | + ) |
| 82 | + |
| 83 | + # Switch project context to the subproject |
| 84 | + project(${SUBDIR}) |
| 85 | + |
| 86 | + # Directory with tests and list of test executables (populated by setup_implementation) |
| 87 | + set(TEST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/tests") |
| 88 | + set(TEST_EXECUTABLES "") |
| 89 | + |
| 90 | + # Register functional and performance test runners |
| 91 | + add_tests(USE_FUNC_TESTS ${FUNC_TEST_EXEC} functional) |
| 92 | + add_tests(USE_PERF_TESTS ${PERF_TEST_EXEC} performance) |
| 93 | + |
| 94 | + message(STATUS "${SUBDIR}") |
| 95 | + |
| 96 | + # List of implementations to configure |
| 97 | + foreach(IMPL IN LISTS IMPLEMENTATIONS) |
| 98 | + setup_implementation( |
| 99 | + NAME ${IMPL} |
| 100 | + PROJ_NAME ${SUBDIR} |
| 101 | + TESTS "${TEST_EXECUTABLES}" |
| 102 | + BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}" |
| 103 | + ) |
| 104 | + endforeach() |
| 105 | +endfunction() |
0 commit comments