From b36a435ac0577ea8eaa784e0dec7152e3e2afff4 Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Tue, 19 Sep 2023 22:37:19 +0200
Subject: [PATCH] Fix compilation if HDF5 is not present

---
 CMakeLists.txt                      | 24 ++++++++++++----------
 src/algebra/CMakeLists.txt          |  1 +
 src/analysis/CMakeLists.txt         |  5 +++++
 src/dev_utils/CMakeLists.txt        |  2 +-
 src/dev_utils/ParallelChecker.hpp   | 32 ++++++++++++++++++-----------
 src/language/modules/CMakeLists.txt |  2 +-
 src/language/utils/CMakeLists.txt   |  2 +-
 src/mesh/CMakeLists.txt             |  2 +-
 src/output/CMakeLists.txt           |  2 +-
 src/scheme/CMakeLists.txt           |  2 +-
 src/utils/CMakeLists.txt            |  2 +-
 tests/CMakeLists.txt                |  4 ++--
 12 files changed, 48 insertions(+), 32 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ef0a0f4fa..220eaf488 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -227,18 +227,20 @@ set(PUGS_ENABLE_HDF5 AUTO CACHE STRING
 if (PUGS_ENABLE_HDF5 MATCHES "^(AUTO|ON)$")
   # May be risky. (make to show pugs build options)
   find_package(HDF5)
-
-  # HighFive
-  set(HIGHFIVE_USE_BOOST  OFF)   # no Boost
-  set(HIGHFIVE_BUILD_DOCS OFF)   # no doc
-  set(HIGHFIVE_UNIT_TESTS OFF)   # no unit tests
-  set(HIGHFIVE_UNIT_TESTS OFF)   # no unit tests
-  set(HIGHFIVE_EXAMPLES OFF)     # no examples
-  set(HIGHFIVE_PARALLEL_HDF5 ON) # activate parallel HDF5
-  add_subdirectory(${PUGS_SOURCE_DIR}/packages/HighFive/)
-
+  if (HDF5_FOUND)
+    # HighFive
+    set(HIGHFIVE_USE_BOOST  OFF)   # no Boost
+    set(HIGHFIVE_BUILD_DOCS OFF)   # no doc
+    set(HIGHFIVE_UNIT_TESTS OFF)   # no unit tests
+    set(HIGHFIVE_UNIT_TESTS OFF)   # no unit tests
+    set(HIGHFIVE_EXAMPLES OFF)     # no examples
+    set(HIGHFIVE_PARALLEL_HDF5 ON) # activate parallel HDF5
+    add_subdirectory(${PUGS_SOURCE_DIR}/packages/HighFive/)
+    set(HIGHFIVE_TARGET HighFive)
+  endif()
   set(PUGS_HAS_HDF5 ${HDF5_FOUND})
 else()
+  unset(HIGHFIVE_TARGET)
   unset(PUGS_HAS_HDF5)
 endif()
 
@@ -615,7 +617,7 @@ target_link_libraries(
   ${KOKKOS_CXX_FLAGS}
   ${OPENMP_LINK_FLAGS}
   ${PUGS_STD_LINK_FLAGS}
-  HighFive
+  ${HIGHFIVE_TARGET}
   stdc++fs
   )
 
diff --git a/src/algebra/CMakeLists.txt b/src/algebra/CMakeLists.txt
index 6310f48dc..74f40290a 100644
--- a/src/algebra/CMakeLists.txt
+++ b/src/algebra/CMakeLists.txt
@@ -11,4 +11,5 @@ target_link_libraries(
   PugsAlgebra
   ${PETSC_LIBRARIES}
   ${SLEPC_LIBRARIES}
+  ${HIGHFIVE_TARGET}
 )
diff --git a/src/analysis/CMakeLists.txt b/src/analysis/CMakeLists.txt
index b675ea696..4ab57395b 100644
--- a/src/analysis/CMakeLists.txt
+++ b/src/analysis/CMakeLists.txt
@@ -12,6 +12,11 @@ add_library(
   TetrahedronGaussQuadrature.cpp
   TriangleGaussQuadrature.cpp)
 
+target_link_libraries(
+  PugsAnalysis
+  ${HIGHFIVE_TARGET}
+)
+
 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
   if((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "12.0.0") AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "13.0.0"))
     # Deactivated since it produces false positive warning in this file only ...
diff --git a/src/dev_utils/CMakeLists.txt b/src/dev_utils/CMakeLists.txt
index 7d709b9f7..c603f0d1d 100644
--- a/src/dev_utils/CMakeLists.txt
+++ b/src/dev_utils/CMakeLists.txt
@@ -7,5 +7,5 @@ add_library(
 
 target_link_libraries(
   PugsDevUtils
-  HighFive
+  ${HIGHFIVE_TARGET}
 )
diff --git a/src/dev_utils/ParallelChecker.hpp b/src/dev_utils/ParallelChecker.hpp
index 4db7c33b7..82525d00f 100644
--- a/src/dev_utils/ParallelChecker.hpp
+++ b/src/dev_utils/ParallelChecker.hpp
@@ -1,7 +1,11 @@
 #ifndef PARALLEL_CHECKER_HPP
 #define PARALLEL_CHECKER_HPP
 
+#include <utils/pugs_config.hpp>
+#ifdef PUGS_HAS_HDF5
 #include <highfive/H5File.hpp>
+#endif   // PUGS_HAS_HDF5
+
 #include <mesh/Connectivity.hpp>
 #include <mesh/ItemArrayVariant.hpp>
 #include <mesh/ItemValueVariant.hpp>
@@ -33,7 +37,17 @@ class ParallelChecker
     write
   };
 
-#ifdef PUGS_HAS_HDF5
+ private:
+  static ParallelChecker* m_instance;
+
+  Mode m_mode  = Mode::automatic;
+  size_t m_tag = 0;
+
+  std::string m_filename = "testme/parallel_checker.h5";
+
+  ParallelChecker() = default;
+
+ public:
   template <typename DataType, ItemType item_type, typename ConnectivityPtr>
   friend void parallel_check(const ItemValue<DataType, item_type, ConnectivityPtr>& item_value,
                              const std::string& name,
@@ -44,6 +58,7 @@ class ParallelChecker
                              const std::string& name,
                              const SourceLocation& source_location);
 
+#ifdef PUGS_HAS_HDF5
  private:
   template <typename T>
   struct TinyVectorDataType;
@@ -73,15 +88,6 @@ class ParallelChecker
     }
   };
 
-  static ParallelChecker* m_instance;
-
-  Mode m_mode  = Mode::automatic;
-  size_t m_tag = 0;
-
-  std::string m_filename = "testme/parallel_checker.h5";
-
-  ParallelChecker() = default;
-
   HighFive::File
   _createOrOpenFileRW() const
   {
@@ -332,6 +338,7 @@ class ParallelChecker
         const std::string& name,
         const SourceLocation& source_location)
   {
+    HighFive::SilenceHDF5 m_silence_hdf5{true};
     this->_printHeader(name, source_location);
 
     try {
@@ -368,6 +375,7 @@ class ParallelChecker
         const std::string& name,
         const SourceLocation& source_location)
   {
+    HighFive::SilenceHDF5 m_silence_hdf5{true};
     this->_printHeader(name, source_location);
 
     try {
@@ -404,6 +412,7 @@ class ParallelChecker
           const std::string& name,
           const SourceLocation& source_location)
   {
+    HighFive::SilenceHDF5 m_silence_hdf5{true};
     this->_printHeader(name, source_location);
 
     try {
@@ -524,6 +533,7 @@ class ParallelChecker
           const std::string& name,
           const SourceLocation& source_location)
   {
+    HighFive::SilenceHDF5 m_silence_hdf5{true};
     this->_printHeader(name, source_location);
 
     try {
@@ -709,7 +719,6 @@ parallel_check(const ItemArray<DataType, item_type, ConnectivityPtr>& item_array
                const std::string& name,
                const SourceLocation& source_location)
 {
-  HighFive::SilenceHDF5 m_silence_hdf5{true};
   if (ParallelChecker::instance().isWriting()) {
     ParallelChecker::instance().write(item_array, name, source_location);
   } else {
@@ -723,7 +732,6 @@ parallel_check(const ItemValue<DataType, item_type, ConnectivityPtr>& item_value
                const std::string& name,
                const SourceLocation& source_location)
 {
-  HighFive::SilenceHDF5 m_silence_hdf5{true};
   if (ParallelChecker::instance().isWriting()) {
     ParallelChecker::instance().write(item_value, name, source_location);
   } else {
diff --git a/src/language/modules/CMakeLists.txt b/src/language/modules/CMakeLists.txt
index 2e92ff898..5f87dcf34 100644
--- a/src/language/modules/CMakeLists.txt
+++ b/src/language/modules/CMakeLists.txt
@@ -19,7 +19,7 @@ add_library(
 
 target_link_libraries(
   PugsLanguageModules
-  HighFive
+  ${HIGHFIVE_TARGET}
 )
 
 add_dependencies(
diff --git a/src/language/utils/CMakeLists.txt b/src/language/utils/CMakeLists.txt
index 9cf233850..33a41962e 100644
--- a/src/language/utils/CMakeLists.txt
+++ b/src/language/utils/CMakeLists.txt
@@ -48,5 +48,5 @@ add_dependencies(PugsLanguageUtils
 
 target_link_libraries(
   PugsLanguageUtils
-  HighFive
+  ${HIGHFIVE_TARGET}
 )
diff --git a/src/mesh/CMakeLists.txt b/src/mesh/CMakeLists.txt
index 55dffcca0..9b82b62f1 100644
--- a/src/mesh/CMakeLists.txt
+++ b/src/mesh/CMakeLists.txt
@@ -45,5 +45,5 @@ add_library(
 
 target_link_libraries(
   PugsMesh
-  HighFive
+  ${HIGHFIVE_TARGET}
 )
diff --git a/src/output/CMakeLists.txt b/src/output/CMakeLists.txt
index 12b22d01f..051e96480 100644
--- a/src/output/CMakeLists.txt
+++ b/src/output/CMakeLists.txt
@@ -9,5 +9,5 @@ add_library(
 
 target_link_libraries(
   PugsOutput
-  HighFive
+  ${HIGHFIVE_TARGET}
 )
diff --git a/src/scheme/CMakeLists.txt b/src/scheme/CMakeLists.txt
index 5d49a9177..dadbe73bb 100644
--- a/src/scheme/CMakeLists.txt
+++ b/src/scheme/CMakeLists.txt
@@ -14,5 +14,5 @@ add_library(
 
 target_link_libraries(
   PugsScheme
-  HighFive
+  ${HIGHFIVE_TARGET}
 )
diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt
index 8a32abb36..ca07c7dc6 100644
--- a/src/utils/CMakeLists.txt
+++ b/src/utils/CMakeLists.txt
@@ -24,7 +24,7 @@ target_link_libraries(
   PugsUtils
   ${PETSC_LIBRARIES}
   ${SLEPC_LIBRARIES}
-  HighFive
+  ${HIGHFIVE_TARGET}
 )
 
 # --------------- get git revision info ---------------
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index c47e88837..04a7ae9e9 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -232,7 +232,7 @@ target_link_libraries (unit_tests
   ${PETSC_LIBRARIES}
   Catch2
   ${PUGS_STD_LINK_FLAGS}
-  HighFive
+  ${HIGHFIVE_TARGET}
   stdc++fs
   )
 
@@ -261,7 +261,7 @@ target_link_libraries (mpi_unit_tests
   ${PETSC_LIBRARIES}
   Catch2
   ${PUGS_STD_LINK_FLAGS}
-  HighFive
+  ${HIGHFIVE_TARGET}
   stdc++fs
   )
 
-- 
GitLab