Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame^] | 1 | Build systems |
| 2 | ############# |
| 3 | |
| 4 | Building with setuptools |
| 5 | ======================== |
| 6 | |
| 7 | For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay |
| 8 | has kindly provided an example project which shows how to set up everything, |
| 9 | including automatic generation of documentation using Sphinx. Please refer to |
| 10 | the [pbtest]_ repository. |
| 11 | |
| 12 | .. [pbtest] https://github.com/pybind/pbtest |
| 13 | |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 14 | .. _cmake: |
| 15 | |
| 16 | Building with CMake |
| 17 | =================== |
| 18 | |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame^] | 19 | For C++ codebases that already have an existing CMake-based build system, the |
| 20 | following snippet should be a good starting point to create bindings across |
| 21 | platforms. It assumes that the code is located in a file named |
| 22 | :file:`example.cpp`, and that the pybind11 repository is located in a |
| 23 | subdirectory named :file:`pybind11`. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 24 | |
| 25 | .. code-block:: cmake |
| 26 | |
| 27 | cmake_minimum_required(VERSION 2.8) |
| 28 | |
| 29 | project(example) |
| 30 | |
| 31 | # Add a CMake parameter for choosing a desired Python version |
| 32 | set(EXAMPLE_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example library") |
Ivan Smirnov | 4f88edd | 2016-01-17 16:42:11 +0000 | [diff] [blame] | 33 | |
| 34 | include(CheckCXXCompilerFlag) |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 35 | |
| 36 | # Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries |
| 37 | if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) |
| 38 | message(STATUS "Setting build type to 'MinSizeRel' as none was specified.") |
| 39 | set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE) |
| 40 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" |
| 41 | "MinSizeRel" "RelWithDebInfo") |
| 42 | endif() |
| 43 | string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE) |
| 44 | |
| 45 | # Try to autodetect Python (can be overridden manually if needed) |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 46 | set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7) |
Wenzel Jakob | d4db8bc | 2016-01-20 01:26:44 +0100 | [diff] [blame] | 47 | if (NOT ${EXAMPLE_PYTHON_VERSION} STREQUAL "") |
| 48 | find_package(PythonLibs ${EXAMPLE_PYTHON_VERSION} EXACT) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 49 | if (NOT PythonLibs_FOUND) |
Wenzel Jakob | d4db8bc | 2016-01-20 01:26:44 +0100 | [diff] [blame] | 50 | find_package(PythonLibs ${EXAMPLE_PYTHON_VERSION} REQUIRED) |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 51 | endif() |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 52 | else() |
| 53 | find_package(PythonLibs REQUIRED) |
| 54 | endif() |
| 55 | |
Wenzel Jakob | c91551b | 2016-02-07 15:45:56 +0100 | [diff] [blame] | 56 | # The above sometimes returns version numbers like "3.4.3+"; the "+" must be removed for the next lines to work |
| 57 | string(REPLACE "+" "" PYTHONLIBS_VERSION_STRING "+${PYTHONLIBS_VERSION_STRING}") |
| 58 | |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 59 | # Uncomment the following line if you will also require a matching Python interpreter |
| 60 | # find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED) |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 61 | |
Wenzel Jakob | f1532bd | 2015-12-07 18:24:43 +0100 | [diff] [blame] | 62 | if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 63 | CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG) |
| 64 | CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_CPP11_FLAG) |
| 65 | |
| 66 | if (HAS_CPP14_FLAG) |
| 67 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") |
| 68 | elseif (HAS_CPP11_FLAG) |
| 69 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") |
| 70 | else() |
| 71 | message(FATAL_ERROR "Unsupported compiler -- at least C++11 support is needed!") |
| 72 | endif() |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 73 | |
| 74 | # Enable link time optimization and set the default symbol |
| 75 | # visibility to hidden (very important to obtain small binaries) |
| 76 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
Wenzel Jakob | f1532bd | 2015-12-07 18:24:43 +0100 | [diff] [blame] | 77 | # Default symbol visibility |
| 78 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden") |
| 79 | |
| 80 | # Check for Link Time Optimization support |
| 81 | CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG) |
| 82 | if (HAS_LTO_FLAG) |
| 83 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto") |
| 84 | endif() |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 85 | endif() |
| 86 | endif() |
| 87 | |
| 88 | # Include path for Python header files |
| 89 | include_directories(${PYTHON_INCLUDE_DIR}) |
| 90 | |
| 91 | # Include path for pybind11 header files -- this may need to be changed depending on your setup |
| 92 | include_directories(${PROJECT_SOURCE_DIR}/pybind11/include) |
| 93 | |
| 94 | # Create the binding library |
| 95 | add_library(example SHARED |
| 96 | example.cpp |
| 97 | # ... extra files go here ... |
| 98 | ) |
| 99 | |
| 100 | # Don't add a 'lib' prefix to the shared library |
| 101 | set_target_properties(example PROPERTIES PREFIX "") |
| 102 | |
| 103 | if (WIN32) |
| 104 | if (MSVC) |
Wenzel Jakob | 518cf72 | 2016-01-21 19:17:58 +0100 | [diff] [blame] | 105 | # /bigobj is needed for bigger binding projects due to the limit to 64k |
| 106 | # addressable sections. /MP enables multithreaded builds (relevant when |
| 107 | # there are many files). |
| 108 | set_target_properties(example PROPERTIES COMPILE_FLAGS "/MP /bigobj ") |
| 109 | |
| 110 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
| 111 | # Enforce size-based optimization and link time code generation on MSVC |
| 112 | # (~30% smaller binaries in experiments). |
| 113 | set_target_properties(example APPEND_STRING PROPERTY COMPILE_FLAGS "/Os /GL ") |
| 114 | set_target_properties(example APPEND_STRING PROPERTY LINK_FLAGS "/LTCG ") |
| 115 | endif() |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 116 | endif() |
| 117 | |
| 118 | # .PYD file extension on Windows |
| 119 | set_target_properties(example PROPERTIES SUFFIX ".pyd") |
| 120 | |
| 121 | # Link against the Python shared library |
| 122 | target_link_libraries(example ${PYTHON_LIBRARY}) |
| 123 | elseif (UNIX) |
| 124 | # It's quite common to have multiple copies of the same Python version |
| 125 | # installed on one's system. E.g.: one copy from the OS and another copy |
| 126 | # that's statically linked into an application like Blender or Maya. |
| 127 | # If we link our plugin library against the OS Python here and import it |
| 128 | # into Blender or Maya later on, this will cause segfaults when multiple |
Wenzel Jakob | 6eb11da | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 129 | # conflicting Python instances are active at the same time (even when they |
| 130 | # are of the same version). |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 131 | |
Wenzel Jakob | 9329669 | 2015-10-13 23:21:54 +0200 | [diff] [blame] | 132 | # Windows is not affected by this issue since it handles DLL imports |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 133 | # differently. The solution for Linux and Mac OS is simple: we just don't |
| 134 | # link against the Python library. The resulting shared library will have |
| 135 | # missing symbols, but that's perfectly fine -- they will be resolved at |
| 136 | # import time. |
| 137 | |
| 138 | # .SO file extension on Linux/Mac OS |
| 139 | set_target_properties(example PROPERTIES SUFFIX ".so") |
| 140 | |
| 141 | # Strip unnecessary sections of the binary on Linux/Mac OS |
| 142 | if(APPLE) |
| 143 | set_target_properties(example PROPERTIES MACOSX_RPATH ".") |
Wenzel Jakob | 48548ea | 2016-01-17 22:36:44 +0100 | [diff] [blame] | 144 | set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ") |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 145 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
| 146 | add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_BINARY_DIR}/example.so) |
| 147 | endif() |
| 148 | else() |
| 149 | if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG) |
| 150 | add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_BINARY_DIR}/example.so) |
| 151 | endif() |
| 152 | endif() |
| 153 | endif() |