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

Windows compile #268

Merged
merged 3 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ ENDIF()
IF (netCDF_FOUND)
SET(NETCDF_C_LIBRARY ${netCDF_LIBRARIES})
SET(NETCDF_C_INCLUDE_DIR ${netCDF_INCLUDE_DIR})
include_directories(${NETCDF_C_INCLUDE_DIR})
message(STATUS "Found netCDF CMake package: ${NETCDF_C_LIBRARY}")
ELSE()
# netCDF not properly packaged. Try to find it manually.
Expand Down Expand Up @@ -490,7 +491,6 @@ endif()

###
# Check for various header files
###
CHECK_INCLUDE_FILE("alloca.h" HAVE_ALLOCA_H)
CHECK_INCLUDE_FILE("malloc.h" HAVE_MALLOC_H)
CHECK_INCLUDE_FILE("ctype.h" HAVE_CTYPE_H)
Expand Down Expand Up @@ -522,6 +522,7 @@ CHECK_INCLUDE_FILE("endian.h" HAVE_ENDIAN_H)
CHECK_INCLUDE_FILE("BaseTsd.h" HAVE_BASETSD_H)
CHECK_INCLUDE_FILE("stddef.h" HAVE_STDDEF_H)
CHECK_INCLUDE_FILE("memory.h" HAVE_MEMORY_H)
SET(CMAKE_REQUIRED_INCLUDES ${NETCDF_C_INCLUDE_DIR})
CHECK_INCLUDE_FILE("netcdf.h" HAVE_NETCDF_H)


Expand Down
42 changes: 38 additions & 4 deletions fortran/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@ SET(netcdff_SOURCES
nf_varmio.F90 nf_varsio.F90
)

SET(netcdff_SOURCES ${netcdff_SOURCES} nf_nc.f90)
# Check if netcdf-c has parallel I/O functions
check_symbol_exists(nc_create_par "netcdf.h" HAVE_PARALLEL)
message (STATUS "found netcdf-c parallel support: ${HAVE_PARALLEL}")
IF (${USE_NETCDF4} AND HAVE_PARALLEL)
SET(netcdff_SOURCES ${netcdff_SOURCES} nf_nc.f90)
message (STATUS "including netcdf-f parallel support")
ELSE ()
SET(netcdff_SOURCES ${netcdff_SOURCES} nf_nc_noparallel.f90)
message (STATUS "no netcdf-f parallel support")
ENDIF(${USE_NETCDF4} AND HAVE_PARALLEL)

IF (USE_NETCDF4)
SET(netcdff_SOURCES ${netcdff_SOURCES}
module_netcdf4_nc_interfaces.f90
module_netcdf4_nf_interfaces.F90 module_netcdf4_f03.f90
nf_lib.c nf_nc4.f90
nf_nc4.f90
)
SET(netcdff_C_SOURCES ${netcdff_C_SOURCES} nf_lib.c)
ENDIF(USE_NETCDF4)


IF (${USE_LOGGING})
SET(netcdff_SOURCES ${netcdff_SOURCES}
nf_logging.F90
Expand Down Expand Up @@ -52,8 +63,9 @@ ENDIF(${USE_NETCDF4})

if (${BUILD_V2})
SET(netcdff_SOURCES ${netcdff_SOURCES}
nf_v2compat.c module_netcdf_fortv2_c_interfaces.f90 nf_fortv2.f90
module_netcdf_fortv2_c_interfaces.f90 nf_fortv2.f90
)
SET(netcdff_C_SOURCES ${netcdff_C_SOURCES} nf_v2compat.c )
endif (${BUILD_V2})

if (${BUILD_F03})
Expand Down Expand Up @@ -117,6 +129,8 @@ endif (${USE_LOGGING})

SET(BUILT_SOURCES ${BUILT_SOURCES} netcdf.inc)

include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/libsrc)

# Default to shared libs on
OPTION(BUILD_SHARED_LIBS "Configure netCDF as a shared library." ON)
SET (LIB_TYPE STATIC)
Expand All @@ -131,8 +145,19 @@ ENDIF()
# 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})
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}")

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
VERSION ${NC4F_LIB_VERSION}
Expand All @@ -148,6 +173,15 @@ if (HAVE_NC_USE_PARALLEL_ENABLED)
target_link_libraries(netcdff PUBLIC MPI::MPI_Fortran)
endif()

# copied from netcdf-c... Changed from ADD_DEFINITIONS to TARGET_COMPILE_DEFINITIONS
# Option to Build DLL
IF(WIN32)
OPTION(LINK_TO_SHARED_LIBS "Link to netcdf-c shared library" ON)
IF(LINK_TO_SHARED_LIBS)
TARGET_COMPILE_DEFINITIONS(netcdff_c PRIVATE DLL_NETCDF)
ENDIF()
ENDIF()

##
# Configuration for post-install RPath
# Adapted from http://www.cmake.org/Wiki/CMake_RPATH_handling
Expand All @@ -174,7 +208,7 @@ IF(NOT MSVC)
ENDIF()

# Installation of the program
INSTALL(TARGETS netcdff
INSTALL(TARGETS netcdff netcdff_c
EXPORT netcdffTargets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
Expand Down
66 changes: 66 additions & 0 deletions fortran/nf_nc_noparallel.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
! -- This file provides functions for parallel I/O when
! -- netCDF is built without parallel support
!
! -- C functions return NC_ENOPAR when built without parallel support
! -- so do the same here

! Parallel I/O access is only available in library build which support
! parallel I/O. To support parallel I/O, netCDF must be built with
! netCDF-4 enabled (configure options --enable-netcdf-4 and --enable-parallel4)
! and with a HDF5 library that supports parallel I/O, or with support for the
! PnetCDF library via the --enable-pnetcdf option.

!-------------------------------- nf_create_par -------------------------------
Function nf_create_par (path, cmode, comm, info, ncid) RESULT(status)

! create parallel file

USE netcdf_nc_interfaces

Implicit NONE

Integer, Intent(IN) :: cmode, comm, info
Character(LEN=*), Intent(IN) :: path
Integer, Intent(OUT) :: ncid

Integer :: status

ncid = 0
status = NC_ENOPAR

End Function nf_create_par
!-------------------------------- nf_open_par --------------------------------
Function nf_open_par (path, mode, comm, info, ncid) RESULT(status)

! open a parallel file

USE netcdf_nc_interfaces

Implicit NONE

Integer, Intent(IN) :: mode, comm, info
Character(LEN=*), Intent(IN) :: path
Integer, Intent(OUT) :: ncid

Integer :: status

ncid = 0
status = NC_ENOPAR

End Function nf_open_par
!-------------------------------- nf_var_par_access -------------------------
Function nf_var_par_access( ncid, varid, iaccess) RESULT (status)

! set parallel variable access

USE netcdf_nc_interfaces

Implicit NONE

Integer, Intent(IN) :: ncid, varid, iaccess

Integer :: status

status = NC_ENOPAR

End Function nf_var_par_access
31 changes: 29 additions & 2 deletions nf_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ foreach (f ${m4_SOURCES})
GEN_m4(${f})
endforeach(f)

# Separate C and Fortran Sources
SET(nf_test_SOURCES
f03lib_f_interfaces.f90 test_get.F test_put.F nf_error.F nf_test.F
test_read.F test_write.F util.F f03lib.c tests.inc)
test_read.F test_write.F util.F tests.inc)
SET(nf_test_C_SOURCES f03lib.c)

SET(tst_int64_SOURCES tst_int64.F)

Expand All @@ -26,7 +28,8 @@ IF (BUILD_V2)
# to generate its input file.
SET(check_PROGRAMS ${check_PROGRAMS} ftest)
SET(TESTS ${TESTS} ftest)
SET(ftest_SOURCES ftest.F f03lib.c)
SET(ftest_SOURCES ftest.F)
SET(ftest_C_SOURCES f03lib.c)
ENDIF(BUILD_V2)

FIND_LIBRARY(NETCDF_C_LIBRARY NAMES netcdf libnetcdf)
Expand All @@ -39,9 +42,33 @@ ENDIF()
execute_process(COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/ref_fills.nc
${CMAKE_CURRENT_BINARY_DIR}/fills.nc)

INCLUDE_DIRECTORIES(BEFORE
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_BINARY_DIR}/fortran
${CMAKE_BINARY_DIR}/libsrc
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/fortran
${CMAKE_SOURCE_DIR}/libsrc)

INCLUDE_DIRECTORIES(
${CMAKE_CPP_FLAGS}
${NETCDF_C_INCLUDE_DIR})

LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/fortran ${CMAKE_SOURCE_DIR}/libsrc
${NC4F_BINARY_DIR}/fortran)

# Create pre-compiled tests.
message ("Create pre-compiled tests.")
FOREACH(t ${TESTS})
ADD_EXECUTABLE(${t} ${${t}_SOURCES})
message(" " ${t} " C_SOURCES: " ${${t}_C_SOURCES})
IF(${t}_C_SOURCES)
message ("Adding C code for Test ${t} to Library")
ADD_LIBRARY(${t}_c OBJECT ${${t}_C_SOURCES})
install(TARGETS ${t}_c DESTINATION lib)
SET(${t}_C_LIBRARY ${t}_c)
TARGET_LINK_LIBRARIES(${t} ${${t}_C_LIBRARY})
ENDIF()
TARGET_LINK_LIBRARIES(${t} netcdff)
ADD_TEST(${t} ${t})
ENDFOREACH()
Expand Down