CMakeLists.txt 10.7 KB
Newer Older
Dean Moldovan's avatar
Dean Moldovan committed
1
# CMakeLists.txt -- Build system for the pybind11 modules
Wenzel Jakob's avatar
Wenzel Jakob committed
2
3
4
5
6
7
#
# 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.

8
cmake_minimum_required(VERSION 3.4)
Wenzel Jakob's avatar
Wenzel Jakob committed
9

10
# The `cmake_minimum_required(VERSION 3.4...3.22)` syntax does not work with
Henry Schreiner's avatar
Henry Schreiner committed
11
12
# some versions of VS that have a patched CMake 3.11. This forces us to emulate
# the behavior using the following workaround:
13
if(${CMAKE_VERSION} VERSION_LESS 3.22)
Henry Schreiner's avatar
Henry Schreiner committed
14
  cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
15
else()
16
  cmake_policy(VERSION 3.22)
17
endif()
Wenzel Jakob's avatar
Wenzel Jakob committed
18

19
20
21
22
23
# Avoid infinite recursion if tests include this as a subdirectory
if(DEFINED PYBIND11_MASTER_PROJECT)
  return()
endif()

24
# Extract project version from source
Henry Schreiner's avatar
Henry Schreiner committed
25
26
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/include/pybind11/detail/common.h"
     pybind11_version_defines REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ")
27
28

foreach(ver ${pybind11_version_defines})
Henry Schreiner's avatar
Henry Schreiner committed
29
30
31
  if(ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$]])
    set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}")
  endif()
32
33
endforeach()

34
if(PYBIND11_VERSION_PATCH MATCHES [[\.([a-zA-Z0-9]+)$]])
Henry Schreiner's avatar
Henry Schreiner committed
35
  set(pybind11_VERSION_TYPE "${CMAKE_MATCH_1}")
36
endif()
37
string(REGEX MATCH "^[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH}")
38

39
project(
Henry Schreiner's avatar
Henry Schreiner committed
40
41
42
  pybind11
  LANGUAGES CXX
  VERSION "${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH}")
43
44
45
46
47
48

# Standard includes
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(CMakeDependentOption)

49
50
51
if(NOT pybind11_FIND_QUIETLY)
  message(STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE}")
endif()
52

53
# Check if pybind11 is being used directly or via add_subdirectory
54
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
55
56
57
58
59
60
61
62
63
64
  ### Warn if not an out-of-source builds
  if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
    set(lines
        "You are building in-place. If that is not what you intended to "
        "do, you can clean the source directory with:\n"
        "rm -r CMakeCache.txt CMakeFiles/ cmake_uninstall.cmake pybind11Config.cmake "
        "pybind11ConfigVersion.cmake tests/CMakeFiles/\n")
    message(AUTHOR_WARNING ${lines})
  endif()

Henry Schreiner's avatar
Henry Schreiner committed
65
  set(PYBIND11_MASTER_PROJECT ON)
66
67
68
69
70
71
72
73
74

  if(OSX AND CMAKE_VERSION VERSION_LESS 3.7)
    # Bug in macOS CMake < 3.7 is unable to download catch
    message(WARNING "CMAKE 3.7+ needed on macOS to download catch, and newer HIGHLY recommended")
  elseif(WINDOWS AND CMAKE_VERSION VERSION_LESS 3.8)
    # Only tested with 3.8+ in CI.
    message(WARNING "CMAKE 3.8+ tested on Windows, previous versions untested")
  endif()

Henry Schreiner's avatar
Henry Schreiner committed
75
  message(STATUS "CMake ${CMAKE_VERSION}")
76

Henry Schreiner's avatar
Henry Schreiner committed
77
78
79
80
  if(CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_EXTENSIONS OFF)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
  endif()
81
82

  set(pybind11_system "")
83
84

  set_property(GLOBAL PROPERTY USE_FOLDERS ON)
85
else()
Henry Schreiner's avatar
Henry Schreiner committed
86
87
  set(PYBIND11_MASTER_PROJECT OFF)
  set(pybind11_system SYSTEM)
Wenzel Jakob's avatar
Wenzel Jakob committed
88
endif()
89

Henry Schreiner's avatar
Henry Schreiner committed
90
# Options
91
option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
92
option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
93
option(PYBIND11_NOPYTHON "Disable search for Python" OFF)
94
95
96
set(PYBIND11_INTERNALS_VERSION
    ""
    CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.")
Henry Schreiner's avatar
Henry Schreiner committed
97

98
cmake_dependent_option(
Henry Schreiner's avatar
Henry Schreiner committed
99
100
101
  USE_PYTHON_INCLUDE_DIR
  "Install pybind11 headers in Python include directory instead of default installation prefix"
  OFF "PYBIND11_INSTALL" OFF)
102

103
104
105
cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF
                       "NOT CMAKE_VERSION VERSION_LESS 3.12" OFF)

106
107
108
109
110
111
112
# 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
113
    include/pybind11/detail/type_caster_base.h
114
115
116
117
118
119
120
121
122
123
124
    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
125
    include/pybind11/gil.h
126
    include/pybind11/iostream.h
127
128
129
130
131
132
    include/pybind11/functional.h
    include/pybind11/numpy.h
    include/pybind11/operators.h
    include/pybind11/pybind11.h
    include/pybind11/pytypes.h
    include/pybind11/stl.h
133
134
    include/pybind11/stl_bind.h
    include/pybind11/stl/filesystem.h)
135

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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()
154

155
# CMake 3.12 added list(TRANSFORM <list> PREPEND
156
# But we can't use it yet
Henry Schreiner's avatar
Henry Schreiner committed
157
158
string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" PYBIND11_HEADERS
               "${PYBIND11_HEADERS}")
Dean Moldovan's avatar
Dean Moldovan committed
159

160
161
# Cache variable so this can be used in parent projects
set(pybind11_INCLUDE_DIR
Henry Schreiner's avatar
Henry Schreiner committed
162
    "${CMAKE_CURRENT_LIST_DIR}/include"
163
164
165
166
167
168
169
170
    CACHE INTERNAL "Directory where pybind11 headers are located")

# Backward compatible variable for add_subdirectory mode
if(NOT PYBIND11_MASTER_PROJECT)
  set(PYBIND11_INCLUDE_DIR
      "${pybind11_INCLUDE_DIR}"
      CACHE INTERNAL "")
endif()
171

172
173
# 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.
174
# You can also place ifs *in* the Config.in, but not here.
175

176
# This section builds targets, but does *not* touch Python
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Non-IMPORT targets cannot be defined twice
if(NOT TARGET pybind11_headers)
  # Build the headers-only target (no Python included):
  # (long name used here to keep this from clashing in subdirectory mode)
  add_library(pybind11_headers INTERFACE)
  add_library(pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target
  add_library(pybind11::headers ALIAS pybind11_headers) # easier to use/remember

  target_include_directories(
    pybind11_headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${pybind11_INCLUDE_DIR}>
                                                  $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

  target_compile_features(pybind11_headers INTERFACE cxx_inheriting_constructors cxx_user_literals
                                                     cxx_right_angle_brackets)
191
192
193
194
  if(NOT "${PYBIND11_INTERNALS_VERSION}" STREQUAL "")
    target_compile_definitions(
      pybind11_headers INTERFACE "PYBIND11_INTERNALS_VERSION=${PYBIND11_INTERNALS_VERSION}")
  endif()
195
196
197
198
else()
  # It is invalid to install a target twice, too.
  set(PYBIND11_INSTALL OFF)
endif()
199

200
include("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11Common.cmake")
201

202
203
204
205
206
# Relative directory setting
if(USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS)
  file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS})
elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)
  file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
207
208
endif()

209
if(PYBIND11_INSTALL)
210
  install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Henry Schreiner's avatar
Henry Schreiner committed
211
  set(PYBIND11_CMAKECONFIG_INSTALL_DIR
212
      "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}"
Henry Schreiner's avatar
Henry Schreiner committed
213
214
      CACHE STRING "install path for pybind11Config.cmake")

215
216
217
218
219
220
  if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
    set(pybind11_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
  else()
    set(pybind11_INCLUDEDIR "\$\{PACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_INCLUDEDIR}")
  endif()

Henry Schreiner's avatar
Henry Schreiner committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  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
248
249
250
251
          tools/FindPythonLibsNew.cmake
          tools/pybind11Common.cmake
          tools/pybind11Tools.cmake
          tools/pybind11NewTools.cmake
Henry Schreiner's avatar
Henry Schreiner committed
252
253
254
255
256
257
    DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})

  if(NOT PYBIND11_EXPORT_NAME)
    set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets")
  endif()

258
  install(TARGETS pybind11_headers EXPORT "${PYBIND11_EXPORT_NAME}")
Henry Schreiner's avatar
Henry Schreiner committed
259
260
261
262
263

  install(
    EXPORT "${PYBIND11_EXPORT_NAME}"
    NAMESPACE "pybind11::"
    DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
264

265
266
267
268
  # 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)
269

270
271
272
    add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P
                                        ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
  endif()
273
endif()
Henry Schreiner's avatar
Henry Schreiner committed
274

275
276
277
# BUILD_TESTING takes priority, but only if this is the master project
if(PYBIND11_MASTER_PROJECT AND DEFINED BUILD_TESTING)
  if(BUILD_TESTING)
278
279
280
281
282
    if(_pybind11_nopython)
      message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")
    else()
      add_subdirectory(tests)
    endif()
283
284
285
  endif()
else()
  if(PYBIND11_TEST)
286
287
288
289
290
    if(_pybind11_nopython)
      message(FATAL_ERROR "Cannot activate tests in NOPYTHON mode")
    else()
      add_subdirectory(tests)
    endif()
291
  endif()
Henry Schreiner's avatar
Henry Schreiner committed
292
endif()
293
294
295
296
297

# Better symmetry with find_package(pybind11 CONFIG) mode.
if(NOT PYBIND11_MASTER_PROJECT)
  set(pybind11_FOUND
      TRUE
298
      CACHE INTERNAL "True if pybind11 and all required components found on the system")
299
endif()