Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 1 | # 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 Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 8 | cmake_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: |
| 12 | if(${CMAKE_VERSION} VERSION_LESS 3.18) |
| 13 | cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) |
| 14 | else() |
| 15 | cmake_policy(VERSION 3.18) |
| 16 | endif() |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 17 | |
| 18 | # Add a CMake parameter for choosing a desired Python version |
Dean Moldovan | 5b5e3de | 2017-01-06 23:38:00 +0100 | [diff] [blame] | 19 | if(NOT PYBIND11_PYTHON_VERSION) |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 20 | set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules") |
Dean Moldovan | 5b5e3de | 2017-01-06 23:38:00 +0100 | [diff] [blame] | 21 | endif() |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 22 | |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 23 | # A user can set versions manually too |
| 24 | set(Python_ADDITIONAL_VERSIONS "3.9;3.8;3.7;3.6;3.5;3.4" CACHE INTERNAL "") |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 25 | find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED) |
| 26 | |
| 27 | include(CheckCXXCompilerFlag) |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 28 | include(CMakeParseArguments) |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 29 | |
Chuck Atkins | d730fbc | 2018-06-12 13:18:48 -0400 | [diff] [blame] | 30 | # Use the language standards abstraction if CMake supports it with the current compiler |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 31 | if(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 Atkins | d730fbc | 2018-06-12 13:18:48 -0400 | [diff] [blame] | 39 | endif() |
Chuck Atkins | d730fbc | 2018-06-12 13:18:48 -0400 | [diff] [blame] | 40 | endif() |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 41 | |
Jason Rhinelander | fad5d33 | 2017-07-12 14:59:16 -0400 | [diff] [blame] | 42 | |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 43 | |
Jason Rhinelander | c137c0a | 2017-02-09 21:27:29 -0500 | [diff] [blame] | 44 | # 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}). |
| 48 | function(_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 Rhinelander | 1bee6e7 | 2017-01-17 02:13:11 -0500 | [diff] [blame] | 55 | endfunction() |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 56 | |
Jason Rhinelander | 1bee6e7 | 2017-01-17 02:13:11 -0500 | [diff] [blame] | 57 | # Internal: find the appropriate link time optimization flags for this compiler |
Jason Rhinelander | c137c0a | 2017-02-09 21:27:29 -0500 | [diff] [blame] | 58 | function(_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 Rhinelander | 1bee6e7 | 2017-01-17 02:13:11 -0500 | [diff] [blame] | 94 | endif() |
| 95 | |
Jason Rhinelander | c137c0a | 2017-02-09 21:27:29 -0500 | [diff] [blame] | 96 | 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 Rhinelander | 1bee6e7 | 2017-01-17 02:13:11 -0500 | [diff] [blame] | 100 | endif() |
Jason Rhinelander | 1bee6e7 | 2017-01-17 02:13:11 -0500 | [diff] [blame] | 101 | endif() |
| 102 | |
Jason Rhinelander | c137c0a | 2017-02-09 21:27:29 -0500 | [diff] [blame] | 103 | # Enable LTO flags if found, except for Debug builds |
| 104 | if (PYBIND11_LTO_CXX_FLAGS) |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 105 | 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 Rhinelander | c137c0a | 2017-02-09 21:27:29 -0500 | [diff] [blame] | 108 | endif() |
| 109 | if (PYBIND11_LTO_LINKER_FLAGS) |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 110 | target_link_libraries(${target_name} PRIVATE "$<${not_debug}:${PYBIND11_LTO_LINKER_FLAGS}>") |
Jason Rhinelander | c137c0a | 2017-02-09 21:27:29 -0500 | [diff] [blame] | 111 | endif() |
Jason Rhinelander | 1bee6e7 | 2017-01-17 02:13:11 -0500 | [diff] [blame] | 112 | endfunction() |
| 113 | |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 114 | # Build a Python extension module: |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 115 | # pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL] |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 116 | # [NO_EXTRAS] [THIN_LTO] source1 [source2 ...]) |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 117 | # |
| 118 | function(pybind11_add_module target_name) |
Axel Huebl | 435dbdd | 2018-08-29 13:20:11 +0200 | [diff] [blame] | 119 | set(options MODULE SHARED EXCLUDE_FROM_ALL NO_EXTRAS SYSTEM THIN_LTO) |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 120 | cmake_parse_arguments(ARG "${options}" "" "" ${ARGN}) |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 121 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 122 | 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. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 129 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 130 | if(ARG_EXCLUDE_FROM_ALL) |
| 131 | set(exclude_from_all EXCLUDE_FROM_ALL) |
Nils Leif Fischer | 141e8cc | 2020-06-26 12:15:10 +0200 | [diff] [blame] | 132 | else() |
| 133 | set(exclude_from_all "") |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 134 | endif() |
| 135 | |
| 136 | add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS}) |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 137 | |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 138 | target_link_libraries(${target_name} PRIVATE pybind11::module) |
| 139 | |
Axel Huebl | 435dbdd | 2018-08-29 13:20:11 +0200 | [diff] [blame] | 140 | if(ARG_SYSTEM) |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 141 | message(STATUS "Warning: this does not have an effect - use NO_SYSTEM_FROM_IMPORTED if using imported targets") |
Nils Leif Fischer | 141e8cc | 2020-06-26 12:15:10 +0200 | [diff] [blame] | 142 | endif() |
| 143 | |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 144 | # Python debug libraries expose slightly different objects before 3.8 |
Axel Huebl | 3a94561 | 2018-08-29 13:18:43 +0200 | [diff] [blame] | 145 | # 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. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 151 | # 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 Rhinelander | 97aa54f | 2017-08-10 12:08:42 -0400 | [diff] [blame] | 155 | # -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. King | 9343e68 | 2018-09-14 08:28:54 -0400 | [diff] [blame] | 161 | set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden") |
Jason Rhinelander | 97aa54f | 2017-08-10 12:08:42 -0400 | [diff] [blame] | 162 | |
Henry Schreiner | 1f53c37 | 2020-07-23 22:55:29 -0400 | [diff] [blame] | 163 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 164 | if(ARG_NO_EXTRAS) |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 165 | return() |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 166 | endif() |
| 167 | |
Henry Schreiner | 6ec1775 | 2020-07-28 00:43:12 -0400 | [diff] [blame^] | 168 | 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. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 182 | |
Dmitry | 8f5a8ab | 2019-08-23 17:18:05 +0300 | [diff] [blame] | 183 | if (NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) |
Jason Rhinelander | 1bee6e7 | 2017-01-17 02:13:11 -0500 | [diff] [blame] | 184 | # 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. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 192 | endif() |
| 193 | endif() |
Jason Rhinelander | 1bee6e7 | 2017-01-17 02:13:11 -0500 | [diff] [blame] | 194 | endif() |
| 195 | |
| 196 | if(MSVC) |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 197 | # /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 Paamand | 06d021b | 2018-10-25 01:35:33 +0200 | [diff] [blame] | 199 | target_compile_options(${target_name} PRIVATE /bigobj) |
Davis E. King | 9343e68 | 2018-09-14 08:28:54 -0400 | [diff] [blame] | 200 | if(CMAKE_VERSION VERSION_LESS 3.11) |
Rune Paamand | 06d021b | 2018-10-25 01:35:33 +0200 | [diff] [blame] | 201 | target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MP>) |
Davis E. King | 9343e68 | 2018-09-14 08:28:54 -0400 | [diff] [blame] | 202 | 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 Paamand | 06d021b | 2018-10-25 01:35:33 +0200 | [diff] [blame] | 207 | target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:$<$<COMPILE_LANGUAGE:CXX>:/MP>>) |
Davis E. King | 9343e68 | 2018-09-14 08:28:54 -0400 | [diff] [blame] | 208 | endif() |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 209 | endif() |
| 210 | endfunction() |