11
2- CMAKE_MINIMUM_REQUIRED (VERSION 2.8.12 )
2+ CMAKE_MINIMUM_REQUIRED (VERSION 3.11 )
33
44# COMMON SETTINGS
55PROJECT (OneDSolver)
@@ -9,6 +9,7 @@ ENABLE_LANGUAGE(C CXX)
99OPTION (BUILD_SV_INSTALLER "Build SimVascular Installer" OFF )
1010OPTION (buildPy "Build Python Interface" OFF )
1111OPTION (buildDocs "Build Documentation" OFF )
12+ OPTION (ENABLE_UNIT_TEST "Enable unit tests" ON )
1213SET (sparseSolverType "skyline" CACHE STRING "Use Sparse Solver" )
1314SET_PROPERTY (CACHE sparseSolverType PROPERTY STRINGS skyline superlu csparse )
1415
@@ -74,7 +75,7 @@ ENDIF()
7475# FILE(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
7576
7677# COMPILER FLAGS
77- SET (CMAKE_CXX_FLAGS "-g -m64 -O3 -std=c++0x -fPIC" )
78+ SET (CMAKE_CXX_FLAGS "-g -m64 -O3 -std=c++20 -fPIC" )
7879
7980# PLACE EXECUTABLE IN BIN FOLDER
8081SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /bin)
@@ -109,3 +110,72 @@ ENDIF()
109110if (BUILD_SV_INSTALLER)
110111 add_subdirectory ("${CMAKE_SOURCE_DIR} /Distribution" )
111112ENDIF ()
113+
114+ # Unit tests and Google Test
115+ if (ENABLE_UNIT_TEST)
116+
117+ # Link pthread on ubuntu20
118+ find_package (Threads REQUIRED )
119+
120+ # Install Google Test
121+ include (FetchContent )
122+ FetchContent_Declare (
123+ googletest
124+ URL https://github.com/google/googletest/archive/refs/heads/main.zip
125+ DOWNLOAD_EXTRACT_TIMESTAMP TRUE
126+ )
127+ FetchContent_MakeAvailable (googletest)
128+
129+ enable_testing ()
130+ include (GoogleTest )
131+
132+ set (TESTBIN_DIRECTORY ${CMAKE_BINARY_DIR} /testbin)
133+
134+ # Copy test files into the build directory so tests can access them
135+ file (GLOB TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR} /Tests/UnitTests/TestFiles/*" )
136+ foreach (TEST_FILE ${TEST_FILES} )
137+ # Print the file being copied for debugging
138+ file (COPY ${TEST_FILE} DESTINATION ${TESTBIN_DIRECTORY} /TestFiles/ )
139+ endforeach ()
140+
141+ # For now, we're filtering out "main" so we can include all the
142+ # other source files in the same way as the main executable.
143+ #
144+ # It would probably be better if we refactored to avoid including
145+ # main in the overall list of source files. Instead, we could just
146+ # include main only when we're building the executable. That could
147+ # be a good first refactoring to avoid this hacky post-processing
148+ # step.
149+ set (SRC_C_FOR_TESTS ${SRC_C} )
150+ list (FILTER SRC_C_FOR_TESTS EXCLUDE REGEX "Source/main.cxx" )
151+
152+ set (SRC_H_FOR_TESTS ${SRC_H} )
153+ list (FILTER SRC_H_FOR_TESTS EXCLUDE REGEX "Source/main.h" )
154+
155+ # Find all .cpp files in the UnitTests folder and subfolders
156+ file (GLOB TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR} /Tests/UnitTests/*.cxx" )
157+
158+ # Combine all test files into a single list
159+ add_executable (UnitTestsExecutable ${TEST_SOURCES} ${SRC_C_FOR_TESTS} ${SRC_H_FOR_TESTS} )
160+
161+ # For some solvers, we may need to link against additional libraries here.
162+ # When we bump up against unit tests that require that, we should consider
163+ # changing the strategy for how we generate executables in cmakelists to
164+ # handle both use cases.
165+ target_link_libraries (UnitTestsExecutable gtest gtest_main pthread )
166+
167+ # It's likely we'll need to include additional directories for some
168+ # versions of the unit tests. That can be updated when/if we update
169+ # the general strategy.
170+ target_include_directories (UnitTestsExecutable PRIVATE ${SRCS_DIR} )
171+
172+ # Set the output directory for the executable
173+ set_target_properties (UnitTestsExecutable PROPERTIES
174+ RUNTIME_OUTPUT_DIRECTORY ${TESTBIN_DIRECTORY}
175+ )
176+
177+ # Add the test to CTest, we're setting the working directory to testbin
178+ # because we're copying the input files there for now
179+ add_test (NAME UnitTests COMMAND UnitTestsExecutable WORKING_DIRECTORY ${TESTBIN_DIRECTORY} )
180+
181+ endif ()
0 commit comments