Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: Fix some issues when linking against netcdf-C #435

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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