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

Commit

Permalink
Improved handling of library imports to get SHTOOLS build reliably wh…
Browse files Browse the repository at this point in the history
…en building wheels. There are still issues with getting all libraries included in the final wheel
  • Loading branch information
daminton committed Feb 16, 2024
1 parent 1c92aef commit 7a6ae2d
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 19 deletions.
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ELSE()
# Define the paths to the source code and python files
SET(SRC "${CMAKE_SOURCE_DIR}/src")
SET(PY "${CMAKE_SOURCE_DIR}/swiftest")
MESSAGE(STATUS "CMAKE_Fortran_PREPROCESS_SOURCE: ${CMAKE_Fortran_PREPROCESS_SOURCE}")

# Make sure paths are correct for Unix or Windows style
FILE(TO_CMAKE_PATH ${SRC} SRC)
FILE(TO_CMAKE_PATH ${PY} PY)
Expand All @@ -49,9 +49,9 @@ ELSE()
SET(INSTALL_INCLUDEDIR ${SKBUILD_HEADERS_DIR})
SET(INSTALL_PYPROJ ${SKBUILD_PLATLIB_DIR}/${SKBUILD_PROJECT_NAME})
IF (APPLE)
SET(CMAKE_INSTALL_RPATH "@loader_path")
SET(CMAKE_INSTALL_RPATH "@loader_path/../lib")
ELSEIF (LINUX)
SET(CMAKE_INSTALL_RPATH $ORIGIN)
SET(CMAKE_INSTALL_RPATH $ORIGIN/../lib)
ENDIF ()
ELSE ()
SET(INSTALL_PYPROJ ${PY})
Expand Down Expand Up @@ -79,7 +79,7 @@ ELSE()

# The following section is modified from Numpy f2py documentation
IF(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
MESSAGE(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.\n")
MESSAGE(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.\n")
ENDIF()

FIND_PACKAGE(NETCDF_Fortran REQUIRED)
Expand Down Expand Up @@ -119,6 +119,9 @@ ELSE()

INCLUDE(SetSwiftestFlags)

IF (NOT BUILD_SHARED_LIBS)
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
ENDIF()
# The source for the SWIFTEST binary and have it placed in the bin folder
ADD_SUBDIRECTORY(${SRC} ${CMAKE_INSTALL_BINDIR})
ADD_SUBDIRECTORY(${PY})
Expand Down
4 changes: 2 additions & 2 deletions buildscripts/build_shtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ case $FC in
;;
*)
echo "Everything else"
make F95="${FC}" CXX="${CXX}" fortran
make F95="${FC}" CXX="${CXX}" fortran-mp
make F95="${FC}" CXX="${CXX}" F95FLAGS="-fPIC -m64 -O3 -std=gnu -ffast-math" fortran
make F95="${FC}" CXX="${CXX}" F95FLAGS="-fPIC -m64 -O3 -std=gnu -ffast-math" fortran-mp
;;
esac

Expand Down
2 changes: 1 addition & 1 deletion buildscripts/set_compilers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ case $OS in
MacOSX)
FC=${HOMEBREW_PREFIX}/bin/gfortran-12
CFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET} -Wno-deprecated-non-prototype -arch ${ARCH}"
FCFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET} -arch ${ARCH} -fno-underscoring"
FCFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET} -arch ${ARCH}"
FFLAGS=$FCFLAGS
LD_LIBRARY_PATH=""
CPATH=""
Expand Down
2 changes: 1 addition & 1 deletion buildscripts/set_environment_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ CPATH="/usr/local/include:${PREFIX}/include:${HOMEBREW_PREFIX}/include:${ROOT_DI
CPPFLAGS="-isystem ${PREFIX}/include -isystem /usr/local/include -Xclang -fopenmp"
LIBS="-lomp"
CFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET} -Wno-deprecated-non-prototype -arch ${ARCH}"
FCFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET} -fno-underscoring"
FCFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET}"
FFLAGS="${FCFLAGS}"
CFLAGS="${FCFLAGS} -Wno-deprecated-non-prototype"
CXXFLAGS="${CFLAGS}"
Expand Down
7 changes: 6 additions & 1 deletion cmake/Modules/FindSHTOOLS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
FIND_PATH(SHTOOLS_INCLUDE_DIR NAMES shtools.h HINTS ENV SHTOOLS_HOME PATH_SUFFIXES include)
FIND_LIBRARY(SHTOOLS_LIBRARY NAMES libSHTOOLS.a HINTS ENV SHTOOLS_HOME PATH_SUFFIXES lib)
ADD_LIBRARY(SHTOOLS::serial UNKNOWN IMPORTED PUBLIC)
FIND_PACKAGE(FFTW3 REQUIRED)
FIND_PACKAGE(BLAS REQUIRED)
SET_TARGET_PROPERTIES(SHTOOLS::serial PROPERTIES
IMPORTED_LOCATION "${SHTOOLS_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${SHTOOLS_INCLUDE_DIR}"
Expand All @@ -22,8 +24,11 @@ SET_TARGET_PROPERTIES(SHTOOLS::parallel PROPERTIES
IMPORTED_LOCATION "${SHTOOLS_LIBRARY_MP}"
INTERFACE_INCLUDE_DIRECTORIES "${SHTOOLS_INCLUDE_DIR}"
)

SET(SHTOOLS_FOUND TRUE)

# These libraries are required
# How do I get them to link to the SHTOOLS library?

MARK_AS_ADVANCED(SHTOOLS_LIBRARY SHTOOLS_LIBRARY_MP SHTOOLS_INCLUDE_DIR)
MESSAGE(STATUS "SHTOOLS library: ${SHTOOLS_LIBRARY}")
MESSAGE(STATUS "SHTOOLS OpenMP library: ${SHTOOLS_LIBRARY_MP}")
Expand Down
14 changes: 10 additions & 4 deletions cmake/Modules/SetSwiftestFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ IF (COMPILER_OPTIONS STREQUAL "GNU")
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS}"
Fortran "-ffree-form" # GNU
)
# Don't add underscores in symbols for C-compatability
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS}"
Fortran "-fno-underscoring" # GNU
)
# Compile code assuming that IEEE signaling NaNs may generate user-visible traps during floating-point operations.
# Setting this option disables optimizations that may change the number of exceptions visible with signaling NaNs.
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS}"
Expand All @@ -93,6 +89,10 @@ IF (COMPILER_OPTIONS STREQUAL "GNU")
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS}"
Fortran "-std=f2018"
)
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS}"
Fortran "-fPIC"
)

ELSEIF (COMPILER_OPTIONS STREQUAL "Intel")
# Disables right margin wrapping in list-directed output
IF (WINOPT)
Expand Down Expand Up @@ -557,6 +557,9 @@ IF (CMAKE_BUILD_TYPE STREQUAL "RELEASE" OR CMAKE_BUILD_TYPE STREQUAL "PROFILE")
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-mkl"
)
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-qmkl"
)
# Enables additional interprocedural optimizations for a single file compilation
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-ip" # Intel
Expand All @@ -572,6 +575,9 @@ IF (CMAKE_BUILD_TYPE STREQUAL "RELEASE" OR CMAKE_BUILD_TYPE STREQUAL "PROFILE")
SET_COMPILE_FLAG(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-finline-functions" # GNU
)
SET_COMPILE_FLAG(CMAKE_Fortran_LINK_FLAGS "${CMAKE_Fortran_FLAGS_RELEASE}"
Fortran "-llapack"
)
ENDIF ()
ENDIF ()

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ LDFLAGS="-Wl,-rpath,${ROOT_DIR}/lib -Wl,-no_compact_unwind -L${PREFIX}/lib -L${
CPATH="/usr/local/include:${PREFIX}/include:${HOMEBREW_PREFIX}/include:${ROOT_DIR}/include"
CPPFLAGS="-isystem ${PREFIX}/include -isystem /usr/local/include"
LIBS="-lomp"
FCFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET} -arch ${ARCH} -fno-underscoring"
FCFLAGS="-mmacosx-version-min=${MACOSX_DEPLOYMENT_TARGET} -arch ${ARCH}"
FFLAGS="${FCFLAGS}"
CFLAGS="${FCFLAGS} -Wno-deprecated-non-prototype -arch ${ARCH}"
CXXFLAGS="${CFLAGS}"
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ ADD_EXECUTABLE(${SWIFTEST_DRIVER} ${DRIVER_src})
#####################################################
# Create a library from the source files, except the driver
ADD_LIBRARY(${SWIFTEST_LIBRARY} ${SWIFTEST_src})
SET_PROPERTY(TARGET ${SWIFTEST_LIBRARY} ${SWIFTEST_DRIVER} PROPERTY POSITION_INDEPENDENT_CODE)

TARGET_LINK_LIBRARIES(${SWIFTEST_LIBRARY} PUBLIC netCDF::netcdff HDF5::HDF5)
TARGET_LINK_LIBRARIES(${SWIFTEST_DRIVER} PUBLIC netCDF::netcdff HDF5::HDF5)
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/bindings_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ subroutine bindings_c2f_string(c_string, f_string)
return
end subroutine bindings_c2f_string

subroutine bindings_c_driver(c_integrator, c_param_file_name, c_display_style) bind(c)
subroutine bindings_c_driver(c_integrator, c_param_file_name, c_display_style) bind(c, name="bindings_c_driver")
implicit none
character(kind=c_char), dimension(*), intent(in) :: c_integrator, c_param_file_name, c_display_style
character(len=:), allocatable :: integrator, param_file_name, display_style
Expand Down
3 changes: 0 additions & 3 deletions swiftest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ ADD_CUSTOM_COMMAND(

PYTHON_ADD_LIBRARY(${SWIFTEST_BINDINGS} MODULE "${CMAKE_CURRENT_BINARY_DIR}/${SWIFTEST_BINDINGS}.c" WITH_SOABI)

IF (NOT BUILD_SHARED_LIBS)
SET_PROPERTY(TARGET ${SWIFTEST_BINDINGS} PROPERTY POSITION_INDEPENDENT_CODE)
ENDIF ()
TARGET_LINK_LIBRARIES(${SWIFTEST_BINDINGS} PUBLIC ${SWIFTEST_LIBRARY} netCDF::netcdff HDF5::HDF5)

IF(USE_OPENMP OR USE_SIMD)
Expand Down

0 comments on commit 7a6ae2d

Please sign in to comment.