Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | # 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 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 | |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 10 | project(pybind11) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 11 | |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 12 | # Check if pybind11 is being used directly or via add_subdirectory |
| 13 | set(MASTER_PROJECT OFF) |
| 14 | if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
| 15 | set(MASTER_PROJECT ON) |
| 16 | endif () |
| 17 | |
| 18 | option(PYBIND11_INSTALL "Install pybind11 header files?" ${MASTER_PROJECT}) |
| 19 | option(PYBIND11_TEST "Build tests?" ${MASTER_PROJECT}) |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 20 | |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 21 | # Add a CMake parameter for choosing a desired Python version |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 22 | set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application") |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 23 | |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 24 | include(CheckCXXCompilerFlag) |
| 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 | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 27 | set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7) |
Dean Moldovan | 928fff6 | 2016-05-22 19:48:47 +0200 | [diff] [blame] | 28 | find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED) |
Wenzel Jakob | faaee1c | 2015-11-30 10:17:01 +0100 | [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 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 37 | # Build a Python extension module: |
| 38 | # pybind11_add_module(<name> source1 [source2 ...]) |
| 39 | # |
| 40 | function(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 Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 43 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 44 | # 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 Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 47 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 48 | # 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() |
| 88 | endfunction() |
Wenzel Jakob | e44e56f | 2016-04-30 22:59:58 +0200 | [diff] [blame] | 89 | |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 90 | if (PYBIND11_TEST) |
| 91 | enable_testing() |
| 92 | add_subdirectory(example) |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 93 | endif() |
| 94 | |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 95 | if (PYBIND11_INSTALL) |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 96 | 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 Čehovin | 19af357 | 2015-11-24 21:20:56 +0100 | [diff] [blame] | 114 | endif() |