From 4f3975ed8e8382e71e0c0347f1249784617f2325 Mon Sep 17 00:00:00 2001
From: ralfulrich <ralf.ulrich@kit.edu>
Date: Wed, 27 Jan 2021 09:37:58 +0100
Subject: [PATCH] make pythia8 xmldoc search more robust

---
 cmake/FindPythia8.cmake                       | 39 +++++++++++++------
 corsika/detail/modules/pythia8/Decay.inl      |  1 -
 .../detail/modules/pythia8/Interaction.inl    |  1 -
 .../pythia8/ConfigurationDirectory.hpp        | 27 -------------
 corsika/modules/pythia8/Pythia8.hpp           |  4 ++
 modules/pythia/CMakeLists.txt                 | 29 ++++++++++----
 .../Pythia8ConfigurationDirectory.hpp.in      | 35 +++++++++++++++++
 7 files changed, 88 insertions(+), 48 deletions(-)
 delete mode 100644 corsika/modules/pythia8/ConfigurationDirectory.hpp
 create mode 100644 modules/pythia/Pythia8ConfigurationDirectory.hpp.in

diff --git a/cmake/FindPythia8.cmake b/cmake/FindPythia8.cmake
index 62106fad4..948ab97ec 100644
--- a/cmake/FindPythia8.cmake
+++ b/cmake/FindPythia8.cmake
@@ -14,7 +14,7 @@
 #
 
 function (_Pythia8_CONFIG_ option variable type doc)
-  execute_process(COMMAND ${Pythia8_CONFIG} ${option}
+  execute_process (COMMAND ${Pythia8_CONFIG} ${option}
     OUTPUT_VARIABLE _local_out_
     RESULT_VARIABLE _local_res_)
   string (REGEX REPLACE "\n$" "" _local_out_ "${_local_out_}")
@@ -25,6 +25,14 @@ function (_Pythia8_CONFIG_ option variable type doc)
   endif ()
 endfunction (_Pythia8_CONFIG_)
   
+#################################################
+#
+# take directory and assume standard install layout
+#
+
+function (_Pythia8_LAYOUT_ dir variable type doc)
+  set (${variable} "${dir}" CACHE ${type} ${doc})
+endfunction (_Pythia8_LAYOUT_)
 
 
 #################################################
@@ -52,21 +60,28 @@ set (_SEARCH_Pythia8_
   /opt/pythia8
   )
 
-find_program (Pythia8_CONFIG
-  NAME pythia8-config
+find_file (Pythia8_DIR
+  NAME Pythia.h
   PATHS ${_SEARCH_Pythia8_}
-  PATH_SUFFIXES "/bin"
-  DOC "The location of the pythia8-config script")
+  PATH_SUFFIXES "/include/Pythia8"
+  DOC "The location of the Pythia8/Pythia.h script"
+  REQUIRED)
+
+set (Pythia8_CONFIG ${Pythia8_DIR}/bin/pythia-config)
 
 if (Pythia8_CONFIG)
   set (HAVE_Pythia8 1 CACHE BOOL "presence of pythia8, found via pythia8-config")
 
-  _Pythia8_CONFIG_ ("--prefix" Pythia8_PREFIX PATH "location of pythia8 installation")
-  _Pythia8_CONFIG_ ("--includedir" Pythia8_INCLUDE_DIR PATH "pythia8 include directory")
-  _Pythia8_CONFIG_ ("--libdir" Pythia8_LIBRARY STRING "the pythia8 libs")
-  _Pythia8_CONFIG_ ("--datadir" Pythia8_DATA_DIR PATH "the pythia8 data dir")
-  _Pythia8_CONFIG_ ("--cxxflags" Pythia8_CXXFLAGS STRING "the pythia8 cxxflags")
-
+  # pythia-config is not relocatable
+  #_Pythia8_CONFIG_ ("--prefix" Pythia8_PREFIX PATH "location of pythia8 installation")
+  #_Pythia8_CONFIG_ ("--includedir" Pythia8_INCLUDE_DIR PATH "pythia8 include directory")
+  #_Pythia8_CONFIG_ ("--libdir" Pythia8_LIBRARY STRING "the pythia8 libs")
+  #_Pythia8_CONFIG_ ("--datadir" Pythia8_DATA_DIR PATH "the pythia8 data dir")
+  _Pythia8_LAYOUT_ ("${Pythia8_DIR}" Pythia8_PREFIX PATH "location of pythia8 installation")
+  _Pythia8_LAYOUT_ ("${Pythia8_DIR}/include" Pythia8_INCLUDE_DIR PATH "pythia8 include directory")
+  _Pythia8_LAYOUT_ ("${Pythia8_DIR}/lib" Pythia8_LIBRARY STRING "the pythia8 libs")
+  _Pythia8_LAYOUT_ ("${Pythia8_DIR}/share/Pythia8/xmldoc" Pythia8_DATA_DIR PATH "the pythia8 data dir")
+  
   # read the config string
   file (READ "${Pythia8_INCLUDE_DIR}/Pythia8/Pythia.h" Pythia8_TMP_PYTHIA_H)
   string (REGEX MATCH "#define PYTHIA_VERSION_INTEGER ([0-9]*)" _ ${Pythia8_TMP_PYTHIA_H})  
@@ -78,4 +93,4 @@ endif ()
 # standard cmake infrastructure:
 include (FindPackageHandleStandardArgs)
 find_package_handle_standard_args (Pythia8 REQUIRED_VARS Pythia8_PREFIX Pythia8_INCLUDE_DIR Pythia8_LIBRARY VERSION_VAR Pythia8_VERSION)
-mark_as_advanced (Pythia8_DATA_DIR Pythia8_CXXFLAGS Pythia8_INCLUDE_DIR Pythia8_LIBRARY)
+mark_as_advanced (Pythia8_DATA_DIR Pythia8_INCLUDE_DIR Pythia8_LIBRARY)
diff --git a/corsika/detail/modules/pythia8/Decay.inl b/corsika/detail/modules/pythia8/Decay.inl
index 9b532ab7c..50d451d27 100644
--- a/corsika/detail/modules/pythia8/Decay.inl
+++ b/corsika/detail/modules/pythia8/Decay.inl
@@ -9,7 +9,6 @@
 #include <corsika/modules/pythia8/Pythia8.hpp>
 #include <corsika/modules/pythia8/Decay.hpp>
 #include <corsika/modules/pythia8/Random.hpp>
-#include <corsika/modules/pythia8/ConfigurationDirectory.hpp>
 
 #include <corsika/framework/utility/COMBoost.hpp>
 
diff --git a/corsika/detail/modules/pythia8/Interaction.inl b/corsika/detail/modules/pythia8/Interaction.inl
index d6c5f756b..98ec35a8e 100644
--- a/corsika/detail/modules/pythia8/Interaction.inl
+++ b/corsika/detail/modules/pythia8/Interaction.inl
@@ -9,7 +9,6 @@
 #pragma once
 
 #include <corsika/modules/pythia8/Interaction.hpp>
-#include <corsika/modules/pythia8/ConfigurationDirectory.hpp>
 
 #include <corsika/framework/geometry/FourVector.hpp>
 #include <corsika/framework/utility/COMBoost.hpp>
diff --git a/corsika/modules/pythia8/ConfigurationDirectory.hpp b/corsika/modules/pythia8/ConfigurationDirectory.hpp
deleted file mode 100644
index 813f03e40..000000000
--- a/corsika/modules/pythia8/ConfigurationDirectory.hpp
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
- *
- * This software is distributed under the terms of the GNU General Public
- * Licence version 3 (GPL Version 3). See file LICENSE for a full version of
- * the license.
- */
-
-#pragma once
-
-#include <string>
-
-/**
- * \file Interaction.inl
- **/
-
-///! Helper macros to convert definitions into strings
-#define CORSIKA_STRINGIFY(x) #x
-#define CORSIKA_TOSTRING(x) CORSIKA_STRINGIFY(x)
-
-/**
- *
- * Location of the pythia XML directory with crucial input and config data:
- **/
-static std::string const CORSIKA_Pythia8_XML_DIR =
-    std::string(CORSIKA_Pythia8_PREFIX) // from cmake
-    + std::string("/share/Pythia8/xmldoc/");
diff --git a/corsika/modules/pythia8/Pythia8.hpp b/corsika/modules/pythia8/Pythia8.hpp
index 5b42ac148..6e8f80e89 100644
--- a/corsika/modules/pythia8/Pythia8.hpp
+++ b/corsika/modules/pythia8/Pythia8.hpp
@@ -8,4 +8,8 @@
 
 #pragma once
 
+// the location of the "xmldoc" pythia directory (data files, config)
+#include <Pythia8ConfigurationDirectory.hpp>
+
+// the main pythia include
 #include <Pythia8/Pythia.h>
diff --git a/modules/pythia/CMakeLists.txt b/modules/pythia/CMakeLists.txt
index 9770c4fc2..85ec967b3 100644
--- a/modules/pythia/CMakeLists.txt
+++ b/modules/pythia/CMakeLists.txt
@@ -27,11 +27,14 @@ if ("x_${USE_PYTHIA8_C8}" STREQUAL "x_SYSTEM")
     C8::ext::pythia8 PROPERTIES
     IMPORTED_LOCATION ${Pythia8_LIBRARY}/libpythia8.a
     IMPORTED_LINK_INTERFACE_LIBRARIES dl
-    INTERFACE_INCLUDE_DIRECTORIES ${Pythia8_INCLUDE_DIR}
-    INTERFACE_COMPILE_DEFINITIONS CORSIKA_Pythia8_PREFIX=\"${Pythia8_PREFIX}\"
+    INTERFACE_INCLUDE_DIRECTORIES ${Pythia8_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR}
     )
   set (Pythia8_FOUND 1 PARENT_SCOPE)
 
+  set (CORSIKA_Pythia8_LOCATION ${Pythia8_PREFIX}) # build and install location, pythia8 is external
+  configure_file (Pythia8ConfigurationDirectory.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/Pythia8ConfigurationDirectory.hpp  @ONLY)
+  configure_file (Pythia8ConfigurationDirectory.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/Pythia8ConfigurationDirectory_install.hpp  @ONLY)
+  
   message (STATUS "Pythia8 (system) at ${Pythia8_PREFIX}")
 
 else ()
@@ -39,20 +42,22 @@ else ()
   set (_C8_Pythia8_VERSION "8245")
   message (STATUS "Building modules/pythia8 using pythia${_C8_Pythia8_VERSION}-stripped.tar.bz2")
   message (STATUS "This will take a bit.....")
+  # this is not a full PYTHIA8 install, it is a bit simplified, e.g. no pythia8-config
   ExternalProject_Add (pythia8
     URL ${CMAKE_CURRENT_SOURCE_DIR}/pythia${_C8_Pythia8_VERSION}-stripped.tar.bz2
     URL_MD5 d3e951a2f101e8cfec26405cb61db83b
     SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/pythia8/source
     INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/pythia8/install
     LOG_DIR ${CMAKE_CURRENT_BINARY_DIR}/pythia8/log
-    CONFIGURE_COMMAND ./configure --cxx-common=\\"${CMAKE_CXX_FLAGS} -O2 -fPIC\\" --prefix=${CMAKE_CURRENT_BINARY_DIR}/pythia8/install 
+    CONFIGURE_COMMAND ./configure --cxx-common=\\"${CMAKE_CXX_FLAGS} -O2 -fPIC\\" --prefix=${CMAKE_CURRENT_BINARY_DIR}/pythia8/install
+    INSTALL_COMMAND make install COMMAND rm -rf ${CMAKE_CURRENT_BINARY_DIR}/pythia8/install/bin ${CMAKE_CURRENT_BINARY_DIR}/pythia8/install/share/Pythia8/examples
     BUILD_IN_SOURCE ON
     EXCLUDE_FROM_ALL TRUE
     LOG_CONFIGURE TRUE
     LOG_BUILD TRUE
     LOG_INSTALL TRUE
     LOG_MERGED_STDOUTERR TRUE
-    )
+    )  
   set (Pythia8_FOUND 1 PARENT_SCOPE)
   ExternalProject_Get_Property (pythia8 INSTALL_DIR) # this is "make"
   set (Pythia8_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/externals/pythia8) # this is "make install"
@@ -63,6 +68,11 @@ else ()
   add_dependencies (C8::ext::pythia8 pythia8)
 
   set (Pythia8_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/share/externals/pythia8)
+  set (CORSIKA_Pythia8_LOCATION ${Pythia8_PREFIX}) # build location
+  configure_file (Pythia8ConfigurationDirectory.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/Pythia8ConfigurationDirectory.hpp  @ONLY)
+  set (CORSIKA_Pythia8_LOCATION ${Pythia8_INSTALL_DIR}) # install location
+  configure_file (Pythia8ConfigurationDirectory.hpp.in ${CMAKE_CURRENT_BINARY_DIR}/Pythia8ConfigurationDirectory_install.hpp  @ONLY)
+  
   install (DIRECTORY ${INSTALL_DIR}/ DESTINATION ${Pythia8_INSTALL_DIR})
   
   set_target_properties (
@@ -70,13 +80,18 @@ else ()
     PROPERTIES
     IMPORTED_LOCATION ${Pythia8_LIBRARY}
     IMPORTED_LINK_INTERFACE_LIBRARIES dl
-    INTERFACE_INCLUDE_DIRECTORIES ${Pythia8_INCLUDE_DIR}
-    INTERFACE_COMPILE_DEFINITIONS CORSIKA_Pythia8_PREFIX=\"${Pythia8_PREFIX}\"
+    INTERFACE_INCLUDE_DIRECTORIES "${Pythia8_INCLUDE_DIR};${CMAKE_CURRENT_BINARY_DIR}"
     )
-
+  
     message (STATUS "Pythia8 build at ${INSTALL_DIR}, final install to ${Pythia8_INSTALL_DIR}")
 endif ()
 
+# the install location of Pythia8
+install (
+  FILES ${CMAKE_CURRENT_BINARY_DIR}/Pythia8ConfigurationDirectory_install.hpp
+  RENAME Pythia8ConfigurationDirectory.hpp
+  DESTINATION include/modules/pythia8
+  )
 
 ##### add pythia8 to CORSIKA8 build
 add_dependencies (CORSIKA8 C8::ext::pythia8)
diff --git a/modules/pythia/Pythia8ConfigurationDirectory.hpp.in b/modules/pythia/Pythia8ConfigurationDirectory.hpp.in
new file mode 100644
index 000000000..d05df326a
--- /dev/null
+++ b/modules/pythia/Pythia8ConfigurationDirectory.hpp.in
@@ -0,0 +1,35 @@
+/*
+ * (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
+ *
+ * This software is distributed under the terms of the GNU General Public
+ * Licence version 3 (GPL Version 3). See file LICENSE for a full version of
+ * the license.
+ */
+
+#pragma once
+
+#include <string>
+
+/**
+ * \file ConfigurationDirectory.hpp
+ *
+ * The location of Pythia XMLDOC configuration data files (particle
+ * data, decays, PDFs etc.) can be at different locations, depending
+ * on wether system version of pythia8, or the local CORSIKA8 version
+ * has been used. Also for the latter it is build in the
+ * ${PROJECT_BINARY_DIR}/modules/pythia/pythia8/install, while during
+ * installation (make install) it is copied to
+ * ${CMAKE_INSTALL_PREFIX}/share/external/pythia8. Thus, at config
+ * time different version of ConfigurationDirectory.hpp are created to
+ * always point to the right locations. 
+ **/
+
+namespace corsika::pythia8 {
+
+  /**
+   *
+   * Location of the pythia XML directory with crucial input and config data, from cmake:
+   **/
+  static std::string const CORSIKA_Pythia8_XML_DIR("@CORSIKA_Pythia8_LOCATION@/share/Pythia8/xmldoc/");
+  
+}
-- 
GitLab