blob: 61728596b396d72f6b4e10e0698a110f708b075d [file] [log] [blame]
Wenzel Jakob28f98aa2015-10-13 02:57:16 +02001.. _cmake:
2
3Building with CMake
4===================
5
6The following snippet should be a good starting point to create bindings across
7platforms. It assumes that the code is located in a file named :file:`example.cpp`,
8and that the pybind11 repository is located in a subdirectory named :file:`pybind11`.
9
10.. code-block:: cmake
11
12 cmake_minimum_required(VERSION 2.8)
13
14 project(example)
15
16 # Add a CMake parameter for choosing a desired Python version
17 set(EXAMPLE_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example library")
Ivan Smirnov4f88edd2016-01-17 16:42:11 +000018
19 include(CheckCXXCompilerFlag)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020020
21 # Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
22 if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
23 message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
24 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
25 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
26 "MinSizeRel" "RelWithDebInfo")
27 endif()
28 string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
29
30 # Try to autodetect Python (can be overridden manually if needed)
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010031 set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
32 if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "")
Wenzel Jakob48548ea2016-01-17 22:36:44 +010033 find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT)
34 if (NOT PythonLibs_FOUND)
35 find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED)
36 endif()
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010037 else()
38 find_package(PythonLibs REQUIRED)
39 endif()
Wenzel Jakob48548ea2016-01-17 22:36:44 +010040 find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010041
42 # Uncomment the following line if you will also require a matching Python interpreter
43 # find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020044
Wenzel Jakobf1532bd2015-12-07 18:24:43 +010045 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
Wenzel Jakob66c9a402016-01-17 22:36:36 +010046 CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
47 CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_CPP11_FLAG)
48
49 if (HAS_CPP14_FLAG)
50 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
51 elseif (HAS_CPP11_FLAG)
52 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
53 else()
54 message(FATAL_ERROR "Unsupported compiler -- at least C++11 support is needed!")
55 endif()
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020056
57 # Enable link time optimization and set the default symbol
58 # visibility to hidden (very important to obtain small binaries)
59 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Wenzel Jakobf1532bd2015-12-07 18:24:43 +010060 # Default symbol visibility
61 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
62
63 # Check for Link Time Optimization support
64 CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
65 if (HAS_LTO_FLAG)
66 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
67 endif()
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020068 endif()
69 endif()
70
71 # Include path for Python header files
72 include_directories(${PYTHON_INCLUDE_DIR})
73
74 # Include path for pybind11 header files -- this may need to be changed depending on your setup
75 include_directories(${PROJECT_SOURCE_DIR}/pybind11/include)
76
77 # Create the binding library
78 add_library(example SHARED
79 example.cpp
80 # ... extra files go here ...
81 )
82
83 # Don't add a 'lib' prefix to the shared library
84 set_target_properties(example PROPERTIES PREFIX "")
85
86 if (WIN32)
87 if (MSVC)
88 # Enforce size-based optimization and link time code generation
89 # on MSVC (~30% smaller binaries in experiments). /bigobj is needed
90 # for bigger binding projects due to the limit to 64k addressable sections
91 # /MP enables multithreaded builds (relevant when there are many files).
92 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
93 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
94 endif()
95
96 # .PYD file extension on Windows
97 set_target_properties(example PROPERTIES SUFFIX ".pyd")
98
99 # Link against the Python shared library
100 target_link_libraries(example ${PYTHON_LIBRARY})
101 elseif (UNIX)
102 # It's quite common to have multiple copies of the same Python version
103 # installed on one's system. E.g.: one copy from the OS and another copy
104 # that's statically linked into an application like Blender or Maya.
105 # If we link our plugin library against the OS Python here and import it
106 # into Blender or Maya later on, this will cause segfaults when multiple
Wenzel Jakob6eb11da2016-01-17 22:36:36 +0100107 # conflicting Python instances are active at the same time (even when they
108 # are of the same version).
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200109
Wenzel Jakob93296692015-10-13 23:21:54 +0200110 # Windows is not affected by this issue since it handles DLL imports
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200111 # differently. The solution for Linux and Mac OS is simple: we just don't
112 # link against the Python library. The resulting shared library will have
113 # missing symbols, but that's perfectly fine -- they will be resolved at
114 # import time.
115
116 # .SO file extension on Linux/Mac OS
117 set_target_properties(example PROPERTIES SUFFIX ".so")
118
119 # Strip unnecessary sections of the binary on Linux/Mac OS
120 if(APPLE)
121 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100122 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200123 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
124 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_BINARY_DIR}/example.so)
125 endif()
126 else()
127 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
128 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_BINARY_DIR}/example.so)
129 endif()
130 endif()
131 endif()