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