blob: ec104b0b5511a7118727c0505bdb6fbbab8d5e4f [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001# CMakeLists.txt -- Build system for the pybind11 modules
Wenzel Jakob38bd7112015-07-05 20:05:44 +02002#
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 Moldovan4563e9a2016-05-22 22:23:18 +02008cmake_minimum_required(VERSION 2.8.12)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02009
Jason Rhinelandera3fec902016-12-26 05:26:21 -050010if (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)
13endif()
14
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020015project(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020016
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020017# Check if pybind11 is being used directly or via add_subdirectory
Dean Moldovan49720f02016-05-26 22:53:38 +020018set(PYBIND11_MASTER_PROJECT OFF)
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020019if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
Dean Moldovan49720f02016-05-26 22:53:38 +020020 set(PYBIND11_MASTER_PROJECT ON)
Wenzel Jakob67a63922016-05-29 12:35:16 +020021endif()
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020022
Dean Moldovan49720f02016-05-26 22:53:38 +020023option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
Wenzel Jakob67a63922016-05-29 12:35:16 +020024option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
Dean Moldovan00a30092016-08-15 13:41:44 +020025option(PYBIND11_WERROR "Report all warnings as errors" OFF)
Wenzel Jakob3350b5e2015-11-24 21:33:18 +010026
Dean Moldovan928fff62016-05-22 19:48:47 +020027list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
Wenzel Jakobfaaee1c2015-11-30 10:17:01 +010028
Lori A. Burns545b4db2016-12-13 10:59:21 -050029include(pybind11Tools)
Dean Moldovanc3c27c42016-05-28 11:08:16 +020030
Dean Moldovan4563e9a2016-05-22 22:23:18 +020031# Cache variables so pybind11_add_module can be used in parent projects
32set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
33set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
Dean Moldovan03d6a512016-05-25 13:39:32 +020034set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
Dean Moldovan4563e9a2016-05-22 22:23:18 +020035set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
36set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020037
Dean Moldovan9fb50c52016-05-27 21:42:43 +020038# Compile with compiler warnings turned on
Wenzel Jakob67a63922016-05-29 12:35:16 +020039function(pybind11_enable_warnings target_name)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020040 if(MSVC)
41 target_compile_options(${target_name} PRIVATE /W4)
42 else()
Matthew Woehlkee15fa9f2017-02-08 17:43:08 -050043 target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual)
Dean Moldovan4563e9a2016-05-22 22:23:18 +020044 endif()
Dean Moldovan00a30092016-08-15 13:41:44 +020045
46 if(PYBIND11_WERROR)
47 if(MSVC)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020048 target_compile_options(${target_name} PRIVATE /WX)
Dean Moldovan00a30092016-08-15 13:41:44 +020049 else()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020050 target_compile_options(${target_name} PRIVATE -Werror)
Dean Moldovan00a30092016-08-15 13:41:44 +020051 endif()
52 endif()
Dean Moldovan4563e9a2016-05-22 22:23:18 +020053endfunction()
Wenzel Jakobe44e56f2016-04-30 22:59:58 +020054
Wenzel Jakobdac38582016-09-29 21:30:00 +020055set(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 Stukowski9a110e62016-11-15 12:38:05 +010062 include/pybind11/options.h
Wenzel Jakobdac38582016-09-29 21:30:00 +020063 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)
74string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/"
75 PYBIND11_HEADERS "${PYBIND11_HEADERS}")
76
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020077if (PYBIND11_TEST)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020078 add_subdirectory(tests)
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020079endif()
80
Lori A. Burns44d7c592016-12-13 10:46:41 -050081include(GNUInstallDirs)
Lori A. Burns5cafc992016-12-13 10:55:38 -050082include(CMakePackageConfigHelpers)
Lori A. Burns44d7c592016-12-13 10:46:41 -050083
84# extract project version from source
85file(STRINGS "${PYBIND11_INCLUDE_DIR}/pybind11/common.h" pybind11_version_defines
86 REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ")
87foreach(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()
91endforeach()
92set(${PROJECT_NAME}_VERSION ${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH})
Dean Moldovanb0f38852016-12-14 01:43:39 +010093message(STATUS "pybind11 v${${PROJECT_NAME}_VERSION}")
Lori A. Burns44d7c592016-12-13 10:46:41 -050094
Lori A. Burns5cafc992016-12-13 10:55:38 -050095if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0
96 # Build an interface library target:
Dean Moldovan71e8a792016-12-17 21:38:57 +010097 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 Moldovanb0f38852016-12-14 01:43:39 +0100101 if(WIN32 OR CYGWIN)
Dean Moldovan71e8a792016-12-17 21:38:57 +0100102 target_link_libraries(module INTERFACE $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
Dean Moldovanb0f38852016-12-14 01:43:39 +0100103 elseif(APPLE)
Dean Moldovan71e8a792016-12-17 21:38:57 +0100104 target_link_libraries(module INTERFACE "-undefined dynamic_lookup")
Lori A. Burns5cafc992016-12-13 10:55:38 -0500105 endif()
Dean Moldovan71e8a792016-12-17 21:38:57 +0100106 target_compile_options(module INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>)
Dean Moldovanb0f38852016-12-14 01:43:39 +0100107
Dean Moldovan71e8a792016-12-17 21:38:57 +0100108 add_library(pybind11::module ALIAS module) # to match exported target
Lori A. Burns5cafc992016-12-13 10:55:38 -0500109endif()
110
Wenzel Jakob3350b5e2015-11-24 21:33:18 +0100111if (PYBIND11_INSTALL)
Lori A. Burns44d7c592016-12-13 10:46:41 -0500112 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. Burns5cafc992016-12-13 10:55:38 -0500117 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 Moldovan71e8a792016-12-17 21:38:57 +0100130 install(TARGETS module
Lori A. Burns5cafc992016-12-13 10:55:38 -0500131 EXPORT "${PROJECT_NAME}Targets")
132 install(EXPORT "${PROJECT_NAME}Targets"
133 NAMESPACE "${PROJECT_NAME}::"
134 DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
Lori A. Burns5cafc992016-12-13 10:55:38 -0500135 endif()
Luka Čehovin19af3572015-11-24 21:20:56 +0100136endif()