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}) |
Dean Moldovan | 00a3009 | 2016-08-15 13:41:44 +0200 | [diff] [blame] | 25 | option(PYBIND11_WERROR "Report all warnings as errors" OFF) |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 26 | |
Dean Moldovan | 928fff6 | 2016-05-22 19:48:47 +0200 | [diff] [blame] | 27 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools") |
Wenzel Jakob | faaee1c | 2015-11-30 10:17:01 +0100 | [diff] [blame] | 28 | |
Lori A. Burns | 545b4db | 2016-12-13 10:59:21 -0500 | [diff] [blame] | 29 | include(pybind11Tools) |
Dean Moldovan | c3c27c4 | 2016-05-28 11:08:16 +0200 | [diff] [blame] | 30 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 31 | # Cache variables so pybind11_add_module can be used in parent projects |
| 32 | set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "") |
| 33 | set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "") |
Dean Moldovan | 03d6a51 | 2016-05-25 13:39:32 +0200 | [diff] [blame] | 34 | set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "") |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 35 | set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "") |
| 36 | set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "") |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 37 | |
Dean Moldovan | 9fb50c5 | 2016-05-27 21:42:43 +0200 | [diff] [blame] | 38 | # Compile with compiler warnings turned on |
Wenzel Jakob | 67a6392 | 2016-05-29 12:35:16 +0200 | [diff] [blame] | 39 | function(pybind11_enable_warnings target_name) |
Dean Moldovan | 9fb50c5 | 2016-05-27 21:42:43 +0200 | [diff] [blame] | 40 | if(MSVC) |
| 41 | target_compile_options(${target_name} PRIVATE /W4) |
| 42 | else() |
Matthew Woehlke | e15fa9f | 2017-02-08 17:43:08 -0500 | [diff] [blame] | 43 | target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual) |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 44 | endif() |
Dean Moldovan | 00a3009 | 2016-08-15 13:41:44 +0200 | [diff] [blame] | 45 | |
| 46 | if(PYBIND11_WERROR) |
| 47 | if(MSVC) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 48 | target_compile_options(${target_name} PRIVATE /WX) |
Dean Moldovan | 00a3009 | 2016-08-15 13:41:44 +0200 | [diff] [blame] | 49 | else() |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 50 | target_compile_options(${target_name} PRIVATE -Werror) |
Dean Moldovan | 00a3009 | 2016-08-15 13:41:44 +0200 | [diff] [blame] | 51 | endif() |
| 52 | endif() |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 53 | endfunction() |
Wenzel Jakob | e44e56f | 2016-04-30 22:59:58 +0200 | [diff] [blame] | 54 | |
Wenzel Jakob | dac3858 | 2016-09-29 21:30:00 +0200 | [diff] [blame] | 55 | set(PYBIND11_HEADERS |
| 56 | include/pybind11/attr.h |
| 57 | include/pybind11/cast.h |
| 58 | include/pybind11/chrono.h |
| 59 | include/pybind11/common.h |
| 60 | include/pybind11/complex.h |
| 61 | include/pybind11/descr.h |
Alexander Stukowski | 9a110e6 | 2016-11-15 12:38:05 +0100 | [diff] [blame] | 62 | include/pybind11/options.h |
Wenzel Jakob | dac3858 | 2016-09-29 21:30:00 +0200 | [diff] [blame] | 63 | include/pybind11/eigen.h |
| 64 | include/pybind11/eval.h |
| 65 | include/pybind11/functional.h |
| 66 | include/pybind11/numpy.h |
| 67 | include/pybind11/operators.h |
| 68 | include/pybind11/pybind11.h |
| 69 | include/pybind11/pytypes.h |
| 70 | include/pybind11/stl.h |
| 71 | include/pybind11/stl_bind.h |
| 72 | include/pybind11/typeid.h |
| 73 | ) |
| 74 | string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/" |
| 75 | PYBIND11_HEADERS "${PYBIND11_HEADERS}") |
| 76 | |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 77 | if (PYBIND11_TEST) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 78 | add_subdirectory(tests) |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 79 | endif() |
| 80 | |
Lori A. Burns | 44d7c59 | 2016-12-13 10:46:41 -0500 | [diff] [blame] | 81 | include(GNUInstallDirs) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 82 | include(CMakePackageConfigHelpers) |
Lori A. Burns | 44d7c59 | 2016-12-13 10:46:41 -0500 | [diff] [blame] | 83 | |
| 84 | # extract project version from source |
| 85 | file(STRINGS "${PYBIND11_INCLUDE_DIR}/pybind11/common.h" pybind11_version_defines |
| 86 | REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ") |
| 87 | foreach(ver ${pybind11_version_defines}) |
| 88 | if (ver MATCHES "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$") |
| 89 | set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "") |
| 90 | endif() |
| 91 | endforeach() |
| 92 | 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] | 93 | message(STATUS "pybind11 v${${PROJECT_NAME}_VERSION}") |
Lori A. Burns | 44d7c59 | 2016-12-13 10:46:41 -0500 | [diff] [blame] | 94 | |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 95 | if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0 |
| 96 | # Build an interface library target: |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 97 | add_library(module INTERFACE) |
| 98 | target_include_directories(module INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}> |
| 99 | $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}> |
| 100 | $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>) |
Dean Moldovan | b0f3885 | 2016-12-14 01:43:39 +0100 | [diff] [blame] | 101 | if(WIN32 OR CYGWIN) |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 102 | target_link_libraries(module INTERFACE $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>) |
Dean Moldovan | b0f3885 | 2016-12-14 01:43:39 +0100 | [diff] [blame] | 103 | elseif(APPLE) |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 104 | target_link_libraries(module INTERFACE "-undefined dynamic_lookup") |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 105 | endif() |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 106 | target_compile_options(module INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>) |
Dean Moldovan | b0f3885 | 2016-12-14 01:43:39 +0100 | [diff] [blame] | 107 | |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 108 | add_library(pybind11::module ALIAS module) # to match exported target |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 109 | endif() |
| 110 | |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 111 | if (PYBIND11_INSTALL) |
Lori A. Burns | 44d7c59 | 2016-12-13 10:46:41 -0500 | [diff] [blame] | 112 | install(FILES ${PYBIND11_HEADERS} |
| 113 | DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/pybind11) |
| 114 | # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share". |
| 115 | set(PYBIND11_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake") |
| 116 | |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 117 | configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in |
| 118 | "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" |
| 119 | INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) |
| 120 | write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake |
| 121 | VERSION ${${PROJECT_NAME}_VERSION} |
| 122 | COMPATIBILITY AnyNewerVersion) |
| 123 | install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake |
| 124 | ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake |
| 125 | tools/FindPythonLibsNew.cmake |
| 126 | tools/pybind11Tools.cmake |
| 127 | DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) |
| 128 | |
| 129 | if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 130 | install(TARGETS module |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 131 | EXPORT "${PROJECT_NAME}Targets") |
| 132 | install(EXPORT "${PROJECT_NAME}Targets" |
| 133 | NAMESPACE "${PROJECT_NAME}::" |
| 134 | DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 135 | endif() |
Luka Čehovin | 19af357 | 2015-11-24 21:20:56 +0100 | [diff] [blame] | 136 | endif() |