blob: ace21675868855917ce142eaf6762fbdc5d99a66 [file] [log] [blame]
Wenzel Jakob4a48afb2016-03-09 21:31:21 +01001Build systems
2#############
3
4Building with setuptools
5========================
6
7For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay
8has kindly provided an example project which shows how to set up everything,
9including automatic generation of documentation using Sphinx. Please refer to
10the [pbtest]_ repository.
11
12.. [pbtest] https://github.com/pybind/pbtest
13
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020014.. _cmake:
15
16Building with CMake
17===================
18
Wenzel Jakob4a48afb2016-03-09 21:31:21 +010019For C++ codebases that already have an existing CMake-based build system, the
20following snippet should be a good starting point to create bindings across
21platforms. It assumes that the code is located in a file named
22:file:`example.cpp`, and that the pybind11 repository is located in a
23subdirectory named :file:`pybind11`.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020024
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 Smirnov4f88edd2016-01-17 16:42:11 +000033
34 include(CheckCXXCompilerFlag)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020035
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 Jakobcaa9d442016-01-17 22:36:34 +010046 set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
Wenzel Jakobd4db8bc2016-01-20 01:26:44 +010047 if (NOT ${EXAMPLE_PYTHON_VERSION} STREQUAL "")
48 find_package(PythonLibs ${EXAMPLE_PYTHON_VERSION} EXACT)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010049 if (NOT PythonLibs_FOUND)
Wenzel Jakobd4db8bc2016-01-20 01:26:44 +010050 find_package(PythonLibs ${EXAMPLE_PYTHON_VERSION} REQUIRED)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010051 endif()
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010052 else()
53 find_package(PythonLibs REQUIRED)
54 endif()
55
Wenzel Jakobc91551b2016-02-07 15:45:56 +010056 # 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 Jakobcaa9d442016-01-17 22:36:34 +010059 # Uncomment the following line if you will also require a matching Python interpreter
60 # find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020061
Wenzel Jakobf1532bd2015-12-07 18:24:43 +010062 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
Wenzel Jakob66c9a402016-01-17 22:36:36 +010063 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 Jakob28f98aa2015-10-13 02:57:16 +020073
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 Jakobf1532bd2015-12-07 18:24:43 +010077 # 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 Jakob28f98aa2015-10-13 02:57:16 +020085 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 Jakob518cf722016-01-21 19:17:58 +0100105 # /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 Jakob28f98aa2015-10-13 02:57:16 +0200116 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 Jakob6eb11da2016-01-17 22:36:36 +0100129 # conflicting Python instances are active at the same time (even when they
130 # are of the same version).
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200131
Wenzel Jakob93296692015-10-13 23:21:54 +0200132 # Windows is not affected by this issue since it handles DLL imports
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200133 # 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 Jakob48548ea2016-01-17 22:36:44 +0100144 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200145 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()