blob: 238132f955efc546bfde693047f413580f9821ef [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)
Wenzel Jakobd4db8bc2016-01-20 01:26:44 +010032 if (NOT ${EXAMPLE_PYTHON_VERSION} STREQUAL "")
33 find_package(PythonLibs ${EXAMPLE_PYTHON_VERSION} EXACT)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010034 if (NOT PythonLibs_FOUND)
Wenzel Jakobd4db8bc2016-01-20 01:26:44 +010035 find_package(PythonLibs ${EXAMPLE_PYTHON_VERSION} REQUIRED)
Wenzel Jakob48548ea2016-01-17 22:36:44 +010036 endif()
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010037 else()
38 find_package(PythonLibs REQUIRED)
39 endif()
40
Wenzel Jakobc91551b2016-02-07 15:45:56 +010041 # The above sometimes returns version numbers like "3.4.3+"; the "+" must be removed for the next lines to work
42 string(REPLACE "+" "" PYTHONLIBS_VERSION_STRING "+${PYTHONLIBS_VERSION_STRING}")
43
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010044 # Uncomment the following line if you will also require a matching Python interpreter
45 # find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020046
Wenzel Jakobf1532bd2015-12-07 18:24:43 +010047 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
Wenzel Jakob66c9a402016-01-17 22:36:36 +010048 CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
49 CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_CPP11_FLAG)
50
51 if (HAS_CPP14_FLAG)
52 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
53 elseif (HAS_CPP11_FLAG)
54 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
55 else()
56 message(FATAL_ERROR "Unsupported compiler -- at least C++11 support is needed!")
57 endif()
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020058
59 # Enable link time optimization and set the default symbol
60 # visibility to hidden (very important to obtain small binaries)
61 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Wenzel Jakobf1532bd2015-12-07 18:24:43 +010062 # Default symbol visibility
63 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
64
65 # Check for Link Time Optimization support
66 CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
67 if (HAS_LTO_FLAG)
68 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
69 endif()
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020070 endif()
71 endif()
72
73 # Include path for Python header files
74 include_directories(${PYTHON_INCLUDE_DIR})
75
76 # Include path for pybind11 header files -- this may need to be changed depending on your setup
77 include_directories(${PROJECT_SOURCE_DIR}/pybind11/include)
78
79 # Create the binding library
80 add_library(example SHARED
81 example.cpp
82 # ... extra files go here ...
83 )
84
85 # Don't add a 'lib' prefix to the shared library
86 set_target_properties(example PROPERTIES PREFIX "")
87
88 if (WIN32)
89 if (MSVC)
Wenzel Jakob518cf722016-01-21 19:17:58 +010090 # /bigobj is needed for bigger binding projects due to the limit to 64k
91 # addressable sections. /MP enables multithreaded builds (relevant when
92 # there are many files).
93 set_target_properties(example PROPERTIES COMPILE_FLAGS "/MP /bigobj ")
94
95 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
96 # Enforce size-based optimization and link time code generation on MSVC
97 # (~30% smaller binaries in experiments).
98 set_target_properties(example APPEND_STRING PROPERTY COMPILE_FLAGS "/Os /GL ")
99 set_target_properties(example APPEND_STRING PROPERTY LINK_FLAGS "/LTCG ")
100 endif()
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200101 endif()
102
103 # .PYD file extension on Windows
104 set_target_properties(example PROPERTIES SUFFIX ".pyd")
105
106 # Link against the Python shared library
107 target_link_libraries(example ${PYTHON_LIBRARY})
108 elseif (UNIX)
109 # It's quite common to have multiple copies of the same Python version
110 # installed on one's system. E.g.: one copy from the OS and another copy
111 # that's statically linked into an application like Blender or Maya.
112 # If we link our plugin library against the OS Python here and import it
113 # into Blender or Maya later on, this will cause segfaults when multiple
Wenzel Jakob6eb11da2016-01-17 22:36:36 +0100114 # conflicting Python instances are active at the same time (even when they
115 # are of the same version).
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200116
Wenzel Jakob93296692015-10-13 23:21:54 +0200117 # Windows is not affected by this issue since it handles DLL imports
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200118 # differently. The solution for Linux and Mac OS is simple: we just don't
119 # link against the Python library. The resulting shared library will have
120 # missing symbols, but that's perfectly fine -- they will be resolved at
121 # import time.
122
123 # .SO file extension on Linux/Mac OS
124 set_target_properties(example PROPERTIES SUFFIX ".so")
125
126 # Strip unnecessary sections of the binary on Linux/Mac OS
127 if(APPLE)
128 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
Wenzel Jakob48548ea2016-01-17 22:36:44 +0100129 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup ")
Wenzel Jakob28f98aa2015-10-13 02:57:16 +0200130 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
131 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_BINARY_DIR}/example.so)
132 endif()
133 else()
134 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
135 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_BINARY_DIR}/example.so)
136 endif()
137 endif()
138 endif()