1 # Define a function to create Cython modules.
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."
7 # This file defines a CMake function to build a Cython Python module.
8 # To use it, first include this file.
10 # include( UseCython )
12 # Then call cython_add_module to create a module.
14 # cython_add_module( <target_name> <pyx_target_name> <output_files> <src1> <src2> ... <srcN> )
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).
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.
27 # Cache variables that effect the behavior include:
30 # CYTHON_NO_DOCSTRINGS
33 # Source file properties that effect the build process are
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.
42 # See also FindCython.cmake
44 #=============================================================================
45 # Copyright 2011 Kitware, Inc.
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
51 # http://www.apache.org/licenses/LICENSE-2.0
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 #=============================================================================
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)
66 find_package(Python3Alt REQUIRED)
68 # (using another C++ extension breaks coverage)
69 set(CYTHON_CXX_EXTENSION "cpp")
70 set(CYTHON_C_EXTENSION "c")
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.
80 # Default to assuming all files are C.
82 set(extension ${CYTHON_C_EXTENSION})
84 set(comment "Compiling Cython C source for ${_name}...")
86 get_filename_component(pyx_file_basename "${pyx_file}" NAME_WE)
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})
94 set(comment "Compiling Cython CXX source for ${_name}...")
96 get_source_file_property(pyx_location ${pyx_file} LOCATION)
98 set(output_file "${_name}.${extension}")
100 # Set additional flags.
102 set(annotate_arg "--annotate")
105 if(CYTHON_NO_DOCSTRINGS)
106 set(no_docstrings_arg "--no-docstrings")
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")
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")
125 set(_generated_files "${output_file}")
127 set_source_files_properties(${_generated_files} PROPERTIES GENERATED TRUE)
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)
135 set(${generated_files} ${_generated_files} PARENT_SCOPE)
137 # Add the command to run the compiler.
140 COMMAND ${PYTHON_EXECUTABLE}
148 # Necessary for autodoc of function arguments
149 --directive embedsignature=True
150 # Necessary for Cython code coverage
152 ${CMAKE_CURRENT_SOURCE_DIR}
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}
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 "")
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})
177 list(APPEND other_module_sources ${_file})
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})
187 include(CMakeParseArguments)