Skip to content

Commit e3569b1

Browse files
committed
Add CMake build files
1 parent 05ce19c commit e3569b1

File tree

9 files changed

+2382
-0
lines changed

9 files changed

+2382
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
3+
project(HelloWorld)
4+
5+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
6+
7+
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
8+
find_package(Cython REQUIRED)
9+
find_package(PythonExtensions REQUIRED)
10+
# include_directories(${PYTHON_INCLUDE_DIRS})
11+
12+
add_cython_target(hello_world hello_world.pyx PY3 OUTPUT_VAR hello_world_src)
13+
add_library(hello_world MODULE ${hello_world_src})
14+
target_include_directories(hello_world PRIVATE ${PYTHON_INCLUDE_DIRS})
15+
set_target_properties(hello_world PROPERTIES PREFIX "")
16+
17+
install(TARGETS hello_world DESTINATION .)
18+
install(FILES say_hello.py
19+
DESTINATION .
20+
RENAME say_hello
21+
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE
22+
)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#.rst:
2+
#
3+
# Find ``cython`` executable.
4+
#
5+
# This module will set the following variables in your project:
6+
#
7+
# ``CYTHON_EXECUTABLE``
8+
# path to the ``cython`` program
9+
#
10+
# ``CYTHON_VERSION``
11+
# version of ``cython``
12+
#
13+
# ``CYTHON_FOUND``
14+
# true if the program was found
15+
#
16+
# For more information on the Cython project, see https://cython.org/.
17+
#
18+
# *Cython is a language that makes writing C extensions for the Python language
19+
# as easy as Python itself.*
20+
#
21+
#=============================================================================
22+
# Copyright 2011 Kitware, Inc.
23+
#
24+
# Licensed under the Apache License, Version 2.0 (the "License");
25+
# you may not use this file except in compliance with the License.
26+
# You may obtain a copy of the License at
27+
#
28+
# http://www.apache.org/licenses/LICENSE-2.0
29+
#
30+
# Unless required by applicable law or agreed to in writing, software
31+
# distributed under the License is distributed on an "AS IS" BASIS,
32+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33+
# See the License for the specific language governing permissions and
34+
# limitations under the License.
35+
#=============================================================================
36+
37+
# Use the Cython executable that lives next to the Python executable
38+
# if it is a local installation.
39+
if(Python_EXECUTABLE)
40+
get_filename_component(_python_path ${Python_EXECUTABLE} PATH)
41+
elseif(Python3_EXECUTABLE)
42+
get_filename_component(_python_path ${Python3_EXECUTABLE} PATH)
43+
elseif(DEFINED PYTHON_EXECUTABLE)
44+
get_filename_component(_python_path ${PYTHON_EXECUTABLE} PATH)
45+
endif()
46+
47+
if(DEFINED _python_path)
48+
find_program(CYTHON_EXECUTABLE
49+
NAMES cython cython.bat cython3
50+
HINTS ${_python_path}
51+
DOC "path to the cython executable")
52+
else()
53+
find_program(CYTHON_EXECUTABLE
54+
NAMES cython cython.bat cython3
55+
DOC "path to the cython executable")
56+
endif()
57+
58+
if(CYTHON_EXECUTABLE)
59+
set(CYTHON_version_command ${CYTHON_EXECUTABLE} --version)
60+
61+
execute_process(COMMAND ${CYTHON_version_command}
62+
OUTPUT_VARIABLE CYTHON_version_output
63+
ERROR_VARIABLE CYTHON_version_error
64+
RESULT_VARIABLE CYTHON_version_result
65+
OUTPUT_STRIP_TRAILING_WHITESPACE
66+
ERROR_STRIP_TRAILING_WHITESPACE)
67+
68+
if(NOT ${CYTHON_version_result} EQUAL 0)
69+
set(_error_msg "Command \"${CYTHON_version_command}\" failed with")
70+
set(_error_msg "${_error_msg} output:\n${CYTHON_version_error}")
71+
message(SEND_ERROR "${_error_msg}")
72+
else()
73+
if("${CYTHON_version_output}" MATCHES "^[Cc]ython version ([^,]+)")
74+
set(CYTHON_VERSION "${CMAKE_MATCH_1}")
75+
else()
76+
if("${CYTHON_version_error}" MATCHES "^[Cc]ython version ([^,]+)")
77+
set(CYTHON_VERSION "${CMAKE_MATCH_1}")
78+
endif()
79+
endif()
80+
endif()
81+
endif()
82+
83+
include(FindPackageHandleStandardArgs)
84+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Cython REQUIRED_VARS CYTHON_EXECUTABLE)
85+
86+
mark_as_advanced(CYTHON_EXECUTABLE)
87+
88+
include(UseCython)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#.rst:
2+
#
3+
# The purpose of the F2PY –Fortran to Python interface generator– project is to provide a
4+
# connection between Python and Fortran languages.
5+
#
6+
# F2PY is a Python package (with a command line tool f2py and a module f2py2e) that facilitates
7+
# creating/building Python C/API extension modules that make it possible to call Fortran 77/90/95
8+
# external subroutines and Fortran 90/95 module subroutines as well as C functions; to access Fortran
9+
# 77 COMMON blocks and Fortran 90/95 module data, including allocatable arrays from Python.
10+
#
11+
# For more information on the F2PY project, see http://www.f2py.com/.
12+
#
13+
# The following variables are defined:
14+
#
15+
# ::
16+
#
17+
# F2PY_EXECUTABLE - absolute path to the F2PY executable
18+
#
19+
# ::
20+
#
21+
# F2PY_VERSION_STRING - the version of F2PY found
22+
# F2PY_VERSION_MAJOR - the F2PY major version
23+
# F2PY_VERSION_MINOR - the F2PY minor version
24+
# F2PY_VERSION_PATCH - the F2PY patch version
25+
#
26+
#
27+
# .. note::
28+
#
29+
# By default, the module finds the F2PY program associated with the installed NumPy package.
30+
#
31+
# Example usage
32+
# ^^^^^^^^^^^^^
33+
#
34+
# Assuming that a package named ``method`` is declared in ``setup.py`` and that the corresponding directory
35+
# containing ``__init__.py`` also exists, the following CMake code can be added to ``method/CMakeLists.txt``
36+
# to ensure the C sources associated with ``cylinder_methods.f90`` are generated and the corresponding module
37+
# is compiled:
38+
#
39+
# .. code-block:: cmake
40+
#
41+
# find_package(F2PY REQUIRED)
42+
#
43+
# set(f2py_module_name "_cylinder_methods")
44+
# set(fortran_src_file "${CMAKE_CURRENT_SOURCE_DIR}/cylinder_methods.f90")
45+
#
46+
# set(generated_module_file ${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_name}${PYTHON_EXTENSION_MODULE_SUFFIX})
47+
#
48+
# add_custom_target(${f2py_module_name} ALL
49+
# DEPENDS ${generated_module_file}
50+
# )
51+
#
52+
# add_custom_command(
53+
# OUTPUT ${generated_module_file}
54+
# COMMAND ${F2PY_EXECUTABLE}
55+
# -m ${f2py_module_name}
56+
# -c
57+
# ${fortran_src_file}
58+
# WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
59+
# )
60+
#
61+
# install(FILES ${generated_module_file} DESTINATION methods)
62+
#
63+
# .. warning::
64+
#
65+
# Using ``f2py`` with ``-c`` argument means that f2py is also responsible to build the module. In that
66+
# case, CMake is not used to find the compiler and configure the associated build system.
67+
#
68+
69+
find_program(F2PY_EXECUTABLE NAMES f2py${PYTHON_VERSION_MAJOR} f2py)
70+
71+
# XXX This is required to support NumPy < v0.15.0. See note in module documentation above.
72+
if(NOT F2PY_EXECUTABLE)
73+
find_package(NumPy)
74+
set(F2PY_EXECUTABLE "${PYTHON_EXECUTABLE}" "-m" "numpy.f2py")
75+
endif()
76+
77+
if(NOT F2PY_INCLUDE_DIR)
78+
execute_process(
79+
COMMAND "${PYTHON_EXECUTABLE}"
80+
-c "import os; from numpy import f2py; print(f2py.get_include() if hasattr(f2py, 'get_include') else os.path.join(os.path.dirname(f2py.__file__), 'src'))"
81+
OUTPUT_VARIABLE _f2py_directory
82+
OUTPUT_STRIP_TRAILING_WHITESPACE
83+
ERROR_QUIET
84+
)
85+
string(REPLACE "\\" "/" _f2py_directory ${_f2py_directory})
86+
87+
set(F2PY_INCLUDE_DIR "${_f2py_directory}" CACHE STRING "F2PY source directory location" FORCE)
88+
endif()
89+
90+
# Set-up the F2PY libraries and include directories
91+
file(GLOB _f2py_sources "${F2PY_INCLUDE_DIR}/*.c")
92+
add_library(_f2py_runtime_library STATIC ${_f2py_sources})
93+
target_include_directories(
94+
_f2py_runtime_library
95+
PRIVATE ${PYTHON_INCLUDE_DIRS} ${NumPy_INCLUDE_DIRS}
96+
)
97+
98+
set_target_properties(_f2py_runtime_library PROPERTIES POSITION_INDEPENDENT_CODE ON)
99+
100+
set(F2PY_LIBRARIES _f2py_runtime_library)
101+
set(F2PY_INCLUDE_DIRS "${F2PY_INCLUDE_DIR}" "${NumPy_INCLUDE_DIRS}")
102+
103+
if(F2PY_EXECUTABLE)
104+
# extract the version string
105+
execute_process(COMMAND "${F2PY_EXECUTABLE}" -v
106+
OUTPUT_VARIABLE F2PY_VERSION_STRING
107+
OUTPUT_STRIP_TRAILING_WHITESPACE)
108+
if("${F2PY_VERSION_STRING}" MATCHES "^([0-9]+)(.([0-9+]))?(.([0-9+]))?$")
109+
set(F2PY_VERSION_MAJOR ${CMAKE_MATCH_1})
110+
set(F2PY_VERSION_MINOR "${CMAKE_MATCH_3}")
111+
set(F2PY_VERSION_PATCH "${CMAKE_MATCH_5}")
112+
endif()
113+
endif()
114+
115+
# handle the QUIETLY and REQUIRED arguments and set F2PY_FOUND to TRUE if
116+
# all listed variables are TRUE
117+
include(FindPackageHandleStandardArgs)
118+
find_package_handle_standard_args(F2PY
119+
REQUIRED_VARS F2PY_EXECUTABLE
120+
VERSION_VAR F2PY_VERSION_STRING
121+
)
122+
123+
mark_as_advanced(F2PY_EXECUTABLE)
124+
125+
include(UseF2PY)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#.rst:
2+
#
3+
# Find the include directory for ``numpy/arrayobject.h`` as well as other NumPy tools like ``conv-template`` and
4+
# ``from-template``.
5+
#
6+
# This module sets the following variables:
7+
#
8+
# ``NumPy_FOUND``
9+
# True if NumPy was found.
10+
# ``NumPy_INCLUDE_DIRS``
11+
# The include directories needed to use NumpPy.
12+
# ``NumPy_VERSION``
13+
# The version of NumPy found.
14+
# ``NumPy_CONV_TEMPLATE_EXECUTABLE``
15+
# Path to conv-template executable.
16+
# ``NumPy_FROM_TEMPLATE_EXECUTABLE``
17+
# Path to from-template executable.
18+
#
19+
# The module will also explicitly define one cache variable:
20+
#
21+
# ``NumPy_INCLUDE_DIR``
22+
#
23+
# .. note::
24+
#
25+
# To support NumPy < v0.15.0 where ``from-template`` and ``conv-template`` are not declared as entry points,
26+
# the module emulates the behavior of standalone executables by setting the corresponding variables with the
27+
# path the the python interpreter and the path to the associated script. For example:
28+
# ::
29+
#
30+
# set(NumPy_CONV_TEMPLATE_EXECUTABLE /path/to/python /path/to/site-packages/numpy/distutils/conv_template.py CACHE STRING "Command executing conv-template program" FORCE)
31+
#
32+
# set(NumPy_FROM_TEMPLATE_EXECUTABLE /path/to/python /path/to/site-packages/numpy/distutils/from_template.py CACHE STRING "Command executing from-template program" FORCE)
33+
#
34+
35+
if(NOT NumPy_FOUND)
36+
set(_find_extra_args)
37+
if(NumPy_FIND_REQUIRED)
38+
list(APPEND _find_extra_args REQUIRED)
39+
endif()
40+
if(NumPy_FIND_QUIET)
41+
list(APPEND _find_extra_args QUIET)
42+
endif()
43+
44+
find_program(NumPy_CONV_TEMPLATE_EXECUTABLE NAMES conv-template)
45+
find_program(NumPy_FROM_TEMPLATE_EXECUTABLE NAMES from-template)
46+
47+
if(PYTHON_EXECUTABLE)
48+
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
49+
-c "import numpy; print(numpy.get_include())"
50+
OUTPUT_VARIABLE _numpy_include_dir
51+
OUTPUT_STRIP_TRAILING_WHITESPACE
52+
ERROR_QUIET
53+
)
54+
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
55+
-c "import numpy; print(numpy.__version__)"
56+
OUTPUT_VARIABLE NumPy_VERSION
57+
OUTPUT_STRIP_TRAILING_WHITESPACE
58+
ERROR_QUIET
59+
)
60+
61+
# XXX This is required to support NumPy < v0.15.0. See note in module documentation above.
62+
if(NOT NumPy_CONV_TEMPLATE_EXECUTABLE)
63+
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
64+
-c "from numpy.distutils import conv_template; print(conv_template.__file__)"
65+
OUTPUT_VARIABLE _numpy_conv_template_file
66+
OUTPUT_STRIP_TRAILING_WHITESPACE
67+
ERROR_QUIET
68+
)
69+
set(NumPy_CONV_TEMPLATE_EXECUTABLE "${PYTHON_EXECUTABLE}" "${_numpy_conv_template_file}" CACHE STRING "Command executing conv-template program" FORCE)
70+
endif()
71+
72+
# XXX This is required to support NumPy < v0.15.0. See note in module documentation above.
73+
if(NOT NumPy_FROM_TEMPLATE_EXECUTABLE)
74+
execute_process(COMMAND "${PYTHON_EXECUTABLE}"
75+
-c "from numpy.distutils import from_template; print(from_template.__file__)"
76+
OUTPUT_VARIABLE _numpy_from_template_file
77+
OUTPUT_STRIP_TRAILING_WHITESPACE
78+
ERROR_QUIET
79+
)
80+
set(NumPy_FROM_TEMPLATE_EXECUTABLE "${PYTHON_EXECUTABLE}" "${_numpy_from_template_file}" CACHE STRING "Command executing from-template program" FORCE)
81+
endif()
82+
endif()
83+
endif()
84+
85+
find_path(NumPy_INCLUDE_DIR
86+
numpy/arrayobject.h
87+
PATHS "${_numpy_include_dir}" "${PYTHON_INCLUDE_DIR}"
88+
PATH_SUFFIXES numpy/core/include
89+
)
90+
91+
set(NumPy_INCLUDE_DIRS ${NumPy_INCLUDE_DIR})
92+
93+
# handle the QUIETLY and REQUIRED arguments and set NumPy_FOUND to TRUE if
94+
# all listed variables are TRUE
95+
include(FindPackageHandleStandardArgs)
96+
find_package_handle_standard_args(NumPy
97+
REQUIRED_VARS
98+
NumPy_INCLUDE_DIR
99+
NumPy_CONV_TEMPLATE_EXECUTABLE
100+
NumPy_FROM_TEMPLATE_EXECUTABLE
101+
VERSION_VAR NumPy_VERSION
102+
)
103+
104+
mark_as_advanced(NumPy_INCLUDE_DIR)

0 commit comments

Comments
 (0)