Skip to content

Commit f485f76

Browse files
committed
Refactor CMake: unify solver build, exclude unit tests from coverage
* Built solver sources into a shared object library (solver_objs) so they can be reused by both the main executable and unit tests. This avoids recompiling solver sources twice when ENABLE_UNIT_TEST is enabled. * Excluded unit tests from code coverage by removing coverage flags from the run_all_unit_tests target (-fno-profile-arcs, -fno-test-coverage). This prevents .gcda files from being generated for test code, ensuring coverage only reflects solver sources.
1 parent 0109e52 commit f485f76

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

Code/Source/solver/CMakeLists.txt

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,20 @@ if(USE_TRILINOS)
241241
link_directories(${Trilinos_TPL_LIBRARY_DIRS})
242242
endif()
243243

244-
simvascular_add_executable(${SV_MULTIPHYSICS_EXE}
245-
SRCS ${CSRCS}
246-
DEV_SCRIPT_NAME "mysvmultiphysics"
247-
INSTALL_SCRIPT_NAME "svmultiphysics"
248-
INSTALL_COMP CoreExecutables
249-
INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR})
244+
# simvascular_add_executable(${SV_MULTIPHYSICS_EXE}
245+
# SRCS ${CSRCS}
246+
# DEV_SCRIPT_NAME "mysvmultiphysics"
247+
# INSTALL_SCRIPT_NAME "svmultiphysics"
248+
# INSTALL_COMP CoreExecutables
249+
# INSTALL_DESTINATION ${SV_INSTALL_RUNTIME_DIR})
250+
251+
# Build solver code (all sources except main.cpp) into a library
252+
list(REMOVE_ITEM CSRCS "main.cpp")
253+
add_library(solver_objs OBJECT ${CSRCS})
254+
add_library(solver STATIC $<TARGET_OBJECTS:solver_objs>)
255+
256+
# Solver main executable
257+
add_executable(${SV_MULTIPHYSICS_EXE} main.cpp $<TARGET_OBJECTS:solver_objs>)
250258

251259
target_link_libraries(${SV_MULTIPHYSICS_EXE}
252260
${GLOBAL_LIBRARIES}
@@ -284,15 +292,12 @@ if(ENABLE_COVERAGE)
284292
find_program(LCOV lcov REQUIRED)
285293
find_program(GENHTML genhtml REQUIRED)
286294

287-
# get parent of project source dir
288-
get_filename_component(PARENT_SOURCE_DIR "${PROJECT_SOURCE_DIR}" DIRECTORY)
289-
290295
# add coverage target
291296
add_custom_target(coverage
292297
# gather data
293298
COMMAND ${LCOV} --directory . --capture --output-file coverage.info
294299
# exclude externals
295-
COMMAND ${LCOV} --remove coverage.info -o coverage.info '/usr/*' '/opt/*' "${PROJECT_SOURCE_DIR}/ThirdParty/*" "${PARENT_SOURCE_DIR}/tests/unitTests/*" '/Library/*' 'v1/*'
300+
COMMAND ${LCOV} --remove coverage.info -o coverage.info '/usr/*' '/opt/*' "${PROJECT_SOURCE_DIR}/ThirdParty/*" '/Library/*' 'v1/*'
296301
# generate report
297302
COMMAND ${GENHTML} --demangle-cpp -o coverage coverage.info
298303
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
@@ -331,22 +336,34 @@ if(ENABLE_UNIT_TEST)
331336
message(STATUS "TEST_SOURCE: ${TEST_SOURCE}")
332337
endforeach()
333338

334-
list(REMOVE_ITEM CSRCS "main.cpp")
335-
list(APPEND CSRCS ${TEST_SOURCES})
339+
# list(REMOVE_ITEM CSRCS "main.cpp")
340+
# list(APPEND CSRCS ${TEST_SOURCES})
336341

337342
# include source files (same as what svMultiPhysics does except for main.cpp)
338-
add_executable(run_all_unit_tests ${CSRCS})
339-
340-
if(USE_TRILINOS)
341-
target_link_libraries(run_all_unit_tests ${Trilinos_LIBRARIES} ${Trilinos_TPL_LIBRARIES})
343+
# add_executable(run_all_unit_tests ${CSRCS})
344+
add_executable(run_all_unit_tests ${TEST_SOURCES})
345+
346+
# Remove coverage flags from unit tests
347+
if(ENABLE_COVERAGE)
348+
target_compile_options(run_all_unit_tests PRIVATE -U_FORTIFY_SOURCE)
349+
target_compile_options(run_all_unit_tests PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-O0>)
350+
target_compile_options(run_all_unit_tests PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-g>)
351+
# explicitly disable coverage
352+
target_compile_options(run_all_unit_tests PRIVATE -fno-profile-arcs -fno-test-coverage)
353+
target_link_options(run_all_unit_tests PRIVATE -fno-profile-arcs -fno-test-coverage)
342354
endif()
343355

344-
if(USE_PETSC)
345-
target_link_libraries(run_all_unit_tests ${PETSC_LIBRARY_DIRS})
346-
endif()
356+
# if(USE_TRILINOS)
357+
# target_link_libraries(run_all_unit_tests ${Trilinos_LIBRARIES} ${Trilinos_TPL_LIBRARIES})
358+
# endif()
359+
360+
# if(USE_PETSC)
361+
# target_link_libraries(run_all_unit_tests ${PETSC_LIBRARY_DIRS})
362+
# endif()
347363

348364
# libraries
349365
target_link_libraries(run_all_unit_tests
366+
solver
350367
${GLOBAL_LIBRARIES}
351368
${INTELRUNTIME_LIBRARIES}
352369
${ZLIB_LIBRARY}
@@ -358,16 +375,27 @@ if(ENABLE_UNIT_TEST)
358375
${TINYXML_LIBRARY_NAME}
359376
${SV_LIB_LINEAR_SOLVER_NAME}${SV_MPI_NAME_EXT}
360377
${VTK_LIBRARIES}
361-
)
362-
363-
# link Google Test
364-
target_link_libraries(
365-
run_all_unit_tests
366378
gtest
367379
GTest::gtest_main
368-
pthread # link pthread on ubuntu20
380+
pthread
369381
)
370382

383+
if(USE_TRILINOS)
384+
target_link_libraries(run_all_unit_tests ${Trilinos_LIBRARIES} ${Trilinos_TPL_LIBRARIES})
385+
endif()
386+
387+
if(USE_PETSC)
388+
target_link_libraries(run_all_unit_tests ${PETSC_LIBRARY_DIRS})
389+
endif()
390+
391+
# link Google Test
392+
# target_link_libraries(
393+
# run_all_unit_tests
394+
# gtest
395+
# GTest::gtest_main
396+
# pthread # link pthread on ubuntu20
397+
# )
398+
371399
# gtest_discover_tests(runUnitTest)
372400
add_test(NAME all_unit_tests COMMAND run_all_unit_tests)
373401

0 commit comments

Comments
 (0)