Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Significantly improved how the NetCDF libraries and dependencies are …
Browse files Browse the repository at this point in the history
…found.
  • Loading branch information
daminton committed Aug 16, 2023
1 parent 428f804 commit fa46010
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 67 deletions.
25 changes: 24 additions & 1 deletion buildscripts/build_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,30 @@
SCRIPT_DIR=$(realpath $(dirname $0))
ROOT_DIR=$(realpath ${SCRIPT_DIR}/..)
BUILD_DIR="${ROOT_DIR}/build"
PREFIX=${BUILD_DIR}/usr/local
PREFIX=/usr/local

# Parse arguments
USTMT="Usage: ${0} [-d {./build}|/path/to/build] [-p {/usr/local}|/prefix/path]"
IFORT=false
PREFIX=/usr/local
COMPILER=""
while getopts ":c:d:" ARG; do
case "${ARG}" in
d)
BUILD_DIR="${OPTARG}"
;;
p)
PREFIX="${OPTARG}"
;;
:)
echo "Error: -${OPTARG} requires an argument."
echo $USTMT
exit 1
;;
*)
;;
esac
done

mkdir -p ${BUILD_DIR}
read -r OS ARCH < <($SCRIPT_DIR/get_platform.sh)
Expand Down
8 changes: 7 additions & 1 deletion buildscripts/build_hdf5.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ if [ $COMPILER = "GNU-Mac" ]; then
fi
COPTS="--disable-shared --enable-build-mode=production --disable-fortran --disable-java --disable-cxx --prefix=${PREFIX} --with-zlib=${PREFIX}"
./configure ${COPTS}
make && make install
make
if [ -w ${PREFIX} ]; then
make install
else
sudo make install
fi

if [ $? -ne 0 ]; then
printf "hdf5 could not be compiled.\n"
exit 1
Expand Down
37 changes: 10 additions & 27 deletions buildscripts/build_netcdf-c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ export FC=${FC}
export F77=${F77}
export CPP=${CPP}

export HDF5_ROOT="${PREFIX}"
export HDF5_ROOT=${HDF5_ROOT:-$PREFIX}
export HDF5_LIBDIR="${HDF5_ROOT}/lib"
export HDF5_INCLUDE_DIR="${HDF5_ROOT}/include"
export HDF5_PLUGIN_PATH="${HDF5_LIBDIR}/plugin"
export NCDIR="${PREFIX}"
export NFDIR="${PREFIX}"
export LD_LIBRARY_PATH="${PREFIX}/lib:${LD_LIBRARY_PATH}"
export CPPFLAGS="${CPPFLAGS} -isystem ${PREFIX}/include"
export LDFLAGS="${LDFLAGS} -L${PREFIX}/lib"
export LD_LIBRARY_PATH="${PREFIX}/lib:${HDF5_LIBDIR}:${LD_LIBRARY_PATH}"
export CPPFLAGS="${CPPFLAGS} -isystem ${PREFIX}/include -isystem ${HDF5_INCLUDE_DIR}"
export LDFLAGS="${LDFLAGS} -L${PREFIX}/lib -L${HDF5_LIBDIR}"
export CPATH="${PREFIX}/include:${CPATH}"
export CFLAGS="${CFLAGS} -Wno-unused-but-set-variable"

Expand All @@ -75,6 +75,7 @@ printf "CPPFLAGS: ${CPPFLAGS}\n"
printf "CPATH: ${CPATH}\n"
printf "LD_LIBRARY_PATH: ${LD_LIBRARY_PATH}\n"
printf "LDFLAGS: ${LDFLAGS}\n"
printf "HDF5_ROOT: ${HDF5_ROOT}\n"
printf "*********************************************************\n"

cd ${BUILD_DIR}/netcdf-c-*
Expand All @@ -84,34 +85,16 @@ if [ ! $COMPILER = "GNU-Mac" ]; then
fi
printf "COPTS: ${COPTS}\n"
./configure $COPTS
make && make check && make install
if [ $? -ne 0 ]; then
printf "netcdf-c could not be compiled."\n
exit 1
fi
make && make check

if [ $COMPILER = "Intel" ]; then
export FCFLAGS="${CFLAGS} -standard-semantics"
if [ -w ${PREFIX} ]; then
make install
else
export FCFLAGS="${CFLAGS}"
sudo make install
fi
export FFLAGS=${CFLAGS}

export LIBS="$(${PREFIX}/bin/nc-config --libs --static)"
printf "\n"
printf "*********************************************************\n"
printf "* BUILDING NETCDF-FORTRAN STATIC LIBRARY *\n"
printf "*********************************************************\n"
cd ../netcdf-fortran-4.6.1
./configure --disable-shared --with-pic --prefix=${PREFIX}
make && make check && make install
if [ $? -ne 0 ]; then
printf "netcdf-fortran could not be compiled.\n"
printf "netcdf-c could not be compiled."\n
exit 1
fi

printf "\n"
printf "*********************************************************\n"
printf "* DEPENDENCIES ARE BUILT *\n"
printf "*********************************************************\n"
printf "Dependencys are installed to: ${PREFIX}\n\n"
8 changes: 7 additions & 1 deletion buildscripts/build_netcdf-fortran.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ printf "*********************************************************\n"

cd ${BUILD_DIR}/netcdf-fortran-*
./configure --disable-shared --with-pic --disable-zstandard-plugin --prefix=${PREFIX}
make && make check && make install
make && make check i
if [ -w ${PREFIX} ]; then
make install
else
sudo make install
fi

if [ $? -ne 0 ]; then
printf "netcdf-fortran could not be compiled.\n"
exit 1
Expand Down
16 changes: 6 additions & 10 deletions buildscripts/build_swiftest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ printf "Dependency libraries in ${PREFIX}\n"

export DEPDIR=$PREFIX
export NETCDF_FORTRAN_HOME=$DEPDIR
export HDF_ROOT=$PREFIX
export HDF5_LIBDIR="${HDF5_ROOT}/lib"
export HDF5_INCLUDE_DIR="${HDF5_ROOT}/include"
export HDF5_PLUGIN_PATH="${HDF5_LIBDIR}/plugin"

export LD_LIBRARY_PATH="${DEPDIR}/lib:${LD_LIBRARY_PATH}"
export CPPFLAGS="${CPPFLAGS} -isystem ${DEPDIR}/include"
export LDFLAGS="${LDFLAGS} -L${DEPDIR}/lib"
Expand All @@ -59,16 +64,7 @@ if [ $COMPILER = "GNU-Mac" ]; then
export LDFLAGS="${LDFLAGS} -Wl,-no_compact_unwind"
printf "MACOSX_DEPLOYMENT_TARGET: ${MACOSX_DEPLOYMENT_TARGET}\n"
fi
NFCFG="${DEPDIR}/bin/nf-config"
if command -v $NFCFG &> /dev/null; then
export LIBS=$($NFCFG --flibs)
else
printf "Error: Cannot find ${NFCFG}.\n"
printf "Is NetCDF-Fortran installed?\n"
exit 1
fi
export LDFLAGS="${LDFLAGS} -L${DEPDIR}/lib"
export CFLAGS="-fPIC"

export SKBUILD_CONFIGURE_OPTIONS="-DBUILD_SHARED_LIBS=OFF"

if [ $COMPILER = "Intel" ]; then
Expand Down
8 changes: 7 additions & 1 deletion buildscripts/build_zlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ printf "*********************************************************\n"
cd ${BUILD_DIR}/zlib-*
./configure --prefix=${PREFIX} --static
make
make install

if [ -w ${PREFIX} ]; then
make install
else
sudo make install
fi

if [ $? -ne 0 ]; then
printf "zlib could not be compiled.\n"
exit 1
Expand Down
94 changes: 86 additions & 8 deletions cmake/Modules/FindNETCDF.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,91 @@
# If not, see: https://www.gnu.org/licenses.

# - Finds the NetCDF libraries
set(NETCDF_FORTRAN_HOME $ENV{NETCDF_FORTRAN_HOME} CACHE STRING "Value of NetCDF library home directory")
find_path(NETCDF_INCLUDE_DIR NAMES netcdf.mod HINTS ENV NETCDF_FORTRAN_HOME PATH_SUFFIXES include)
find_library(NETCDF_FORTRAN_LIBRARY NAMES netcdff HINTS ENV NETCDF_FORTRAN_HOME PATH_SUFFIXES lib)
find_library(NETCDF_LIBRARY NAMES netcdf HINTS ENV NETCDF_FORTRAN_HOME PATH_SUFFIXES lib)

set(NETCDF_FOUND TRUE)
# Note for posterity: When building static libraries, NETCDF_FORTRAN_LIBRARY must come *before* NETCDF_LIBRARY. Otherwise you get a bunch of "undefined reference to" errors
FIND_PATH(NETCDF_INCLUDE_DIR
NAMES netcdf.mod
HINTS
ENV NETCDF_INCLUDE_DIR
ENV NETCDF_FORTRAN_HOME
ENV CPATH
PATH_SUFFIXES
include
REQUIRED
)

mark_as_advanced(NETCDF_LIBRARY NETCDF_FORTRAN_LIBRARY NETCDF_INCLUDE_DIR)
set(NETCDF_LIBRARIES ${NETCDF_FORTRAN_LIBRARY} ${NETCDF_LIBRARY} CACHE STRING "NetCDF libraries")
MESSAGE(STATUS "NetCDF-Fortran include directory: ${NETCDF_INCLUDE_DIR}")

IF (BUILD_SHARED_LIBS)
SET(NETCDFF "netcdff")
ELSE ()
SET(NETCDFF "libnetcdff.a")
SET(NETCDF "libnetcdf.a")
ENDIF()

FIND_LIBRARY(NETCDF_FORTRAN_LIBRARY
NAMES ${NETCDFF}
HINTS
ENV NETCDF_FORTRAN_HOME
ENV NETCDF_HOME
ENV LD_LIBRARY_PATH
PATH_SUFFIXES
lib
${CMAKE_LIBRARY_ARCHITECTURE}
REQUIRED
)

MESSAGE(STATUS "NetCDF-Fortran Library: ${NETCDF_FORTRAN_LIBRARY}")

IF (BUILD_SHARED_LIBS)
SET(NETCDF_LIBRARIES ${NETCDF_FORTRAN_LIBRARY} CACHE STRING "NetCDF Fortran library")
ELSE ()
FIND_LIBRARY(NETCDF_LIBRARY
NAMES ${NETCDF}
HINTS
ENV NETCDF_HOME
ENV LD_LIBRARY_PATH
PATH_SUFFIXES
lib
${CMAKE_LIBRARY_ARCHITECTURE}
REQUIRED
)

MESSAGE(STATUS "NetCDF-C Library: ${NETCDF_LIBRARY}")

FIND_PATH(NCBIN
NAMES nc-config
HINTS
ENV NETCDF_HOME
ENV PATH
PATH_SUFFIXES
bin
)

IF (NCBIN) # The nc-config utility is available. Parse its output for unique flags
SET(CMD "${NCBIN}/nc-config")
LIST(APPEND CMD "--libs")
LIST(APPEND CMD "--static")
MESSAGE(STATUS "NetCDF configuration command: ${CMD}")
EXECUTE_PROCESS(COMMAND ${CMD} OUTPUT_VARIABLE EXTRA_FLAGS ERROR_VARIABLE ERR RESULT_VARIABLE RES OUTPUT_STRIP_TRAILING_WHITESPACE)
IF (EXTRA_FLAGS)
SEPARATE_ARGUMENTS(EXTRA_FLAGS NATIVE_COMMAND "${EXTRA_FLAGS}")
LIST(REMOVE_DUPLICATES EXTRA_FLAGS)
LIST(FILTER EXTRA_FLAGS EXCLUDE REGEX "netcdf+|-L+")
MESSAGE(STATUS "Extra library flags: ${EXTRA_FLAGS}")
ELSE ()
MESSAGE(STATUS "Cannot execute ${CMD}")
MESSAGE(STATUS "OUTPUT: ${EXTRA_FLAGS}")
MESSAGE(STATUS "RESUL : ${RES}")
MESSAGE(STATUS "ERROR : ${ERR}")
MESSAGE(FATAL_ERROR "Cannot configure NetCDF for static")
ENDIF ()
ELSE ()
MESSAGE(FATAL_ERROR "Cannot find nc-config")
ENDIF ()

# Note for posterity: When building static libraries, NETCDF_FORTRAN_LIBRARY must come *before* NETCDF_LIBRARY. Otherwise you get a bunch of "undefined reference to" errors
SET(NETCDF_LIBRARIES ${NETCDF_FORTRAN_LIBRARY} ${NETCDF_LIBRARY} ${EXTRA_FLAGS} CACHE STRING "NetCDF Fortran and dependant static libraries")
ENDIF ()

SET(NETCDF_FOUND TRUE)
MARK_AS_ADVANCED(NETCDF_LIBRARIES NETCDF_INCLUDE_DIR)
4 changes: 3 additions & 1 deletion cmake/Modules/SetFortranFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ ELSEIF (COMPILER_OPTIONS STREQUAL "Intel")
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS}"
Fortran "-pad" # Intel
"/Qpad" # Intel Windows

)
ENDIF ()

IF (NOT BUILD_SHARED_LIBS)
SET_COMPILE_FLAG(CMAKE_FORTRAN_FLAGS "${CMAKE_FORTRAN_FLAGS}"
Fortran "-fPIC"
)

IF (COMPILER_OPTIONS STREQUAL "Intel")
# Use static Intel libraries
Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ requires = [
build-backend = "setuptools.build_meta"

[tool.cibuildwheel.linux]
environment = {SCRIPT_DIR="buildscripts", BUILD_DIR="build", PREFIX="/usr/local", SKBUILD_CONFIGURE_OPTIONS="-DBUILD_SHARED_LIBS=OFF", CFLAGS="-fPIC", CPPFLAGS="-isystem /usr/local/include", NETCDF_FORTRAN_HOME="/usr/local", LIBS="-lm -lz -ldl"}
environment = {SKBUILD_CONFIGURE_OPTIONS="-DBUILD_SHARED_LIBS=OFF",CPATH="/usr:/usr/local",LD_LIBRARY_PATH="/usr/lib:/usr/lib64:/usr/local/lib:/usr/lib/x86_64-linux-gnu:/usr/lib/aarch64-linux-gnu"}
before-all = [
"yum install wget m4 doxygen -y",
"bash ${SCRIPT_DIR}/fetch_dependencies.sh -d ${BUILD_DIR}",
"bash ${SCRIPT_DIR}/build_dependencies.sh -c GNU-Linux -p ${PREFIX}"
"yum install netcdf-fortran-static netcdf-static hdf5-static -y",
]

2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"""

from skbuild import setup
import os
import subprocess

with open('version.txt') as version_file:
version = version_file.read().strip()
Expand Down
19 changes: 6 additions & 13 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,21 @@ ELSEIF(BT STREQUAL "PROFILE")
ADD_DEFINITIONS(-DPROFILE)
ENDIF()

# Define the executable name
SET(SWIFTEST_DRIVER swiftest_driver)
ADD_EXECUTABLE(${SWIFTEST_DRIVER} ${DRIVER_src})

#####################################################
# Add the needed libraries
#####################################################
# Create a library from the source files, except the driver
SET(SWIFTEST_LIBRARY swiftest)
ADD_LIBRARY(${SWIFTEST_LIBRARY} ${SWIFTEST_src})

# Define the executable name
SET(SWIFTEST_DRIVER swiftest_driver)
ADD_EXECUTABLE(${SWIFTEST_DRIVER} ${DRIVER_src})

MESSAGE(STATUS "NetCDF Include directory: ${NETCDF_INCLUDE_DIR}")
TARGET_INCLUDE_DIRECTORIES(${SWIFTEST_LIBRARY} PUBLIC ${NETCDF_INCLUDE_DIR})
TARGET_INCLUDE_DIRECTORIES(${SWIFTEST_DRIVER} PUBLIC ${NETCDF_INCLUDE_DIR})

IF (BUILD_SHARED_LIBS)
TARGET_LINK_LIBRARIES(${SWIFTEST_LIBRARY} PUBLIC ${NETCDF_LIBRARIES})
TARGET_LINK_LIBRARIES(${SWIFTEST_DRIVER} PUBLIC ${SWIFTEST_LIBRARY})
ELSE ()
TARGET_LINK_LIBRARIES(${SWIFTEST_LIBRARY} PUBLIC ${NETCDF_LIBRARIES} $ENV{LIBS})
TARGET_LINK_LIBRARIES(${SWIFTEST_DRIVER} PUBLIC ${SWIFTEST_LIBRARY} ${NETCDF_LIBRARIES} $ENV{LIBS})
ENDIF (BUILD_SHARED_LIBS)
TARGET_LINK_LIBRARIES(${SWIFTEST_LIBRARY} PUBLIC ${NETCDF_LIBRARIES})
TARGET_LINK_LIBRARIES(${SWIFTEST_DRIVER} PUBLIC ${SWIFTEST_LIBRARY} ${NETCDF_LIBRARIES})

IF(USE_OPENMP OR USE_SIMD)
SET_PROPERTY(TARGET ${SWIFTEST_LIBRARY} ${SWIFTEST_DRIVER} APPEND_STRING PROPERTY COMPILE_FLAGS "${OpenMP_Fortran_FLAGS} ")
Expand Down

0 comments on commit fa46010

Please sign in to comment.