Skip to content

Commit

Permalink
Automatically download googletest as an external if tests are enabled
Browse files Browse the repository at this point in the history
This also fixes the target name to be `GTest::gtest` instead of the older deprecated `GTest::GTest` provided by FindGTest.CMake.
  • Loading branch information
sethrj committed May 27, 2024
1 parent 696c004 commit ffd031a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
23 changes: 6 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ if(NOT CELERITAS_USE_CUDA)
celeritas_optional_language(HIP)
endif()

# Required dependencies: find or fetch inside `external`
celeritas_find_or_external_package(nlohmann_json 3.7.0)
set(CELERITAS_USE_JSON ON)

# Optional dependencies
celeritas_optional_package(Geant4 "Enable Geant4 adapter tools")
celeritas_optional_package(HepMC3 "Enable HepMC3 event record reader")
option(CELERITAS_USE_JSON "Enable JSON I/O " ON)
celeritas_optional_package(MPI "Enable distributed memory parallelism")
celeritas_optional_package(OpenMP "Enable CPU shared-memory parallelism")
celeritas_optional_package(Python "Use Python to generate and preprocess")
Expand Down Expand Up @@ -317,8 +314,10 @@ if(CELERITAS_USE_HepMC3)
set(HepMC3_LIBRARIES HepMC3::HepMC3)
endif()

if(CELERITAS_USE_JSON AND NOT CELERITAS_EXTERNAL_nlohmann_json)
if(NOT nlohmann_json_FOUND)
if(CELERITAS_USE_JSON)
# Required dependencies: find or fetch inside `external`
celeritas_find_or_external_package(nlohmann_json 3.7.0)
if(NOT CELERITAS_EXTERNAL_nlohmann_json AND NOT nlohmann_json_FOUND)
find_package(nlohmann_json 3.7.0 REQUIRED)
endif()
set(nlohmann_json_LIBRARIES nlohmann_json::nlohmann_json)
Expand Down Expand Up @@ -459,17 +458,7 @@ if(CELERITAS_BUILD_DOCS)
endif()

if(CELERITAS_BUILD_TESTS)
# TODO: download and build GTest as a subproject if not available
if(NOT GTest_FOUND)
find_package(GTest 1.10)
endif()
if(NOT GTest_FOUND)
celeritas_error_incompatible_option(
"Googletest (GTest) is required for testing but was not found"
CELERITAS_BUILD_TESTS
OFF
)
endif()
celeritas_find_or_external_package(GTest 1.10)
endif()

#----------------------------------------------------------------------------#
Expand Down
45 changes: 45 additions & 0 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,41 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
#-----------------------------------------------------------------------------#

# Save some variables to see if badly behaving packages change them
set(_checked_cmake_variables
BUILD_SHARED_LIBS CMAKE_CXX_STANDARD CMAKE_CXX_EXTENSIONS
)
if(NOT WIN32)
# RPath doesn't matter on windows, and some CI externalas change them
list(APPEND _checked_cmake_variables
CMAKE_INSTALL_RPATH CMAKE_INSTALL_RPATH_USE_LINK_PATH
)
endif()
foreach(_var IN LISTS _checked_cmake_variables)
set(_saved_${_var} "${${_var}}")
endforeach()

#-----------------------------------------------------------------------------#
# GTest
#-----------------------------------------------------------------------------#

if(CELERITAS_EXTERNAL_GTest)
FetchContent_Declare(
GTest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)

set(BUILD_GMOCK OFF) # For compatibility
set(INSTALL_GTEST OFF)
set(gtest_hide_internal_symbols ON)
FetchContent_MakeAvailable(GTest)
endif()

#-----------------------------------------------------------------------------#
# nlohmann_json
#-----------------------------------------------------------------------------#

if(CELERITAS_EXTERNAL_nlohmann_json)
FetchContent_Declare(
nlohmann_json
Expand All @@ -15,3 +50,13 @@ if(CELERITAS_EXTERNAL_nlohmann_json)
endif()

#-----------------------------------------------------------------------------#
foreach(_var IN LISTS _checked_variables)
set(_orig "${_saved_${_var}}")
set(_new "${${_var}}")
if(NOT "${_new}" STREQUAL "${_orig}")
message(SEND_ERROR "An externally fetched package changed the value of ${_var} from ${_orig} to ${_new}. We are saving the original value as a cache variable: please reconfigure.")
set(${_var} "${_orig}" CACHE STRING "Reset to Celeritas default" FORCE)
endif()
endforeach()

#-----------------------------------------------------------------------------#
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ endif()
celeritas_add_library(testcel_harness ${_harness_sources})
target_compile_features(testcel_harness PUBLIC cxx_std_17)
celeritas_target_link_libraries(testcel_harness
PUBLIC Celeritas::corecel GTest::GTest
PUBLIC Celeritas::corecel GTest::gtest
PRIVATE ${nlohmann_json_LIBRARIES}
)
celeritas_target_include_directories(testcel_harness
Expand Down

0 comments on commit ffd031a

Please sign in to comment.