From 1b7c973a8b338592c706140927b8e09a30edf29d Mon Sep 17 00:00:00 2001
From: Ben Morgan <ben.morgan@warwick.ac.uk>
Date: Mon, 24 Apr 2023 16:53:48 +0100
Subject: [PATCH] Simplify build of GeoModelG4

- Can check standalone status with CMAKE_CURRENT_SOURCE_DIR
- No requirement to quote project name
- Geant4_USE_FILE is not required to use/link Geant4
- Rationalize ordering of compile/include/link commands
---
 GeoModelG4/CMakeLists.txt                | 21 +++++-------------
 GeoModelG4/GeoMaterial2G4/CMakeLists.txt | 21 +++++++++---------
 GeoModelG4/GeoModel2G4/CMakeLists.txt    | 28 +++++++++++-------------
 3 files changed, 29 insertions(+), 41 deletions(-)

diff --git a/GeoModelG4/CMakeLists.txt b/GeoModelG4/CMakeLists.txt
index 37c37439c..c8f4e326e 100644
--- a/GeoModelG4/CMakeLists.txt
+++ b/GeoModelG4/CMakeLists.txt
@@ -3,17 +3,15 @@
 # CMake settings
 cmake_minimum_required(VERSION 3.16...3.26)
 
-# Dummy call to 'project()', needed to set 'PROJECT_SOURCE_DIR'
-project( "GeoModelG4" )
 #Set up the project. Check if we build it with GeoModel or individually
-if( (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) OR GEOMODEL_BUILD_GEOMODELG4_FROM_FULLSIMLIGHT )
+if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) 
     # I am top-level project.
     # Make the root module directory visible to CMake.
     list( APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake )
     # get global GeoModel version
     include( GeoModel-version ) 
     # set the project, with the version taken from the GeoModel parent project
-    project( "GeoModelG4" VERSION ${GeoModel_VERSION} LANGUAGES CXX )
+    project( GeoModelG4 VERSION ${GeoModel_VERSION} LANGUAGES CXX )
     # Define color codes for CMake messages
     include( cmake_colors_defs )
     # Warn the users about what they are doing
@@ -22,31 +20,24 @@ if( (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) OR GEOMODEL_BUILD_GEOMODELG4_
     include( BuildType )
     # Set default build and C++ options
     include( configure_cpp_options )
-    set( CMAKE_FIND_FRAMEWORK "LAST" CACHE STRING
-         "Framework finding behaviour on macOS" )
+    set( CMAKE_FIND_FRAMEWORK "LAST" CACHE STRING "Framework finding behaviour on macOS" )
     # Find the base GeoModel packages, which must be installed on the target system already
     find_package( GeoModelCore REQUIRED ${GeoModel_VERSION} ) 
-    # Find Geant4, if not triggered by FullSimLight, where Geant4 is already found/set.
-    if( NOT GEOMODEL_BUILD_GEOMODELG4_FROM_FULLSIMLIGHT )
-        find_package( Geant4 REQUIRED )
-        include( SetupXercesC )
-    endif()
     # Print Build Info on screen
     include( PrintBuildInfo )
 else()
     # I am called from other projects with add_subdirectory().
     message( STATUS "Building ${PROJECT_NAME} as part of the root GeoModel project.")
     # Set the project
-    project( "GeoModelG4" VERSION ${GeoModel_VERSION} LANGUAGES CXX )
-    # External dependencies:
-    find_package( Geant4 REQUIRED )
+    project( GeoModelG4 VERSION ${GeoModel_VERSION} LANGUAGES CXX )
 endif()
 
+find_package( Geant4 REQUIRED )
+
 # Set up the build of the three libraries of the project.
 add_subdirectory(GeoMaterial2G4)
 add_subdirectory(GeoModel2G4)
 
-
 install(EXPORT GeoMaterial2G4-export FILE GeoModelG4-GeoMaterial2G4.cmake DESTINATION lib/cmake/GeoModelG4)
 install(EXPORT GeoModel2G4-export FILE GeoModelG4-GeoModel2G4.cmake DESTINATION lib/cmake/GeoModelG4)
 install(FILES cmake/GeoModelG4Config.cmake DESTINATION lib/cmake/GeoModelG4)
diff --git a/GeoModelG4/GeoMaterial2G4/CMakeLists.txt b/GeoModelG4/GeoMaterial2G4/CMakeLists.txt
index 6832e1f89..434517536 100644
--- a/GeoModelG4/GeoMaterial2G4/CMakeLists.txt
+++ b/GeoModelG4/GeoMaterial2G4/CMakeLists.txt
@@ -4,7 +4,7 @@
 # Package: GeoMaterial2G4
 ################################################################################
 cmake_minimum_required(VERSION 3.16...3.26)
-project( "GeoMaterial2G4" VERSION ${GeoModel_VERSION} LANGUAGES CXX )
+project(GeoMaterial2G4 VERSION ${GeoModel_VERSION} LANGUAGES CXX)
 
 # Use the GNU install directory names.
 include( GNUInstallDirs )  # it defines CMAKE_INSTALL_LIBDIR
@@ -13,24 +13,23 @@ include( GNUInstallDirs )  # it defines CMAKE_INSTALL_LIBDIR
 file( GLOB SOURCES src/*.cxx )
 file( GLOB HEADERS GeoMaterial2G4/*.h )
 
-# include Geant4 headers
-include(${Geant4_USE_FILE})
-
 # Set target and properties
 add_library( GeoMaterial2G4 SHARED ${HEADERS} ${SOURCES} )
+target_include_directories( GeoMaterial2G4
+  PUBLIC
+    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+    $<INSTALL_INTERFACE:include> )
 target_link_libraries( GeoMaterial2G4 PUBLIC GeoModelCore::GeoModelKernel ${Geant4_LIBRARIES} )
 
-target_include_directories( GeoMaterial2G4 PUBLIC
-   $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
-   $<INSTALL_INTERFACE:include> )
-
 # Set installation of library headers
 set_property( TARGET GeoMaterial2G4 PROPERTY PUBLIC_HEADER ${HEADERS} )
 
 set_target_properties( GeoMaterial2G4 PROPERTIES
-   VERSION ${PROJECT_VERSION}
-   SOVERSION ${PROJECT_VERSION_MAJOR} )
+  VERSION ${PROJECT_VERSION}
+  SOVERSION ${PROJECT_VERSION_MAJOR} )
 
 # new test GeoModelG4
-install( TARGETS GeoMaterial2G4 EXPORT GeoMaterial2G4-export LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+install(TARGETS GeoMaterial2G4
+  EXPORT GeoMaterial2G4-export 
+  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
   PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/GeoMaterial2G4 )
diff --git a/GeoModelG4/GeoModel2G4/CMakeLists.txt b/GeoModelG4/GeoModel2G4/CMakeLists.txt
index 80a3c65c8..143c8f22d 100644
--- a/GeoModelG4/GeoModel2G4/CMakeLists.txt
+++ b/GeoModelG4/GeoModel2G4/CMakeLists.txt
@@ -7,7 +7,7 @@
 cmake_minimum_required(VERSION 3.16...3.26)
 
 # Declare the package name
-project( "GeoModel2G4" VERSION ${GeoModel_VERSION}  LANGUAGES CXX )
+project(GeoModel2G4 VERSION ${GeoModel_VERSION}  LANGUAGES CXX)
 
 ##########################################################
 # NOTE! The original package also needs this Athena stuff:
@@ -28,24 +28,20 @@ include( GNUInstallDirs )  # it defines CMAKE_INSTALL_LIBDIR
 file( GLOB SOURCES src/*.cxx )
 file( GLOB HEADERS GeoModel2G4/*.h )
 
-# include Geant4 headers
-include(${Geant4_USE_FILE})
-
 # set the path where the G4 extensions are found (e.g., the LArCustomSolid ATLAS extension)
 set(PATHEXTENSIONS ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR} )
-add_definitions (-DG4EXTENSION_SOLID_DIR=${PATHEXTENSIONS})
 message("==> NOTE: G4 solid extensions will be searched in: ${PATHEXTENSIONS}. If you need custom extensions check if this is the right path. If not, set the right path by setting the environmental variable 'G4EXTENSION_SOLID_DIR' before running the application that uses the GeoModel2G4 library (e.g., 'export G4EXTENSION_SOLID_DIR=/usr/local/Cellar/geomodel-extension-atlaslar/5.4.0/lib') " )
 
 # Set target and properties
-add_library( GeoModel2G4 SHARED ${HEADERS} ${SOURCES} )
-target_link_libraries( GeoModel2G4
-  PUBLIC  ${GEANT4_LIBRARIES}
-  PRIVATE GeoMaterial2G4 GeoModelCore::GeoModelKernel )
-
-target_include_directories( GeoModel2G4 SYSTEM PUBLIC ${GEANT4_INCLUDE_DIRS} )
-target_include_directories( GeoModel2G4 PUBLIC
-   $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
-   $<INSTALL_INTERFACE:include> )
+add_library(GeoModel2G4 SHARED ${HEADERS} ${SOURCES})
+target_compile_definitions(GeoModel2G4 PRIVATE G4EXTENSION_SOLID_DIR=${PATHEXTENSIONS})
+target_include_directories(GeoModel2G4
+  PUBLIC
+    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+    $<INSTALL_INTERFACE:include> )
+target_link_libraries(GeoModel2G4
+  PUBLIC  ${Geant4_LIBRARIES}
+  PRIVATE GeoMaterial2G4 GeoModelCore::GeoModelKernel)
 
 # Set installation of library headers
 set_property( TARGET GeoModel2G4 PROPERTY PUBLIC_HEADER ${HEADERS} )
@@ -55,5 +51,7 @@ set_target_properties( GeoModel2G4 PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION ${PROJECT_VERSION_MAJOR} )
 # Install the library.
-install( TARGETS GeoModel2G4 EXPORT GeoModel2G4-export LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+install( TARGETS GeoModel2G4
+  EXPORT GeoModel2G4-export 
+  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
   PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/GeoModel2G4 )
-- 
GitLab