From 71e3b1d46f8c0701bc5a203de6ada793e9097dba Mon Sep 17 00:00:00 2001 From: Tobit Flatscher <53856473+2b-t@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:32:08 +0000 Subject: [PATCH] refactor: Clean up CMake file --- CMakeLists.txt | 158 ++++++++++++++++++++++++++----------------------- 1 file changed, 84 insertions(+), 74 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c357b1..07b80db 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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!") @@ -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 @@ -56,6 +61,9 @@ target_include_directories(lbt_obj PUBLIC $ $ ) +target_link_libraries(lbt_obj PUBLIC + ${LBT_LIBRARIES} +) add_executable(lbt $ @@ -65,70 +73,72 @@ target_include_directories(lbt PUBLIC $ $ ) -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 - $ - $ - ) - add_executable(run_lbt_tests - $ - $ - 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 + $ + $ + ) + 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 + $ + $ + 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()