]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/cpp/cmake_modules/UseCython.cmake
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / cmake_modules / UseCython.cmake
1 # Define a function to create Cython modules.
2 #
3 # For more information on the Cython project, see http://cython.org/.
4 # "Cython is a language that makes writing C extensions for the Python language
5 # as easy as Python itself."
6 #
7 # This file defines a CMake function to build a Cython Python module.
8 # To use it, first include this file.
9 #
10 # include( UseCython )
11 #
12 # Then call cython_add_module to create a module.
13 #
14 # cython_add_module( <target_name> <pyx_target_name> <output_files> <src1> <src2> ... <srcN> )
15 #
16 # Where <module_name> is the desired name of the target for the resulting Python module,
17 # <pyx_target_name> is the desired name of the target that runs the Cython compiler
18 # to generate the needed C or C++ files, <output_files> is a variable to hold the
19 # files generated by Cython, and <src1> <src2> ... are source files
20 # to be compiled into the module, e.g. *.pyx, *.c, *.cxx, etc.
21 # only one .pyx file may be present for each target
22 # (this is an inherent limitation of Cython).
23 #
24 # The sample paths set with the CMake include_directories() command will be used
25 # for include directories to search for *.pxd when running the Cython compiler.
26 #
27 # Cache variables that effect the behavior include:
28 #
29 # CYTHON_ANNOTATE
30 # CYTHON_NO_DOCSTRINGS
31 # CYTHON_FLAGS
32 #
33 # Source file properties that effect the build process are
34 #
35 # CYTHON_IS_CXX
36 # CYTHON_IS_PUBLIC
37 # CYTHON_IS_API
38 #
39 # If this is set of a *.pyx file with CMake set_source_files_properties()
40 # command, the file will be compiled as a C++ file.
41 #
42 # See also FindCython.cmake
43
44 #=============================================================================
45 # Copyright 2011 Kitware, Inc.
46 #
47 # Licensed under the Apache License, Version 2.0 (the "License");
48 # you may not use this file except in compliance with the License.
49 # You may obtain a copy of the License at
50 #
51 # http://www.apache.org/licenses/LICENSE-2.0
52 #
53 # Unless required by applicable law or agreed to in writing, software
54 # distributed under the License is distributed on an "AS IS" BASIS,
55 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
56 # See the License for the specific language governing permissions and
57 # limitations under the License.
58 #=============================================================================
59
60 # Configuration options.
61 set(CYTHON_ANNOTATE OFF CACHE BOOL "Create an annotated .html file when compiling *.pyx.")
62 set(CYTHON_NO_DOCSTRINGS OFF CACHE BOOL "Strip docstrings from the compiled module.")
63 set(CYTHON_FLAGS "" CACHE STRING "Extra flags to the cython compiler.")
64 mark_as_advanced(CYTHON_ANNOTATE CYTHON_NO_DOCSTRINGS CYTHON_FLAGS)
65
66 find_package(Python3Alt REQUIRED)
67
68 # (using another C++ extension breaks coverage)
69 set(CYTHON_CXX_EXTENSION "cpp")
70 set(CYTHON_C_EXTENSION "c")
71
72 # Create a *.c or *.cpp file from a *.pyx file.
73 # Input the generated file basename. The generate files will put into the variable
74 # placed in the "generated_files" argument. Finally all the *.py and *.pyx files.
75 function(compile_pyx
76 _name
77 pyx_target_name
78 generated_files
79 pyx_file)
80 # Default to assuming all files are C.
81 set(cxx_arg "")
82 set(extension ${CYTHON_C_EXTENSION})
83 set(pyx_lang "C")
84 set(comment "Compiling Cython C source for ${_name}...")
85
86 get_filename_component(pyx_file_basename "${pyx_file}" NAME_WE)
87
88 # Determine if it is a C or C++ file.
89 get_source_file_property(property_is_cxx ${pyx_file} CYTHON_IS_CXX)
90 if(${property_is_cxx})
91 set(cxx_arg "--cplus")
92 set(extension ${CYTHON_CXX_EXTENSION})
93 set(pyx_lang "CXX")
94 set(comment "Compiling Cython CXX source for ${_name}...")
95 endif()
96 get_source_file_property(pyx_location ${pyx_file} LOCATION)
97
98 set(output_file "${_name}.${extension}")
99
100 # Set additional flags.
101 if(CYTHON_ANNOTATE)
102 set(annotate_arg "--annotate")
103 endif()
104
105 if(CYTHON_NO_DOCSTRINGS)
106 set(no_docstrings_arg "--no-docstrings")
107 endif()
108
109 if(NOT WIN32)
110 string( TOLOWER "${CMAKE_BUILD_TYPE}" build_type )
111 if("${build_type}" STREQUAL "debug"
112 OR "${build_type}" STREQUAL "relwithdebinfo")
113 set(cython_debug_arg "--gdb")
114 endif()
115 endif()
116
117 # Determining generated file names.
118 get_source_file_property(property_is_public ${pyx_file} CYTHON_PUBLIC)
119 get_source_file_property(property_is_api ${pyx_file} CYTHON_API)
120 if(${property_is_api})
121 set(_generated_files "${output_file}" "${_name}.h" "${name}_api.h")
122 elseif(${property_is_public})
123 set(_generated_files "${output_file}" "${_name}.h")
124 else()
125 set(_generated_files "${output_file}")
126 endif()
127 set_source_files_properties(${_generated_files} PROPERTIES GENERATED TRUE)
128
129 if(NOT WIN32)
130 # Cython creates a lot of compiler warning detritus on clang
131 set_source_files_properties(${_generated_files} PROPERTIES COMPILE_FLAGS
132 -Wno-unused-function)
133 endif()
134
135 set(${generated_files} ${_generated_files} PARENT_SCOPE)
136
137 # Add the command to run the compiler.
138 add_custom_target(
139 ${pyx_target_name}
140 COMMAND ${PYTHON_EXECUTABLE}
141 -m
142 cython
143 ${cxx_arg}
144 ${annotate_arg}
145 ${no_docstrings_arg}
146 ${cython_debug_arg}
147 ${CYTHON_FLAGS}
148 # Necessary for autodoc of function arguments
149 --directive embedsignature=True
150 # Necessary for Cython code coverage
151 --working
152 ${CMAKE_CURRENT_SOURCE_DIR}
153 --output-file
154 "${CMAKE_CURRENT_BINARY_DIR}/${output_file}"
155 "${CMAKE_CURRENT_SOURCE_DIR}/${pyx_file}"
156 DEPENDS ${pyx_location}
157 # Do not specify byproducts for now since they don't work with the older
158 # version of cmake available in the apt repositories.
159 #BYPRODUCTS ${_generated_files}
160 COMMENT ${comment})
161
162 # Remove their visibility to the user.
163 set(corresponding_pxd_file "" CACHE INTERNAL "")
164 set(header_location "" CACHE INTERNAL "")
165 set(pxd_location "" CACHE INTERNAL "")
166 endfunction()
167
168 # cython_add_module( <name> src1 src2 ... srcN )
169 # Build the Cython Python module.
170 function(cython_add_module _name pyx_target_name generated_files)
171 set(pyx_module_source "")
172 set(other_module_sources "")
173 foreach(_file ${ARGN})
174 if(${_file} MATCHES ".*\\.py[x]?$")
175 list(APPEND pyx_module_source ${_file})
176 else()
177 list(APPEND other_module_sources ${_file})
178 endif()
179 endforeach()
180 compile_pyx(${_name} ${pyx_target_name} _generated_files ${pyx_module_source})
181 set(${generated_files} ${_generated_files} PARENT_SCOPE)
182 include_directories(${PYTHON_INCLUDE_DIRS})
183 python_add_module(${_name} ${_generated_files} ${other_module_sources})
184 add_dependencies(${_name} ${pyx_target_name})
185 endfunction()
186
187 include(CMakeParseArguments)