diff --git a/CHANGELOG.md b/CHANGELOG.md index e7c62005..aa502de8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed + +- Added support for Baselibs built with netCDF as a shared library +- Add `NETCDF_INCLUDE_DIR` as alias to `INC_NETCDF` for spack compatibility + ### Fixed - Updated the CI to work with latest Baselibs diff --git a/external_libraries/FindBaselibs.cmake b/external_libraries/FindBaselibs.cmake index 18e87380..dea33829 100644 --- a/external_libraries/FindBaselibs.cmake +++ b/external_libraries/FindBaselibs.cmake @@ -121,6 +121,7 @@ if (Baselibs_FOUND) set (INC_HDF5 ${BASEDIR}/include/hdf5) set (INC_NETCDF ${BASEDIR}/include/netcdf) + set (NETCDF_INCLUDE_DIR ${INC_NETCDF}) set (INC_HDF ${BASEDIR}/include/hdf) # Need to do a bit of kludgy stuff here to allow Fortran linker to @@ -183,22 +184,34 @@ if (Baselibs_FOUND) # We also need to append the pthread flag at link time list(APPEND NETCDF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) + # Now we try to detect if the netcdf library was linked statically or + # dynamically by looking for hdf5 in NETCDF_LIBRARIES. Could be fragile, + # but nf-config --flibs for a shared build should never have libhdf5 + if ("hdf5" IN_LIST NETCDF_LIBRARIES) + set(NETCDF_LIBRARY_TYPE STATIC) + set(NETCDF_LIBRARY_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) + else () + set(NETCDF_LIBRARY_TYPE SHARED) + set(NETCDF_LIBRARY_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) + endif () + message(STATUS "Detected that NetCDF in Baselibs was built as ${NETCDF_LIBRARY_TYPE}") + # Create targets # - NetCDF C - add_library(NetCDF::NetCDF_C STATIC IMPORTED) + add_library(NetCDF::NetCDF_C ${NETCDF_LIBRARY_TYPE} IMPORTED) set_target_properties(NetCDF::NetCDF_C PROPERTIES - IMPORTED_LOCATION ${BASEDIR}/lib/libnetcdf.a - INTERFACE_INCLUDE_DIRECTORIES "${INC_NETCDF}" + IMPORTED_LOCATION ${BASEDIR}/lib/libnetcdf${NETCDF_LIBRARY_SUFFIX} + INTERFACE_INCLUDE_DIRECTORIES "${NETCDF_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "${NETCDF_LIBRARIES}" INTERFACE_LINK_DIRECTORIES "${BASEDIR}/lib" ) set(NetCDF_C_FOUND TRUE CACHE BOOL "NetCDF C Found" FORCE) # - NetCDF Fortran - add_library(NetCDF::NetCDF_Fortran STATIC IMPORTED) + add_library(NetCDF::NetCDF_Fortran ${NETCDF_LIBRARY_TYPE} IMPORTED) set_target_properties(NetCDF::NetCDF_Fortran PROPERTIES - IMPORTED_LOCATION ${BASEDIR}/lib/libnetcdff.a - INTERFACE_INCLUDE_DIRECTORIES "${INC_NETCDF}" + IMPORTED_LOCATION ${BASEDIR}/lib/libnetcdff${NETCDF_LIBRARY_SUFFIX} + INTERFACE_INCLUDE_DIRECTORIES "${NETCDF_INCLUDE_DIR}" INTERFACE_LINK_LIBRARIES "${NETCDF_LIBRARIES}" INTERFACE_LINK_DIRECTORIES "${BASEDIR}/lib" ) diff --git a/python/f2py/UseF2Py.cmake b/python/f2py/UseF2Py.cmake index 816286c2..a4d02666 100644 --- a/python/f2py/UseF2Py.cmake +++ b/python/f2py/UseF2Py.cmake @@ -175,7 +175,7 @@ macro (add_f2py_module _name) if (Baselibs_FOUND) # include dirs - foreach(_dir ${INC_NETCDF}) + foreach(_dir ${NETCDF_INCLUDE_DIR}) list(APPEND _inc_opts "-I${_dir}") endforeach() diff --git a/python/f2py2/UseF2Py2.cmake b/python/f2py2/UseF2Py2.cmake index 50e61de8..babff69c 100644 --- a/python/f2py2/UseF2Py2.cmake +++ b/python/f2py2/UseF2Py2.cmake @@ -175,7 +175,7 @@ macro (add_f2py2_module _name) if (Baselibs_FOUND) # include dirs - foreach(_dir ${INC_NETCDF}) + foreach(_dir ${NETCDF_INCLUDE_DIR}) list(APPEND _inc_opts "-I${_dir}") endforeach() diff --git a/python/f2py3/UseF2Py3.cmake b/python/f2py3/UseF2Py3.cmake index 98464bc1..141b608d 100644 --- a/python/f2py3/UseF2Py3.cmake +++ b/python/f2py3/UseF2Py3.cmake @@ -175,7 +175,7 @@ macro (add_f2py3_module _name) if (Baselibs_FOUND) # include dirs - foreach(_dir ${INC_NETCDF}) + foreach(_dir ${NETCDF_INCLUDE_DIR}) list(APPEND _inc_opts "-I${_dir}") endforeach()