2016-08-12 11:50:00 +00:00
|
|
|
# CMakeLists.txt -- Build system for the pybind11 modules
|
2015-07-05 18:05:44 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
|
|
|
#
|
|
|
|
# All rights reserved. Use of this source code is governed by a
|
|
|
|
# BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2020-07-28 04:43:12 +00:00
|
|
|
cmake_minimum_required(VERSION 3.7)
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2020-08-01 02:45:19 +00:00
|
|
|
# The `cmake_minimum_required(VERSION 3.7...3.18)` syntax does not work with
|
|
|
|
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
|
|
|
|
# the behavior using the following workaround:
|
2020-07-28 04:43:12 +00:00
|
|
|
if(${CMAKE_VERSION} VERSION_LESS 3.18)
|
2020-07-30 20:20:10 +00:00
|
|
|
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
|
2017-08-30 12:17:54 +00:00
|
|
|
else()
|
2020-07-30 20:20:10 +00:00
|
|
|
cmake_policy(VERSION 3.18)
|
2017-08-30 12:17:54 +00:00
|
|
|
endif()
|
2015-07-05 18:05:44 +00:00
|
|
|
|
2020-07-28 04:43:12 +00:00
|
|
|
# Extract project version from source
|
2020-07-30 20:20:10 +00:00
|
|
|
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/pybind11/detail/common.h"
|
|
|
|
pybind11_version_defines REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ")
|
2020-07-28 04:43:12 +00:00
|
|
|
|
|
|
|
foreach(ver ${pybind11_version_defines})
|
2020-07-30 20:20:10 +00:00
|
|
|
if(ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$]])
|
|
|
|
set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}")
|
|
|
|
endif()
|
2020-07-28 04:43:12 +00:00
|
|
|
endforeach()
|
|
|
|
|
2020-07-30 20:04:26 +00:00
|
|
|
if(PYBIND11_VERSION_PATCH MATCHES [[([a-zA-Z]+)]])
|
2020-08-01 02:45:19 +00:00
|
|
|
set(pybind11_VERSION_TYPE "${CMAKE_MATCH_1}")
|
2020-07-30 20:04:26 +00:00
|
|
|
endif()
|
|
|
|
string(REGEX MATCH "[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH}")
|
|
|
|
|
2020-07-28 04:43:12 +00:00
|
|
|
project(
|
2020-07-30 20:20:10 +00:00
|
|
|
pybind11
|
|
|
|
LANGUAGES CXX
|
|
|
|
VERSION "${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH}")
|
2020-07-28 04:43:12 +00:00
|
|
|
|
|
|
|
# Standard includes
|
|
|
|
include(GNUInstallDirs)
|
|
|
|
include(CMakePackageConfigHelpers)
|
|
|
|
include(CMakeDependentOption)
|
|
|
|
|
2020-08-01 02:45:19 +00:00
|
|
|
message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}")
|
2020-07-30 20:04:26 +00:00
|
|
|
|
2016-05-22 22:12:37 +00:00
|
|
|
# Check if pybind11 is being used directly or via add_subdirectory
|
2020-07-28 04:43:12 +00:00
|
|
|
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
|
2020-07-30 20:20:10 +00:00
|
|
|
set(PYBIND11_MASTER_PROJECT ON)
|
|
|
|
message(STATUS "CMake ${CMAKE_VERSION}")
|
2020-07-28 04:43:12 +00:00
|
|
|
|
2020-07-30 20:20:10 +00:00
|
|
|
if(CMAKE_CXX_STANDARD)
|
|
|
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
|
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
|
|
endif()
|
2020-07-28 04:43:12 +00:00
|
|
|
else()
|
2020-07-30 20:20:10 +00:00
|
|
|
set(PYBIND11_MASTER_PROJECT OFF)
|
|
|
|
set(pybind11_system SYSTEM)
|
2016-05-29 10:35:16 +00:00
|
|
|
endif()
|
2016-05-22 22:12:37 +00:00
|
|
|
|
2020-08-01 02:45:19 +00:00
|
|
|
# Options
|
2016-05-26 20:53:38 +00:00
|
|
|
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
|
2020-07-28 04:43:12 +00:00
|
|
|
option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
|
|
|
|
option(PYBIND11_CLASSIC_LTO "Use the classic LTO flag algorithm, even on CMake 3.9+" OFF)
|
2020-08-01 02:45:19 +00:00
|
|
|
|
2020-07-28 04:43:12 +00:00
|
|
|
cmake_dependent_option(
|
2020-07-30 20:20:10 +00:00
|
|
|
USE_PYTHON_INCLUDE_DIR
|
|
|
|
"Install pybind11 headers in Python include directory instead of default installation prefix"
|
|
|
|
OFF "PYBIND11_INSTALL" OFF)
|
2020-07-28 04:43:12 +00:00
|
|
|
|
|
|
|
# NB: when adding a header don't forget to also add it to setup.py
|
|
|
|
set(PYBIND11_HEADERS
|
|
|
|
include/pybind11/detail/class.h
|
|
|
|
include/pybind11/detail/common.h
|
|
|
|
include/pybind11/detail/descr.h
|
|
|
|
include/pybind11/detail/init.h
|
|
|
|
include/pybind11/detail/internals.h
|
|
|
|
include/pybind11/detail/typeid.h
|
|
|
|
include/pybind11/attr.h
|
|
|
|
include/pybind11/buffer_info.h
|
|
|
|
include/pybind11/cast.h
|
|
|
|
include/pybind11/chrono.h
|
|
|
|
include/pybind11/common.h
|
|
|
|
include/pybind11/complex.h
|
|
|
|
include/pybind11/options.h
|
|
|
|
include/pybind11/eigen.h
|
|
|
|
include/pybind11/embed.h
|
|
|
|
include/pybind11/eval.h
|
2020-08-01 19:24:30 +00:00
|
|
|
include/pybind11/iostream.h
|
2020-07-28 04:43:12 +00:00
|
|
|
include/pybind11/functional.h
|
|
|
|
include/pybind11/numpy.h
|
|
|
|
include/pybind11/operators.h
|
|
|
|
include/pybind11/pybind11.h
|
|
|
|
include/pybind11/pytypes.h
|
|
|
|
include/pybind11/stl.h
|
2020-07-30 20:20:10 +00:00
|
|
|
include/pybind11/stl_bind.h)
|
2020-07-30 20:04:26 +00:00
|
|
|
|
2020-08-01 19:24:30 +00:00
|
|
|
# Compare with grep and warn if mismatched
|
|
|
|
if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12)
|
|
|
|
file(
|
|
|
|
GLOB_RECURSE _pybind11_header_check
|
|
|
|
LIST_DIRECTORIES false
|
|
|
|
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
|
|
|
|
CONFIGURE_DEPENDS "include/pybind11/*.h")
|
|
|
|
set(_pybind11_here_only ${PYBIND11_HEADERS})
|
|
|
|
set(_pybind11_disk_only ${_pybind11_header_check})
|
|
|
|
list(REMOVE_ITEM _pybind11_here_only ${_pybind11_header_check})
|
|
|
|
list(REMOVE_ITEM _pybind11_disk_only ${PYBIND11_HEADERS})
|
|
|
|
if(_pybind11_here_only)
|
|
|
|
message(AUTHOR_WARNING "PYBIND11_HEADERS has extra files:" ${_pybind11_here_only})
|
|
|
|
endif()
|
|
|
|
if(_pybind11_disk_only)
|
|
|
|
message(AUTHOR_WARNING "PYBIND11_HEADERS is missing files:" ${_pybind11_disk_only})
|
|
|
|
endif()
|
|
|
|
endif()
|
2020-07-30 20:04:26 +00:00
|
|
|
|
2020-08-01 19:24:30 +00:00
|
|
|
# CMake 3.12 added list(TRANSFORM <list> PREPEND
|
2020-07-28 04:43:12 +00:00
|
|
|
# But we can't use it yet
|
2020-07-30 20:20:10 +00:00
|
|
|
string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS
|
|
|
|
"${PYBIND11_HEADERS}")
|
2016-05-28 09:08:16 +00:00
|
|
|
|
2020-07-28 04:43:12 +00:00
|
|
|
# Classic mode
|
|
|
|
|
2020-08-01 02:45:19 +00:00
|
|
|
include("${CMAKE_CURRENT_LIST_DIR}/tools/pybind11Tools.cmake")
|
2016-05-28 09:08:16 +00:00
|
|
|
|
2016-05-22 20:23:18 +00:00
|
|
|
# Cache variables so pybind11_add_module can be used in parent projects
|
2020-07-30 20:20:10 +00:00
|
|
|
set(PYBIND11_INCLUDE_DIR
|
|
|
|
"${CMAKE_CURRENT_LIST_DIR}/include"
|
|
|
|
CACHE INTERNAL "")
|
|
|
|
set(PYTHON_INCLUDE_DIRS
|
|
|
|
${PYTHON_INCLUDE_DIRS}
|
|
|
|
CACHE INTERNAL "")
|
|
|
|
set(PYTHON_LIBRARIES
|
|
|
|
${PYTHON_LIBRARIES}
|
|
|
|
CACHE INTERNAL "")
|
|
|
|
set(PYTHON_MODULE_PREFIX
|
|
|
|
${PYTHON_MODULE_PREFIX}
|
|
|
|
CACHE INTERNAL "")
|
|
|
|
set(PYTHON_MODULE_EXTENSION
|
|
|
|
${PYTHON_MODULE_EXTENSION}
|
|
|
|
CACHE INTERNAL "")
|
|
|
|
set(PYTHON_VERSION_MAJOR
|
|
|
|
${PYTHON_VERSION_MAJOR}
|
|
|
|
CACHE INTERNAL "")
|
|
|
|
set(PYTHON_VERSION_MINOR
|
|
|
|
${PYTHON_VERSION_MINOR}
|
|
|
|
CACHE INTERNAL "")
|
|
|
|
set(PYTHON_IS_DEBUG
|
|
|
|
"${PYTHON_IS_DEBUG}"
|
|
|
|
CACHE INTERNAL "")
|
2016-09-29 19:30:00 +00:00
|
|
|
|
2020-07-28 04:43:12 +00:00
|
|
|
if(USE_PYTHON_INCLUDE_DIR)
|
2020-07-30 20:20:10 +00:00
|
|
|
file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
|
2017-02-14 12:16:14 +00:00
|
|
|
endif()
|
|
|
|
|
2020-07-28 04:43:12 +00:00
|
|
|
# Note: when creating targets, you cannot use if statements at configure time -
|
|
|
|
# you need generator expressions, because those will be placed in the target file.
|
2020-07-30 20:04:26 +00:00
|
|
|
# You can also place ifs *in* the Config.in, but not here.
|
2020-07-28 04:43:12 +00:00
|
|
|
|
|
|
|
# Build an interface library target:
|
|
|
|
add_library(pybind11 INTERFACE)
|
2020-07-30 20:20:10 +00:00
|
|
|
add_library(pybind11::pybind11 ALIAS pybind11) # to match exported target
|
2020-07-28 04:43:12 +00:00
|
|
|
|
2020-07-30 20:20:10 +00:00
|
|
|
target_include_directories(
|
|
|
|
pybind11 ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
|
|
|
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
|
2020-07-28 04:43:12 +00:00
|
|
|
# Only add Python for build - must be added during the import for config since it has to be re-discovered.
|
|
|
|
target_include_directories(pybind11 SYSTEM INTERFACE $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>)
|
|
|
|
|
|
|
|
if(CMAKE_VERSION VERSION_LESS 3.13)
|
2020-07-30 20:20:10 +00:00
|
|
|
target_compile_features(pybind11 INTERFACE cxx_inheriting_constructors cxx_user_literals
|
|
|
|
cxx_right_angle_brackets)
|
2020-07-28 04:43:12 +00:00
|
|
|
else()
|
2020-07-30 20:20:10 +00:00
|
|
|
# This was added in CMake 3.8, but we are keeping a consistent breaking
|
|
|
|
# point for the config file at 3.13. A config generated by CMake 3.13+
|
|
|
|
# can only be read in 3.13+ due to the SHELL usage later, so this is safe to do.
|
|
|
|
target_compile_features(pybind11 INTERFACE cxx_std_11)
|
2020-07-28 04:43:12 +00:00
|
|
|
endif()
|
|
|
|
|
|
|
|
add_library(module INTERFACE)
|
|
|
|
add_library(pybind11::module ALIAS module)
|
|
|
|
|
|
|
|
target_link_libraries(module INTERFACE pybind11::pybind11)
|
|
|
|
|
|
|
|
# See https://github.com/Kitware/CMake/blob/master/Modules/CMakePlatformId.h.in for platform IDs
|
|
|
|
# Note: CMake 3.15 allows $<PLATFORM_ID:Windows,Cygwin>
|
2020-07-30 20:20:10 +00:00
|
|
|
target_link_libraries(
|
|
|
|
module
|
|
|
|
INTERFACE
|
|
|
|
"$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES}>>")
|
2020-07-28 04:43:12 +00:00
|
|
|
|
|
|
|
if(CMAKE_VERSION VERSION_LESS 3.13)
|
2020-07-30 20:20:10 +00:00
|
|
|
target_link_libraries(module INTERFACE "$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>")
|
2020-07-28 04:43:12 +00:00
|
|
|
else()
|
2020-07-30 20:20:10 +00:00
|
|
|
# SHELL (3.12+) forces this to remain together, and link_options was added in 3.13+
|
|
|
|
# This is safer, because you are ensured the deduplication pass in CMake will not consider
|
|
|
|
# these separate and remove one but not the other.
|
|
|
|
target_link_options(module INTERFACE "$<$<PLATFORM_ID:Darwin>:SHELL:-undefined dynamic_lookup>")
|
2016-12-13 15:55:38 +00:00
|
|
|
endif()
|
|
|
|
|
2020-07-30 00:10:38 +00:00
|
|
|
# Workaround for Python 2.7 and C++17 (C++14 as a warning) incompatibility
|
2020-07-30 20:04:26 +00:00
|
|
|
# This adds the flags -Wno-register and -Wno-deprecated-register if the compiler
|
|
|
|
# is Clang 3.9+ or AppleClang and the compile language is CXX, or /wd5033 for MSVC (all languages,
|
|
|
|
# since MSVC didn't recognize COMPILE_LANGUAGE until CMake 3.11+).
|
2020-07-30 20:20:10 +00:00
|
|
|
set(clang_4plus
|
|
|
|
"$<AND:$<CXX_COMPILER_ID:Clang>,$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,3.9>>>")
|
2020-07-28 04:43:12 +00:00
|
|
|
set(no_register "$<OR:${clang_4plus},$<CXX_COMPILER_ID:AppleClang>>")
|
2020-07-30 00:10:38 +00:00
|
|
|
set(cxx_no_register "$<AND:$<COMPILE_LANGUAGE:CXX>,${no_register}>")
|
|
|
|
set(msvc "$<CXX_COMPILER_ID:MSVC>")
|
|
|
|
target_compile_options(
|
|
|
|
pybind11 INTERFACE "$<${cxx_no_register}:-Wno-register;-Wno-deprecated-register>"
|
|
|
|
"$<${msvc}:/wd5033>")
|
2020-07-28 04:43:12 +00:00
|
|
|
|
|
|
|
add_library(embed INTERFACE)
|
|
|
|
add_library(pybind11::embed ALIAS embed)
|
|
|
|
target_link_libraries(embed INTERFACE pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
|
|
|
|
|
|
|
|
if(PYBIND11_INSTALL)
|
2020-07-30 20:20:10 +00:00
|
|
|
install(DIRECTORY ${PYBIND11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
|
|
|
# GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
|
|
|
|
set(PYBIND11_CMAKECONFIG_INSTALL_DIR
|
|
|
|
"share/cmake/${PROJECT_NAME}"
|
|
|
|
CACHE STRING "install path for pybind11Config.cmake")
|
|
|
|
|
|
|
|
configure_package_config_file(
|
|
|
|
tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
|
|
|
|
INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
|
|
|
|
|
|
|
|
if(CMAKE_VERSION VERSION_LESS 3.14)
|
|
|
|
# Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does
|
|
|
|
# not depend on architecture specific settings or libraries.
|
|
|
|
set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
|
|
|
|
unset(CMAKE_SIZEOF_VOID_P)
|
|
|
|
|
|
|
|
write_basic_package_version_file(
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
|
|
|
|
VERSION ${PROJECT_VERSION}
|
|
|
|
COMPATIBILITY AnyNewerVersion)
|
|
|
|
|
|
|
|
set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P})
|
|
|
|
else()
|
|
|
|
# CMake 3.14+ natively supports header-only libraries
|
|
|
|
write_basic_package_version_file(
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
|
|
|
|
VERSION ${PROJECT_VERSION}
|
|
|
|
COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
install(
|
|
|
|
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
|
|
|
|
tools/FindPythonLibsNew.cmake tools/pybind11Tools.cmake
|
|
|
|
DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
|
|
|
|
|
|
|
|
if(NOT PYBIND11_EXPORT_NAME)
|
|
|
|
set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
install(TARGETS pybind11 module embed EXPORT "${PYBIND11_EXPORT_NAME}")
|
|
|
|
|
|
|
|
install(
|
|
|
|
EXPORT "${PYBIND11_EXPORT_NAME}"
|
|
|
|
NAMESPACE "pybind11::"
|
|
|
|
DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
|
2020-07-31 01:16:50 +00:00
|
|
|
|
2020-07-31 08:16:40 +00:00
|
|
|
# Uninstall target
|
|
|
|
if(PYBIND11_MASTER_PROJECT)
|
|
|
|
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in"
|
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)
|
2020-07-31 01:16:50 +00:00
|
|
|
|
2020-07-31 08:16:40 +00:00
|
|
|
add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P
|
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
|
|
|
|
endif()
|
2020-07-31 01:16:50 +00:00
|
|
|
endif()
|
2020-08-01 02:45:19 +00:00
|
|
|
|
|
|
|
if(PYBIND11_TEST OR (BUILD_TESTING AND PYBIND11_MASTER_PROJECT))
|
|
|
|
add_subdirectory(tests)
|
|
|
|
endif()
|