Skip to content

Commit

Permalink
refactor: Clean up CMake file
Browse files Browse the repository at this point in the history
  • Loading branch information
2b-t committed Nov 3, 2024
1 parent e73e17a commit 71e3b1d
Showing 1 changed file with 84 additions and 74 deletions.
158 changes: 84 additions & 74 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
cmake_minimum_required(VERSION 3.1)
project(lbt)

if(CMAKE_COMPILER_IS_GNUCC)
option(ENABLE_COVERAGE "Enable coverage reporting for GCC/Clang" False)
option(BUILD_TESTING "Build unit tests" OFF)

if(ENABLE_COVERAGE)
message(STATUS "Building with coverage reporting for GCC/Clang.")
add_compile_options(--coverage -O0)
link_libraries(--coverage)
else ()
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
endif()
else()
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
if(CMAKE_COMPILER_IS_GNUCXX)
option(ENABLE_COVERAGE "Enable coverage reporting for GCC/Clang" False)
endif()

set(CMAKE_BUILD_TYPE Release)
# The vast majority of code is written for C++17 but certain tests require C++20
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Libraries: VTK, nlohmann_json, OpenMP (optional)
find_package(VTK REQUIRED)
if(NOT VTK_FOUND)
message(FATAL_ERROR "VTK library not found!")
Expand All @@ -45,6 +35,21 @@ else()
message(WARNING "OpenMP not found: Multi-threading disabled!")
endif()

if(ENABLE_COVERAGE)
message(STATUS "Building with coverage reporting for GCC/Clang.")
add_compile_options(--coverage -O0)
link_libraries(--coverage)
else()
message(STATUS "Compiling in release.")
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
endif()

set(LBT_LIBRARIES ${VTK_LIBRARIES} nlohmann_json::nlohmann_json)
if(OpenMP_CXX_FOUND)
set(LBT_LIBRARIES ${LBT_LIBRARIES} OpenMP::OpenMP_CXX)
endif()

add_library(lbt_obj OBJECT
src/common/disclaimer.cpp
src/common/openmp_manager.cpp
Expand All @@ -56,6 +61,9 @@ target_include_directories(lbt_obj PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(lbt_obj PUBLIC
${LBT_LIBRARIES}
)

add_executable(lbt
$<TARGET_OBJECTS:lbt_obj>
Expand All @@ -65,70 +73,72 @@ target_include_directories(lbt PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
set(LBT_LIBRARIES ${VTK_LIBRARIES} nlohmann_json::nlohmann_json)
if(OpenMP_CXX_FOUND)
set(LBT_LIBRARIES ${LBT_LIBRARIES} OpenMP::OpenMP_CXX)
endif()
target_link_libraries(lbt_obj ${LBT_LIBRARIES})
target_link_libraries(lbt ${LBT_LIBRARIES})


# Testing
enable_testing()
target_link_libraries(lbt PUBLIC
${LBT_LIBRARIES}
)

include(GoogleTest)
if(NOT GTest_FOUND)
set(LBT_TEST_LIBRARIES ${LBT_LIBRARIES} gtest pthread)

add_library(lbt_test_obj OBJECT
test/common/disclaimer_test.cpp
test/common/openmp_manager_test.cpp
test/common/output_utilities_test.cpp
test/common/stream_manager_test.cpp
test/common/timer_test.cpp
test/common/tuple_utilities_test.cpp
test/common/type_definitions_test.cpp
test/common/vtk_utilities_test.cpp
test/continuums/simple_continuum_test.cpp
test/continuums/vtk_continuum_test.cpp
test/geometry/vtk_import_test.cpp
test/lattices/lattices_test.cpp
test/materials/materials_test.cpp
test/math/math_test.cpp
test/populations/aa_population_test.cpp
test/populations/ab_population_test.cpp
test/populations/boundaries/guo_test.cpp
test/populations/boundaries/normal_test.cpp
test/populations/boundaries/orientation_test.cpp
test/populations/indexing/aa_pattern_test.cpp
test/populations/indexing/indexing_test.cpp
test/simulation/simulation_test.cpp
test/test_utilities/test_utilities_test.cpp
test/units/characteristic_numbers_test.cpp
test/units/literals_test.cpp
test/units/units_test.cpp
test/converter_test.cpp
)
target_include_directories(lbt_test_obj PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
add_executable(run_lbt_tests
$<TARGET_OBJECTS:lbt_obj>
$<TARGET_OBJECTS:lbt_test_obj>
test/run_tests.cpp
)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
enable_testing()

target_compile_definitions(run_lbt_tests PUBLIC NDEBUG)
include(GoogleTest)
if(NOT GTest_FOUND)
set(LBT_TEST_LIBRARIES ${LBT_LIBRARIES} gtest pthread)

gtest_discover_tests(run_lbt_tests
TEST_SUFFIX .noArgs
TEST_LIST noArgsTests
)
set_tests_properties(${noArgsTests} PROPERTIES TIMEOUT 10)
add_library(lbt_test_obj OBJECT
test/common/disclaimer_test.cpp
test/common/openmp_manager_test.cpp
test/common/output_utilities_test.cpp
test/common/stream_manager_test.cpp
test/common/timer_test.cpp
test/common/tuple_utilities_test.cpp
test/common/type_definitions_test.cpp
test/common/vtk_utilities_test.cpp
test/continuums/simple_continuum_test.cpp
test/continuums/vtk_continuum_test.cpp
test/geometry/vtk_import_test.cpp
test/lattices/lattices_test.cpp
test/materials/materials_test.cpp
test/math/math_test.cpp
test/populations/aa_population_test.cpp
test/populations/ab_population_test.cpp
test/populations/boundaries/guo_test.cpp
test/populations/boundaries/normal_test.cpp
test/populations/boundaries/orientation_test.cpp
test/populations/indexing/aa_pattern_test.cpp
test/populations/indexing/indexing_test.cpp
test/simulation/simulation_test.cpp
test/test_utilities/test_utilities_test.cpp
test/units/characteristic_numbers_test.cpp
test/units/literals_test.cpp
test/units/units_test.cpp
test/converter_test.cpp
)
target_include_directories(lbt_test_obj PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(lbt_test_obj PUBLIC
${LBT_TEST_LIBRARIES}
)

target_link_libraries(lbt_test_obj ${LBT_TEST_LIBRARIES})
target_link_libraries(run_lbt_tests ${LBT_TEST_LIBRARIES})
else()
message(FATAL_ERROR "GoogleTest not found!")
add_executable(run_lbt_tests
$<TARGET_OBJECTS:lbt_obj>
$<TARGET_OBJECTS:lbt_test_obj>
test/run_tests.cpp
)
target_compile_definitions(run_lbt_tests PUBLIC
NDEBUG
)
target_link_libraries(run_lbt_tests PUBLIC
${LBT_TEST_LIBRARIES}
)
gtest_discover_tests(run_lbt_tests
TEST_SUFFIX .noArgs
TEST_LIST noArgsTests
)
set_tests_properties(${noArgsTests} PROPERTIES TIMEOUT 10)
else()
message(FATAL_ERROR "GoogleTest not found!")
endif()
endif()

0 comments on commit 71e3b1d

Please sign in to comment.