From 0e8861e086a535cc6bb2cfedb29794dc618f1ed2 Mon Sep 17 00:00:00 2001
From: Oliver Heidmann <oliver.heidmann@mpimet.mpg.de>
Date: Mon, 3 Mar 2025 15:16:28 +0100
Subject: [PATCH] removed usage of lists and replaced it by target specific
 cmake routines

---
 CMakeLists.txt     |  24 +++++-----
 src/CMakeLists.txt | 114 ++++++++++++++++++++++++---------------------
 2 files changed, 74 insertions(+), 64 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fd2272523..0f2fd15fc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,36 +19,34 @@ if(${CDI_PTHREAD})
   include(FindThreads)
   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)
+  target_compile_definitions(cdilib PRIVATE HAVE_PTHREAD=1)
+  target_link_libraries(cdilib pthread)
 endif()
 
 # enable default internal libs
 option(CDI_LIBGRIB "GRIB support [default=ON]" ON)
 if(${CDI_LIBGRIB})
-  list(APPEND cdi_compile_defs
-       HAVE_LIBGRIB=1
-       HAVE_LIBGRIB_API=1)
+  target_compile_definitions(cdilib PRIVATE HAVE_LIBGRIB=1 HAVE_LIBGRIB_API=1)
 endif()
 
 option(CDI_LIBGRIBEX "Use the CGRIBEX library [default=ON]" ON)
 if(${CDI_LIBGRIBEX})
-  list(APPEND cdi_compile_defs LIBCGRIBEX=1)
+  target_compile_definitions(cdilib PRIVATE LIBCGRIBEX=1)
 endif()
 
 option(CDI_EXTRA "Use the extra library [default=ON]" ON)
 if(${CDI_EXTRA})
-  list(APPEND cdi_compile_defs HAVE_LIBEXTRA=1)
+  target_compile_definitions(cdilib PRIVATE HAVE_LIBEXTRA=1)
 endif()
 
 option(CDI_IEG "Use the extra library [default=ON]" ON)
 if(${CDI_IEG})
-  list(APPEND cdi_compile_defs HAVE_LIBIEG=1)
+  target_compile_definitions(cdilib PRIVATE HAVE_LIBIEG=1)
 endif()
 
 option(CDI_SERVICE "Use the extra library [default=ON]" ON)
 if(${CDI_SERVICE})
-  list(APPEND cdi_compile_defs HAVE_LIBSERVICE=1)
+  target_compile_definitions(cdilib PRIVATE HAVE_LIBSERVICE=1)
 endif()
 
 # ecCodes
@@ -56,7 +54,9 @@ option(CDI_ECCODES "Use the eccodes library [default=ON]" ON)
 if(${CDI_ECCODES} OR eccodes_ROOT)
   find_package(eccodes REQUIRED)
   if (${eccodes_FOUND})
-    list(APPEND cdi_linked_libs eccodes)
+    target_compile_definitions(cdilib PRIVATE HAVE_LIBGRIB_API=${eccodes_FOUND})
+    message(VERBOSE "added compile definition HAVE_LIBGRIB_API=${eccodes_FOUND}")
+    target_link_libraries(cdilib eccodes)
   endif ()
 endif()
 
@@ -70,7 +70,7 @@ if(${CDI_NETCDF} OR netCDF_ROOT )
       HAVE_LIBNC_DAP=${netCDF_FOUND}
       HAVE_NETCDF4=${netCDF_FOUND}
     )
-    list(APPEND cdi_linked_libs netCDF::netcdf)
+    target_link_libraries(cdilib netCDF::netcdf)
   endif ()
 endif()
 
@@ -78,7 +78,7 @@ message(VERBOSE "looking for config.h in: ${PROJECT_BINARY_DIR}/src")
 
 #adding subdirectories
 ## lib and general files
-list(APPEND cdi_compile_defs CDI=1 CDI_SIZE_TYPE=size_t PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}")
+target_compile_definitions(cdilib PRIVATE CDI=1 CDI_SIZE_TYPE=size_t PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}")
 add_subdirectory(src)
 
 #app
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 250f7a928..1fc148b5e 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,4 +1,5 @@
-list( APPEND cdi_src_files
+
+target_sources(cdilib PRIVATE
   async_worker.c
   async_worker.h
   basetime.c
@@ -139,69 +140,78 @@ list( APPEND cdi_src_files
   zaxis.c
   zaxis.h
 )
-list( APPEND cdi_pio_files
-  cdipio.h
-  pio.c
-  pio.h
-  pio_cdf_int.c
-  pio_cdf_int.h
-  pio_client.c
-  pio_client.h
-  pio_comm.c
-  pio_comm.h
-  pio_conf.c
-  pio_conf.h
-  pio_dbuffer.c
-  pio_dbuffer.h
-  pio_dist_grid.c
-  pio_dist_grid.h
-  pio_id_set.h
-  pio_idxlist_cache.c
-  pio_idxlist_cache.h
-  pio_impl.h
-  pio_interface.c
-  pio_interface.h
-  pio_mpi_fw_at_all.c
-  pio_mpi_fw_at_reblock.c
-  pio_mpi_fw_ordered.c
-  pio_mpinonb.c
-  pio_posixasynch.c
-  pio_posixfpguardsendrecv.c
-  pio_posixnonb.c
-  pio_record_send.c
-  pio_roles.c
-  pio_rpc.c
-  pio_rpc.h
-  pio_serialize.c
-  pio_serialize.h
-  pio_server.c
-  pio_server.h
-  pio_util.c
-  pio_util.h
-  pio_xmap_cache.c
-  pio_xmap_cache.h
-)
 
-list( APPEND cdi_unknown
-  cfortran.h
-  getline.c
-  make_fint.c
-  resource_unpack.c
-  resource_unpack.h
-)
+option(CDI_BUILD_PIO "Build unknown sources in libcdi" OFF)
+mark_as_advanced(CDI_BUILD_PIO)
+if (CDI_BUILD_PIO)
+  target_sources(cdilib PRIVATE
+    cdipio.h
+    pio.c
+    pio.h
+    pio_cdf_int.c
+    pio_cdf_int.h
+    pio_client.c
+    pio_client.h
+    pio_comm.c
+    pio_comm.h
+    pio_conf.c
+    pio_conf.h
+    pio_dbuffer.c
+    pio_dbuffer.h
+    pio_dist_grid.c
+    pio_dist_grid.h
+    pio_id_set.h
+    pio_idxlist_cache.c
+    pio_idxlist_cache.h
+    pio_impl.h
+    pio_interface.c
+    pio_interface.h
+    pio_mpi_fw_at_all.c
+    pio_mpi_fw_at_reblock.c
+    pio_mpi_fw_ordered.c
+    pio_mpinonb.c
+    pio_posixasynch.c
+    pio_posixfpguardsendrecv.c
+    pio_posixnonb.c
+    pio_record_send.c
+    pio_roles.c
+    pio_rpc.c
+    pio_rpc.h
+    pio_serialize.c
+    pio_serialize.h
+    pio_server.c
+    pio_server.h
+    pio_util.c
+    pio_util.h
+    pio_xmap_cache.c
+    pio_xmap_cache.h
+  )
+endif()
 
 option(CDI_BUILD_UNKNOWN "Build unknown sources in libcdi" ON)
 mark_as_advanced(CDI_BUILD_UNKNOWN)
 if (CDI_BUILD_UNKNOWN)
-  list(APPEND cdi_src_files ${cdi_unknown})
+  target_sources( cdilib PRIVATE
+    cfortran.h
+    getline.c
+    make_fint.c
+    resource_unpack.c
+    resource_unpack.h
+  )
 endif ()
 
-# Support exporting all symbolds on Windows
+# Support exporting all symbols on Windows
 set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
 
+set_property(TARGET cdilib PROPERTY CDI_INSTALL_HEADERS
+  ${CMAKE_CURRENT_SOURCE_DIR}/cdi.h
+  ${CMAKE_CURRENT_SOURCE_DIR}/calendar.h
+  ${CMAKE_CURRENT_SOURCE_DIR}/cdi_datetime.h
+  ${CMAKE_CURRENT_SOURCE_DIR}/julian_date.h
 )
 
 target_include_directories(cdilib PRIVATE "${PROJECT_BINARY_DIR}/src/config.h")
+
 target_link_libraries(cdilib ${cdi_linked_libs})
 target_compile_definitions(cdilib PRIVATE PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}" ${cdi_compile_defs})
 
-- 
GitLab