blob: 742699aca79af00e45174e47e79dabf94a9ad6c0 [file] [log] [blame]
Lori A. Burns545b4db2016-12-13 10:59:21 -05001# tools/pybind11Tools.cmake -- Build system for the pybind11 modules
2#
3# Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
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
Henry Schreiner6ec17752020-07-28 00:43:12 -04008cmake_minimum_required(VERSION 3.7)
9
10# VERSION 3.7...3.18, but some versions of VS have a patched CMake 3.11
11# that do not work properly with this syntax, so using the following workaround:
12if(${CMAKE_VERSION} VERSION_LESS 3.18)
13 cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
14else()
15 cmake_policy(VERSION 3.18)
16endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -050017
18# Add a CMake parameter for choosing a desired Python version
Dean Moldovan5b5e3de2017-01-06 23:38:00 +010019if(NOT PYBIND11_PYTHON_VERSION)
Henry Schreiner6ec17752020-07-28 00:43:12 -040020 set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules")
Dean Moldovan5b5e3de2017-01-06 23:38:00 +010021endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -050022
Henry Schreiner6ec17752020-07-28 00:43:12 -040023# A user can set versions manually too
24set(Python_ADDITIONAL_VERSIONS "3.9;3.8;3.7;3.6;3.5;3.4" CACHE INTERNAL "")
Lori A. Burns545b4db2016-12-13 10:59:21 -050025find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
26
27include(CheckCXXCompilerFlag)
Dean Moldovan0cbec5c2016-12-16 22:58:37 +010028include(CMakeParseArguments)
Lori A. Burns545b4db2016-12-13 10:59:21 -050029
Henry Schreiner1b92cd12020-07-29 15:02:53 -040030# Warn or error if old variable name used
Henry Schreiner6ec17752020-07-28 00:43:12 -040031if(PYBIND11_CPP_STANDARD)
Henry Schreiner6ec17752020-07-28 00:43:12 -040032 if(NOT CMAKE_CXX_STANDARD)
33 string(REGEX MATCH
34 [=[..^]=]
35 VAL
36 "${PYBIND11_CPP_STANDARD}")
Henry Schreiner1b92cd12020-07-29 15:02:53 -040037 set(supported_standards 11 14 17 20)
38 if("${VAL}" IN_LIST supported_standards)
39 message(WARNING "USE -DCMAKE_CXX_STANDARD=${VAL} instead of PYBIND11_PYTHON_VERSION")
40 set(CMAKE_CXX_STANDARD ${VAL})
41 else()
42 message(FATAL_ERROR "PYBIND11_CPP_STANDARD should be replaced with CMAKE_CXX_STANDARD")
43 endif()
Chuck Atkinsd730fbc2018-06-12 13:18:48 -040044 endif()
Chuck Atkinsd730fbc2018-06-12 13:18:48 -040045endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -050046
Jason Rhinelanderfad5d332017-07-12 14:59:16 -040047
Lori A. Burns545b4db2016-12-13 10:59:21 -050048
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -050049# Checks whether the given CXX/linker flags can compile and link a cxx file. cxxflags and
50# linkerflags are lists of flags to use. The result variable is a unique variable name for each set
51# of flags: the compilation result will be cached base on the result variable. If the flags work,
52# sets them in cxxflags_out/linkerflags_out internal cache variables (in addition to ${result}).
53function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerflags cxxflags_out linkerflags_out)
54 set(CMAKE_REQUIRED_LIBRARIES ${linkerflags})
55 check_cxx_compiler_flag("${cxxflags}" ${result})
56 if (${result})
57 set(${cxxflags_out} "${cxxflags}" CACHE INTERNAL "" FORCE)
58 set(${linkerflags_out} "${linkerflags}" CACHE INTERNAL "" FORCE)
59 endif()
Jason Rhinelander1bee6e72017-01-17 02:13:11 -050060endfunction()
Dean Moldovan0cbec5c2016-12-16 22:58:37 +010061
Jason Rhinelander1bee6e72017-01-17 02:13:11 -050062# Internal: find the appropriate link time optimization flags for this compiler
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -050063function(_pybind11_add_lto_flags target_name prefer_thin_lto)
64 if (NOT DEFINED PYBIND11_LTO_CXX_FLAGS)
65 set(PYBIND11_LTO_CXX_FLAGS "" CACHE INTERNAL "")
66 set(PYBIND11_LTO_LINKER_FLAGS "" CACHE INTERNAL "")
67
68 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
69 set(cxx_append "")
70 set(linker_append "")
71 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE)
72 # Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it
73 set(linker_append ";$<$<CONFIG:MinSizeRel>:-O3>")
74 elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
75 set(cxx_append ";-fno-fat-lto-objects")
76 endif()
77
78 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND prefer_thin_lto)
79 _pybind11_return_if_cxx_and_linker_flags_work(HAS_FLTO_THIN
80 "-flto=thin${cxx_append}" "-flto=thin${linker_append}"
81 PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
82 endif()
83
84 if (NOT HAS_FLTO_THIN)
85 _pybind11_return_if_cxx_and_linker_flags_work(HAS_FLTO
86 "-flto${cxx_append}" "-flto${linker_append}"
87 PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
88 endif()
89 elseif (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
90 # Intel equivalent to LTO is called IPO
91 _pybind11_return_if_cxx_and_linker_flags_work(HAS_INTEL_IPO
92 "-ipo" "-ipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
93 elseif(MSVC)
94 # cmake only interprets libraries as linker flags when they start with a - (otherwise it
95 # converts /LTCG to \LTCG as if it was a Windows path). Luckily MSVC supports passing flags
96 # with - instead of /, even if it is a bit non-standard:
97 _pybind11_return_if_cxx_and_linker_flags_work(HAS_MSVC_GL_LTCG
98 "/GL" "-LTCG" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
Jason Rhinelander1bee6e72017-01-17 02:13:11 -050099 endif()
100
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -0500101 if (PYBIND11_LTO_CXX_FLAGS)
102 message(STATUS "LTO enabled")
103 else()
104 message(STATUS "LTO disabled (not supported by the compiler and/or linker)")
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500105 endif()
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500106 endif()
107
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -0500108 # Enable LTO flags if found, except for Debug builds
109 if (PYBIND11_LTO_CXX_FLAGS)
Henry Schreiner6ec17752020-07-28 00:43:12 -0400110 set(not_debug "$<NOT:$<CONFIG:Debug>>")
111 set(cxx_lang "$<COMPILE_LANGUAGE:CXX>")
112 target_compile_options(${target_name} PRIVATE "$<$<AND:${not_debug},${cxx_lang}>:${PYBIND11_LTO_CXX_FLAGS}>")
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -0500113 endif()
114 if (PYBIND11_LTO_LINKER_FLAGS)
Henry Schreiner6ec17752020-07-28 00:43:12 -0400115 target_link_libraries(${target_name} PRIVATE "$<${not_debug}:${PYBIND11_LTO_LINKER_FLAGS}>")
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -0500116 endif()
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500117endfunction()
118
Lori A. Burns545b4db2016-12-13 10:59:21 -0500119# Build a Python extension module:
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100120# pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
Henry Schreiner6ec17752020-07-28 00:43:12 -0400121# [NO_EXTRAS] [THIN_LTO] source1 [source2 ...])
Lori A. Burns545b4db2016-12-13 10:59:21 -0500122#
123function(pybind11_add_module target_name)
Axel Huebl435dbdd2018-08-29 13:20:11 +0200124 set(options MODULE SHARED EXCLUDE_FROM_ALL NO_EXTRAS SYSTEM THIN_LTO)
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100125 cmake_parse_arguments(ARG "${options}" "" "" ${ARGN})
Lori A. Burns545b4db2016-12-13 10:59:21 -0500126
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100127 if(ARG_MODULE AND ARG_SHARED)
128 message(FATAL_ERROR "Can't be both MODULE and SHARED")
129 elseif(ARG_SHARED)
130 set(lib_type SHARED)
131 else()
132 set(lib_type MODULE)
133 endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -0500134
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100135 if(ARG_EXCLUDE_FROM_ALL)
136 set(exclude_from_all EXCLUDE_FROM_ALL)
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200137 else()
138 set(exclude_from_all "")
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100139 endif()
140
141 add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS})
Lori A. Burns545b4db2016-12-13 10:59:21 -0500142
Henry Schreiner6ec17752020-07-28 00:43:12 -0400143 target_link_libraries(${target_name} PRIVATE pybind11::module)
144
Axel Huebl435dbdd2018-08-29 13:20:11 +0200145 if(ARG_SYSTEM)
Henry Schreiner6ec17752020-07-28 00:43:12 -0400146 message(STATUS "Warning: this does not have an effect - use NO_SYSTEM_FROM_IMPORTED if using imported targets")
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200147 endif()
148
Henry Schreiner6ec17752020-07-28 00:43:12 -0400149 # Python debug libraries expose slightly different objects before 3.8
Axel Huebl3a945612018-08-29 13:18:43 +0200150 # https://docs.python.org/3.6/c-api/intro.html#debugging-builds
151 # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
152 if(PYTHON_IS_DEBUG)
153 target_compile_definitions(${target_name} PRIVATE Py_DEBUG)
154 endif()
155
Lori A. Burns545b4db2016-12-13 10:59:21 -0500156 # The prefix and extension are provided by FindPythonLibsNew.cmake
157 set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
158 set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
159
Jason Rhinelander97aa54f2017-08-10 12:08:42 -0400160 # -fvisibility=hidden is required to allow multiple modules compiled against
161 # different pybind versions to work properly, and for some features (e.g.
162 # py::module_local). We force it on everything inside the `pybind11`
163 # namespace; also turning it on for a pybind module compilation here avoids
164 # potential warnings or issues from having mixed hidden/non-hidden types.
165 set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
Davis E. King9343e682018-09-14 08:28:54 -0400166 set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
Jason Rhinelander97aa54f2017-08-10 12:08:42 -0400167
Henry Schreiner1f53c372020-07-23 22:55:29 -0400168
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100169 if(ARG_NO_EXTRAS)
Henry Schreiner6ec17752020-07-28 00:43:12 -0400170 return()
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100171 endif()
172
Henry Schreiner6ec17752020-07-28 00:43:12 -0400173 if(CMAKE_VERSION VERSION_LESS 3.9 OR PYBIND11_CLASSIC_LTO)
174 _pybind11_add_lto_flags(${target_name} ${ARG_THIN_LTO})
175 else()
176 include(CheckIPOSupported)
177 check_ipo_supported(RESULT supported OUTPUT error)
178 if(supported)
179 set_property(
180 TARGET
181 ${target_name}
182 PROPERTY
183 INTERPROCEDURAL_OPTIMIZATION TRUE
184 )
185 endif()
186 endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -0500187
Dmitry8f5a8ab2019-08-23 17:18:05 +0300188 if (NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500189 # Strip unnecessary sections of the binary on Linux/Mac OS
190 if(CMAKE_STRIP)
191 if(APPLE)
192 add_custom_command(TARGET ${target_name} POST_BUILD
193 COMMAND ${CMAKE_STRIP} -x $<TARGET_FILE:${target_name}>)
194 else()
195 add_custom_command(TARGET ${target_name} POST_BUILD
196 COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
Lori A. Burns545b4db2016-12-13 10:59:21 -0500197 endif()
198 endif()
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500199 endif()
200
201 if(MSVC)
Lori A. Burns545b4db2016-12-13 10:59:21 -0500202 # /MP enables multithreaded builds (relevant when there are many files), /bigobj is
203 # needed for bigger binding projects due to the limit to 64k addressable sections
Rune Paamand06d021b2018-10-25 01:35:33 +0200204 target_compile_options(${target_name} PRIVATE /bigobj)
Davis E. King9343e682018-09-14 08:28:54 -0400205 if(CMAKE_VERSION VERSION_LESS 3.11)
Rune Paamand06d021b2018-10-25 01:35:33 +0200206 target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MP>)
Davis E. King9343e682018-09-14 08:28:54 -0400207 else()
208 # Only set these options for C++ files. This is important so that, for
209 # instance, projects that include other types of source files like CUDA
210 # .cu files don't get these options propagated to nvcc since that would
211 # cause the build to fail.
Rune Paamand06d021b2018-10-25 01:35:33 +0200212 target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:$<$<COMPILE_LANGUAGE:CXX>:/MP>>)
Davis E. King9343e682018-09-14 08:28:54 -0400213 endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -0500214 endif()
215endfunction()