Skip to content

Commit

Permalink
Merge pull request #435 from ZedThree/fix-cmake-netcdf-c-linking
Browse files Browse the repository at this point in the history
CMake: Fix some issues when linking against netcdf-C
  • Loading branch information
WardF authored Mar 14, 2024
2 parents 624858f + b61fc19 commit 9c4cb9b
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions fortran/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ SET(netcdff_SOURCES
)

# Check if netcdf-c has parallel I/O feature enabled
IF (NETCDF_C_INCLUDE_DIR)
if (NETCDF_C_INCLUDE_DIR)
file(READ "${NETCDF_C_INCLUDE_DIR}/netcdf_meta.h" header)
string(REGEX MATCH "#define NC_HAS_PARALLEL *[01]" macrodef "${header}")
string(REGEX MATCH "[01]" HAVE_PARALLEL "${macrodef}")
IF (HAVE_PARALLEL)
SET(netcdff_SOURCES ${netcdff_SOURCES} nf_nc.F90)
MESSAGE(STATUS "Whether NetCDF-C built with paralle I/O enabled: yes")
ELSE()
SET(netcdff_SOURCES ${netcdff_SOURCES} nf_nc_noparallel.F90)
MESSAGE(STATUS "Whether NetCDF-C built with paralle I/O enabled: no")
ENDIF(HAVE_PARALLEL)
ENDIF(NETCDF_C_INCLUDE_DIR)
else()
# Probably using CMake config file directly
set(HAVE_PARALLEL ${netCDF_HAS_PARALLEL})
endif(NETCDF_C_INCLUDE_DIR)

if (HAVE_PARALLEL)
set(netcdff_SOURCES ${netcdff_SOURCES} nf_nc.F90)
# Just for a more readable message
set(HAVE_PARALLEL "yes")
else()
set(netcdff_SOURCES ${netcdff_SOURCES} nf_nc_noparallel.F90)
set(HAVE_PARALLEL "no")
endif(HAVE_PARALLEL)
message(STATUS "Whether NetCDF-C built with parallel I/O enabled: ${HAVE_PARALLEL}")

IF (USE_NETCDF4)
SET(netcdff_SOURCES ${netcdff_SOURCES}
Expand Down Expand Up @@ -141,24 +147,17 @@ IF (BUILD_SHARED_LIBS)
ENDIF()

# This is what we are building: a convenience library of Fortran 2003 functions.
# This just builds SHARED, even though STATIC is also specified
# ADD_LIBRARY(netcdff STATIC SHARED ${netcdff_SOURCES})
# Builds only static, not shared
# Compile C-code to object, then link with Fortran
ADD_LIBRARY(netcdff_c OBJECT ${netcdff_C_SOURCES})
add_library(netcdff_c OBJECT ${netcdff_C_SOURCES})
install(TARGETS netcdff_c OBJECTS DESTINATION lib)
# or is this better? list(APPEND NETCDF_C_LIBRARY netcdff_c)
SET(NETCDF_C_LIBRARY ${NETCDF_C_LIBRARY} netcdff_c)
set_target_properties(netcdff_c PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${NETCDF_C_INCLUDE_DIR}"
)
target_include_directories(netcdff_c PUBLIC "${NETCDF_C_INCLUDE_DIR}")
target_link_libraries(netcdff_c PUBLIC netCDF::netcdf)

ADD_LIBRARY(netcdff ${netcdff_SOURCES})
TARGET_LINK_LIBRARIES(netcdff PUBLIC netCDF::netcdf)
TARGET_LINK_LIBRARIES(netcdff PRIVATE ${NETCDF_C_LIBRARY})
TARGET_LINK_LIBRARIES(netcdff PUBLIC ${EXTRA_DEPS})
SET_TARGET_PROPERTIES(netcdff PROPERTIES
add_library(netcdff ${netcdff_SOURCES})
target_link_libraries(netcdff
PUBLIC netCDF::netcdf ${EXTRA_DEPS}
PRIVATE netcdff_c
)
set_target_properties(netcdff PROPERTIES
VERSION ${NC4F_LIB_VERSION}
SOVERSION ${NC4F_SO_VERSION}
)
Expand Down

0 comments on commit 9c4cb9b

Please sign in to comment.