From a292726783d71fa4bf5652e434ed088ff9c3df74 Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Thu, 24 Feb 2022 10:22:17 +0100
Subject: [PATCH] Fix installation and usage of AnalysisTree headers

Patch the AnalysisTree and AnalysisTreeQA buils systems to create also
correct dictionaries for ROOT versions below v6.18.0.
Add the directories where the header files of both packages are installed
to ROOT_INCLUDE_PATH which are paths used by rootcint to find needed
header files.
---
 CMakeLists.txt                       |   2 +
 external/AnalysisTree.patch          | 112 +++++++++++++++++++++++++++
 external/AnalysisTreeQA.patch        |  45 +++++++++++
 external/InstallAnalysisTree.cmake   |   1 +
 external/InstallAnalysisTreeQA.cmake |   1 +
 5 files changed, 161 insertions(+)
 create mode 100644 external/AnalysisTree.patch
 create mode 100644 external/AnalysisTreeQA.patch

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 40da32e7cb..6afe8ca762 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -498,6 +498,8 @@ if(NOT CBMROOT_MINIMAL)
       "${CMAKE_INSTALL_PREFIX}/include/mvd"
       "${CMAKE_INSTALL_PREFIX}/include/littrack"
       "${CMAKE_INSTALL_PREFIX}/include/KF"
+      "${CMAKE_INSTALL_PREFIX}/include/AnalysisTree"
+      "${CMAKE_INSTALL_PREFIX}/include/AnalysisTreeQA"
      )
   set(CMAKE_INSTALL_LIBDIR lib)
   SET(VMCWORKDIR ${CMAKE_INSTALL_PREFIX}/share/cbmroot)
diff --git a/external/AnalysisTree.patch b/external/AnalysisTree.patch
new file mode 100644
index 0000000000..0b79724ec9
--- /dev/null
+++ b/external/AnalysisTree.patch
@@ -0,0 +1,112 @@
+diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
+index ab4c300..765e1db 100644
+--- a/core/CMakeLists.txt
++++ b/core/CMakeLists.txt
+@@ -16,7 +16,28 @@ string(REPLACE ".cpp" ".hpp" HEADERS "${SOURCES}")
+ list(APPEND HEADERS "Constants.hpp" "Detector.hpp")
+ 
+ add_library(AnalysisTreeBase SHARED ${SOURCES} G__AnalysisTreeBase.cxx)
+-ROOT_GENERATE_DICTIONARY(G__AnalysisTreeBase ${HEADERS} LINKDEF AnalysisTreeCoreLinkDef.h)
++
++if(${ROOT_VERSION} VERSION_GREATER 6.18.0)
++  message(STATUS "ROOT ${ROOT_VERSION}, use the standard ROOT_GENERATE_DICTIONARY macro.")
++  ROOT_GENERATE_DICTIONARY(G__AnalysisTreeBase ${HEADERS} LINKDEF AnalysisTreeCoreLinkDef.h)
++else()
++  message(STATUS "ROOT ${ROOT_VERSION} has a broken ROOT_GENERATE_DICTIONARY macro, so build the dictionary ourself.")
++  set(dictionary_includes ${HEADERS})
++  set(dictionary_linkdef "AnalysisTreeCoreLinkDef.h")
++  set(dictionary_source "G__AnalysisTreeBase.cxx")
++
++  add_custom_command(OUTPUT ${dictionary_source}
++                     COMMAND ${ROOT_rootcling_CMD} -f ${dictionary_source}
++                     -rml libAnalysisTreeBase
++                     -rmf libAnalysisTreeBase.rootmap
++                     -s   libAnalysisTreeBase
++                     -inlineInputHeader
++                     -I${CMAKE_CURRENT_SOURCE_DIR} ${dictionary_includes}
++                     ${CMAKE_CURRENT_SOURCE_DIR}/${dictionary_linkdef}
++                     DEPENDS ${dictionary_includes} ${dictionary_linkdef}
++                    )
++endif()
++
+ target_link_libraries(AnalysisTreeBase ${ROOT_LIBRARIES} EG)
+ 
+ add_custom_command(TARGET AnalysisTreeBase PRE_BUILD
+diff --git a/core/Constants.hpp b/core/Constants.hpp
+index ed7b9c6..f9cbcc3 100644
+--- a/core/Constants.hpp
++++ b/core/Constants.hpp
+@@ -109,4 +109,4 @@ enum EventHeaderFields : ShortInt_t {
+ 
+ }// namespace AnalysisTree
+ 
+-#endif
+\ No newline at end of file
++#endif
+diff --git a/core/Module.hpp b/core/Module.hpp
+index 9c523aa..c54d17e 100644
+--- a/core/Module.hpp
++++ b/core/Module.hpp
+@@ -93,4 +93,4 @@ class ModulePosition : public IndexedObject {
+ };
+ }// namespace AnalysisTree
+ 
+-#endif
+\ No newline at end of file
++#endif
+diff --git a/infra/CMakeLists.txt b/infra/CMakeLists.txt
+index ed7bd9b..a63a7b1 100644
+--- a/infra/CMakeLists.txt
++++ b/infra/CMakeLists.txt
+@@ -30,12 +30,34 @@ add_library(AnalysisTreeInfra SHARED ${SOURCES} G__AnalysisTreeInfra.cxx)
+ target_compile_definitions(AnalysisTreeInfra PUBLIC
+         $<$<BOOL:${Boost_FOUND}>:ANALYSISTREE_BOOST_FOUND>)
+ 
+-ROOT_GENERATE_DICTIONARY(G__AnalysisTreeInfra ${HEADERS}
+-        LINKDEF AnalysisTreeInfraLinkDef.h
+-        OPTIONS
+-            -I${CMAKE_BINARY_DIR}/include
+-            $<$<BOOL:${Boost_FOUND}>:-DANALYSISTREE_BOOST_FOUND>
+-        )
++if(${ROOT_VERSION} VERSION_GREATER 6.18.0)
++  message(STATUS "ROOT ${ROOT_VERSION}, use the standard ROOT_GENERATE_DICTIONARY macro.")
++  ROOT_GENERATE_DICTIONARY(G__AnalysisTreeInfra ${HEADERS}
++          LINKDEF AnalysisTreeInfraLinkDef.h
++          OPTIONS
++              -I${CMAKE_BINARY_DIR}/include
++              $<$<BOOL:${Boost_FOUND}>:-DANALYSISTREE_BOOST_FOUND>
++          )
++else()
++  message(STATUS "ROOT ${ROOT_VERSION} has a broken ROOT_GENERATE_DICTIONARY macro, so build the dictionary ourself.")
++  set(dictionary_includes ${HEADERS})
++  set(dictionary_linkdef "AnalysisTreeInfraLinkDef.h")
++  set(dictionary_source "G__AnalysisTreeInfra.cxx")
++
++  add_custom_command(OUTPUT ${dictionary_source}
++                     COMMAND ${ROOT_rootcling_CMD} -f ${dictionary_source}
++                     -rml libAnalysisTreeInfra
++                     -rmf libAnalysisTreeInfra.rootmap
++                     -s   libAnalysisTreeInfra
++                     -inlineInputHeader
++                     -I${CMAKE_CURRENT_SOURCE_DIR} ${dictionary_includes}
++                     -I${CMAKE_BINARY_DIR}/include
++                     -I${CMAKE_BINARY_DIR}/include/AnalysisTree
++                     $<$<BOOL:${Boost_FOUND}>:-DANALYSISTREE_BOOST_FOUND>
++                     ${CMAKE_CURRENT_SOURCE_DIR}/${dictionary_linkdef}
++                     DEPENDS ${dictionary_includes} ${dictionary_linkdef}
++                    )
++endif()
+ target_link_libraries(AnalysisTreeInfra
+         PUBLIC
+             ${ROOT_LIBRARIES}
+diff --git a/infra/SimpleCut.hpp b/infra/SimpleCut.hpp
+index 4ec5458..21fe68c 100644
+--- a/infra/SimpleCut.hpp
++++ b/infra/SimpleCut.hpp
+@@ -116,4 +116,4 @@ SimpleCut RangeCut(const std::string& variable_name, float lo, float hi, const s
+ SimpleCut EqualsCut(const std::string& variable_name, int value, const std::string& title = "");
+ 
+ }// namespace AnalysisTree
+-#endif//ANALYSISTREE_SIMPLECUT_H
+\ No newline at end of file
++#endif//ANALYSISTREE_SIMPLECUT_H
diff --git a/external/AnalysisTreeQA.patch b/external/AnalysisTreeQA.patch
new file mode 100644
index 0000000000..eda48c5c76
--- /dev/null
+++ b/external/AnalysisTreeQA.patch
@@ -0,0 +1,45 @@
+diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
+index ee90134..0861bc8 100644
+--- a/src/CMakeLists.txt
++++ b/src/CMakeLists.txt
+@@ -20,11 +20,35 @@ endif ()
+ 
+ #add_dependencies(AnalysisTreeQA ${PROJECT_DEPENDENCIES})
+ 
+-ROOT_GENERATE_DICTIONARY(G__AnalysisTreeQA
+-        ${HEADERS}
+-        LINKDEF AnalysisTreeQALinkDef.h
+-        OPTIONS -I${CMAKE_BINARY_DIR}/include
+-        )
++if(${ROOT_VERSION} VERSION_GREATER 6.18.0)
++  message(STATUS "ROOT ${ROOT_VERSION}, use the standard ROOT_GENERATE_DICTIONARY macro.")
++  ROOT_GENERATE_DICTIONARY(G__AnalysisTreeQA
++          ${HEADERS}
++          LINKDEF AnalysisTreeQALinkDef.h
++          OPTIONS -I${CMAKE_BINARY_DIR}/include
++          )
++else()
++  message(STATUS "ROOT ${ROOT_VERSION} has a broken ROOT_GENERATE_DICTIONARY macro, so build the dictionary ourself.")
++  set(dictionary_includes ${HEADERS})
++  set(dictionary_linkdef "AnalysisTreeQALinkDef.h")
++  set(dictionary_source "G__AnalysisTreeQA.cxx")
++
++  add_custom_command(OUTPUT ${dictionary_source}
++                     COMMAND ${ROOT_rootcling_CMD} -f ${dictionary_source}
++                     -rml libAnalysisTreeQA
++                     -rmf libAnalysisTreeQA.rootmap
++                     -s   libAnalysisTreeQA
++                     -inlineInputHeader
++                     -I${AnalysisTree_INCLUDE_DIR}
++                     -I${CMAKE_CURRENT_SOURCE_DIR}
++                     ${dictionary_includes}
++                     ${CMAKE_CURRENT_SOURCE_DIR}/${dictionary_linkdef}
++                     DEPENDS ${dictionary_includes} ${dictionary_linkdef}
++                    )
++endif()
++
++
++
+ target_link_libraries(AnalysisTreeQA
+         PUBLIC
+         AnalysisTreeBase
diff --git a/external/InstallAnalysisTree.cmake b/external/InstallAnalysisTree.cmake
index 3793682c2d..0adf61ee62 100644
--- a/external/InstallAnalysisTree.cmake
+++ b/external/InstallAnalysisTree.cmake
@@ -10,6 +10,7 @@ download_project_if_needed(PROJECT         AnalysisTree_source
         GIT_TAG         ${ANALYSISTREE_VERSION}
         SOURCE_DIR      ${CMAKE_CURRENT_SOURCE_DIR}/AnalysisTree
         TEST_FILE       CMakeLists.txt
+        PATCH_COMMAND   "patch -p1 < ${CMAKE_CURRENT_SOURCE_DIR}/AnalysisTree.patch"
         )
 
 If(ProjectUpdated)
diff --git a/external/InstallAnalysisTreeQA.cmake b/external/InstallAnalysisTreeQA.cmake
index 8b57eb3067..eff500d4c1 100644
--- a/external/InstallAnalysisTreeQA.cmake
+++ b/external/InstallAnalysisTreeQA.cmake
@@ -9,6 +9,7 @@ download_project_if_needed(PROJECT         AnalysisTreeQA_source
         GIT_TAG         ${ANALYSISTREEQA_VERSION}
         SOURCE_DIR      ${CMAKE_CURRENT_SOURCE_DIR}/AnalysisTreeQA
         TEST_FILE       CMakeLists.txt
+        PATCH_COMMAND   "patch -p1 < ${CMAKE_CURRENT_SOURCE_DIR}/AnalysisTreeQA.patch"
         )
 
 If(ProjectUpdated)
-- 
GitLab