From 0729cbc02146662ccebd3289bf30f7a9aa2304a6 Mon Sep 17 00:00:00 2001
From: "lintao@ihep.ac.cn" <lintao@ihep.ac.cn>
Date: Thu, 20 Jun 2024 17:37:23 +0000
Subject: [PATCH] Optimize cmake and reduce the find_package.

---
 CMakeLists.txt                            | 16 +++++++++++-----
 Detector/DetCEPCv4/CMakeLists.txt         | 12 ------------
 Detector/DetCRD/CMakeLists.txt            | 12 ------------
 Detector/DetDriftChamber/CMakeLists.txt   | 11 -----------
 Detector/DetEcalMatrix/CMakeLists.txt     | 12 ------------
 Detector/DetSegmentation/CMakeLists.txt   |  5 -----
 Generator/CMakeLists.txt                  |  3 ---
 Simulation/DetSimAna/CMakeLists.txt       |  2 --
 Simulation/DetSimCore/CMakeLists.txt      |  6 ------
 Simulation/DetSimDedx/CMakeLists.txt      | 11 -----------
 Simulation/DetSimFastModel/CMakeLists.txt |  4 ----
 Simulation/DetSimGeom/CMakeLists.txt      |  3 ---
 Simulation/DetSimSD/CMakeLists.txt        |  3 ---
 cmake/CEPCSWDependencies.cmake            | 15 ++++++++++++---
 14 files changed, 23 insertions(+), 92 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 88d59037..b72981f4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -6,11 +6,6 @@ project(CEPCSW)
 include(cmake/CEPCSWEnv.cmake)
 #
 
-find_package(ROOT COMPONENTS RIO Tree)
-
-find_package(Gaudi)
-find_package(Eigen3)
-
 include(GNUInstallDirs)
 include(CTest)
 
@@ -19,6 +14,17 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
     "Install path prefix, prepended onto install directories." FORCE )
 endif()
 
+set(CMAKE_SKIP_BUILD_RPATH TRUE)
+set(CMAKE_EXECUTABLE_SUFFIX .exe)
+
+# Put bin/lib/include under CMAKE_BINARY_DIR
+set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
+include_directories(${CMAKE_BINARY_DIR}/include)
+
+# set Install/lib
+set(CMAKE_INSTALL_LIBDIR lib)
+
 # Set up C++ Standard
 # ``-DCMAKE_CXX_STANDARD=<standard>`` when invoking CMake
 set(CMAKE_CXX_STANDARD 17 CACHE STRING "")
diff --git a/Detector/DetCEPCv4/CMakeLists.txt b/Detector/DetCEPCv4/CMakeLists.txt
index 03b897ae..c7520576 100644
--- a/Detector/DetCEPCv4/CMakeLists.txt
+++ b/Detector/DetCEPCv4/CMakeLists.txt
@@ -4,18 +4,6 @@
 # Based on package: lcgeo
 ################################################################################
 
-find_package(DD4hep COMPONENTS DDRec DDG4 DDParsers REQUIRED)
-# find_package(DD4hep)
-find_package(Geant4)
-include(${Geant4_USE_FILE})
-
-set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH}  ${DD4hep_ROOT}/cmake )
-include( DD4hep )
-
-find_package(ROOT COMPONENTS MathCore GenVector Geom REQUIRED)
-
-# install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/compact DESTINATION Detector/DetCEPCv4)
-
 gaudi_add_module(DetCEPCv4
                  SOURCES src/tracker/VXD04_geo.cpp
                          src/tracker/FTD_Simple_Staggered_geo.cpp
diff --git a/Detector/DetCRD/CMakeLists.txt b/Detector/DetCRD/CMakeLists.txt
index f7628431..e271c24d 100644
--- a/Detector/DetCRD/CMakeLists.txt
+++ b/Detector/DetCRD/CMakeLists.txt
@@ -3,18 +3,6 @@
 #   CEPC Reference Detector (CRD)
 ################################################################################
 
-find_package(DD4hep COMPONENTS DDRec DDG4 DDParsers REQUIRED)
-# find_package(DD4hep)
-find_package(Geant4)
-include(${Geant4_USE_FILE})
-
-set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH}  ${DD4hep_ROOT}/cmake )
-include( DD4hep )
-
-find_package(ROOT COMPONENTS MathCore GenVector Geom REQUIRED)
-
-# install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/compact DESTINATION Detector/DetCRD)
-
 gaudi_add_module(DetCRD
                  SOURCES src/Calorimeter/CRDEcal_v01.cpp
                          src/Calorimeter/LongCrystalBarBarrelCalorimeter32Polygon_v01.cpp
diff --git a/Detector/DetDriftChamber/CMakeLists.txt b/Detector/DetDriftChamber/CMakeLists.txt
index fbf8e01c..234a9470 100644
--- a/Detector/DetDriftChamber/CMakeLists.txt
+++ b/Detector/DetDriftChamber/CMakeLists.txt
@@ -3,17 +3,6 @@
 # Based on package: lcgeo
 ################################################################################
 
-find_package(DD4hep COMPONENTS DDRec DDG4 DDParsers REQUIRED)
-find_package(Geant4)
-include(${Geant4_USE_FILE})
-
-set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH}  ${DD4hep_ROOT}/cmake )
-include( DD4hep )
-
-find_package(ROOT COMPONENTS MathCore GenVector Geom REQUIRED)
-
-# install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/compact DESTINATION Detector/DetDriftChamber)
-
 gaudi_add_module(DetDriftChamber
                  SOURCES src/driftchamber/DriftChamber.cpp
 		 LINK DetSegmentation
diff --git a/Detector/DetEcalMatrix/CMakeLists.txt b/Detector/DetEcalMatrix/CMakeLists.txt
index 10b59035..24abbe6f 100644
--- a/Detector/DetEcalMatrix/CMakeLists.txt
+++ b/Detector/DetEcalMatrix/CMakeLists.txt
@@ -3,18 +3,6 @@
 # Based on package: lcgeo
 ################################################################################
 
-
-find_package(DD4hep COMPONENTS DDRec DDG4 DDParsers REQUIRED)
-find_package(Geant4)
-include(${Geant4_USE_FILE})
-
-set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH}  ${DD4hep_ROOT}/cmake )
-include( DD4hep )
-
-find_package(ROOT COMPONENTS MathCore GenVector Geom REQUIRED)
-
-# TODO: how to handle 
-# install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/compact DESTINATION Detector/DetEcalMatrix)
 gaudi_add_module(DetEcalMatrix
                  SOURCES src/calorimeter/EcalMatrix.cpp
 		 LINK ${DD4hep_COMPONENT_LIBRARIES} 
diff --git a/Detector/DetSegmentation/CMakeLists.txt b/Detector/DetSegmentation/CMakeLists.txt
index e6273def..b3907a95 100644
--- a/Detector/DetSegmentation/CMakeLists.txt
+++ b/Detector/DetSegmentation/CMakeLists.txt
@@ -2,11 +2,6 @@
 ##Package : DetSegmentation
 #################################################################################
 
-find_package(DD4hep COMPONENTS DDRec DDG4 DDParsers REQUIRED)
-set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH}  ${DD4hep_ROOT}/cmake )
-
-include(DD4hep)
-
 gaudi_add_library(DetSegmentation
                   SOURCES src/GridDriftChamber.cpp
                   LINK Gaudi::GaudiKernel
diff --git a/Generator/CMakeLists.txt b/Generator/CMakeLists.txt
index c227254a..54c3bde8 100644
--- a/Generator/CMakeLists.txt
+++ b/Generator/CMakeLists.txt
@@ -1,8 +1,5 @@
 ########################################
 
-find_package(Geant4 REQUIRED)
-include(${Geant4_USE_FILE})
-
 gaudi_add_module(GenAlgo
                  SOURCES src/IGenTool.cpp 
                          src/GenAlgo.cpp 
diff --git a/Simulation/DetSimAna/CMakeLists.txt b/Simulation/DetSimAna/CMakeLists.txt
index 733c9686..f3daa7f6 100644
--- a/Simulation/DetSimAna/CMakeLists.txt
+++ b/Simulation/DetSimAna/CMakeLists.txt
@@ -1,5 +1,3 @@
-find_package(Geant4 REQUIRED ui_all vis_all)
-include(${Geant4_USE_FILE})
 
 gaudi_add_module(DetSimAna
                  SOURCES src/Edm4hepWriterAnaElemTool.cpp
diff --git a/Simulation/DetSimCore/CMakeLists.txt b/Simulation/DetSimCore/CMakeLists.txt
index e64cd3c4..c9ddf2d8 100644
--- a/Simulation/DetSimCore/CMakeLists.txt
+++ b/Simulation/DetSimCore/CMakeLists.txt
@@ -1,10 +1,4 @@
 
-find_package(Geant4 REQUIRED ui_all vis_all)
-include(${Geant4_USE_FILE})
-
-
-message(" Geant4_LIBRARIES: ${Geant4_LIBRARIES}")
-
 gaudi_add_module(DetSimCore 
                  SOURCES src/DetSimAlg.cpp
                          src/DetSimSvc.cpp
diff --git a/Simulation/DetSimDedx/CMakeLists.txt b/Simulation/DetSimDedx/CMakeLists.txt
index 867ff20c..5397cd40 100644
--- a/Simulation/DetSimDedx/CMakeLists.txt
+++ b/Simulation/DetSimDedx/CMakeLists.txt
@@ -1,13 +1,4 @@
 
-find_package(Geant4 REQUIRED ui_all vis_all)
-include(${Geant4_USE_FILE})
-find_package(Garfield REQUIRED)
-message(Garfield::Garfield)
-message("libonnxruntime ${OnnxRuntime_LIBRARY}")
-message("libonnxruntime include ${OnnxRuntime_INCLUDE_DIR}")
-find_package(OnnxRuntime REQUIRED)
-
-message("libonnxruntime ${OnnxRuntime_LIBRARY}")
 gaudi_add_module(DetSimDedx
                  SOURCES src/DummyDedxSimTool.cpp
                          src/BetheBlochEquationDedxSimTool.cpp
@@ -23,8 +14,6 @@ gaudi_add_module(DetSimDedx
                       k4FWCore::k4FWCore
                       Garfield::Garfield
                       OnnxRuntime
-                      #${OnnxRuntime_LIBRARY}
-                      #/cvmfs/sft.cern.ch/lcg/views/LCG_103/x86_64-centos7-gcc11-opt/lib/libonnxruntime.so
                       ${CLHEP_LIBRARIES}
  
 )
diff --git a/Simulation/DetSimFastModel/CMakeLists.txt b/Simulation/DetSimFastModel/CMakeLists.txt
index 30dced2b..33ab24c7 100644
--- a/Simulation/DetSimFastModel/CMakeLists.txt
+++ b/Simulation/DetSimFastModel/CMakeLists.txt
@@ -1,8 +1,4 @@
 
-find_package(Geant4 REQUIRED ui_all vis_all)
-include(${Geant4_USE_FILE})
-
-
 gaudi_add_module(DetSimFastModel
                  SOURCES src/DummyFastSimG4Tool.cpp
                          src/DummyFastSimG4Model.cpp
diff --git a/Simulation/DetSimGeom/CMakeLists.txt b/Simulation/DetSimGeom/CMakeLists.txt
index aec48800..62caab08 100644
--- a/Simulation/DetSimGeom/CMakeLists.txt
+++ b/Simulation/DetSimGeom/CMakeLists.txt
@@ -1,7 +1,4 @@
 
-find_package(Geant4 REQUIRED ui_all vis_all)
-include(${Geant4_USE_FILE})
-
 gaudi_add_module(DetSimGeom 
                  SOURCES src/WorldDetElemTool.cpp
                          src/AnExampleDetElemTool.cpp
diff --git a/Simulation/DetSimSD/CMakeLists.txt b/Simulation/DetSimSD/CMakeLists.txt
index f3a9618a..0c2d5a84 100644
--- a/Simulation/DetSimSD/CMakeLists.txt
+++ b/Simulation/DetSimSD/CMakeLists.txt
@@ -1,7 +1,4 @@
 
-find_package(Geant4 REQUIRED ui_all vis_all)
-include(${Geant4_USE_FILE})
-
 gaudi_add_library(DetSimSDLib
                   SOURCES src/Geant4Hits.cpp
                           src/DDG4SensitiveDetector.cpp
diff --git a/cmake/CEPCSWDependencies.cmake b/cmake/CEPCSWDependencies.cmake
index f14a91c5..ed2b35cc 100644
--- a/cmake/CEPCSWDependencies.cmake
+++ b/cmake/CEPCSWDependencies.cmake
@@ -20,22 +20,31 @@ Find all the dependencies here, so in each package user don't need to find the p
 - CKF
 #]]
 
+##############################################################################
+# import Gaudi at beginning
+##############################################################################
+find_package(Gaudi REQUIRED)
+
+
 find_package(CLHEP REQUIRED;CONFIG)
 find_package(DD4hep COMPONENTS DDCore DDG4 DDParsers DDRec REQUIRED)
+include(DD4hep) # include necessary macro
 find_package(EDM4HEP REQUIRED)
-#find_package(Garfield REQUIRED)
+find_package(Eigen3)
 find_package(Garfield)
 find_package(Geant4 REQUIRED ui_all vis_all)
+include(${Geant4_USE_FILE})
 find_package(GEAR REQUIRED)
+find_package(GenFit)
 find_package(GSL REQUIRED)
 find_package(HepMC)
 find_package(k4FWCore REQUIRED)
 find_package(LCContent REQUIRED)
 find_package(LCIO REQUIRED)
+find_package(OnnxRuntime REQUIRED)
 find_package(PandoraSDK REQUIRED)
 find_package(podio REQUIRED)
-find_package(ROOT COMPONENTS EG Graf Graf3d Gpad MathCore Net RIO Tree TreePlayer REQUIRED)
-find_package(GenFit)
+find_package(ROOT COMPONENTS EG Geom GenVector Graf Graf3d Gpad MathCore Net RIO Tree TreePlayer REQUIRED)
 
 if (CEPCSW_USE_SYSTEM_CKF_BELLE)
   message("Try to use an existing installation of CKF BELLE")
-- 
GitLab