Commit 318c55a3 authored by Sebastian Müller's avatar Sebastian Müller 🐈
Browse files

Set rpath in generated module to look locally for dependencies

parent a2a36620
[build-system]
requires = ["setuptools>=42", "wheel", "scikit-build", "cmake", "ninja", "oldest_supported_numpy"]
requires = ["setuptools>=42", "wheel", "scikit-build", "cmake", "ninja", "oldest_supported_numpy", "fypp"]
# download fortran-lib-test master
include(FetchContent)
set(LIB_NAME PROJ-lib)
FetchContent_Declare(
${LIB_NAME}
GIT_REPOSITORY https://git.ufz.de/muellese/fortran-lib-test.git
GIT_TAG master
GIT_TAG other_forces
)
FetchContent_MakeAvailable(${LIB_NAME})
# Generate position-independent code (-fPIC)
set_property(TARGET ${LIB_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
# set_property(TARGET ${LIB_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
install(TARGETS ${LIB_NAME}
RUNTIME DESTINATION wrap_lib
LIBRARY DESTINATION wrap_lib
ARCHIVE DESTINATION wrap_lib
)
get_target_property(LIB_INCLUDES ${LIB_NAME} INCLUDE_DIRECTORIES)
set(f2py_module_name "wrapper")
set(fortran_src_file "${CMAKE_CURRENT_SOURCE_DIR}/wrapper.f90")
set(fortran_src_file "${CMAKE_CURRENT_SOURCE_DIR}/${f2py_module_name}.f90")
set(generated_module_file ${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_name}${PYTHON_EXTENSION_MODULE_SUFFIX})
add_custom_target(${f2py_module_name} ALL DEPENDS ${generated_module_file} ${LIB_NAME})
# call f2py and link against external library (static)
# rpath should be set to look locally for the needed .so file
if (APPLE)
set(rpath "@loader_path")
else()
set(rpath "$$ORIGIN") # need double $ to not expand the variable
endif()
add_custom_command(
OUTPUT ${generated_module_file}
COMMAND ${F2PY_EXECUTABLE}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
# use Command Line Tool Mode of CMake to set LDFLAGS for f2py (rpath)
COMMAND ${CMAKE_COMMAND} -E env 'LDFLAGS=-Wl,-rpath,${rpath}'
${F2PY_EXECUTABLE}
-m ${f2py_module_name}
-c ${fortran_src_file}
-I${LIB_INCLUDES}
-L${LIB_INCLUDES}
-l${LIB_NAME}
--f90exec=${CMAKE_Fortran_COMPILER}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
install(FILES ${generated_module_file} DESTINATION wrap_lib)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment