From b17a698fc4c4bff0dbac56fe2376d7da1b074649 Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Thu, 22 Feb 2024 16:36:29 +0100
Subject: [PATCH] Fix compilation problem with external package Hal

The compilation of some analysis files did crash due to missing header
files when not setting the environment variables SIMPATH and FAIRROOTPATH.
The problem was that the external package Hal internally uses this
environment variables and changed compilation targets if they are found.
Add a patch which fixes this but the change should go upstream.
---
 external/Hal.patch        | 105 ++++++++++++++++++++++++++++++++++++++
 external/InstallHal.cmake |  15 +++---
 2 files changed, 113 insertions(+), 7 deletions(-)
 create mode 100644 external/Hal.patch

diff --git a/external/Hal.patch b/external/Hal.patch
new file mode 100644
index 0000000000..b9b1213ca7
--- /dev/null
+++ b/external/Hal.patch
@@ -0,0 +1,105 @@
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 9376018..c87ecb1 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -15,7 +15,18 @@ endif()
+ Set(USE_FAIRROOT FALSE)
+ Set(USE_EXAMPLES FALSE)
+ #check if simpath detected
+-if(DEFINED ENV{SIMPATH})
++
++if(NOT DEFINED ENV{SIMPATH} AND NOT DEFINED SIMPATH)
++message (FATAL_ERROR "\
++You did not define the environment variable SIMPATH or define SIMPATH when calling cmake. \
++Either of the two is needed to properly find the external packages. \
++Please either set this environment variable or pass -DSIMPATH=<path> and execute cmake again. \
++")
++endif()
++if (NOT DEFINED SIMPATH)
++  set(SIMPATH $ENV{SIMPATH})
++endif()
++if(DEFINED SIMPATH)
+     Set(USE_FAIRROOT TRUE)      
+ endif()
+ #check if fairroot disabled
+diff --git a/cmake/fair/CheckCompiler.cmake b/cmake/fair/CheckCompiler.cmake
+index 2bc05b2..91a6fa2 100644
+--- a/cmake/fair/CheckCompiler.cmake
++++ b/cmake/fair/CheckCompiler.cmake
+@@ -19,7 +19,7 @@ endif (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
+ # the compiler and compiler flags used to install fairsoft.
+ # Compare compiler and compiler flags used to compile fairsoft with the compiler and flags used now
+ # In case of differences print a warning
+-Find_Program(FAIRSOFT_CONFIG fairsoft-config PATHS $ENV{SIMPATH}/bin $ENV{FAIRSOFT_ROOT}/bin NO_DEFAULT_PATH)
++Find_Program(FAIRSOFT_CONFIG fairsoft-config PATHS ${SIMPATH}/bin $ENV{SIMPATH}/bin $ENV{FAIRSOFT_ROOT}/bin NO_DEFAULT_PATH)
+ 
+ If(FAIRSOFT_CONFIG)
+   Message(STATUS "fairsoft-config found")
+diff --git a/cmake/modules/CMakeFairRoot.cmake b/cmake/modules/CMakeFairRoot.cmake
+index 1fad98c..19a3290 100644
+--- a/cmake/modules/CMakeFairRoot.cmake
++++ b/cmake/modules/CMakeFairRoot.cmake
+@@ -22,9 +22,12 @@ IF(NOT UNIX)
+ ENDIF(NOT UNIX)  
+     
+     
+-IF(NOT DEFINED ENV{SIMPATH})
++if(NOT DEFINED ENV{SIMPATH} AND NOT DEFINED SIMPATH)
+   MESSAGE(FATAL_ERROR "You did not define the environment variable SIMPATH which is nedded to find the external packages. Please set this variable and execute cmake again.")
+ ENDIF(NOT DEFINED ENV{SIMPATH})
++if(NOT SIMPATH)
++  Set(SIMPATH $ENV{SIMPATH})
++endif()
+     
+ find_package(ROOT 6.00.00 REQUIRED)
+   if("${ROOT_VERSION_MAJOR}.${ROOT_VERSION_MINOR}" VERSION_GREATER 6.16)
+@@ -36,7 +39,6 @@ find_package(ROOT 6.00.00 REQUIRED)
+     set(VMCLIB VMCLibrary)
+   endif()
+ endif()
+-Set(SIMPATH $ENV{SIMPATH})
+ Set(Boost_NO_SYSTEM_PATHS TRUE)
+ Set(Boost_NO_BOOST_CMAKE TRUE)
+ Set(BOOST_ROOT ${SIMPATH})
+diff --git a/cmake/modules/HalMacros.cmake b/cmake/modules/HalMacros.cmake
+index c48a4ca..284a5a9 100644
+--- a/cmake/modules/HalMacros.cmake
++++ b/cmake/modules/HalMacros.cmake
+@@ -8,15 +8,17 @@ Macro(SetFairBase)
+       SET(FAIRBASE ${FAIRBASE})
+     Else (FAIRBASE)
+       Message(STATUS "NOT Found FAIRBASE")
+-      IF(NOT DEFINED ENV{FAIRROOTPATH})
++      if(NOT DEFINED ENV{FAIRROOTPATH} AND NOT DEFINED FAIRROOTPATH)
+          MESSAGE(FATAL_ERROR "You did not define the environment variable FAIRROOTPATH which is needed to find FairRoot. Please set this variable and execute cmake again.")
+          SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/fair" ${CMAKE_MODULE_PATH})
+          MESSAGE(WARNING "CMP ${CMAKE_MODULE_PATH}")
+-      ENDIF(NOT DEFINED ENV{FAIRROOTPATH})
+-      SET(FAIRROOTPATH $ENV{FAIRROOTPATH})
++      endif()
++      if(NOT FAIRROOTPATH)
++        SET(FAIRROOTPATH $ENV{FAIRROOTPATH})
++      endif()
+     EndIf (FAIRBASE)
+-    set(CMAKE_MODULE_PATH "$ENV{FAIRROOTPATH}/share/fairbase/cmake/modules_old" ${CMAKE_MODULE_PATH})
+-    set(CMAKE_MODULE_PATH "$ENV{FAIRROOTPATH}/share/fairbase/cmake/modules"  ${CMAKE_MODULE_PATH})
++    set(CMAKE_MODULE_PATH "${FAIRROOTPATH}/share/fairbase/cmake/modules_old" ${CMAKE_MODULE_PATH})
++    set(CMAKE_MODULE_PATH "${FAIRROOTPATH}/share/fairbase/cmake/modules"  ${CMAKE_MODULE_PATH})
+    IF(FAIRROOTPATH)
+       Set(CheckSrcDir "${FAIRROOTPATH}/share/fairbase/cmake/checks")
+     ELSE(FAIRROOTPATH)
+diff --git a/fair/CMakeLists.txt b/fair/CMakeLists.txt
+index 266e82e..46246e6 100644
+--- a/fair/CMakeLists.txt
++++ b/fair/CMakeLists.txt
+@@ -8,8 +8,8 @@ ${HAL_CORE}/fair
+ Set(SYSTEM_INCLUDE_DIRECTORIES
+  ${ROOT_INCLUDE_DIR} 
+  ${BASE_INCLUDE_DIRECTORIES}
+- $ENV{SIMPATH}/include
+- $ENV{SIMPATH}/include/vmc
++ ${SIMPATH}/include
++ ${SIMPATH}/include/vmc
+ )
+ 
+ Set(LINK_DIRECTORIES
diff --git a/external/InstallHal.cmake b/external/InstallHal.cmake
index 81aba3e597..2ba6f2836c 100644
--- a/external/InstallHal.cmake
+++ b/external/InstallHal.cmake
@@ -11,7 +11,7 @@ download_project_if_needed(PROJECT         Hal_source
                            GIT_REPOSITORY  ${HAL_SRC_URL}
                            GIT_TAG         ${HAL_VERSION}
                            SOURCE_DIR      ${CMAKE_CURRENT_SOURCE_DIR}/Hal
-                       #    PATCH_COMMAND   "patch -p1 < ${CMAKE_CURRENT_SOURCE_DIR}/Hal.patch"
+                           PATCH_COMMAND   "patch -p1 < ${CMAKE_CURRENT_SOURCE_DIR}/Hal.patch"
                           )
 
 If(ProjectUpdated)
@@ -41,15 +41,16 @@ ExternalProject_Add(HAL
         -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}
         -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
         -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
-             -DROOTSYS=${SIMPATH}
-             -DGSL_DIR=${GSLPATH}
-             -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
-             -DCMAKE_BUILD_TYPE=RelWithDebInfo
-             "${EXTRA_ARGS}"
+        -DROOTSYS=${SIMPATH}
+        -DSIMPATH=${SIMPATH}
+        -DFAIRROOTPATH=${FAIRROOTPATH}
+        -DGSL_DIR=${GSLPATH}
+        -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
+        -DCMAKE_BUILD_TYPE=RelWithDebInfo
+        "${EXTRA_ARGS}"
   INSTALL_COMMAND  ${CMAKE_COMMAND} --build . --target install
 )
 
-
 foreach(HAL_LIB_LOCAL ${HAL_LIB_LIST})
     add_library(${HAL_LIB_LOCAL} SHARED IMPORTED GLOBAL)
     set_target_properties(${HAL_LIB_LOCAL} PROPERTIES
-- 
GitLab