1# CMakeLists.txt -- 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 8cmake_minimum_required(VERSION 3.4) 9 10# The `cmake_minimum_required(VERSION 3.4...3.18)` syntax does not work with 11# some versions of VS that have a patched CMake 3.11. This forces us to emulate 12# the behavior using the following workaround: 13if(${CMAKE_VERSION} VERSION_LESS 3.18) 14 cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) 15else() 16 cmake_policy(VERSION 3.18) 17endif() 18 19# Extract project version from source 20file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/pybind11/detail/common.h" 21 pybind11_version_defines REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ") 22 23foreach(ver ${pybind11_version_defines}) 24 if(ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$]]) 25 set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}") 26 endif() 27endforeach() 28 29if(PYBIND11_VERSION_PATCH MATCHES [[\.([a-zA-Z0-9]+)$]]) 30 set(pybind11_VERSION_TYPE "${CMAKE_MATCH_1}") 31endif() 32string(REGEX MATCH "^[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH}") 33 34project( 35 pybind11 36 LANGUAGES CXX 37 VERSION "${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH}") 38 39# Standard includes 40include(GNUInstallDirs) 41include(CMakePackageConfigHelpers) 42include(CMakeDependentOption) 43 44if(NOT pybind11_FIND_QUIETLY) 45 message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}") 46endif() 47 48# Avoid infinite recursion if tests include this as a subdirectory 49if(DEFINED PYBIND11_MASTER_PROJECT) 50 set(PYBIND11_TEST OFF) 51endif() 52 53# Check if pybind11 is being used directly or via add_subdirectory 54if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR AND NOT DEFINED PYBIND11_MASTER_PROJECT) 55 ### Warn if not an out-of-source builds 56 if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR) 57 set(lines 58 "You are building in-place. If that is not what you intended to " 59 "do, you can clean the source directory with:\n" 60 "rm -r CMakeCache.txt CMakeFiles/ cmake_uninstall.cmake pybind11Config.cmake " 61 "pybind11ConfigVersion.cmake tests/CMakeFiles/\n") 62 message(AUTHOR_WARNING ${lines}) 63 endif() 64 65 set(PYBIND11_MASTER_PROJECT ON) 66 67 if(OSX AND CMAKE_VERSION VERSION_LESS 3.7) 68 # Bug in macOS CMake < 3.7 is unable to download catch 69 message(WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended") 70 elseif(WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8) 71 # Only tested with 3.8+ in CI. 72 message(WARNING "CMAKE 3.8+ tested on Windows, previous versions untested") 73 endif() 74 75 message(STATUS "CMake ${CMAKE_VERSION}") 76 77 if(CMAKE_CXX_STANDARD) 78 set(CMAKE_CXX_EXTENSIONS OFF) 79 set(CMAKE_CXX_STANDARD_REQUIRED ON) 80 endif() 81 82 set(pybind11_system "") 83else() 84 set(PYBIND11_MASTER_PROJECT OFF) 85 set(pybind11_system SYSTEM) 86endif() 87 88# Options 89option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) 90option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) 91option(PYBIND11_NOPYTHON "Disable search for Python" OFF) 92 93cmake_dependent_option( 94 USE_PYTHON_INCLUDE_DIR 95 "Install pybind11 headers in Python include directory instead of default installation prefix" 96 OFF "PYBIND11_INSTALL" OFF) 97 98cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF 99 "NOT CMAKE_VERSION VERSION_LESS 3.12" OFF) 100 101# NB: when adding a header don't forget to also add it to setup.py 102set(PYBIND11_HEADERS 103 include/pybind11/detail/class.h 104 include/pybind11/detail/common.h 105 include/pybind11/detail/descr.h 106 include/pybind11/detail/init.h 107 include/pybind11/detail/internals.h 108 include/pybind11/detail/typeid.h 109 include/pybind11/attr.h 110 include/pybind11/buffer_info.h 111 include/pybind11/cast.h 112 include/pybind11/chrono.h 113 include/pybind11/common.h 114 include/pybind11/complex.h 115 include/pybind11/options.h 116 include/pybind11/eigen.h 117 include/pybind11/embed.h 118 include/pybind11/eval.h 119 include/pybind11/iostream.h 120 include/pybind11/functional.h 121 include/pybind11/numpy.h 122 include/pybind11/operators.h 123 include/pybind11/pybind11.h 124 include/pybind11/pytypes.h 125 include/pybind11/stl.h 126 include/pybind11/stl_bind.h) 127 128# Compare with grep and warn if mismatched 129if(PYBIND11_MASTER_PROJECT AND NOT CMAKE_VERSION VERSION_LESS 3.12) 130 file( 131 GLOB_RECURSE _pybind11_header_check 132 LIST_DIRECTORIES false 133 RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" 134 CONFIGURE_DEPENDS "include/pybind11/*.h") 135 set(_pybind11_here_only ${PYBIND11_HEADERS}) 136 set(_pybind11_disk_only ${_pybind11_header_check}) 137 list(REMOVE_ITEM _pybind11_here_only ${_pybind11_header_check}) 138 list(REMOVE_ITEM _pybind11_disk_only ${PYBIND11_HEADERS}) 139 if(_pybind11_here_only) 140 message(AUTHOR_WARNING "PYBIND11_HEADERS has extra files:" ${_pybind11_here_only}) 141 endif() 142 if(_pybind11_disk_only) 143 message(AUTHOR_WARNING "PYBIND11_HEADERS is missing files:" ${_pybind11_disk_only}) 144 endif() 145endif() 146 147# CMake 3.12 added list(TRANSFORM <list> PREPEND 148# But we can't use it yet 149string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS 150 "${PYBIND11_HEADERS}") 151 152# Cache variable so this can be used in parent projects 153set(pybind11_INCLUDE_DIR 154 "${CMAKE_CURRENT_LIST_DIR}/include" 155 CACHE INTERNAL "Directory where pybind11 headers are located") 156 157# Backward compatible variable for add_subdirectory mode 158if(NOT PYBIND11_MASTER_PROJECT) 159 set(PYBIND11_INCLUDE_DIR 160 "${pybind11_INCLUDE_DIR}" 161 CACHE INTERNAL "") 162endif() 163 164# Note: when creating targets, you cannot use if statements at configure time - 165# you need generator expressions, because those will be placed in the target file. 166# You can also place ifs *in* the Config.in, but not here. 167 168# This section builds targets, but does *not* touch Python 169# Non-IMPORT targets cannot be defined twice 170if(NOT TARGET pybind11_headers) 171 # Build the headers-only target (no Python included): 172 # (long name used here to keep this from clashing in subdirectory mode) 173 add_library(pybind11_headers INTERFACE) 174 add_library(pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target 175 add_library(pybind11::headers ALIAS pybind11_headers) # easier to use/remember 176 177 target_include_directories( 178 pybind11_headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${pybind11_INCLUDE_DIR}> 179 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) 180 181 target_compile_features(pybind11_headers INTERFACE cxx_inheriting_constructors cxx_user_literals 182 cxx_right_angle_brackets) 183else() 184 # It is invalid to install a target twice, too. 185 set(PYBIND11_INSTALL OFF) 186endif() 187 188include("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11Common.cmake") 189 190# Relative directory setting 191if(USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS) 192 file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS}) 193elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR) 194 file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS}) 195endif() 196 197if(PYBIND11_INSTALL) 198 install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 199 set(PYBIND11_CMAKECONFIG_INSTALL_DIR 200 "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}" 201 CACHE STRING "install path for pybind11Config.cmake") 202 203 configure_package_config_file( 204 tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" 205 INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) 206 207 if(CMAKE_VERSION VERSION_LESS 3.14) 208 # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does 209 # not depend on architecture specific settings or libraries. 210 set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) 211 unset(CMAKE_SIZEOF_VOID_P) 212 213 write_basic_package_version_file( 214 ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake 215 VERSION ${PROJECT_VERSION} 216 COMPATIBILITY AnyNewerVersion) 217 218 set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P}) 219 else() 220 # CMake 3.14+ natively supports header-only libraries 221 write_basic_package_version_file( 222 ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake 223 VERSION ${PROJECT_VERSION} 224 COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT) 225 endif() 226 227 install( 228 FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake 229 ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake 230 tools/FindPythonLibsNew.cmake 231 tools/pybind11Common.cmake 232 tools/pybind11Tools.cmake 233 tools/pybind11NewTools.cmake 234 DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) 235 236 if(NOT PYBIND11_EXPORT_NAME) 237 set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets") 238 endif() 239 240 install(TARGETS pybind11_headers EXPORT "${PYBIND11_EXPORT_NAME}") 241 242 install( 243 EXPORT "${PYBIND11_EXPORT_NAME}" 244 NAMESPACE "pybind11::" 245 DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) 246 247 # Uninstall target 248 if(PYBIND11_MASTER_PROJECT) 249 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in" 250 "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY) 251 252 add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P 253 ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) 254 endif() 255endif() 256 257# BUILD_TESTING takes priority, but only if this is the master project 258if(PYBIND11_MASTER_PROJECT AND DEFINED BUILD_TESTING) 259 if(BUILD_TESTING) 260 if(_pybind11_nopython) 261 message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode") 262 else() 263 add_subdirectory(tests) 264 endif() 265 endif() 266else() 267 if(PYBIND11_TEST) 268 if(_pybind11_nopython) 269 message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode") 270 else() 271 add_subdirectory(tests) 272 endif() 273 endif() 274endif() 275 276# Better symmetry with find_package(pybind11 CONFIG) mode. 277if(NOT PYBIND11_MASTER_PROJECT) 278 set(pybind11_FOUND 279 TRUE 280 CACHE INTERNAL "True if pybind11 and all required components found on the system") 281endif() 282