From fb699c393cf74a56acce355d188bb63b6f8e3ece Mon Sep 17 00:00:00 2001
From: ralfulrich <ralf.ulrich@kit.edu>
Date: Wed, 13 Oct 2021 10:10:55 +0200
Subject: [PATCH] move from definitions to include file

---
 CMakeLists.txt                                | 84 +++++++++++--------
 cmake/config.hpp.in                           | 21 +++++
 corsika/corsika.hpp                           | 41 ++++-----
 .../detail/framework/utility/CorsikaData.inl  |  5 +-
 4 files changed, 94 insertions(+), 57 deletions(-)
 create mode 100644 cmake/config.hpp.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 468fa72f7..91dffe74c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -73,14 +73,6 @@ include (corsikaUtilities) # extra cmake functions
 #
 include (corsikaDefines)
 
-#+++++++++++++++++++++++++++++
-# the location of the "corsika-data" is of fundamental importance
-# this should not be done in corsikaDefines.cmake since we may want 
-# to change behaviour at config/build/install time. 
-# And: Only the compile_definition of the CORSIKA8 target matters.
-#
-set (CORSIKA_CMAKE_DATA_DIR ${CMAKE_SOURCE_DIR}/modules/data)
-
 #+++++++++++++++++++++++++++++
 # check if compiler is C++17 compliant
 #
@@ -234,13 +226,6 @@ target_link_libraries (
   CONAN_PKG::cli11
   cnpy # for SaveBoostHistogram
   )
-target_compile_definitions (
-  CORSIKA8
-  INTERFACE
-  $<BUILD_INTERFACE:CORSIKA_CMAKE_DATA_DIR="${CORSIKA_CMAKE_DATA_DIR}">
-  $<INSTALL_INTERFACE:CORSIKA_CMAKE_DATA_DIR="${CMAKE_INSTALL_FULL_DATADIR}/corsika/data">
-)
-
 
 # "src" is needed, since some headers (namely GeneratedParticleProperties.inc ec.) are produced by python script from e.g. ParticleData.xml
 add_subdirectory (src)
@@ -275,57 +260,83 @@ install (
   EXPORT CORSIKA8PublicTargets
   PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}
   )
-
 #
-# header only part
+# header only part, just copy entire directory tree
 #
 install (DIRECTORY corsika DESTINATION include)
-
+#
+# generate cmake config package version (for find_package)
+#
 write_basic_package_version_file (
   ${PROJECT_BINARY_DIR}/corsikaConfigVersion.cmake
   VERSION ${c8_version}
   COMPATIBILITY SameMajorVersion
   )
-
-# export targets in build tree
+#
+# export targets in build tree (for find_package)
+#
 export (
   EXPORT CORSIKA8PublicTargets
   FILE "${CMAKE_CURRENT_BINARY_DIR}/corsikaTargets.cmake"
   NAMESPACE CORSIKA8::
   )
-
-# export targets in install tree
+#
+# export targets in install tree (for find_package)
+#
 install (
   EXPORT CORSIKA8PublicTargets
   FILE corsikaTargets.cmake
   NAMESPACE CORSIKA8::
   DESTINATION lib/cmake/corsika
   )
-
-# config for build tree
+#
+# config for build tree (for find_package)
+#
 configure_package_config_file (
   cmake/corsikaConfig.cmake.in
   ${PROJECT_BINARY_DIR}/corsikaConfig.cmake
   INSTALL_DESTINATION ${PROJECT_BINARY_DIR}/do_not_need_this
   )
-
-# corsikaDefines for build tree
+#
+# corsikaDefines 
+#
 configure_package_config_file (
   cmake/corsikaDefines.cmake
   ${PROJECT_BINARY_DIR}/corsikaDefines.cmake
   INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}
   )
-
-# config for install tree
-# overwrite with install location (if it was build as part of corsika)
+#
+# config for install tree  (for find_package)
+# overwrite with install locations (if it was build as part of corsika)
+#
 set (Pythia8_INCDIR ${Pythia8_INCDIR_INSTALL})
 set (Pythia8_LIBDIR ${Pythia8_LIBDIR_INSTALL})
 configure_package_config_file (
   cmake/corsikaConfig.cmake.in
-  ${PROJECT_BINARY_DIR}/cmake/corsikaConfig.cmake
+  ${PROJECT_BINARY_DIR}/cmake/corsikaConfig.cmake-install
   INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}
   )
-
+#
+# generate "corsika/config.hpp" file with cmake-level details and paths
+# first for build-tree
+#
+# the location of the "corsika-data" is of fundamental importance
+set (CORSIKA_CMAKE_DATA_DIR ${CMAKE_SOURCE_DIR}/modules/data)
+configure_file (
+  cmake/config.hpp.in
+  ${PROJECT_BINARY_DIR}/corsika/config.hpp
+)
+#
+# second also for install-tree
+#
+set (CORSIKA_CMAKE_DATA_DIR "${CMAKE_INSTALL_FULL_DATADIR}/corsika/data")
+configure_file (
+  cmake/config.hpp.in
+  ${PROJECT_BINARY_DIR}/config.hpp-install
+)
+#
+# installation of cmake and config files
+#
 install (
   FILES
   ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake
@@ -335,17 +346,22 @@ install (
   ${CMAKE_BINARY_DIR}/corsikaConfigVersion.cmake
   DESTINATION lib/cmake/corsika
   )
-
+#
+# also install-level config.hpp
+# 
+install (FILES 
+  ${PROJECT_BINARY_DIR}/config.hpp-install
+  RENAME config.hpp
+  DESTINATION include/corsika
+  )
 #
 # examples
 #
 install (DIRECTORY examples DESTINATION ${CMAKE_INSTALL_DATADIR}/corsika)
-
 #
 # data
 #
 install (DIRECTORY modules/data DESTINATION ${CMAKE_INSTALL_DATADIR}/corsika)
-
 #
 # tools
 #
diff --git a/cmake/config.hpp.in b/cmake/config.hpp.in
new file mode 100644
index 000000000..cf388f732
--- /dev/null
+++ b/cmake/config.hpp.in
@@ -0,0 +1,21 @@
+namespace corsika {
+
+  /**
+   * @file config.hpp
+   *
+   * @brief CORSIKA_DATA_DIR is a cmake-time marker for the location of corsika-data.
+   *
+   * the location of the "corsika-data" is of fundamental importance
+   * this should not be done in corsikaDefines.cmake since we may want
+   * to change behaviour at config/build/install time.
+   *
+   * There is a build-time default location of corsika-data at: "modules/data",
+   * but there is also an install-time default location of corsika-data at
+   * "share/corsika/data".
+   * The search for corsika-data can always be overwritten by the
+   * user with the `CORSIKA_DATA` envrionment variable.
+   */
+
+  static std::string const CORSIKA_DATA_DIR = "@CORSIKA_CMAKE_DATA_DIR@";
+
+} // namespace corsika
\ No newline at end of file
diff --git a/corsika/corsika.hpp b/corsika/corsika.hpp
index 36fa90b63..022325b73 100644
--- a/corsika/corsika.hpp
+++ b/corsika/corsika.hpp
@@ -9,26 +9,27 @@
 #pragma once
 
 /**
-  @file corsika.hpp
-
-  The CORSIKA 8 air shower simulation framework.
-
-  @mainpage Technical documentation of the CORSIKA 8 software framework
-
-  Software documentatin and reference guide for the CORSIKA 8
-  software framework for air shower simulations. CORSIKA 8 is developed
-  at <a href="https://gitlab.ikp.kit.edu/AirShowerPhysics">https://gitlab.ikp.kit.edu</a>.
-  If you got the code from somewhere else, consider to switch to the official development
-  repository. If you want to report bugs, or want to suggest features or future
-  development, please submit an "issue" on this gitlab server. We only accept Issues and
-  discussion via our central development server https://gitlab.ikp.kit.edu.
-
-  Write to corsika-devel@lists.kit.edu, or even register yourself at
-  https://www.lists.kit.edu/sympa/info/corsika-devel to get in contact
-  with other developers.
-
-  For more information about the project, see @ref md_README.
-  **/
+ * @file corsika.hpp
+ *
+ * The CORSIKA 8 air shower simulation framework.
+ *
+ * @mainpage Technical documentation of the CORSIKA 8 software framework.
+ *
+ * Software documentatin and reference guide for the CORSIKA 8
+ * software framework for air shower simulations. CORSIKA 8 is developed
+ * at <a
+ * href="https://gitlab.ikp.kit.edu/AirShowerPhysics">https://gitlab.ikp.kit.edu</a>. If
+ * you got the code from somewhere else, consider to switch to the official development
+ * repository. If you want to report bugs, or want to suggest features or future
+ * development, please submit an "issue" on this gitlab server. We only accept Issues and
+ * discussion via our central development server https://gitlab.ikp.kit.edu.
+ *
+ * Write to corsika-devel@lists.kit.edu, or even register yourself at
+ * https://www.lists.kit.edu/sympa/info/corsika-devel to get in contact
+ * with other developers.
+ *
+ * For more information about the project, see @ref md_README.
+ */
 
 /*! Usage:
  *  to get the version X.YY.Z,
diff --git a/corsika/detail/framework/utility/CorsikaData.inl b/corsika/detail/framework/utility/CorsikaData.inl
index 189894ae5..af5937227 100644
--- a/corsika/detail/framework/utility/CorsikaData.inl
+++ b/corsika/detail/framework/utility/CorsikaData.inl
@@ -7,6 +7,7 @@
  */
 #pragma once
 
+#include <corsika/config.hpp>
 #include <corsika/framework/core/Logging.hpp>
 
 #include <boost/filesystem/path.hpp>
@@ -18,9 +19,7 @@
 namespace corsika {
 
   inline boost::filesystem::path corsika_data(boost::filesystem::path const& key) {
-    std::string const corsika_CMAKE_Data_Dir =
-        std::string(CORSIKA_CMAKE_DATA_DIR); // from cmake
-    boost::filesystem::path fname = boost::filesystem::path(corsika_CMAKE_Data_Dir) / key;
+    boost::filesystem::path fname = boost::filesystem::path(CORSIKA_DATA_DIR) / key;
     // LCOV_EXCL_START, this cannot be easily tested system-independently
     if (auto const* p = std::getenv("CORSIKA_DATA"); p != nullptr) {
       fname = boost::filesystem::path(p) / key;
-- 
GitLab