From 62b32dce0cbf1bc070f702d395e554d83f105aa3 Mon Sep 17 00:00:00 2001
From: Mathieu Westphal <mathieu.westphal@kitware.com>
Date: Wed, 26 Feb 2025 14:43:38 +0100
Subject: [PATCH] CMake: Add proper support for optional linking to netcdf and
 eccodes

---
 CMakeLists.txt       | 3 +++
 app/CMakeLists.txt   | 2 +-
 src/CMakeLists.txt   | 2 +-
 tests/CMakeLists.txt | 4 ++--
 4 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5054cd361..610bdc256 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,6 +17,7 @@ if(${CDI_PTHREAD})
   find_package(Threads REQUIRED)
   set_target_properties(Threads::Threads PROPERTIES THREADS_PREFER_PTHREAD_FLAG TRUE)
   list(APPEND cdi_compile_defs HAVE_PTHREAD=1)
+  list(APPEND cdi_linked_libs pthread)
 endif()
 
 # enable default internal libs
@@ -53,6 +54,7 @@ if(${CDI_ECCODES} OR eccodes_ROOT)
   if (${eccodes_FOUND})
     list(APPEND cdi_compile_defs HAVE_LIBGRIB_API=${eccodes_FOUND})
     message(VERBOSE "added compile definition HAVE_LIBGRIB_API=${eccodes_FOUND}")
+    list(APPEND cdi_linked_libs eccodes)
   else()
     message(WARNING "eccodes not found, compiling without eccodes")
   endif ()
@@ -70,6 +72,7 @@ if(${CDI_NETCDF} OR netCDF_ROOT )
       HAVE_LIBGRIB_API=${netCDF_FOUND}
       HAVE_LIBGRIB=${netCDF_FOUND}
     )
+    list(APPEND cdi_linked_libs netCDF::netcdf)
   else()
     message(WARNING "netcdf target not found, compiling without netcdf")
   endif ()
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index ff3047c99..4264972e7 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -7,6 +7,6 @@ target_include_directories(cdi PRIVATE
   $<INSTALL_INTERFACE:include
 )
 
-target_link_libraries(cdi PRIVATE cdilib netCDF::netcdf pthread)
+target_link_libraries(cdi PRIVATE cdilib ${cdi_linked_libs})
 target_include_directories(cdi PRIVATE "${PROJECT_BINARY_DIR}/src/config.h")
 target_compile_definitions(cdi PRIVATE  ${cdi_netcdf_definitions})
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8328f34e5..f21e7c023 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -199,7 +199,7 @@ add_library(cdilib
 )
 
 target_include_directories(cdilib PRIVATE "${PROJECT_BINARY_DIR}/src/config.h")
-target_link_libraries(cdilib eccodes netCDF::netcdf)
+target_link_libraries(cdilib ${cdi_linked_libs})
 target_compile_definitions(cdilib PRIVATE PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}" ${cdi_compile_defs})
 
 add_library(cdilib::cdilib ALIAS cdilib)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index cf18bbac6..2e02cf49b 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -31,11 +31,11 @@ list(APPEND test_lib_src
 
 add_library(test_lib ${test_lib_src})
 target_include_directories(test_lib PRIVATE ../src/)
-target_link_libraries(test_lib PRIVATE cdilib eccodes)
+target_link_libraries(test_lib PRIVATE cdilib ${cdi_linked_libs})
 
 foreach(target ${current_targets})
   target_include_directories(${target} PRIVATE ../src/)
-  target_link_libraries(${target} PRIVATE cdilib test_lib ${netCDF_LIBRARIES} )
+  target_link_libraries(${target} PRIVATE cdilib test_lib ${cdi_linked_libs})
   add_test(NAME ${target} COMMAND ${target})
   if(labels_${target})
     set_property(TEST ${target} PROPERTY LABELS labels_${target})
-- 
GitLab