blob: 5720f2ab04d7264c036cc5910fa2a6c9b0b91809 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001# CMakeLists.txt -- Build system for the pybind11 examples
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 Moldovan4563e9a2016-05-22 22:23:18 +02008cmake_minimum_required(VERSION 2.8.12)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02009
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020010project(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020011
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020012# Check if pybind11 is being used directly or via add_subdirectory
13set(MASTER_PROJECT OFF)
14if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
15 set(MASTER_PROJECT ON)
16endif ()
17
18option(PYBIND11_INSTALL "Install pybind11 header files?" ${MASTER_PROJECT})
19option(PYBIND11_TEST "Build tests?" ${MASTER_PROJECT})
Wenzel Jakob3350b5e2015-11-24 21:33:18 +010020
Wenzel Jakobbcd31822015-10-12 23:57:20 +020021# Add a CMake parameter for choosing a desired Python version
Wenzel Jakobb1b71402015-10-18 16:48:30 +020022set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020023
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010024include(CheckCXXCompilerFlag)
25
Dean Moldovan928fff62016-05-22 19:48:47 +020026list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010027set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
Dean Moldovan928fff62016-05-22 19:48:47 +020028find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
Wenzel Jakobfaaee1c2015-11-30 10:17:01 +010029
Dean Moldovan4563e9a2016-05-22 22:23:18 +020030# Cache variables so pybind11_add_module can be used in parent projects
31set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
32set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
Dean Moldovan03d6a512016-05-25 13:39:32 +020033set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
Dean Moldovan4563e9a2016-05-22 22:23:18 +020034set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
35set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020036
Dean Moldovan4563e9a2016-05-22 22:23:18 +020037# Build a Python extension module:
38# pybind11_add_module(<name> source1 [source2 ...])
39#
40function(pybind11_add_module target_name)
41 add_library(${target_name} MODULE ${ARGN})
42 target_include_directories(${target_name} PUBLIC ${PYBIND11_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS})
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020043
Dean Moldovan4563e9a2016-05-22 22:23:18 +020044 # The prefix and extension are provided by FindPythonLibsNew.cmake
45 set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
46 set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020047
Dean Moldovan4563e9a2016-05-22 22:23:18 +020048 # It's quite common to have multiple copies of the same Python version
49 # installed on one's system. E.g.: one copy from the OS and another copy
50 # that's statically linked into an application like Blender or Maya.
51 # If we link our plugin library against the OS Python here and import it
52 # into Blender or Maya later on, this will cause segfaults when multiple
53 # conflicting Python instances are active at the same time (even when they
54 # are of the same version).
55
56 # Windows is not affected by this issue since it handles DLL imports
57 # differently. The solution for Linux and Mac OS is simple: we just don't
58 # link against the Python library. The resulting shared library will have
59 # missing symbols, but that's perfectly fine -- they will be resolved at
60 # import time.
61 if(MSVC)
62 target_link_libraries(${target_name} ${PYTHON_LIBRARIES})
63 elseif(APPLE)
64 # Make sure OS X does not have any issues with missing symbols
65 set_target_properties(${target_name} PROPERTIES MACOSX_RPATH ".")
66 target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
67 endif()
68
69 # Make sure C++11/14 are enabled
70 if(NOT MSVC)
71 check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
72 check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
73
74 if (HAS_CPP14_FLAG)
75 target_compile_options(${target_name} PUBLIC "-std=c++14")
76 elseif (HAS_CPP11_FLAG)
77 target_compile_options(${target_name} PUBLIC "-std=c++11")
78 else()
79 message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
80 endif()
81
82 string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
83 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
84 # Default symbol visibility
85 target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
86 endif()
87 endif()
88endfunction()
Wenzel Jakobe44e56f2016-04-30 22:59:58 +020089
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020090if (PYBIND11_TEST)
91 enable_testing()
92 add_subdirectory(example)
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020093endif()
94
Wenzel Jakob3350b5e2015-11-24 21:33:18 +010095if (PYBIND11_INSTALL)
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020096 set(PYBIND11_HEADERS
97 include/pybind11/attr.h
98 include/pybind11/cast.h
99 include/pybind11/common.h
100 include/pybind11/complex.h
101 include/pybind11/descr.h
102 include/pybind11/eigen.h
103 include/pybind11/functional.h
104 include/pybind11/numpy.h
105 include/pybind11/operators.h
106 include/pybind11/pybind11.h
107 include/pybind11/pytypes.h
108 include/pybind11/stl.h
109 include/pybind11/stl_bind.h
110 include/pybind11/typeid.h
111 )
112
113 install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
Luka Čehovin19af3572015-11-24 21:20:56 +0100114endif()