blob: 3a141dd6003560eebae0d3e721d1a030e648c344 [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
Chuck Atkinsd730fbc2018-06-12 13:18:48 -040030# Use the language standards abstraction if CMake supports it with the current compiler
Henry Schreiner6ec17752020-07-28 00:43:12 -040031if(PYBIND11_CPP_STANDARD)
32 message(WARNING "USE -DCMAKE_CXX_STANDARD=11 instead of PYBIND11_PYTHON_VERSION")
33 if(NOT CMAKE_CXX_STANDARD)
34 string(REGEX MATCH
35 [=[..^]=]
36 VAL
37 "${PYBIND11_CPP_STANDARD}")
38 set(CMAKE_CXX_STANDARD ${VAL})
Chuck Atkinsd730fbc2018-06-12 13:18:48 -040039 endif()
Chuck Atkinsd730fbc2018-06-12 13:18:48 -040040endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -050041
Jason Rhinelanderfad5d332017-07-12 14:59:16 -040042
Lori A. Burns545b4db2016-12-13 10:59:21 -050043
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -050044# Checks whether the given CXX/linker flags can compile and link a cxx file. cxxflags and
45# linkerflags are lists of flags to use. The result variable is a unique variable name for each set
46# of flags: the compilation result will be cached base on the result variable. If the flags work,
47# sets them in cxxflags_out/linkerflags_out internal cache variables (in addition to ${result}).
48function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerflags cxxflags_out linkerflags_out)
49 set(CMAKE_REQUIRED_LIBRARIES ${linkerflags})
50 check_cxx_compiler_flag("${cxxflags}" ${result})
51 if (${result})
52 set(${cxxflags_out} "${cxxflags}" CACHE INTERNAL "" FORCE)
53 set(${linkerflags_out} "${linkerflags}" CACHE INTERNAL "" FORCE)
54 endif()
Jason Rhinelander1bee6e72017-01-17 02:13:11 -050055endfunction()
Dean Moldovan0cbec5c2016-12-16 22:58:37 +010056
Jason Rhinelander1bee6e72017-01-17 02:13:11 -050057# Internal: find the appropriate link time optimization flags for this compiler
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -050058function(_pybind11_add_lto_flags target_name prefer_thin_lto)
59 if (NOT DEFINED PYBIND11_LTO_CXX_FLAGS)
60 set(PYBIND11_LTO_CXX_FLAGS "" CACHE INTERNAL "")
61 set(PYBIND11_LTO_LINKER_FLAGS "" CACHE INTERNAL "")
62
63 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
64 set(cxx_append "")
65 set(linker_append "")
66 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE)
67 # Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it
68 set(linker_append ";$<$<CONFIG:MinSizeRel>:-O3>")
69 elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
70 set(cxx_append ";-fno-fat-lto-objects")
71 endif()
72
73 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND prefer_thin_lto)
74 _pybind11_return_if_cxx_and_linker_flags_work(HAS_FLTO_THIN
75 "-flto=thin${cxx_append}" "-flto=thin${linker_append}"
76 PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
77 endif()
78
79 if (NOT HAS_FLTO_THIN)
80 _pybind11_return_if_cxx_and_linker_flags_work(HAS_FLTO
81 "-flto${cxx_append}" "-flto${linker_append}"
82 PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
83 endif()
84 elseif (CMAKE_CXX_COMPILER_ID MATCHES "Intel")
85 # Intel equivalent to LTO is called IPO
86 _pybind11_return_if_cxx_and_linker_flags_work(HAS_INTEL_IPO
87 "-ipo" "-ipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
88 elseif(MSVC)
89 # cmake only interprets libraries as linker flags when they start with a - (otherwise it
90 # converts /LTCG to \LTCG as if it was a Windows path). Luckily MSVC supports passing flags
91 # with - instead of /, even if it is a bit non-standard:
92 _pybind11_return_if_cxx_and_linker_flags_work(HAS_MSVC_GL_LTCG
93 "/GL" "-LTCG" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
Jason Rhinelander1bee6e72017-01-17 02:13:11 -050094 endif()
95
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -050096 if (PYBIND11_LTO_CXX_FLAGS)
97 message(STATUS "LTO enabled")
98 else()
99 message(STATUS "LTO disabled (not supported by the compiler and/or linker)")
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500100 endif()
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500101 endif()
102
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -0500103 # Enable LTO flags if found, except for Debug builds
104 if (PYBIND11_LTO_CXX_FLAGS)
Henry Schreiner6ec17752020-07-28 00:43:12 -0400105 set(not_debug "$<NOT:$<CONFIG:Debug>>")
106 set(cxx_lang "$<COMPILE_LANGUAGE:CXX>")
107 target_compile_options(${target_name} PRIVATE "$<$<AND:${not_debug},${cxx_lang}>:${PYBIND11_LTO_CXX_FLAGS}>")
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -0500108 endif()
109 if (PYBIND11_LTO_LINKER_FLAGS)
Henry Schreiner6ec17752020-07-28 00:43:12 -0400110 target_link_libraries(${target_name} PRIVATE "$<${not_debug}:${PYBIND11_LTO_LINKER_FLAGS}>")
Jason Rhinelanderc137c0a2017-02-09 21:27:29 -0500111 endif()
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500112endfunction()
113
Lori A. Burns545b4db2016-12-13 10:59:21 -0500114# Build a Python extension module:
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100115# pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
Henry Schreiner6ec17752020-07-28 00:43:12 -0400116# [NO_EXTRAS] [THIN_LTO] source1 [source2 ...])
Lori A. Burns545b4db2016-12-13 10:59:21 -0500117#
118function(pybind11_add_module target_name)
Axel Huebl435dbdd2018-08-29 13:20:11 +0200119 set(options MODULE SHARED EXCLUDE_FROM_ALL NO_EXTRAS SYSTEM THIN_LTO)
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100120 cmake_parse_arguments(ARG "${options}" "" "" ${ARGN})
Lori A. Burns545b4db2016-12-13 10:59:21 -0500121
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100122 if(ARG_MODULE AND ARG_SHARED)
123 message(FATAL_ERROR "Can't be both MODULE and SHARED")
124 elseif(ARG_SHARED)
125 set(lib_type SHARED)
126 else()
127 set(lib_type MODULE)
128 endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -0500129
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100130 if(ARG_EXCLUDE_FROM_ALL)
131 set(exclude_from_all EXCLUDE_FROM_ALL)
Nils Leif Fischer141e8cc2020-06-26 12:15:10 +0200132 else()
133 set(exclude_from_all "")
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100134 endif()
135
136 add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS})
Lori A. Burns545b4db2016-12-13 10:59:21 -0500137
Henry Schreiner6ec17752020-07-28 00:43:12 -0400138 target_link_libraries(${target_name} PRIVATE pybind11::module)
139
Axel Huebl435dbdd2018-08-29 13:20:11 +0200140 if(ARG_SYSTEM)
Henry Schreiner6ec17752020-07-28 00:43:12 -0400141 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 +0200142 endif()
143
Henry Schreiner6ec17752020-07-28 00:43:12 -0400144 # Python debug libraries expose slightly different objects before 3.8
Axel Huebl3a945612018-08-29 13:18:43 +0200145 # https://docs.python.org/3.6/c-api/intro.html#debugging-builds
146 # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
147 if(PYTHON_IS_DEBUG)
148 target_compile_definitions(${target_name} PRIVATE Py_DEBUG)
149 endif()
150
Lori A. Burns545b4db2016-12-13 10:59:21 -0500151 # The prefix and extension are provided by FindPythonLibsNew.cmake
152 set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
153 set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
154
Jason Rhinelander97aa54f2017-08-10 12:08:42 -0400155 # -fvisibility=hidden is required to allow multiple modules compiled against
156 # different pybind versions to work properly, and for some features (e.g.
157 # py::module_local). We force it on everything inside the `pybind11`
158 # namespace; also turning it on for a pybind module compilation here avoids
159 # potential warnings or issues from having mixed hidden/non-hidden types.
160 set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
Davis E. King9343e682018-09-14 08:28:54 -0400161 set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
Jason Rhinelander97aa54f2017-08-10 12:08:42 -0400162
Henry Schreiner1f53c372020-07-23 22:55:29 -0400163
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100164 if(ARG_NO_EXTRAS)
Henry Schreiner6ec17752020-07-28 00:43:12 -0400165 return()
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100166 endif()
167
Henry Schreiner6ec17752020-07-28 00:43:12 -0400168 if(CMAKE_VERSION VERSION_LESS 3.9 OR PYBIND11_CLASSIC_LTO)
169 _pybind11_add_lto_flags(${target_name} ${ARG_THIN_LTO})
170 else()
171 include(CheckIPOSupported)
172 check_ipo_supported(RESULT supported OUTPUT error)
173 if(supported)
174 set_property(
175 TARGET
176 ${target_name}
177 PROPERTY
178 INTERPROCEDURAL_OPTIMIZATION TRUE
179 )
180 endif()
181 endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -0500182
Dmitry8f5a8ab2019-08-23 17:18:05 +0300183 if (NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500184 # Strip unnecessary sections of the binary on Linux/Mac OS
185 if(CMAKE_STRIP)
186 if(APPLE)
187 add_custom_command(TARGET ${target_name} POST_BUILD
188 COMMAND ${CMAKE_STRIP} -x $<TARGET_FILE:${target_name}>)
189 else()
190 add_custom_command(TARGET ${target_name} POST_BUILD
191 COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
Lori A. Burns545b4db2016-12-13 10:59:21 -0500192 endif()
193 endif()
Jason Rhinelander1bee6e72017-01-17 02:13:11 -0500194 endif()
195
196 if(MSVC)
Lori A. Burns545b4db2016-12-13 10:59:21 -0500197 # /MP enables multithreaded builds (relevant when there are many files), /bigobj is
198 # needed for bigger binding projects due to the limit to 64k addressable sections
Rune Paamand06d021b2018-10-25 01:35:33 +0200199 target_compile_options(${target_name} PRIVATE /bigobj)
Davis E. King9343e682018-09-14 08:28:54 -0400200 if(CMAKE_VERSION VERSION_LESS 3.11)
Rune Paamand06d021b2018-10-25 01:35:33 +0200201 target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MP>)
Davis E. King9343e682018-09-14 08:28:54 -0400202 else()
203 # Only set these options for C++ files. This is important so that, for
204 # instance, projects that include other types of source files like CUDA
205 # .cu files don't get these options propagated to nvcc since that would
206 # cause the build to fail.
Rune Paamand06d021b2018-10-25 01:35:33 +0200207 target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:$<$<COMPILE_LANGUAGE:CXX>:/MP>>)
Davis E. King9343e682018-09-14 08:28:54 -0400208 endif()
Lori A. Burns545b4db2016-12-13 10:59:21 -0500209 endif()
210endfunction()