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 |
Dean Moldovan | 49720f0 | 2016-05-26 22:53:38 +0200 | [diff] [blame] | 13 | set(PYBIND11_MASTER_PROJECT OFF) |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 14 | if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) |
Dean Moldovan | 49720f0 | 2016-05-26 22:53:38 +0200 | [diff] [blame] | 15 | set(PYBIND11_MASTER_PROJECT ON) |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 16 | endif () |
| 17 | |
Dean Moldovan | 49720f0 | 2016-05-26 22:53:38 +0200 | [diff] [blame] | 18 | option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) |
| 19 | option(PYBIND11_TEST "Build tests?" ${PYBIND11_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 | |
Dean Moldovan | 928fff6 | 2016-05-22 19:48:47 +0200 | [diff] [blame] | 24 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools") |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 25 | set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7) |
Dean Moldovan | 928fff6 | 2016-05-22 19:48:47 +0200 | [diff] [blame] | 26 | find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED) |
Wenzel Jakob | faaee1c | 2015-11-30 10:17:01 +0100 | [diff] [blame] | 27 | |
Dean Moldovan | c3c27c4 | 2016-05-28 11:08:16 +0200 | [diff] [blame^] | 28 | include(CheckCXXCompilerFlag) |
| 29 | |
| 30 | if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD) |
| 31 | check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG) |
| 32 | check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG) |
| 33 | |
| 34 | if (HAS_CPP14_FLAG) |
| 35 | set(PYBIND11_CPP_STANDARD -std=c++14) |
| 36 | elseif (HAS_CPP11_FLAG) |
| 37 | set(PYBIND11_CPP_STANDARD -std=c++11) |
| 38 | else() |
| 39 | message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!") |
| 40 | endif() |
| 41 | |
| 42 | set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING |
| 43 | "C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available.") |
| 44 | endif() |
| 45 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 46 | # Cache variables so pybind11_add_module can be used in parent projects |
| 47 | set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "") |
| 48 | set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "") |
Dean Moldovan | 03d6a51 | 2016-05-25 13:39:32 +0200 | [diff] [blame] | 49 | set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "") |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 50 | set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "") |
| 51 | set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "") |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 52 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 53 | # Build a Python extension module: |
| 54 | # pybind11_add_module(<name> source1 [source2 ...]) |
| 55 | # |
| 56 | function(pybind11_add_module target_name) |
| 57 | add_library(${target_name} MODULE ${ARGN}) |
| 58 | target_include_directories(${target_name} PUBLIC ${PYBIND11_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS}) |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 59 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 60 | # The prefix and extension are provided by FindPythonLibsNew.cmake |
| 61 | set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}") |
| 62 | set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}") |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 63 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 64 | # It's quite common to have multiple copies of the same Python version |
| 65 | # installed on one's system. E.g.: one copy from the OS and another copy |
| 66 | # that's statically linked into an application like Blender or Maya. |
| 67 | # If we link our plugin library against the OS Python here and import it |
| 68 | # into Blender or Maya later on, this will cause segfaults when multiple |
| 69 | # conflicting Python instances are active at the same time (even when they |
| 70 | # are of the same version). |
| 71 | |
| 72 | # Windows is not affected by this issue since it handles DLL imports |
| 73 | # differently. The solution for Linux and Mac OS is simple: we just don't |
| 74 | # link against the Python library. The resulting shared library will have |
| 75 | # missing symbols, but that's perfectly fine -- they will be resolved at |
| 76 | # import time. |
| 77 | if(MSVC) |
Dean Moldovan | 9fb50c5 | 2016-05-27 21:42:43 +0200 | [diff] [blame] | 78 | target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES}) |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 79 | elseif(APPLE) |
| 80 | # Make sure OS X does not have any issues with missing symbols |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 81 | target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup") |
| 82 | endif() |
| 83 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 84 | if(NOT MSVC) |
Dean Moldovan | 9fb50c5 | 2016-05-27 21:42:43 +0200 | [diff] [blame] | 85 | # Make sure C++11/14 are enabled |
Dean Moldovan | c3c27c4 | 2016-05-28 11:08:16 +0200 | [diff] [blame^] | 86 | target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD}) |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 87 | |
Dean Moldovan | 9fb50c5 | 2016-05-27 21:42:43 +0200 | [diff] [blame] | 88 | # Enable link time optimization and set the default symbol |
| 89 | # visibility to hidden (very important to obtain small binaries) |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 90 | string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) |
| 91 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
Dean Moldovan | 9fb50c5 | 2016-05-27 21:42:43 +0200 | [diff] [blame] | 92 | # Check for Link Time Optimization support (GCC/Clang) |
| 93 | check_cxx_compiler_flag("-flto" HAS_LTO_FLAG) |
| 94 | if(HAS_LTO_FLAG) |
| 95 | target_compile_options(${target_name} PRIVATE -flto) |
| 96 | endif() |
| 97 | |
| 98 | # Intel equivalent to LTO is called IPO |
| 99 | if(CMAKE_CXX_COMPILER_ID MATCHES "Intel") |
| 100 | check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG) |
| 101 | if(HAS_IPO_FLAG) |
| 102 | target_compile_options(${target_name} PRIVATE -ipo) |
| 103 | endif() |
| 104 | endif() |
| 105 | |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 106 | # Default symbol visibility |
| 107 | target_compile_options(${target_name} PRIVATE "-fvisibility=hidden") |
Dean Moldovan | 9fb50c5 | 2016-05-27 21:42:43 +0200 | [diff] [blame] | 108 | |
| 109 | # Strip unnecessary sections of the binary on Linux/Mac OS |
| 110 | if(CMAKE_STRIP) |
| 111 | if(APPLE) |
| 112 | add_custom_command(TARGET ${target_name} POST_BUILD |
| 113 | COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>) |
| 114 | else() |
| 115 | add_custom_command(TARGET ${target_name} POST_BUILD |
| 116 | COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>) |
| 117 | endif() |
| 118 | endif() |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 119 | endif() |
Dean Moldovan | 9fb50c5 | 2016-05-27 21:42:43 +0200 | [diff] [blame] | 120 | elseif(MSVC) |
| 121 | # /MP enables multithreaded builds (relevant when there are many files), /bigobj is |
| 122 | # needed for bigger binding projects due to the limit to 64k addressable sections |
| 123 | target_compile_options(${target_name} PRIVATE /MP /bigobj) |
| 124 | |
| 125 | # Enforce link time code generation on MSVC, except in debug mode |
| 126 | target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>) |
| 127 | # Fancy generator expressions don't work with linker flags, for reasons unknown |
| 128 | set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG) |
| 129 | set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG) |
| 130 | set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG) |
| 131 | endif() |
| 132 | endfunction() |
| 133 | |
| 134 | # Compile with compiler warnings turned on |
| 135 | function(pybind11_turn_on_warnings target_name) |
| 136 | if(MSVC) |
| 137 | target_compile_options(${target_name} PRIVATE /W4) |
| 138 | else() |
| 139 | target_compile_options(${target_name} PRIVATE -Wall -Wextra) |
Dean Moldovan | 4563e9a | 2016-05-22 22:23:18 +0200 | [diff] [blame] | 140 | endif() |
| 141 | endfunction() |
Wenzel Jakob | e44e56f | 2016-04-30 22:59:58 +0200 | [diff] [blame] | 142 | |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 143 | if (PYBIND11_TEST) |
| 144 | enable_testing() |
| 145 | add_subdirectory(example) |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 146 | endif() |
| 147 | |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 148 | if (PYBIND11_INSTALL) |
Dean Moldovan | 8c6b0b8 | 2016-05-23 00:12:37 +0200 | [diff] [blame] | 149 | set(PYBIND11_HEADERS |
| 150 | include/pybind11/attr.h |
| 151 | include/pybind11/cast.h |
| 152 | include/pybind11/common.h |
| 153 | include/pybind11/complex.h |
| 154 | include/pybind11/descr.h |
| 155 | include/pybind11/eigen.h |
| 156 | include/pybind11/functional.h |
| 157 | include/pybind11/numpy.h |
| 158 | include/pybind11/operators.h |
| 159 | include/pybind11/pybind11.h |
| 160 | include/pybind11/pytypes.h |
| 161 | include/pybind11/stl.h |
| 162 | include/pybind11/stl_bind.h |
| 163 | include/pybind11/typeid.h |
| 164 | ) |
| 165 | |
| 166 | install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11) |
Luka Čehovin | 19af357 | 2015-11-24 21:20:56 +0100 | [diff] [blame] | 167 | endif() |