summaryrefslogtreecommitdiff
path: root/doc/source/f2py/code
diff options
context:
space:
mode:
authorDeveloper-Ecosystem-Engineering <65677710+Developer-Ecosystem-Engineering@users.noreply.github.com>2021-11-18 14:31:58 -0800
committerGitHub <noreply@github.com>2021-11-18 14:31:58 -0800
commit5e9ce0c0529e3085498ac892941a020a65c7369a (patch)
treea70d9e941549b4a51b493f1b170ef33ce0d5a217 /doc/source/f2py/code
parent2ff7ab64d4e7d5928e96ca95b85350aa9caa2b63 (diff)
parent056abda14dab7fa8daf7a1ab44144aeb2250c216 (diff)
downloadnumpy-5e9ce0c0529e3085498ac892941a020a65c7369a.tar.gz
Merge branch 'numpy:main' into as_min_max
Diffstat (limited to 'doc/source/f2py/code')
-rw-r--r--doc/source/f2py/code/CMakeLists.txt80
-rw-r--r--doc/source/f2py/code/CMakeLists_skbuild.txt89
-rw-r--r--doc/source/f2py/code/meson.build38
-rw-r--r--doc/source/f2py/code/meson_upd.build37
-rw-r--r--doc/source/f2py/code/pyproj_skbuild.toml5
-rw-r--r--doc/source/f2py/code/setup_skbuild.py10
6 files changed, 259 insertions, 0 deletions
diff --git a/doc/source/f2py/code/CMakeLists.txt b/doc/source/f2py/code/CMakeLists.txt
new file mode 100644
index 000000000..62ff193bb
--- /dev/null
+++ b/doc/source/f2py/code/CMakeLists.txt
@@ -0,0 +1,80 @@
+### setup project ###
+cmake_minimum_required(VERSION 3.17.3) # 3.17 > for Python3_SOABI
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+project(fibby
+ VERSION 1.0
+ DESCRIPTION "FIB module"
+ LANGUAGES C Fortran
+ )
+
+# Safety net
+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"
+ )
+endif()
+
+# Grab Python
+find_package(Python3 3.9 REQUIRED
+ COMPONENTS Interpreter Development NumPy)
+
+# Grab the variables from a local Python installation
+# F2PY headers
+execute_process(
+ COMMAND "${Python3_EXECUTABLE}"
+ -c "import numpy.f2py; print(numpy.f2py.get_include())"
+ OUTPUT_VARIABLE F2PY_INCLUDE_DIR
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+
+# Project scope; consider using target_include_directories instead
+include_directories(
+ BEFORE
+ ${Python3_INCLUDE_DIRS}
+ ${Python3_NumPy_INCLUDE_DIRS}
+ ${F2PY_INCLUDE_DIR}
+ )
+
+message(STATUS ${Python3_INCLUDE_DIRS})
+message(STATUS ${F2PY_INCLUDE_DIR})
+message(STATUS ${Python3_NumPy_INCLUDE_DIRS})
+
+# Vars
+set(f2py_module_name "fibby")
+set(fortran_src_file "${CMAKE_SOURCE_DIR}/fib1.f")
+set(f2py_module_c "${f2py_module_name}module.c")
+set(generated_module_file "${f2py_module_name}${Python3_SOABI}")
+
+# Generate sources
+add_custom_target(
+ genpyf
+ DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_c}"
+ )
+add_custom_command(
+ OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_c}"
+ COMMAND ${Python3_EXECUTABLE} -m "numpy.f2py"
+ "${fortran_src_file}"
+ -m "fibby"
+ --lower # Important
+ DEPENDS fib1.f # Fortran source
+ )
+
+# Set up target
+add_library(${CMAKE_PROJECT_NAME} SHARED
+ "${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_c}" # Generated
+ "${F2PY_INCLUDE_DIR}/fortranobject.c" # From NumPy
+ "${fortran_src_file}" # Fortran source(s)
+ )
+
+# Depend on sources
+add_dependencies(${CMAKE_PROJECT_NAME} genpyf)
+
+set_target_properties(
+ ${CMAKE_PROJECT_NAME}
+ PROPERTIES
+ PREFIX ""
+ OUTPUT_NAME "${CMAKE_PROJECT_NAME}"
+ LINKER_LANGUAGE C
+ )
diff --git a/doc/source/f2py/code/CMakeLists_skbuild.txt b/doc/source/f2py/code/CMakeLists_skbuild.txt
new file mode 100644
index 000000000..97bc5c744
--- /dev/null
+++ b/doc/source/f2py/code/CMakeLists_skbuild.txt
@@ -0,0 +1,89 @@
+### setup project ###
+cmake_minimum_required(VERSION 3.17.3)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+project(fibby
+ VERSION 1.0
+ DESCRIPTION "FIB module"
+ LANGUAGES C Fortran
+ )
+
+# Safety net
+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"
+ )
+endif()
+
+# Grab Python
+find_package(Python3 3.9 REQUIRED
+ COMPONENTS Interpreter Development)
+
+# Ensure scikit-build modules
+if (NOT SKBUILD)
+ # Kanged -->https://github.com/Kitware/torch_liberator/blob/master/CMakeLists.txt
+ # If skbuild is not the driver; include its utilities in CMAKE_MODULE_PATH
+ execute_process(
+ COMMAND "${Python3_EXECUTABLE}"
+ -c "import os, skbuild; print(os.path.dirname(skbuild.__file__))"
+ OUTPUT_VARIABLE SKBLD_DIR
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+ )
+ set(SKBLD_CMAKE_DIR "${SKBLD_DIR}/resources/cmake")
+ list(APPEND CMAKE_MODULE_PATH ${SKBLD_CMAKE_DIR})
+endif()
+
+# scikit-build style includes
+find_package(PythonExtensions REQUIRED) # for ${PYTHON_EXTENSION_MODULE_SUFFIX}
+find_package(NumPy REQUIRED) # for ${NumPy_INCLUDE_DIRS}
+find_package(F2PY REQUIRED) # for ${F2PY_INCLUDE_DIR}
+
+# Prepping the module
+set(f2py_module_name "fibby")
+set(fortran_src_file "${CMAKE_SOURCE_DIR}/fib1.f")
+set(generated_module_file ${f2py_module_name}${PYTHON_EXTENSION_MODULE_SUFFIX})
+
+# Target for enforcing dependencies
+add_custom_target(${f2py_module_name} ALL
+ DEPENDS "${fortran_src_file}"
+ )
+
+# Custom command for generating .c
+add_custom_command(
+ OUTPUT "${f2py_module_name}module.c"
+ COMMAND ${F2PY_EXECUTABLE}
+ -m ${f2py_module_name}
+ ${fortran_src_file}
+ --lower
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+ DEPENDS ${fortran_src_file}
+ )
+
+add_library(${generated_module_file} MODULE
+ "${f2py_module_name}module.c"
+ "${F2PY_INCLUDE_DIR}/fortranobject.c"
+ "${fortran_src_file}")
+
+target_include_directories(${generated_module_file} PUBLIC
+ ${F2PY_INCLUDE_DIRS}
+ ${PYTHON_INCLUDE_DIRS})
+set_target_properties(${generated_module_file} PROPERTIES SUFFIX "")
+set_target_properties(${generated_module_file} PROPERTIES PREFIX "")
+
+# Linker fixes
+if (UNIX)
+ if (APPLE)
+ set_target_properties(${generated_module_file} PROPERTIES
+ LINK_FLAGS '-Wl,-dylib,-undefined,dynamic_lookup')
+ else()
+ set_target_properties(${generated_module_file} PROPERTIES
+ LINK_FLAGS '-Wl,--allow-shlib-undefined')
+ endif()
+endif()
+
+if (SKBUILD)
+ install(TARGETS ${generated_module_file} DESTINATION fibby)
+else()
+ install(TARGETS ${generated_module_file} DESTINATION ${CMAKE_SOURCE_DIR}/fibby)
+endif()
diff --git a/doc/source/f2py/code/meson.build b/doc/source/f2py/code/meson.build
new file mode 100644
index 000000000..b756abf8f
--- /dev/null
+++ b/doc/source/f2py/code/meson.build
@@ -0,0 +1,38 @@
+project('f2py_examples', 'c',
+ version : '0.1',
+ default_options : ['warning_level=2'])
+
+add_languages('fortran')
+
+py_mod = import('python')
+py3 = py_mod.find_installation('python3')
+py3_dep = py3.dependency()
+message(py3.path())
+message(py3.get_install_dir())
+
+incdir_numpy = run_command(py3,
+ ['-c', 'import os; os.chdir(".."); import numpy; print(numpy.get_include())'],
+ check : true
+).stdout().strip()
+
+incdir_f2py = run_command(py3,
+ ['-c', 'import os; os.chdir(".."); import numpy.f2py; print(numpy.f2py.get_include())'],
+ check : true
+).stdout().strip()
+
+fibby_source = custom_target('fibbymodule.c',
+ input : ['fib1.f'],
+ output : ['fibbymodule.c'],
+ command : [ py3, '-m', 'numpy.f2py', '@INPUT@',
+ '-m', 'fibby', '--lower' ]
+ )
+
+inc_np = include_directories(incdir_numpy, incdir_f2py)
+
+py3.extension_module('fibby',
+ 'fib1.f',
+ fibby_source,
+ incdir_f2py+'/fortranobject.c',
+ include_directories: inc_np,
+ dependencies : py3_dep,
+ install : true)
diff --git a/doc/source/f2py/code/meson_upd.build b/doc/source/f2py/code/meson_upd.build
new file mode 100644
index 000000000..97bd8d175
--- /dev/null
+++ b/doc/source/f2py/code/meson_upd.build
@@ -0,0 +1,37 @@
+project('f2py_examples', 'c',
+ version : '0.1',
+ default_options : ['warning_level=2'])
+
+add_languages('fortran')
+
+py_mod = import('python')
+py3 = py_mod.find_installation('python3')
+py3_dep = py3.dependency()
+message(py3.path())
+message(py3.get_install_dir())
+
+incdir_numpy = run_command(py3,
+ ['-c', 'import os; os.chdir(".."); import numpy; print(numpy.get_include())'],
+ check : true
+).stdout().strip()
+
+incdir_f2py = run_command(py3,
+ ['-c', 'import os; os.chdir(".."); import numpy.f2py; print(numpy.f2py.get_include())'],
+ check : true
+).stdout().strip()
+
+fibby_source = custom_target('fibbymodule.c',
+ input : ['fib1.f'],
+ output : ['fibbymodule.c'],
+ command : [ py3, '-m', 'numpy.f2py', '@INPUT@',
+ '-m', 'fibby', '--lower' ])
+
+inc_np = include_directories(incdir_numpy, incdir_f2py)
+
+py3.extension_module('fibby',
+ 'fib1.f',
+ fibby_source,
+ incdir_f2py+'/fortranobject.c',
+ include_directories: inc_np,
+ dependencies : py3_dep,
+ install : true)
diff --git a/doc/source/f2py/code/pyproj_skbuild.toml b/doc/source/f2py/code/pyproj_skbuild.toml
new file mode 100644
index 000000000..6686d1736
--- /dev/null
+++ b/doc/source/f2py/code/pyproj_skbuild.toml
@@ -0,0 +1,5 @@
+[project]
+requires-python = ">=3.7"
+
+[build-system]
+requires = ["setuptools>=42", "wheel", "scikit-build", "cmake>=3.18", "numpy>=1.21"]
diff --git a/doc/source/f2py/code/setup_skbuild.py b/doc/source/f2py/code/setup_skbuild.py
new file mode 100644
index 000000000..4dfc6af8b
--- /dev/null
+++ b/doc/source/f2py/code/setup_skbuild.py
@@ -0,0 +1,10 @@
+from skbuild import setup
+
+setup(
+ name="fibby",
+ version="0.0.1",
+ description="a minimal example package (fortran version)",
+ license="MIT",
+ packages=['fibby'],
+ cmake_args=['-DSKBUILD=ON']
+)