Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | # CMakeLists.txt -- Build system for the pybind11 modules |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 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 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 8 | cmake_minimum_required(VERSION 2.8.12) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 9 | |
Jason Rhinelander | a3fec90 | 2016-12-26 05:26:21 -0500 | [diff] [blame] | 10 | if (POLICY CMP0048) |
| 11 | # cmake warns if loaded from a min-3.0-required parent dir, so silence the warning: |
| 12 | cmake_policy(SET CMP0048 NEW) |
| 13 | endif() |
| 14 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 15 | project(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 16 | |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 17 | # Check if pybind11 is being used directly or via add_subdirectory |
Dean Moldovan | 49720f0 | 2016-05-26 22:53:38 +0200 | [diff] [blame] | 18 | set(PYBIND11_MASTER_PROJECT OFF) |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 19 | if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
Dean Moldovan | 49720f0 | 2016-05-26 22:53:38 +0200 | [diff] [blame] | 20 | set(PYBIND11_MASTER_PROJECT ON) |
Wenzel Jakob | 67a6392 | 2016-05-29 12:35:16 +0200 | [diff] [blame] | 21 | endif() |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 22 | |
Dean Moldovan | 49720f0 | 2016-05-26 22:53:38 +0200 | [diff] [blame] | 23 | option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) |
Wenzel Jakob | 67a6392 | 2016-05-29 12:35:16 +0200 | [diff] [blame] | 24 | option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 25 | |
Dean Moldovan | 928fff6 | 2016-05-22 19:48:47 +0200 | [diff] [blame] | 26 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools") |
Wenzel Jakob | faaee1c | 2015-11-30 10:17:01 +0100 | [diff] [blame] | 27 | |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 28 | include(pybind11Tools) |
Dean Moldovan | c3c27c4 | 2016-05-28 11:08:16 +0200 | [diff] [blame] | 29 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 30 | # Cache variables so pybind11_add_module can be used in parent projects |
| 31 | set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "") |
| 32 | set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "") |
Dean Moldovan | 03d6a51 | 2016-05-25 13:39:32 +0200 | [diff] [blame] | 33 | set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "") |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 34 | set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "") |
| 35 | set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "") |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 36 | |
Wenzel Jakob | dac3858 | 2016-09-29 21:30:00 +0200 | [diff] [blame] | 37 | set(PYBIND11_HEADERS |
| 38 | include/pybind11/attr.h |
| 39 | include/pybind11/cast.h |
| 40 | include/pybind11/chrono.h |
Dean Moldovan | c91f8bd | 2017-02-13 18:11:24 +0100 | [diff] [blame] | 41 | include/pybind11/class_support.h |
Wenzel Jakob | dac3858 | 2016-09-29 21:30:00 +0200 | [diff] [blame] | 42 | include/pybind11/common.h |
| 43 | include/pybind11/complex.h |
| 44 | include/pybind11/descr.h |
Alexander Stukowski | 9a110e6 | 2016-11-15 12:38:05 +0100 | [diff] [blame] | 45 | include/pybind11/options.h |
Wenzel Jakob | dac3858 | 2016-09-29 21:30:00 +0200 | [diff] [blame] | 46 | include/pybind11/eigen.h |
| 47 | include/pybind11/eval.h |
| 48 | include/pybind11/functional.h |
| 49 | include/pybind11/numpy.h |
| 50 | include/pybind11/operators.h |
| 51 | include/pybind11/pybind11.h |
| 52 | include/pybind11/pytypes.h |
| 53 | include/pybind11/stl.h |
| 54 | include/pybind11/stl_bind.h |
| 55 | include/pybind11/typeid.h |
| 56 | ) |
| 57 | string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" |
| 58 | PYBIND11_HEADERS "${PYBIND11_HEADERS}") |
| 59 | |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 60 | if (PYBIND11_TEST) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 61 | add_subdirectory(tests) |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 62 | endif() |
| 63 | |
Lori A. Burns | 44d7c59 | 2016-12-13 10:46:41 -0500 | [diff] [blame] | 64 | include(GNUInstallDirs) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 65 | include(CMakePackageConfigHelpers) |
Lori A. Burns | 44d7c59 | 2016-12-13 10:46:41 -0500 | [diff] [blame] | 66 | |
| 67 | # extract project version from source |
| 68 | file(STRINGS "${PYBIND11_INCLUDE_DIR}/pybind11/common.h" pybind11_version_defines |
| 69 | REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ") |
| 70 | foreach(ver ${pybind11_version_defines}) |
| 71 | if (ver MATCHES "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$") |
| 72 | set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "") |
| 73 | endif() |
| 74 | endforeach() |
| 75 | set(${PROJECT_NAME}_VERSION ${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH}) |
Dean Moldovan | b0f3885 | 2016-12-14 01:43:39 +0100 | [diff] [blame] | 76 | message(STATUS "pybind11 v${${PROJECT_NAME}_VERSION}") |
Lori A. Burns | 44d7c59 | 2016-12-13 10:46:41 -0500 | [diff] [blame] | 77 | |
Sylvain Corlay | d5ce82b | 2017-02-14 13:16:14 +0100 | [diff] [blame] | 78 | option (USE_PYTHON_INCLUDE_DIR "Install pybind11 headers in Python include directory instead of default installation prefix" OFF) |
| 79 | if (USE_PYTHON_INCLUDE_DIR) |
| 80 | file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS}) |
| 81 | endif() |
| 82 | |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 83 | if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0 |
| 84 | # Build an interface library target: |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 85 | add_library(module INTERFACE) |
| 86 | target_include_directories(module INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}> |
| 87 | $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}> |
| 88 | $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) |
Dean Moldovan | b0f3885 | 2016-12-14 01:43:39 +0100 | [diff] [blame] | 89 | if(WIN32 OR CYGWIN) |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 90 | target_link_libraries(module INTERFACE $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>) |
Dean Moldovan | b0f3885 | 2016-12-14 01:43:39 +0100 | [diff] [blame] | 91 | elseif(APPLE) |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 92 | target_link_libraries(module INTERFACE "-undefined dynamic_lookup") |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 93 | endif() |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 94 | target_compile_options(module INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>) |
Dean Moldovan | b0f3885 | 2016-12-14 01:43:39 +0100 | [diff] [blame] | 95 | |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 96 | add_library(pybind11::module ALIAS module) # to match exported target |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 97 | endif() |
| 98 | |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 99 | if (PYBIND11_INSTALL) |
Lori A. Burns | 44d7c59 | 2016-12-13 10:46:41 -0500 | [diff] [blame] | 100 | install(FILES ${PYBIND11_HEADERS} |
| 101 | DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/pybind11) |
| 102 | # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share". |
| 103 | set(PYBIND11_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake") |
| 104 | |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 105 | configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in |
| 106 | "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" |
| 107 | INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) |
Ghislain Antony Vaillant | 5b50376 | 2017-03-24 22:09:38 +0000 | [diff] [blame] | 108 | # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does |
| 109 | # not depend on architecture specific settings or libraries. |
| 110 | set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) |
| 111 | unset(CMAKE_SIZEOF_VOID_P) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 112 | write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake |
| 113 | VERSION ${${PROJECT_NAME}_VERSION} |
| 114 | COMPATIBILITY AnyNewerVersion) |
Ghislain Antony Vaillant | 5b50376 | 2017-03-24 22:09:38 +0000 | [diff] [blame] | 115 | set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P}) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 116 | install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake |
| 117 | ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake |
| 118 | tools/FindPythonLibsNew.cmake |
| 119 | tools/pybind11Tools.cmake |
| 120 | DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) |
| 121 | |
| 122 | if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 123 | install(TARGETS module |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 124 | EXPORT "${PROJECT_NAME}Targets") |
| 125 | install(EXPORT "${PROJECT_NAME}Targets" |
| 126 | NAMESPACE "${PROJECT_NAME}::" |
| 127 | DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 128 | endif() |
Luka Čehovin | 19af357 | 2015-11-24 21:20:56 +0100 | [diff] [blame] | 129 | endif() |