The UFZ services GitLab and Mattermost will be unavailable on Monday, January 24 from 06:00 AM to 08:00 AM due to maintenance work.

Want to get a quick-start introduction to Shell, Git and GitLab? Join the next beginners course on 28/31 January. Register now!

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

Add example python module

parent 5952ad32
cmake_minimum_required(VERSION 3.12.0)
project(globals_fortran)
enable_language(Fortran)
find_package(PythonExtensions REQUIRED)
find_package(F2PY REQUIRED)
find_package(Python3 REQUIRED COMPONENTS NumPy)
add_subdirectory(wrap_lib)
[build-system]
requires = ["setuptools>=42", "wheel", "scikit-build", "cmake", "ninja", "numpy"]
import sys
from skbuild import setup
# Require pytest-runner only when running tests
pytest_runner = (['pytest-runner>=2.0,<3dev']
if any(arg in sys.argv for arg in ('pytest', 'test'))
else [])
setup_requires = pytest_runner
setup(
name="wrap_lib",
version="0.1",
description="a minimal example package (fortran version)",
author='Sebastian Mueller',
license="MIT",
packages=['wrap_lib'],
install_requires=['numpy'],
tests_require=['pytest'],
setup_requires=setup_requires
)
import warp_lib
def test_set():
warp_lib.set_globals(10, 20.0, [1., 2., 3.])
# 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
)
FetchContent_MakeAvailable(${LIB_NAME})
# Generate position-independent code (-fPIC)
set_property(TARGET ${LIB_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
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(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)
add_custom_command(
OUTPUT ${generated_module_file}
COMMAND ${F2PY_EXECUTABLE}
-m ${f2py_module_name}
-c ${fortran_src_file}
-I${LIB_INCLUDES}
-L${LIB_INCLUDES}
-l${LIB_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
install(FILES ${generated_module_file} DESTINATION wrap_lib)
from .wrapper import set_globals, set_container, print_vars
subroutine set_globals(n,A,B,C)
USE mo_lib_functions, ONLY: set_global_vars
implicit none
integer(4), intent(in) :: n
integer(4), intent(in) :: A
real(8), intent(in) :: B
real(8), dimension(n), intent(in) :: C
call set_global_vars(A,B,C)
end subroutine set_globals
subroutine set_container(n,A,B,C)
USE mo_lib_functions, ONLY: set_container_vars
implicit none
integer(4), intent(in) :: n
integer(4), intent(in) :: A
real(8), intent(in) :: B
real(8), dimension(n), intent(in) :: C
call set_container_vars(A,B,C)
end subroutine set_container
subroutine print_vars()
USE mo_lib_functions, ONLY: print_globals
implicit none
call print_globals()
end subroutine print_vars
Markdown is supported
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