CMakeLists.txt revision 12391
1955SN/A# CMakeLists.txt -- Build system for the pybind11 modules
2955SN/A#
39812Sandreas.hansson@arm.com# Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
49812Sandreas.hansson@arm.com#
59812Sandreas.hansson@arm.com# All rights reserved. Use of this source code is governed by a
69812Sandreas.hansson@arm.com# BSD-style license that can be found in the LICENSE file.
79812Sandreas.hansson@arm.com
89812Sandreas.hansson@arm.comcmake_minimum_required(VERSION 2.8.12)
99812Sandreas.hansson@arm.com
109812Sandreas.hansson@arm.comif (POLICY CMP0048)
119812Sandreas.hansson@arm.com  # cmake warns if loaded from a min-3.0-required parent dir, so silence the warning:
129812Sandreas.hansson@arm.com  cmake_policy(SET CMP0048 NEW)
139812Sandreas.hansson@arm.comendif()
149812Sandreas.hansson@arm.com
157816Ssteve.reinhardt@amd.com# CMake versions < 3.4.0 do not support try_compile/pthread checks without C as active language.
165871Snate@binkert.orgif(CMAKE_VERSION VERSION_LESS 3.4.0)
171762SN/A  project(pybind11)
18955SN/Aelse()
19955SN/A  project(pybind11 CXX)
20955SN/Aendif()
21955SN/A
22955SN/A# Check if pybind11 is being used directly or via add_subdirectory
23955SN/Aset(PYBIND11_MASTER_PROJECT OFF)
24955SN/Aif (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
25955SN/A  set(PYBIND11_MASTER_PROJECT ON)
26955SN/Aendif()
27955SN/A
28955SN/Aoption(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
29955SN/Aoption(PYBIND11_TEST    "Build pybind11 test suite?"     ${PYBIND11_MASTER_PROJECT})
30955SN/A
31955SN/Alist(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
32955SN/A
33955SN/Ainclude(pybind11Tools)
34955SN/A
35955SN/A# Cache variables so pybind11_add_module can be used in parent projects
36955SN/Aset(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
37955SN/Aset(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
38955SN/Aset(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
39955SN/Aset(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
40955SN/Aset(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
41955SN/A
422665Ssaidi@eecs.umich.edu# NB: when adding a header don't forget to also add it to setup.py
432665Ssaidi@eecs.umich.eduset(PYBIND11_HEADERS
445863Snate@binkert.org  include/pybind11/detail/class.h
45955SN/A  include/pybind11/detail/common.h
46955SN/A  include/pybind11/detail/descr.h
47955SN/A  include/pybind11/detail/init.h
48955SN/A  include/pybind11/detail/internals.h
49955SN/A  include/pybind11/detail/typeid.h
508878Ssteve.reinhardt@amd.com  include/pybind11/attr.h
512632Sstever@eecs.umich.edu  include/pybind11/buffer_info.h
528878Ssteve.reinhardt@amd.com  include/pybind11/cast.h
532632Sstever@eecs.umich.edu  include/pybind11/chrono.h
54955SN/A  include/pybind11/common.h
558878Ssteve.reinhardt@amd.com  include/pybind11/complex.h
562632Sstever@eecs.umich.edu  include/pybind11/options.h
572761Sstever@eecs.umich.edu  include/pybind11/eigen.h
582632Sstever@eecs.umich.edu  include/pybind11/embed.h
592632Sstever@eecs.umich.edu  include/pybind11/eval.h
602632Sstever@eecs.umich.edu  include/pybind11/functional.h
612761Sstever@eecs.umich.edu  include/pybind11/numpy.h
622761Sstever@eecs.umich.edu  include/pybind11/operators.h
632761Sstever@eecs.umich.edu  include/pybind11/pybind11.h
648878Ssteve.reinhardt@amd.com  include/pybind11/pytypes.h
658878Ssteve.reinhardt@amd.com  include/pybind11/stl.h
662761Sstever@eecs.umich.edu  include/pybind11/stl_bind.h
672761Sstever@eecs.umich.edu)
682761Sstever@eecs.umich.edustring(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/"
692761Sstever@eecs.umich.edu       PYBIND11_HEADERS "${PYBIND11_HEADERS}")
702761Sstever@eecs.umich.edu
718878Ssteve.reinhardt@amd.comif (PYBIND11_TEST)
728878Ssteve.reinhardt@amd.com  add_subdirectory(tests)
732632Sstever@eecs.umich.eduendif()
742632Sstever@eecs.umich.edu
758878Ssteve.reinhardt@amd.cominclude(GNUInstallDirs)
768878Ssteve.reinhardt@amd.cominclude(CMakePackageConfigHelpers)
772632Sstever@eecs.umich.edu
78955SN/A# extract project version from source
79955SN/Afile(STRINGS "${PYBIND11_INCLUDE_DIR}/pybind11/detail/common.h" pybind11_version_defines
80955SN/A     REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) ")
815863Snate@binkert.orgforeach(ver ${pybind11_version_defines})
825863Snate@binkert.org  if (ver MATCHES "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$")
835863Snate@binkert.org    set(PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2}" CACHE INTERNAL "")
845863Snate@binkert.org  endif()
855863Snate@binkert.orgendforeach()
865863Snate@binkert.orgset(${PROJECT_NAME}_VERSION ${PYBIND11_VERSION_MAJOR}.${PYBIND11_VERSION_MINOR}.${PYBIND11_VERSION_PATCH})
875863Snate@binkert.orgmessage(STATUS "pybind11 v${${PROJECT_NAME}_VERSION}")
885863Snate@binkert.org
895863Snate@binkert.orgoption (USE_PYTHON_INCLUDE_DIR "Install pybind11 headers in Python include directory instead of default installation prefix" OFF)
905863Snate@binkert.orgif (USE_PYTHON_INCLUDE_DIR)
915863Snate@binkert.org    file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
928878Ssteve.reinhardt@amd.comendif()
935863Snate@binkert.org
945863Snate@binkert.orgif(NOT (CMAKE_VERSION VERSION_LESS 3.0))  # CMake >= 3.0
955863Snate@binkert.org  # Build an interface library target:
969812Sandreas.hansson@arm.com  add_library(pybind11 INTERFACE)
979812Sandreas.hansson@arm.com  add_library(pybind11::pybind11 ALIAS pybind11)  # to match exported target
985863Snate@binkert.org  target_include_directories(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
999812Sandreas.hansson@arm.com                                                $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>
1005863Snate@binkert.org                                                $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
1015863Snate@binkert.org  target_compile_options(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>)
1025863Snate@binkert.org
1039812Sandreas.hansson@arm.com  add_library(module INTERFACE)
1049812Sandreas.hansson@arm.com  add_library(pybind11::module ALIAS module)
1055863Snate@binkert.org  if(NOT MSVC)
1065863Snate@binkert.org    target_compile_options(module INTERFACE -fvisibility=hidden)
1078878Ssteve.reinhardt@amd.com  endif()
1085863Snate@binkert.org  target_link_libraries(module INTERFACE pybind11::pybind11)
1095863Snate@binkert.org  if(WIN32 OR CYGWIN)
1105863Snate@binkert.org    target_link_libraries(module INTERFACE $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
1116654Snate@binkert.org  elseif(APPLE)
112955SN/A    target_link_libraries(module INTERFACE "-undefined dynamic_lookup")
1135396Ssaidi@eecs.umich.edu  endif()
1145863Snate@binkert.org
1155863Snate@binkert.org  add_library(embed INTERFACE)
1164202Sbinkertn@umich.edu  add_library(pybind11::embed ALIAS embed)
1175863Snate@binkert.org  target_link_libraries(embed INTERFACE pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
1185863Snate@binkert.orgendif()
1195863Snate@binkert.org
1205863Snate@binkert.orgif (PYBIND11_INSTALL)
121955SN/A  install(DIRECTORY ${PYBIND11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
1226654Snate@binkert.org  # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
1235273Sstever@gmail.com  set(PYBIND11_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake")
1245871Snate@binkert.org
1255273Sstever@gmail.com  configure_package_config_file(tools/${PROJECT_NAME}Config.cmake.in
1266655Snate@binkert.org                                "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
1278878Ssteve.reinhardt@amd.com                                INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
1286655Snate@binkert.org  # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does
1296655Snate@binkert.org  # not depend on architecture specific settings or libraries.
1309219Spower.jg@gmail.com  set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
1316655Snate@binkert.org  unset(CMAKE_SIZEOF_VOID_P)
1325871Snate@binkert.org  write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
1336654Snate@binkert.org                                   VERSION ${${PROJECT_NAME}_VERSION}
1348947Sandreas.hansson@arm.com                                   COMPATIBILITY AnyNewerVersion)
1355396Ssaidi@eecs.umich.edu  set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P})
1368120Sgblack@eecs.umich.edu  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
1378120Sgblack@eecs.umich.edu                ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
1388120Sgblack@eecs.umich.edu                tools/FindPythonLibsNew.cmake
1398120Sgblack@eecs.umich.edu                tools/pybind11Tools.cmake
1408120Sgblack@eecs.umich.edu          DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
1418120Sgblack@eecs.umich.edu
1428120Sgblack@eecs.umich.edu  if(NOT (CMAKE_VERSION VERSION_LESS 3.0))
1438120Sgblack@eecs.umich.edu    if(NOT PYBIND11_EXPORT_NAME)
1448879Ssteve.reinhardt@amd.com      set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets")
1458879Ssteve.reinhardt@amd.com    endif()
1468879Ssteve.reinhardt@amd.com
1478879Ssteve.reinhardt@amd.com    install(TARGETS pybind11 module embed
1488879Ssteve.reinhardt@amd.com            EXPORT "${PYBIND11_EXPORT_NAME}")
1498879Ssteve.reinhardt@amd.com    if(PYBIND11_MASTER_PROJECT)
1508879Ssteve.reinhardt@amd.com      install(EXPORT "${PYBIND11_EXPORT_NAME}"
1518879Ssteve.reinhardt@amd.com              NAMESPACE "${PROJECT_NAME}::"
1528879Ssteve.reinhardt@amd.com              DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR})
1538879Ssteve.reinhardt@amd.com    endif()
1548879Ssteve.reinhardt@amd.com  endif()
1558879Ssteve.reinhardt@amd.comendif()
1568879Ssteve.reinhardt@amd.com