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 | |
| 8 | cmake_minimum_required(VERSION 2.8) |
| 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 | |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 12 | option(PYBIND11_INSTALL "Install pybind11 header files?" ON) |
| 13 | |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 14 | # Add a CMake parameter for choosing a desired Python version |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 15 | 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] | 16 | |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame^] | 17 | include(CheckCXXCompilerFlag) |
| 18 | |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 19 | # Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 20 | if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) |
| 21 | message(STATUS "Setting build type to 'MinSizeRel' as none was specified.") |
| 22 | set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE) |
| 23 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" |
| 24 | "MinSizeRel" "RelWithDebInfo") |
| 25 | endif() |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 26 | string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) |
Wenzel Jakob | 3b806d4 | 2015-10-11 16:29:35 +0200 | [diff] [blame] | 27 | |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame^] | 28 | set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7) |
| 29 | if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "") |
| 30 | find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT) |
| 31 | if (NOT PythonLibs_FOUND) |
| 32 | find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED) |
| 33 | endif() |
| 34 | else() |
| 35 | find_package(PythonLibs REQUIRED) |
| 36 | endif() |
| 37 | find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED) |
Wenzel Jakob | faaee1c | 2015-11-30 10:17:01 +0100 | [diff] [blame] | 38 | |
| 39 | if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") |
Wenzel Jakob | f233166 | 2015-11-28 14:24:44 +0100 | [diff] [blame] | 40 | # Enable C++11 mode on C++ / Clang |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 41 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 42 | |
| 43 | # Enable link time optimization and set the default symbol |
| 44 | # visibility to hidden (very important to obtain small binaries) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 45 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
Wenzel Jakob | faaee1c | 2015-11-30 10:17:01 +0100 | [diff] [blame] | 46 | # Default symbol visibility |
| 47 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden") |
| 48 | |
| 49 | # Check for Link Time Optimization support |
| 50 | CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG) |
| 51 | if (HAS_LTO_FLAG) |
| 52 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto") |
| 53 | endif() |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 54 | endif() |
| 55 | endif() |
| 56 | |
| 57 | # Compile with compiler warnings turned on |
| 58 | if(MSVC) |
| 59 | if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") |
| 60 | string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 61 | else() |
| 62 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") |
| 63 | endif() |
Wenzel Jakob | f233166 | 2015-11-28 14:24:44 +0100 | [diff] [blame] | 64 | elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 65 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") |
| 66 | endif() |
| 67 | |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 68 | # Include path for Python header files |
| 69 | include_directories(${PYTHON_INCLUDE_DIR}) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 70 | |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 71 | # Include path for pybind11 header files |
| 72 | include_directories(include) |
| 73 | |
Luka Čehovin | 19af357 | 2015-11-24 21:20:56 +0100 | [diff] [blame] | 74 | set(PYBIND11_HEADERS |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 75 | include/pybind11/cast.h |
| 76 | include/pybind11/common.h |
| 77 | include/pybind11/operators.h |
| 78 | include/pybind11/pybind11.h |
| 79 | include/pybind11/pytypes.h |
| 80 | include/pybind11/typeid.h |
| 81 | include/pybind11/numpy.h |
Jan Dohl | 3e2736b | 2015-12-15 03:24:40 +0100 | [diff] [blame] | 82 | include/pybind11/complex.h |
Wenzel Jakob | e52cf8a | 2015-12-15 11:32:29 +0100 | [diff] [blame] | 83 | include/pybind11/stl.h |
Erik O'Leary | a1718a6 | 2015-12-21 20:26:02 -0600 | [diff] [blame] | 84 | include/pybind11/functional.h |
Luka Čehovin | 19af357 | 2015-11-24 21:20:56 +0100 | [diff] [blame] | 85 | ) |
| 86 | |
| 87 | # Create the binding library |
| 88 | add_library(example SHARED |
| 89 | ${PYBIND11_HEADERS} |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 90 | example/example.cpp |
| 91 | example/example1.cpp |
| 92 | example/example2.cpp |
| 93 | example/example3.cpp |
| 94 | example/example4.cpp |
| 95 | example/example5.cpp |
| 96 | example/example6.cpp |
| 97 | example/example7.cpp |
| 98 | example/example8.cpp |
| 99 | example/example9.cpp |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 100 | example/example10.cpp |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 101 | example/example11.cpp |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 102 | example/example12.cpp |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 103 | ) |
| 104 | |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 105 | # Don't add a 'lib' prefix to the shared library |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 106 | set_target_properties(example PROPERTIES PREFIX "") |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 107 | |
Wenzel Jakob | 607654f | 2015-10-13 23:58:10 +0200 | [diff] [blame] | 108 | # Always write the output file directly into the 'example' directory (even on MSVC) |
| 109 | set(CompilerFlags |
| 110 | LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG |
| 111 | LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO |
| 112 | RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG |
| 113 | RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO) |
| 114 | |
| 115 | foreach(CompilerFlag ${CompilerFlags}) |
| 116 | set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example) |
| 117 | endforeach() |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 118 | |
| 119 | if (WIN32) |
| 120 | if (MSVC) |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 121 | # Enforce size-based optimization and link time code generation |
| 122 | # on MSVC (~30% smaller binaries in experiments). /bigobj is needed |
| 123 | # for bigger binding projects due to the limit to 64k addressable sections |
| 124 | # /MP enables multithreaded builds (relevant when there are many files). |
Wenzel Jakob | 02f770d | 2015-09-01 21:38:20 +0200 | [diff] [blame] | 125 | set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj") |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 126 | set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG") |
| 127 | endif() |
| 128 | |
| 129 | # .PYD file extension on Windows |
| 130 | set_target_properties(example PROPERTIES SUFFIX ".pyd") |
| 131 | |
| 132 | # Link against the Python shared library |
| 133 | target_link_libraries(example ${PYTHON_LIBRARY}) |
| 134 | elseif (UNIX) |
| 135 | # It's quite common to have multiple copies of the same Python version |
| 136 | # installed on one's system. E.g.: one copy from the OS and another copy |
| 137 | # that's statically linked into an application like Blender or Maya. |
| 138 | # If we link our plugin library against the OS Python here and import it |
| 139 | # into Blender or Maya later on, this will cause segfaults when multiple |
| 140 | # conflicting Python instances are active at the same time. |
| 141 | |
Wenzel Jakob | bcd3182 | 2015-10-12 23:57:20 +0200 | [diff] [blame] | 142 | # Windows is not affected by this issue since it handles DLL imports |
| 143 | # differently. The solution for Linux and Mac OS is simple: we just don't |
| 144 | # link against the Python library. The resulting shared library will have |
| 145 | # missing symbols, but that's perfectly fine -- they will be resolved at |
| 146 | # import time. |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 147 | |
| 148 | # .SO file extension on Linux/Mac OS |
| 149 | set_target_properties(example PROPERTIES SUFFIX ".so") |
| 150 | |
Wenzel Jakob | 867ae37 | 2015-10-15 22:41:25 +0200 | [diff] [blame] | 151 | # Optimize for a small binary size |
| 152 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
| 153 | set_target_properties(example PROPERTIES COMPILE_FLAGS "-Os") |
| 154 | endif() |
| 155 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 156 | # Strip unnecessary sections of the binary on Linux/Mac OS |
| 157 | if(APPLE) |
| 158 | set_target_properties(example PROPERTIES MACOSX_RPATH ".") |
| 159 | set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip") |
| 160 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
| 161 | add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so) |
| 162 | endif() |
| 163 | else() |
| 164 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
| 165 | add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so) |
| 166 | endif() |
| 167 | endif() |
| 168 | endif() |
| 169 | |
| 170 | enable_testing() |
| 171 | set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py) |
Wenzel Jakob | 6d6fd09 | 2015-10-01 17:34:26 +0200 | [diff] [blame] | 172 | foreach(i RANGE 1 12) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 173 | add_test(NAME example${i} COMMAND ${RUN_TEST} example${i}) |
| 174 | endforeach() |
Luka Čehovin | 19af357 | 2015-11-24 21:20:56 +0100 | [diff] [blame] | 175 | |
Wenzel Jakob | 3350b5e | 2015-11-24 21:33:18 +0100 | [diff] [blame] | 176 | if (PYBIND11_INSTALL) |
Luka Čehovin | 19af357 | 2015-11-24 21:20:56 +0100 | [diff] [blame] | 177 | install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11) |
| 178 | endif() |