Skip to content

Commit e4d463e

Browse files
committed
Unify and streamline test configuration in CMakeLists.txt using helper functions; centralize reusable logic in cmake/functions.cmake. Remove example_processes and example_threads task-specific configurations.
1 parent b6356f9 commit e4d463e

4 files changed

Lines changed: 140 additions & 154 deletions

File tree

cmake/functions.cmake

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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()

tasks/CMakeLists.txt

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(parallel_programming_course LANGUAGES C CXX)
3+
14
message(STATUS "Student's tasks")
25

3-
project("parallel_programming_course")
4-
set(exec_func_tests "ppc_func_tests")
5-
set(exec_perf_tests "ppc_perf_tests")
6-
7-
# Init func tests executable files
8-
set(list_of_exec_tests "")
9-
if (USE_FUNC_TESTS)
10-
add_executable(${exec_func_tests} "${CMAKE_CURRENT_SOURCE_DIR}/common/runners/functional.cpp")
11-
list(APPEND list_of_exec_tests ${exec_func_tests})
12-
endif (USE_FUNC_TESTS)
13-
14-
# Init perf tests executable files
15-
if (USE_PERF_TESTS)
16-
add_executable(${exec_perf_tests} "${CMAKE_CURRENT_SOURCE_DIR}/common/runners/performance.cpp")
17-
list(APPEND list_of_exec_tests ${exec_perf_tests})
18-
endif (USE_PERF_TESTS)
19-
20-
SUBDIRLIST(subdirs ${CMAKE_CURRENT_LIST_DIR})
21-
foreach(subd ${subdirs})
22-
if (subd STREQUAL "common")
23-
continue()
24-
endif ()
25-
26-
set(SETTINGS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${subd}/settings.json")
27-
add_compile_definitions(PPC_SETTINGS_${subd}=\"${SETTINGS_PATH}\")
28-
add_compile_definitions(PPC_ID_${subd}=\"${subd}\")
29-
30-
add_subdirectory(${subd})
31-
endforeach()
6+
# ——— Testing options ————————————————————————————————————————
7+
option(USE_FUNC_TESTS "Enable functional tests" OFF)
8+
option(USE_PERF_TESTS "Enable performance tests" OFF)
9+
10+
# Test runner executables
11+
set(FUNC_TEST_EXEC ppc_func_tests)
12+
set(PERF_TEST_EXEC ppc_perf_tests)
3213

33-
foreach (exec_func ${list_of_exec_tests})
34-
enable_testing()
35-
add_test(NAME ${exec_func} COMMAND ${exec_func})
14+
# ——— Global compile definitions —————————————————————————————————————
15+
add_compile_definitions(
16+
PATH_TO_PPC_PROJECT="${PROJECT_SOURCE_DIR}"
17+
)
3618

37-
# Install the executable
38-
install(TARGETS ${exec_func} RUNTIME DESTINATION bin)
39-
endforeach ()
19+
# ——— Include helper scripts ——————————————————————————————————————
20+
include(${CMAKE_SOURCE_DIR}/cmake/functions.cmake)
4021

41-
# Install the library
42-
install(TARGETS ${name_lib} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib)
22+
# ——— Initialize test executables —————————————————————————————————————
23+
ppc_add_test(${FUNC_TEST_EXEC} common/runners/functional.cpp USE_FUNC_TESTS)
24+
ppc_add_test(${PERF_TEST_EXEC} common/runners/performance.cpp USE_PERF_TESTS)
25+
26+
# ——— List of implementations ————————————————————————————————————————
27+
set(IMPLEMENTATIONS all mpi omp seq stl tbb)
28+
29+
# ——— Configure each subproject —————————————————————————————————————
30+
file(GLOB subdirs RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/*")
31+
foreach(sub IN LISTS subdirs)
32+
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${sub}" AND NOT sub STREQUAL "common")
33+
ppc_configure_subproject(${sub})
34+
endif()
35+
endforeach()
4336

44-
add_compile_definitions(PATH_TO_PPC_PROJECT="${CMAKE_SOURCE_DIR}")
37+
# ——— Install library target ——————————————————————————————————————
38+
install(TARGETS ${name_lib}
39+
ARCHIVE DESTINATION lib
40+
LIBRARY DESTINATION lib
41+
)

tasks/example_processes/CMakeLists.txt

Lines changed: 0 additions & 58 deletions
This file was deleted.

tasks/example_threads/CMakeLists.txt

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)