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 | |
| 10 | project(pybind) |
| 11 | |
| 12 | if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) |
| 13 | message(STATUS "Setting build type to 'MinSizeRel' as none was specified.") |
| 14 | set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE) |
| 15 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" |
| 16 | "MinSizeRel" "RelWithDebInfo") |
| 17 | endif() |
| 18 | |
Wenzel Jakob | 3b806d4 | 2015-10-11 16:29:35 +0200 | [diff] [blame^] | 19 | set(PYBIND_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application") |
| 20 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 21 | set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6) |
Wenzel Jakob | 3b806d4 | 2015-10-11 16:29:35 +0200 | [diff] [blame^] | 22 | find_package(PythonLibs ${PYBIND_PYTHON_VERSION} REQUIRED) |
| 23 | find_package(PythonInterp ${PYBIND_PYTHON_VERSION} REQUIRED) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 24 | |
| 25 | string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) |
| 26 | if (UNIX) |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 27 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 28 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 29 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto") |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 30 | endif() |
| 31 | endif() |
| 32 | |
| 33 | # Compile with compiler warnings turned on |
| 34 | if(MSVC) |
| 35 | if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") |
| 36 | string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| 37 | else() |
| 38 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") |
| 39 | endif() |
| 40 | else() |
| 41 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") |
| 42 | endif() |
| 43 | |
| 44 | include_directories(${PYTHON_INCLUDE_DIR} include) |
| 45 | |
| 46 | add_library(example SHARED |
| 47 | include/pybind/cast.h |
| 48 | include/pybind/common.h |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 49 | include/pybind/operators.h |
| 50 | include/pybind/pybind.h |
| 51 | include/pybind/pytypes.h |
| 52 | include/pybind/typeid.h |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 53 | include/pybind/numpy.h |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 54 | example/example.cpp |
| 55 | example/example1.cpp |
| 56 | example/example2.cpp |
| 57 | example/example3.cpp |
| 58 | example/example4.cpp |
| 59 | example/example5.cpp |
| 60 | example/example6.cpp |
| 61 | example/example7.cpp |
| 62 | example/example8.cpp |
| 63 | example/example9.cpp |
Wenzel Jakob | d4258ba | 2015-07-26 16:33:49 +0200 | [diff] [blame] | 64 | example/example10.cpp |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 65 | example/example11.cpp |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 66 | example/example12.cpp |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 67 | ) |
| 68 | |
| 69 | set_target_properties(example PROPERTIES PREFIX "") |
| 70 | set_target_properties(example PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/example) |
| 71 | |
| 72 | if (WIN32) |
| 73 | if (MSVC) |
| 74 | # Enforce size-based optimization and link time code generation on MSVC (~30% smaller binaries in experiments) |
Wenzel Jakob | 02f770d | 2015-09-01 21:38:20 +0200 | [diff] [blame] | 75 | # /bigobj is needed for bigger binding projects due to the limit to 64k addressable sections |
| 76 | # /MP enables multithreaded builds |
| 77 | set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj") |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 78 | set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG") |
| 79 | endif() |
| 80 | |
| 81 | # .PYD file extension on Windows |
| 82 | set_target_properties(example PROPERTIES SUFFIX ".pyd") |
| 83 | |
| 84 | # Link against the Python shared library |
| 85 | target_link_libraries(example ${PYTHON_LIBRARY}) |
| 86 | elseif (UNIX) |
| 87 | # It's quite common to have multiple copies of the same Python version |
| 88 | # installed on one's system. E.g.: one copy from the OS and another copy |
| 89 | # that's statically linked into an application like Blender or Maya. |
| 90 | # If we link our plugin library against the OS Python here and import it |
| 91 | # into Blender or Maya later on, this will cause segfaults when multiple |
| 92 | # conflicting Python instances are active at the same time. |
| 93 | |
| 94 | # Windows does not seem to be affected by this issue. The solution for Linux |
| 95 | # and Mac OS is simple: we just don't link against the Python library. The |
| 96 | # resulting shared library will have missing symbols, but that's perfectly |
| 97 | # fine -- they will be resolved at import time. |
| 98 | |
| 99 | # .SO file extension on Linux/Mac OS |
| 100 | set_target_properties(example PROPERTIES SUFFIX ".so") |
| 101 | |
| 102 | # Strip unnecessary sections of the binary on Linux/Mac OS |
| 103 | if(APPLE) |
| 104 | set_target_properties(example PROPERTIES MACOSX_RPATH ".") |
| 105 | set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip") |
| 106 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
| 107 | add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so) |
| 108 | endif() |
| 109 | else() |
| 110 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
| 111 | add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so) |
| 112 | endif() |
| 113 | endif() |
| 114 | endif() |
| 115 | |
| 116 | enable_testing() |
| 117 | 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] | 118 | foreach(i RANGE 1 12) |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 119 | add_test(NAME example${i} COMMAND ${RUN_TEST} example${i}) |
| 120 | endforeach() |