From 2b43d366c806eb4d5904c310df86e18f9a82c909 Mon Sep 17 00:00:00 2001
From: ralfulrich <ralf.ulrich@kit.edu>
Date: Sun, 10 Oct 2021 00:55:52 +0200
Subject: [PATCH] make CORSIKA_DATA optional. update docs

---
 CMakeLists.txt                                | 14 +++++++++++
 README.md                                     |  9 +-------
 README.rst                                    |  9 +-------
 cmake/corsikaConfig.cmake.in                  |  4 ++++
 .../detail/framework/utility/CorsikaData.inl  | 23 +++++++++++++------
 corsika/detail/modules/epos/Interaction.inl   | 15 +++++-------
 tests/modules/testQGSJetII.cpp                | 19 ---------------
 7 files changed, 42 insertions(+), 51 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0605a361b..a3b93ad49 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -68,6 +68,14 @@ include (corsikaUtilities) # extra cmake functions
 #
 include (corsikaDefines)
 
+#+++++++++++++++++++++++++++++
+# the location of the CORSIKA_DATA_DIR 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_DATA_DIR ${CMAKE_SOURCE_DIR}/modules/data)
+
 #+++++++++++++++++++++++++++++
 # check if compiler is C++17 compliant
 #
@@ -221,6 +229,12 @@ target_link_libraries (
   CONAN_PKG::cli11
   cnpy # for SaveBoostHistogram
   )
+target_compile_definitions (
+  CORSIKA8
+  INTERFACE
+  CORSIKA_DATA_DIR="${CORSIKA_DATA_DIR}"
+)
+
 
 # "src" is needed, since some headers (namely GeneratedParticleProperties.inc ec.) are produced by python script from e.g. ParticleData.xml
 add_subdirectory (src)
diff --git a/README.md b/README.md
index 6d89368fb..5f48b1449 100644
--- a/README.md
+++ b/README.md
@@ -120,13 +120,6 @@ make install
 
 ## Runing Unit Tests
 
-Note, before you run *any* executbale you must also define the
-`CORSIKA_DATA` environment variable to point to the location where you
-cloned corsika `modules/data`, thus typically 
-```shell
-export CORSIKA_DATA=$PWD/../corsika/modules/data
-```
-
 To run the Unit Tests, just type `ctest` in your build area.
 
 
@@ -144,7 +137,7 @@ e.g. `corsika-install/share/corsika/examples/` to your working place
 Next, you need to define the environment variable `corsika_DIR` to point to, either, 
 your build, or your install area. Thus, e.g. 
 ```shell
-export corsika_DIR=<dir where you installed CORSIKA 8 to, or where you buld it">
+export corsika_DIR=<dir where you installed CORSIKA 8 to, or where you build it>
 ```
 
 Then compile your example/application with
diff --git a/README.rst b/README.rst
index 27eb3d1d8..2f4d28d21 100644
--- a/README.rst
+++ b/README.rst
@@ -129,13 +129,6 @@ Follow these steps to download and install CORSIKA 8, master development version
 Runing Unit Tests
 -----------------
 
-Note, before you run *any* executbale you must also define the
-:code:`CORSIKA_DATA` environment variable to point to the location where you
-cloned corsika :code:`modules/data`, thus typically 
-::
-   
-  export CORSIKA_DATA=$PWD/../corsika/modules/data
-
 To run the Unit Tests, just type :code:`ctest` in your build area.
 
 
@@ -155,7 +148,7 @@ Next, you need to define the environment variable :code:`corsika_DIR` to point t
 your build, or your install area. Thus, e.g. 
 ::
    
-  export corsika_DIR=<dir where you installed CORSIKA 8 to, or where you buld it">
+  export corsika_DIR=<dir where you installed CORSIKA 8 to, or where you build it>
 
 Then compile your example/application with
 ::
diff --git a/cmake/corsikaConfig.cmake.in b/cmake/corsikaConfig.cmake.in
index 3a833b71c..d8b5d2496 100644
--- a/cmake/corsikaConfig.cmake.in
+++ b/cmake/corsikaConfig.cmake.in
@@ -62,6 +62,10 @@ check_required_components (corsika)
 #+++++++++++++++++++++++++++++++
 # add further definitions / options
 #
+target_compile_definitions (CORSIKA8::CORSIKA8
+  INTERFACE
+  CORSIKA_DATA_DIR=\"@CORSIKA_DATA_DIR@\"
+)
 if (WITH_HISTORY)
   set_property (
     TARGET CORSIKA8::CORSIKA8
diff --git a/corsika/detail/framework/utility/CorsikaData.inl b/corsika/detail/framework/utility/CorsikaData.inl
index c3a82ad11..8ba553d75 100644
--- a/corsika/detail/framework/utility/CorsikaData.inl
+++ b/corsika/detail/framework/utility/CorsikaData.inl
@@ -7,16 +7,25 @@
  */
 #pragma once
 
+#include <corsika/framework/core/Logging.hpp>
+
 #include <boost/filesystem/path.hpp>
 
 #include <cstdlib>
 #include <stdexcept>
 #include <string>
 
-inline boost::filesystem::path corsika::corsika_data(boost::filesystem::path const& key) {
-  if (auto const* p = std::getenv("CORSIKA_DATA"); p != nullptr) {
-    return boost::filesystem::path(p) / key;
-  } else { // LCOV_EXCL_START, this cannot be easily tested system-independently
-    throw std::runtime_error("CORSIKA_DATA not set");
-  } // LCOV_EXCL_STOP
-}
+namespace corsika {
+
+  inline boost::filesystem::path corsika_data(boost::filesystem::path const& key) {
+    std::string const corsika_Data_Dir = std::string(CORSIKA_DATA_DIR); // from cmake
+    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;
+    }
+    // LCOV_EXCL_STOP
+    CORSIKA_LOG_INFO("opening data file={}", fname);
+    return fname;
+  }
+} // namespace corsika
diff --git a/corsika/detail/modules/epos/Interaction.inl b/corsika/detail/modules/epos/Interaction.inl
index dde502eb0..1e69cff4e 100644
--- a/corsika/detail/modules/epos/Interaction.inl
+++ b/corsika/detail/modules/epos/Interaction.inl
@@ -9,14 +9,16 @@
 #pragma once
 
 #include <corsika/modules/epos/Interaction.hpp>
+#include <corsika/modules/epos/EposStack.hpp>
 
 #include <corsika/media/Environment.hpp>
 #include <corsika/media/NuclearComposition.hpp>
-#include <corsika/framework/geometry/FourVector.hpp>
-#include <corsika/modules/epos/EposStack.hpp>
+
+#include <corsika/framework/utility/COMBoost.hpp>
+#include <corsika/framework/utility/CorsikaData.hpp>
+
 #include <corsika/setup/SetupStack.hpp>
 #include <corsika/setup/SetupTrajectory.hpp>
-#include <corsika/framework/utility/COMBoost.hpp>
 
 #include <epos.hpp>
 
@@ -33,13 +35,8 @@ namespace corsika::epos {
       : data_path_(dataPath)
       , epos_listing_(epos_printout_on) {
     if (dataPath == "") {
-      if (std::getenv("CORSIKA_DATA")) {
-        data_path_ = std::string(std::getenv("CORSIKA_DATA")) + "/EPOS/";
-        CORSIKA_LOGGER_DEBUG(logger_, "Searching for EPOSLHC data tables in {}",
-                             data_path_);
-      }
+      data_path_ = (std::string(corsika_data("EPOS").c_str()) + "/").c_str();
     }
-
     // initialize Eposlhc
     static bool initialized = false;
     if (!initialized) {
diff --git a/tests/modules/testQGSJetII.cpp b/tests/modules/testQGSJetII.cpp
index eddf34aca..84f1b4ebe 100644
--- a/tests/modules/testQGSJetII.cpp
+++ b/tests/modules/testQGSJetII.cpp
@@ -46,25 +46,6 @@ auto sumMomentum(TStackView const& view, CoordinateSystemPtr const& vCS) {
   return sum;
 }
 
-TEST_CASE("CORSIKA_DATA", "[processes]") {
-
-  logging::set_level(logging::level::info);
-
-  SECTION("check CORSIKA_DATA") {
-
-    const char* CORSIKA_DATA = std::getenv("CORSIKA_DATA");
-    // these CHECKS are needed:
-    CHECK(CORSIKA_DATA != 0);
-    CHECK(boost::filesystem::is_directory(boost::filesystem::path(CORSIKA_DATA) /
-                                          "QGSJetII"));
-    CORSIKA_LOG_INFO(
-        "data: {}"
-        " isDir: {}"
-        "/QGSJetII",
-        CORSIKA_DATA, boost::filesystem::is_directory(CORSIKA_DATA));
-  }
-}
-
 TEST_CASE("QgsjetII", "[processes]") {
 
   logging::set_level(logging::level::info);
-- 
GitLab