blob: 812ec094aafde3c497ebcfcf024c82146490affe [file] [log] [blame]
Henry Schreiner1729aae2020-08-19 12:26:26 -04001# tools/pybind11NewTools.cmake -- Build system for the pybind11 modules
2#
3# Copyright (c) 2020 Wenzel Jakob <wenzel@inf.ethz.ch> and Henry Schreiner
4#
5# All rights reserved. Use of this source code is governed by a
6# BSD-style license that can be found in the LICENSE file.
7
8get_property(
9 is_config
10 TARGET pybind11::headers
11 PROPERTY IMPORTED)
12
13if(pybind11_FIND_QUIETLY)
14 set(_pybind11_quiet QUIET)
15endif()
16
17if(CMAKE_VERSION VERSION_LESS 3.12)
18 message(FATAL_ERROR "You cannot use the new FindPython module with CMake < 3.12")
19endif()
20
21if(NOT Python_FOUND
22 AND NOT Python3_FOUND
23 AND NOT Python2_FOUND)
24 if(NOT DEFINED Python_FIND_IMPLEMENTATIONS)
25 set(Python_FIND_IMPLEMENTATIONS CPython PyPy)
26 endif()
27
28 # GitHub Actions like activation
29 if(NOT DEFINED Python_ROOT_DIR AND DEFINED ENV{pythonLocation})
30 set(Python_ROOT_DIR "$ENV{pythonLocation}")
31 endif()
32
33 find_package(Python REQUIRED COMPONENTS Interpreter Development ${_pybind11_quiet})
34
35 # If we are in submodule mode, export the Python targets to global targets.
36 # If this behavior is not desired, FindPython _before_ pybind11.
37 if(NOT is_config)
38 set_property(TARGET Python::Python PROPERTY IMPORTED_GLOBAL TRUE)
39 set_property(TARGET Python::Interpreter PROPERTY IMPORTED_GLOBAL TRUE)
40 if(TARGET Python::Module)
41 set_property(TARGET Python::Module PROPERTY IMPORTED_GLOBAL TRUE)
42 endif()
43 endif()
44endif()
45
46if(Python_FOUND)
47 set(_Python
48 Python
49 CACHE INTERNAL "" FORCE)
50elseif(Python3_FOUND AND NOT Python2_FOUND)
51 set(_Python
52 Python3
53 CACHE INTERNAL "" FORCE)
54elseif(Python2_FOUND AND NOT Python3_FOUND)
55 set(_Python
56 Python2
57 CACHE INTERNAL "" FORCE)
58else()
59 message(AUTHOR_WARNING "Python2 and Python3 both present, pybind11 in "
60 "PYBIND11_NOPYTHON mode (manually activate to silence warning)")
61 set(_pybind11_nopython ON)
62 return()
63endif()
64
65if(PYBIND11_MASTER_PROJECT)
66 if(${_Python}_INTERPRETER_ID MATCHES "PyPy")
67 message(STATUS "PyPy ${${_Python}_PyPy_VERSION} (Py ${${_Python}_VERSION})")
68 else()
69 message(STATUS "${_Python} ${${_Python}_VERSION}")
70 endif()
71endif()
72
73# Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
Henry Schreinerce1a07e2020-09-04 21:54:09 -040074execute_process(
75 COMMAND "${${_Python}_EXECUTABLE}" "-c" "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))"
76 RESULT_VARIABLE PYTHON_IS_DEBUG)
77
78# Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is
79# required for PyPy3 (as of 7.3.1)
80execute_process(
81 COMMAND "${${_Python}_EXECUTABLE}" "-c"
82 "from distutils import sysconfig; print(sysconfig.get_config_var('SO'))"
83 OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION
84 ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
85
86# This needs to be available for the pybind11_extension function
87set(PYTHON_MODULE_EXTENSION
88 "${_PYTHON_MODULE_EXTENSION}"
89 CACHE INTERNAL "")
Henry Schreiner1729aae2020-08-19 12:26:26 -040090
91# Python debug libraries expose slightly different objects before 3.8
92# https://docs.python.org/3.6/c-api/intro.html#debugging-builds
93# https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
94if(PYTHON_IS_DEBUG)
95 set_property(
Henry Schreiner56df3c42020-08-21 15:27:21 -040096 TARGET pybind11::pybind11
Henry Schreiner1729aae2020-08-19 12:26:26 -040097 APPEND
98 PROPERTY INTERFACE_COMPILE_DEFINITIONS Py_DEBUG)
99endif()
100
101# Check on every access - since Python2 and Python3 could have been used - do nothing in that case.
102
103if(DEFINED ${_Python}_INCLUDE_DIRS)
104 set_property(
105 TARGET pybind11::pybind11
106 APPEND
107 PROPERTY INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${${_Python}_INCLUDE_DIRS}>)
108endif()
109
110if(DEFINED ${_Python}_VERSION AND ${_Python}_VERSION VERSION_LESS 3)
111 set_property(
112 TARGET pybind11::pybind11
113 APPEND
114 PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python2_no_register)
115endif()
116
117# In CMake 3.18+, you can find these separately, so include an if
118if(TARGET ${_Python}::${_Python})
119 set_property(
120 TARGET pybind11::embed
121 APPEND
122 PROPERTY INTERFACE_LINK_LIBRARIES ${_Python}::${_Python})
123endif()
124
125# CMake 3.15+ has this
126if(TARGET ${_Python}::Module)
127 set_property(
128 TARGET pybind11::module
129 APPEND
130 PROPERTY INTERFACE_LINK_LIBRARIES ${_Python}::Module)
131else()
132 set_property(
133 TARGET pybind11::module
134 APPEND
135 PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_link_helper)
136endif()
137
Henry Schreinerce1a07e2020-09-04 21:54:09 -0400138# WITHOUT_SOABI and WITH_SOABI will disable the custom extension handling used by pybind11.
139# WITH_SOABI is passed on to python_add_library.
Henry Schreiner1729aae2020-08-19 12:26:26 -0400140function(pybind11_add_module target_name)
Wenzel Jakob36c666f2020-09-04 23:31:05 +0200141 cmake_parse_arguments(PARSE_ARGV 1 ARG
142 "STATIC;SHARED;MODULE;THIN_LTO;OPT_SIZE;NO_EXTRAS;WITHOUT_SOABI" "" "")
Henry Schreiner1729aae2020-08-19 12:26:26 -0400143
144 if(ARG_ADD_LIBRARY_STATIC)
145 set(type STATIC)
146 elseif(ARG_ADD_LIBRARY_SHARED)
147 set(type SHARED)
148 else()
149 set(type MODULE)
150 endif()
151
152 if("${_Python}" STREQUAL "Python")
Henry Schreinerce1a07e2020-09-04 21:54:09 -0400153 python_add_library(${target_name} ${type} ${ARG_UNPARSED_ARGUMENTS})
Henry Schreiner1729aae2020-08-19 12:26:26 -0400154 elseif("${_Python}" STREQUAL "Python3")
Henry Schreinerce1a07e2020-09-04 21:54:09 -0400155 python3_add_library(${target_name} ${type} ${ARG_UNPARSED_ARGUMENTS})
Henry Schreiner1729aae2020-08-19 12:26:26 -0400156 elseif("${_Python}" STREQUAL "Python2")
Henry Schreinerce1a07e2020-09-04 21:54:09 -0400157 python2_add_library(${target_name} ${type} ${ARG_UNPARSED_ARGUMENTS})
Henry Schreiner1729aae2020-08-19 12:26:26 -0400158 else()
159 message(FATAL_ERROR "Cannot detect FindPython version: ${_Python}")
160 endif()
161
162 target_link_libraries(${target_name} PRIVATE pybind11::headers)
163
164 if(type STREQUAL "MODULE")
165 target_link_libraries(${target_name} PRIVATE pybind11::module)
166 else()
167 target_link_libraries(${target_name} PRIVATE pybind11::embed)
168 endif()
169
170 if(MSVC)
171 target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
172 endif()
173
174 if(DEFINED ${_Python}_VERSION AND ${_Python}_VERSION VERSION_LESS 3)
175 target_link_libraries(${target_name} PRIVATE pybind11::python2_no_register)
176 endif()
177
178 set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden"
179 CUDA_VISIBILITY_PRESET "hidden")
180
Henry Schreinerce1a07e2020-09-04 21:54:09 -0400181 # If we don't pass a WITH_SOABI or WITHOUT_SOABI, use our own default handling of extensions
182 if("${type}" STREQUAL "MODULE" AND (NOT ARG_WITHOUT_SOABI OR NOT "WITH_SOABI" IN_LIST
183 ARG_UNPARSED_ARGUMENTS))
184 pybind11_extension(${target_name})
185 endif()
186
Henry Schreiner1729aae2020-08-19 12:26:26 -0400187 if(ARG_NO_EXTRAS)
188 return()
189 endif()
190
191 if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION)
192 if(ARG_THIN_LTO)
193 target_link_libraries(${target_name} PRIVATE pybind11::thin_lto)
194 else()
195 target_link_libraries(${target_name} PRIVATE pybind11::lto)
196 endif()
197 endif()
198
199 if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
200 # Strip unnecessary sections of the binary on Linux/Mac OS
201 pybind11_strip(${target_name})
202 endif()
203
204 if(MSVC)
205 target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
206 endif()
Wenzel Jakob36c666f2020-09-04 23:31:05 +0200207
208 if(ARG_OPT_SIZE)
209 target_link_libraries(${target_name} PRIVATE pybind11::opt_size)
210 endif()
Henry Schreiner1729aae2020-08-19 12:26:26 -0400211endfunction()
212
213function(pybind11_extension name)
Henry Schreinerce1a07e2020-09-04 21:54:09 -0400214 # The extension is precomputed
215 set_target_properties(${name} PROPERTIES PREFIX "" SUFFIX "${PYTHON_MODULE_EXTENSION}")
Henry Schreiner1729aae2020-08-19 12:26:26 -0400216
Henry Schreiner1729aae2020-08-19 12:26:26 -0400217endfunction()