Skip to content

Commit

Permalink
Fix weird edge cases when trying to find_package(Celeritas) (#1586)
Browse files Browse the repository at this point in the history
* Set CMake policies in CeleritasConfig

* Export cached "hints" as cached variables

* Xerces-C workaround no longer needed

This was a confluence of a policy overwrite from PTL, CMake policy
CMP0102 (where `mark_as_advanced` writes an empty cache variable if a
local variable is named), and `find_path` which has some funny logic for
pulling from cache/local values (and maybe CMP0126 as well).

* Normalized QUIET/REQUIRED in configure script

* Improve thrust finding

* Revert "Improve thrust finding"

This reverts commit 02e4f17.

* Require thrust if thrust found

---------

Co-authored-by: Ben Morgan <[email protected]>
  • Loading branch information
sethrj and drbenmorgan authored Jan 20, 2025
1 parent c9e643a commit 81ff8a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -667,12 +667,19 @@ foreach(_key
)
set(_val "${${_key}}")
if(_val)
set(_cache_val "$CACHE{${_key}}")
if(_cache_val)
list(APPEND CELERITAS_EXPORT_VARIABLES
"set(${_key} \"${_cache_val}\" CACHE PATH \"Set by CeleritasConfig.cmake\")"
)
else()
list(APPEND CELERITAS_EXPORT_VARIABLES
"if(NOT DEFINED ${_key})"
" set(${_key} \"${_val}\")"
"endif()"
)
endif()
endif()
endforeach()
list(JOIN CELERITAS_EXPORT_VARIABLES "\n" CELERITAS_EXPORT_VARIABLES)

Expand Down
20 changes: 11 additions & 9 deletions cmake/CeleritasConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ endif()
#-----------------------------------------------------------------------------#
# Dependencies
#-----------------------------------------------------------------------------#
cmake_policy(PUSH)
cmake_policy(VERSION 3.10...3.31)

include(CMakeFindDependencyMacro)

if(CELERITAS_USE_CUDA)
find_dependency(CUDAToolkit REQUIRED QUIET)
find_dependency(CUDAToolkit REQUIRED)

if(NOT CMAKE_CUDA_COMPILER_LOADED)
# enable_language only really works in top-level CMake directory, not even subprojects
Expand All @@ -67,15 +69,15 @@ elseif(CELERITAS_USE_HIP)
endif()

if(CELERITAS_USE_Geant4)
# FIXME: needed for Geant4 11.1+ to avoid causing errors in VecGeom's
# FindXercesC
find_dependency(XercesC)
# Geant4 calls `include_directories` for CLHEP :( which is not what we want!
# Save and restore include directories around the call -- even though as a
# standalone project Celeritas will never have directory-level includes
get_directory_property(_include_dirs INCLUDE_DIRECTORIES)
find_dependency(Geant4 @Geant4_VERSION@ REQUIRED)
set_directory_properties(PROPERTIES INCLUDE_DIRECTORIES "${_include_dirs}")

# Re-establish policy if Geant4 11.1/.2's PTL overwrote it
cmake_policy(VERSION 3.10...3.31)
endif()

if(CELERITAS_USE_HepMC3)
Expand All @@ -97,18 +99,16 @@ if(CELERITAS_USE_PNG)
endif()

if(CELERITAS_USE_Python)
set(_version 3.6)
find_dependency(Python ${_version} REQUIRED COMPONENTS Interpreter)
find_dependency(Python 3.6 REQUIRED COMPONENTS Interpreter)
unset(_components)
unset(_version)
endif()

if(CELERITAS_USE_ROOT)
find_dependency(ROOT @ROOT_VERSION@ REQUIRED)
endif()

if(CELERITAS_USE_Thrust)
find_dependency(Thrust)
find_dependency(Thrust REQUIRED)
endif()

if(CELERITAS_USE_VecGeom)
Expand All @@ -126,14 +126,16 @@ if(CELERITAS_BUILD_TESTS)
if(CMAKE_VERSION VERSION_LESS 3.20)
# First look for standard CMake installation
# (automatically done for CMake >= 3.20)
find_dependency(GTest @GTest_VERSION@ QUIET NO_MODULE)
find_dependency(GTest @GTest_VERSION@ NO_MODULE)
endif()
if(NOT GTest_FOUND)
# If not found, try again
find_dependency(GTest)
endif()
endif()

cmake_policy(POP)

#-----------------------------------------------------------------------------#
# Targets
#-----------------------------------------------------------------------------#
Expand Down

0 comments on commit 81ff8a7

Please sign in to comment.