mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
feat: drop CMake 3.6 and below, modernize CMake
fix: include PYTHON_IS_DEBUG
This commit is contained in:
committed by
Henry Schreiner
parent
1491c94c9d
commit
6ec1775fff
@@ -5,56 +5,41 @@
|
||||
# All rights reserved. Use of this source code is governed by a
|
||||
# BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
cmake_minimum_required(VERSION 2.8.12)
|
||||
cmake_minimum_required(VERSION 3.7)
|
||||
|
||||
# VERSION 3.7...3.18, but some versions of VS have a patched CMake 3.11
|
||||
# that do not work properly with this syntax, so using the following workaround:
|
||||
if(${CMAKE_VERSION} VERSION_LESS 3.18)
|
||||
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
|
||||
else()
|
||||
cmake_policy(VERSION 3.18)
|
||||
endif()
|
||||
|
||||
# Add a CMake parameter for choosing a desired Python version
|
||||
if(NOT PYBIND11_PYTHON_VERSION)
|
||||
set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules")
|
||||
set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules")
|
||||
endif()
|
||||
|
||||
set(Python_ADDITIONAL_VERSIONS 3.9 3.8 3.7 3.6 3.5 3.4)
|
||||
# A user can set versions manually too
|
||||
set(Python_ADDITIONAL_VERSIONS "3.9;3.8;3.7;3.6;3.5;3.4" CACHE INTERNAL "")
|
||||
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
|
||||
|
||||
include(CheckCXXCompilerFlag)
|
||||
include(CMakeParseArguments)
|
||||
|
||||
# Use the language standards abstraction if CMake supports it with the current compiler
|
||||
if(NOT CMAKE_VERSION VERSION_LESS 3.1)
|
||||
if(NOT CMAKE_CXX_STANDARD)
|
||||
if(CMAKE_CXX14_STANDARD_COMPILE_OPTION)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
elseif(CMAKE_CXX11_STANDARD_COMPILE_OPTION)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(PYBIND11_CPP_STANDARD)
|
||||
message(WARNING "USE -DCMAKE_CXX_STANDARD=11 instead of PYBIND11_PYTHON_VERSION")
|
||||
if(NOT CMAKE_CXX_STANDARD)
|
||||
string(REGEX MATCH
|
||||
[=[..^]=]
|
||||
VAL
|
||||
"${PYBIND11_CPP_STANDARD}")
|
||||
set(CMAKE_CXX_STANDARD ${VAL})
|
||||
endif()
|
||||
endif()
|
||||
if(CMAKE_CXX_STANDARD)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Fall back to heuristics
|
||||
if(NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD)
|
||||
if(MSVC)
|
||||
set(PYBIND11_CPP_STANDARD /std:c++14)
|
||||
else()
|
||||
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
|
||||
if(HAS_CPP14_FLAG)
|
||||
set(PYBIND11_CPP_STANDARD -std=c++14)
|
||||
else()
|
||||
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
|
||||
if(HAS_CPP11_FLAG)
|
||||
set(PYBIND11_CPP_STANDARD -std=c++11)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(NOT PYBIND11_CPP_STANDARD)
|
||||
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
|
||||
endif()
|
||||
set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
|
||||
"C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE)
|
||||
endif()
|
||||
|
||||
# Checks whether the given CXX/linker flags can compile and link a cxx file. cxxflags and
|
||||
# linkerflags are lists of flags to use. The result variable is a unique variable name for each set
|
||||
@@ -117,16 +102,18 @@ function(_pybind11_add_lto_flags target_name prefer_thin_lto)
|
||||
|
||||
# Enable LTO flags if found, except for Debug builds
|
||||
if (PYBIND11_LTO_CXX_FLAGS)
|
||||
target_compile_options(${target_name} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:${PYBIND11_LTO_CXX_FLAGS}>")
|
||||
set(not_debug "$<NOT:$<CONFIG:Debug>>")
|
||||
set(cxx_lang "$<COMPILE_LANGUAGE:CXX>")
|
||||
target_compile_options(${target_name} PRIVATE "$<$<AND:${not_debug},${cxx_lang}>:${PYBIND11_LTO_CXX_FLAGS}>")
|
||||
endif()
|
||||
if (PYBIND11_LTO_LINKER_FLAGS)
|
||||
target_link_libraries(${target_name} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:${PYBIND11_LTO_LINKER_FLAGS}>")
|
||||
target_link_libraries(${target_name} PRIVATE "$<${not_debug}:${PYBIND11_LTO_LINKER_FLAGS}>")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Build a Python extension module:
|
||||
# pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
|
||||
# [NO_EXTRAS] [SYSTEM] [THIN_LTO] source1 [source2 ...])
|
||||
# [NO_EXTRAS] [THIN_LTO] source1 [source2 ...])
|
||||
#
|
||||
function(pybind11_add_module target_name)
|
||||
set(options MODULE SHARED EXCLUDE_FROM_ALL NO_EXTRAS SYSTEM THIN_LTO)
|
||||
@@ -148,29 +135,13 @@ function(pybind11_add_module target_name)
|
||||
|
||||
add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS})
|
||||
|
||||
target_link_libraries(${target_name} PRIVATE pybind11::module)
|
||||
|
||||
if(ARG_SYSTEM)
|
||||
set(inc_isystem SYSTEM)
|
||||
else()
|
||||
set(inc_isystem "")
|
||||
message(STATUS "Warning: this does not have an effect - use NO_SYSTEM_FROM_IMPORTED if using imported targets")
|
||||
endif()
|
||||
|
||||
set(PYBIND11_INCLUDE_DIR_SELECTED "")
|
||||
if(PYBIND11_INCLUDE_DIR)
|
||||
# from project CMakeLists.txt
|
||||
set(PYBIND11_INCLUDE_DIR_SELECTED ${PYBIND11_INCLUDE_DIR})
|
||||
elseif(pybind11_INCLUDE_DIR)
|
||||
# from pybind11Config
|
||||
set(PYBIND11_INCLUDE_DIR_SELECTED ${pybind11_INCLUDE_DIR})
|
||||
else()
|
||||
message(FATAL "No pybind11_INCLUDE_DIR available. Use "
|
||||
"find_package(pybind11) before calling pybind11_add_module.")
|
||||
endif()
|
||||
|
||||
target_include_directories(${target_name} ${inc_isystem}
|
||||
PRIVATE ${PYBIND11_INCLUDE_DIR_SELECTED}
|
||||
PRIVATE ${PYTHON_INCLUDE_DIRS})
|
||||
|
||||
# Python debug libraries expose slightly different objects
|
||||
# Python debug libraries expose slightly different objects before 3.8
|
||||
# https://docs.python.org/3.6/c-api/intro.html#debugging-builds
|
||||
# https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib
|
||||
if(PYTHON_IS_DEBUG)
|
||||
@@ -189,54 +160,25 @@ function(pybind11_add_module target_name)
|
||||
set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden")
|
||||
set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
|
||||
|
||||
if(WIN32 OR CYGWIN)
|
||||
# Link against the Python shared library on Windows
|
||||
target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
|
||||
elseif(APPLE)
|
||||
# It's quite common to have multiple copies of the same Python version
|
||||
# installed on one's system. E.g.: one copy from the OS and another copy
|
||||
# that's statically linked into an application like Blender or Maya.
|
||||
# If we link our plugin library against the OS Python here and import it
|
||||
# into Blender or Maya later on, this will cause segfaults when multiple
|
||||
# conflicting Python instances are active at the same time (even when they
|
||||
# are of the same version).
|
||||
|
||||
# Windows is not affected by this issue since it handles DLL imports
|
||||
# differently. The solution for Linux and Mac OS is simple: we just don't
|
||||
# link against the Python library. The resulting shared library will have
|
||||
# missing symbols, but that's perfectly fine -- they will be resolved at
|
||||
# import time.
|
||||
|
||||
target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
|
||||
|
||||
if(ARG_SHARED)
|
||||
# Suppress CMake >= 3.0 warning for shared libraries
|
||||
set_target_properties(${target_name} PROPERTIES MACOSX_RPATH ON)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Make sure C++11/14 are enabled
|
||||
if(PYBIND11_CPP_STANDARD)
|
||||
if(CMAKE_VERSION VERSION_LESS 3.3)
|
||||
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
|
||||
else()
|
||||
target_compile_options(${target_name} PUBLIC $<$<COMPILE_LANGUAGE:CXX>:${PYBIND11_CPP_STANDARD}>)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Python 2 doesn't really support C++17, Clang warns/errors over it
|
||||
if(CMAKE_CXX_STANDARD
|
||||
AND CMAKE_CXX_COMPILER_ID MATCHES "Clang"
|
||||
AND PYTHON_VERSION VERSION_LESS 3.0
|
||||
AND NOT CMAKE_CXX_STANDARD LESS 17)
|
||||
target_compile_options(${target_name} PUBLIC -Wno-register)
|
||||
endif()
|
||||
|
||||
if(ARG_NO_EXTRAS)
|
||||
return()
|
||||
return()
|
||||
endif()
|
||||
|
||||
_pybind11_add_lto_flags(${target_name} ${ARG_THIN_LTO})
|
||||
if(CMAKE_VERSION VERSION_LESS 3.9 OR PYBIND11_CLASSIC_LTO)
|
||||
_pybind11_add_lto_flags(${target_name} ${ARG_THIN_LTO})
|
||||
else()
|
||||
include(CheckIPOSupported)
|
||||
check_ipo_supported(RESULT supported OUTPUT error)
|
||||
if(supported)
|
||||
set_property(
|
||||
TARGET
|
||||
${target_name}
|
||||
PROPERTY
|
||||
INTERPROCEDURAL_OPTIMIZATION TRUE
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
|
||||
# Strip unnecessary sections of the binary on Linux/Mac OS
|
||||
|
||||
Reference in New Issue
Block a user