Skip to content

Commit

Permalink
Reduce number of Geant4 libraries linked in G4VG and elsewhere (#1109)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj committed Feb 15, 2024
1 parent 529f748 commit eb015e7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/celer-g4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ if(CELERITAS_USE_Geant4)
nlohmann_json::nlohmann_json
)
endif()
celeritas_get_g4libs(_g4_libs geometry persistency intercoms run tasking
physicslists interfaces)
list(APPEND LIBRARIES ${_g4_libs})
else()
set(SOURCES
celer-g4.nogeant.cc
Expand Down
64 changes: 60 additions & 4 deletions cmake/CeleritasUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,27 @@ CMake configuration utility functions for Celeritas.

.. command:: celeritas_version_to_hex

Convert a version number to a C-formatted hexadecimal string.
Convert a version number to a C-formatted hexadecimal string::

celeritas_version_to_hex(<var> <version_prefix>)

The code will set a version to zero if ``<version_prefix>_VERSION`` is not
found. If ``<version_prefix>_MAJOR`` is defined it will use that; otherwise
it will try to split the version into major/minor/patch.
The code will set a version to zero if ``<version_prefix>_VERSION`` is not
found. If ``<version_prefix>_MAJOR`` is defined it will use that; otherwise
it will try to split the version into major/minor/patch.

.. command:: celeritas_get_geant_libs

Construct a list of Geant4 libraries for linking by adding a ``Geant4::G4``
prefix and the necessary suffix::

celeritas_get_g4libs(<var> [<lib> ...])

Example::

celeritas_get_g4libs(_g4libs geometry persistency)
target_link_library(foo ${_g4libs})

If Geant4 is unavailable, the result will be empty.

#]=======================================================================]
include_guard(GLOBAL)
Expand Down Expand Up @@ -600,4 +614,46 @@ function(celeritas_version_to_hex var version)
set(${var} "0x${_temp_version}" PARENT_SCOPE)
endfunction()


#-----------------------------------------------------------------------------#

function(celeritas_get_g4libs var)
set(_result)
if(NOT CELERITAS_USE_Geant4)
# No Geant4, no libs
elseif(Geant4_VERSION VERSION_LESS 10.6)
# Old Geant4 doesn't have granularity
set(_result ${Geant4_LIBRARIES})
else()
# Determine the default library extension
if(TARGET Geant4::G4global-static)
set(_g4_static TRUE)
else()
set(_g4_static FALSE)
endif()
if(TARGET Geant4::G4global)
set(_g4_shared TRUE)
else()
set(_g4_shared FALSE)
endif()
if(NOT _g4_static OR (BUILD_SHARED_LIBS AND _g4_shared))
# Use shared if static is unavailable, or if shared is available and we're
# building shared
set(_ext "")
else()
set(_ext "-static")
endif()

foreach(_lib IN LISTS ARGN)
set(_lib Geant4::G4${_lib}${_ext})
if(NOT TARGET "${_lib}")
message(AUTHOR_WARNING "No Geant4 library '${_lib}' exists")
else()
list(APPEND _result "${_lib}")
endif()
endforeach()
endif()
set(${var} "${_result}" PARENT_SCOPE)
endfunction()

#-----------------------------------------------------------------------------#
8 changes: 7 additions & 1 deletion src/accel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

set(SOURCES)
set(PRIVATE_DEPS)
set(PUBLIC_DEPS Celeritas::celeritas Celeritas::corecel ${Geant4_LIBRARIES})
set(PUBLIC_DEPS Celeritas::celeritas Celeritas::corecel)

#-----------------------------------------------------------------------------#
# Main code
Expand Down Expand Up @@ -49,6 +49,12 @@ if(CELERITAS_USE_OpenMP)
list(APPEND PRIVATE_DEPS OpenMP::OpenMP_CXX)
endif()

celeritas_get_g4libs(_g4_private digits_hits run)
list(APPEND PRIVATE_DEPS ${_g4_private})

celeritas_get_g4libs(_g4_public event global track tracking intercoms geometry)
list(APPEND PUBLIC_DEPS ${_g4_public})

#-----------------------------------------------------------------------------#
# Create library
#-----------------------------------------------------------------------------#
Expand Down
4 changes: 3 additions & 1 deletion src/celeritas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ if(CELERITAS_USE_Geant4)
ext/detail/GeantPhysicsList.cc
ext/detail/GeantProcessImporter.cc
)
set(_cg4_libs Celeritas::corecel XercesC::XercesC ${Geant4_LIBRARIES})
celeritas_get_g4libs(_cg4_libs global geometry materials processes run
physicslists tasking)
list(APPEND _cg4_libs Celeritas::corecel XercesC::XercesC)

celeritas_add_object_library(celeritas_geant4 ${_cg4_sources})
celeritas_target_link_libraries(celeritas_geant4 PRIVATE ${_cg4_libs})
Expand Down
6 changes: 4 additions & 2 deletions src/geocel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ if(CELERITAS_USE_VecGeom AND CELERITAS_USE_Geant4)
g4vg/LogicalVolumeConverter.cc
g4vg/SolidConverter.cc
)
set(_cg4vg_libs Celeritas::corecel VecGeom::vecgeom ${Geant4_LIBRARIES})
celeritas_get_g4libs(_cg4vg_libs geometry)
list(APPEND _cg4vg_libs Celeritas::corecel VecGeom::vecgeom)

celeritas_add_object_library(geocel_g4vg ${_cg4vg_sources})
celeritas_target_link_libraries(geocel_g4vg PRIVATE ${_cg4vg_libs})
Expand All @@ -50,7 +51,8 @@ if(CELERITAS_USE_Geant4)
g4/GeantGeoParams.cc
g4/detail/GeantGeoNavCollection.cc
)
set(_cg4_libs Celeritas::corecel XercesC::XercesC ${Geant4_LIBRARIES})
celeritas_get_g4libs(_cg4_libs global geometry persistency intercoms)
list(APPEND _cg4_libs Celeritas::corecel XercesC::XercesC)

celeritas_add_object_library(geocel_geant4 ${_cg4_sources})
celeritas_target_link_libraries(geocel_geant4 PRIVATE ${_cg4_libs})
Expand Down
1 change: 0 additions & 1 deletion src/geocel/g4vg/SolidConverter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <G4EllipticalCone.hh>
#include <G4EllipticalTube.hh>
#include <G4ExtrudedSolid.hh>
#include <G4GDMLWriteStructure.hh>
#include <G4GenericPolycone.hh>
#include <G4GenericTrap.hh>
#include <G4Hype.hh>
Expand Down

0 comments on commit eb015e7

Please sign in to comment.