From acd163db40f22f626be315efb6b04fbb09914fb7 Mon Sep 17 00:00:00 2001
From: ralfulrich <ralf.ulrich@kit.edu>
Date: Thu, 1 Oct 2020 23:19:17 +0200
Subject: [PATCH] use FindZLIB to build cnpy and dependencies

---
 Framework/Utilities/CMakeLists.txt | 40 +++++++++++++++++++++---------
 ThirdParty/CMakeLists.txt          | 13 ++++++++--
 ThirdParty/cnpy/CMakeLists.txt     | 19 ++------------
 3 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/Framework/Utilities/CMakeLists.txt b/Framework/Utilities/CMakeLists.txt
index b1c8ebed0..5618ab4a8 100644
--- a/Framework/Utilities/CMakeLists.txt
+++ b/Framework/Utilities/CMakeLists.txt
@@ -31,9 +31,27 @@ set (
   sgn.h
   CorsikaFenv.h
   MetaProgramming.h
-  SaveBoostHistogram.hpp
   )
 
+set (
+  UTILITIES_DEPENDS
+  CORSIKAgeometry
+  CORSIKAunits
+  C8::ext::boost # so far only for MetaProgramming
+  C8::ext::eigen3 # for COMboost
+  )
+
+if (TARGET cnpy)
+  LIST (APPEND
+    UTILITIES_HEADERS
+    SaveBoostHistogram.hpp
+    )
+  LIST (APPEND
+    UTILITIES_DEPENDS
+    cnpy # for SaveBoostHistogram
+    )
+endif (TARGET cnpy)
+  
 set (
   UTILITIES_NAMESPACE
   corsika/utl
@@ -53,11 +71,7 @@ set_target_properties (
 # target dependencies on other libraries (also the header onlys)
 target_link_libraries (
   CORSIKAutilities
-  CORSIKAgeometry
-  CORSIKAunits
-  C8::ext::boost # so far only for MetaProgramming
-  C8::ext::eigen3 # for COMboost
-  cnpy # for SaveBoostHistogram
+  ${UTILITIES_DEPENDS}
   )
 
 target_include_directories (
@@ -91,9 +105,11 @@ target_link_libraries (
   CORSIKAtesting
 )
 
-CORSIKA_ADD_TEST(testSaveBoostHistogram)
-target_link_libraries (
-  testSaveBoostHistogram
-  CORSIKAutilities
-  CORSIKAtesting
-)
+if (TARGET cnpy)
+  CORSIKA_ADD_TEST(testSaveBoostHistogram)
+  target_link_libraries (
+    testSaveBoostHistogram
+    CORSIKAutilities
+    CORSIKAtesting
+    )
+endif (TARGET cnpy)
diff --git a/ThirdParty/CMakeLists.txt b/ThirdParty/CMakeLists.txt
index 056f7074c..b30fdf95b 100644
--- a/ThirdParty/CMakeLists.txt
+++ b/ThirdParty/CMakeLists.txt
@@ -35,7 +35,7 @@ message (STATUS "USE_BOOST_C8='${USE_BOOST_C8}'")
 
 add_library (C8::ext::boost INTERFACE IMPORTED GLOBAL)
 if ("x_${USE_BOOST_C8}" STREQUAL "x_SYSTEM")
-  find_package (Boost REQUIRED mp11 iterator core format interval optional type_index histogram)
+  find_package (Boost REQUIRED COMPONENTS mp11 iterator core format interval optional type_index histogram)
 
   message (STATUS "Using system-level boost version ${Boost_VERSION} at ${Boost_INCLUDE_DIR}")
   set_target_properties (
@@ -300,4 +300,13 @@ else (Boost_IOSTREAMS_FOUND)
     )
 endif (Boost_IOSTREAMS_FOUND)
 
-add_subdirectory(cnpy)
+
+# libz needed for cnpy, used for SaveHistograms
+find_package (ZLIB QUIET)
+
+if (ZLIB_FOUND)
+  message (STATUS "Found ZLIB. Build cnpy for SaveHistograms")  
+  add_subdirectory (cnpy)
+else (ZLIB_FOUND)
+  message (WARNING "Did not find ZLIB. Cannot build cnpy for SaveHistograms")  
+endif (ZLIB_FOUND)
diff --git a/ThirdParty/cnpy/CMakeLists.txt b/ThirdParty/cnpy/CMakeLists.txt
index 868aafd1d..4a115728d 100644
--- a/ThirdParty/cnpy/CMakeLists.txt
+++ b/ThirdParty/cnpy/CMakeLists.txt
@@ -21,15 +21,13 @@ set_target_properties (
   PROPERTIES
   VERSION ${PROJECT_VERSION}
   SOVERSION 1
-#  PUBLIC_HEADER "${MODEL_HEADERS}"
   )
 
-find_package(ZLIB)
-
 # target dependencies on other libraries (also the header onlys)
 target_link_libraries (
   cnpy
-  ${ZLIB_LIBRARIES}
+  PUBLIC
+  ZLIB::ZLIB
   )
 
 target_include_directories (
@@ -43,18 +41,5 @@ install (
   TARGETS cnpy
   LIBRARY DESTINATION lib
   ARCHIVE DESTINATION lib
-#  PUBLIC_HEADER DESTINATION include/${MODEL_NAMESPACE}
   )
 
-
-# --------------------
-# code unit testing
-# CORSIKA_ADD_TEST(testNullModel)
-#target_link_libraries (
-#  testNullModel  ProcessNullModel
-#  CORSIKAsetup
-#  CORSIKAgeometry
-#  CORSIKAunits
-#  CORSIKAthirdparty # for catch2
-#  )
-
-- 
GitLab