blob: b1fcb9bb8a2ed72c7be24aabd3091de8913534fd [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001# 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
8cmake_minimum_required(VERSION 2.8)
9
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020010project(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020011
Wenzel Jakob3350b5e2015-11-24 21:33:18 +010012option(PYBIND11_INSTALL "Install pybind11 header files?" ON)
13
Wenzel Jakobbcd31822015-10-12 23:57:20 +020014# Add a CMake parameter for choosing a desired Python version
Wenzel Jakobb1b71402015-10-18 16:48:30 +020015set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020016
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010017include(CheckCXXCompilerFlag)
18
Wenzel Jakobbcd31822015-10-12 23:57:20 +020019# Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
Wenzel Jakob38bd7112015-07-05 20:05:44 +020020if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
21 message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
22 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
23 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
24 "MinSizeRel" "RelWithDebInfo")
25endif()
Wenzel Jakobbcd31822015-10-12 23:57:20 +020026string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
Wenzel Jakob3b806d42015-10-11 16:29:35 +020027
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010028set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
29if (NOT ${PYBIND11_PYTHON_VERSION} STREQUAL "")
30 find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} EXACT)
31 if (NOT PythonLibs_FOUND)
32 find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED)
33 endif()
34else()
35 find_package(PythonLibs REQUIRED)
36endif()
37find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} EXACT REQUIRED)
Wenzel Jakobfaaee1c2015-11-30 10:17:01 +010038
39if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
Wenzel Jakob66c9a402016-01-17 22:36:36 +010040 CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
41 CHECK_CXX_COMPILER_FLAG("-std=c++11" HAS_CPP11_FLAG)
42
43 if (HAS_CPP14_FLAG)
44 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
45 elseif (HAS_CPP11_FLAG)
46 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
47 else()
48 message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
49 endif()
Wenzel Jakobbcd31822015-10-12 23:57:20 +020050
51 # Enable link time optimization and set the default symbol
52 # visibility to hidden (very important to obtain small binaries)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020053 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Wenzel Jakobfaaee1c2015-11-30 10:17:01 +010054 # Default symbol visibility
55 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
56
57 # Check for Link Time Optimization support
58 CHECK_CXX_COMPILER_FLAG("-flto" HAS_LTO_FLAG)
59 if (HAS_LTO_FLAG)
60 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
61 endif()
Wenzel Jakob38bd7112015-07-05 20:05:44 +020062 endif()
63endif()
64
65# Compile with compiler warnings turned on
66if(MSVC)
67 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
68 string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
69 else()
70 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
71 endif()
Wenzel Jakobf2331662015-11-28 14:24:44 +010072elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
Wenzel Jakob38bd7112015-07-05 20:05:44 +020073 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
74endif()
75
Wenzel Jakobbcd31822015-10-12 23:57:20 +020076# Include path for Python header files
77include_directories(${PYTHON_INCLUDE_DIR})
Wenzel Jakob38bd7112015-07-05 20:05:44 +020078
Wenzel Jakobbcd31822015-10-12 23:57:20 +020079# Include path for pybind11 header files
80include_directories(include)
81
Luka Čehovin19af3572015-11-24 21:20:56 +010082set(PYBIND11_HEADERS
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020083 include/pybind11/cast.h
84 include/pybind11/common.h
Wenzel Jakob66c9a402016-01-17 22:36:36 +010085 include/pybind11/complex.h
86 include/pybind11/descr.h
87 include/pybind11/functional.h
88 include/pybind11/numpy.h
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020089 include/pybind11/operators.h
90 include/pybind11/pybind11.h
91 include/pybind11/pytypes.h
Wenzel Jakobe52cf8a2015-12-15 11:32:29 +010092 include/pybind11/stl.h
Wenzel Jakob66c9a402016-01-17 22:36:36 +010093 include/pybind11/typeid.h
Luka Čehovin19af3572015-11-24 21:20:56 +010094)
95
96# Create the binding library
97add_library(example SHARED
98 ${PYBIND11_HEADERS}
Wenzel Jakob38bd7112015-07-05 20:05:44 +020099 example/example.cpp
100 example/example1.cpp
101 example/example2.cpp
102 example/example3.cpp
103 example/example4.cpp
104 example/example5.cpp
105 example/example6.cpp
106 example/example7.cpp
107 example/example8.cpp
108 example/example9.cpp
Wenzel Jakobd4258ba2015-07-26 16:33:49 +0200109 example/example10.cpp
Wenzel Jakoba576e6a2015-07-29 17:51:54 +0200110 example/example11.cpp
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +0200111 example/example12.cpp
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200112)
113
Wenzel Jakobbcd31822015-10-12 23:57:20 +0200114# Don't add a 'lib' prefix to the shared library
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200115set_target_properties(example PROPERTIES PREFIX "")
Wenzel Jakobbcd31822015-10-12 23:57:20 +0200116
Wenzel Jakob607654f2015-10-13 23:58:10 +0200117# Always write the output file directly into the 'example' directory (even on MSVC)
118set(CompilerFlags
119 LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG
120 LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
121 RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG
122 RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO)
123
124foreach(CompilerFlag ${CompilerFlags})
125 set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
126endforeach()
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200127
128if (WIN32)
129 if (MSVC)
Wenzel Jakobbcd31822015-10-12 23:57:20 +0200130 # Enforce size-based optimization and link time code generation
131 # on MSVC (~30% smaller binaries in experiments). /bigobj is needed
132 # for bigger binding projects due to the limit to 64k addressable sections
133 # /MP enables multithreaded builds (relevant when there are many files).
Wenzel Jakob02f770d2015-09-01 21:38:20 +0200134 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200135 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
136 endif()
137
138 # .PYD file extension on Windows
139 set_target_properties(example PROPERTIES SUFFIX ".pyd")
140
141 # Link against the Python shared library
142 target_link_libraries(example ${PYTHON_LIBRARY})
143elseif (UNIX)
144 # It's quite common to have multiple copies of the same Python version
145 # installed on one's system. E.g.: one copy from the OS and another copy
146 # that's statically linked into an application like Blender or Maya.
147 # If we link our plugin library against the OS Python here and import it
148 # into Blender or Maya later on, this will cause segfaults when multiple
Wenzel Jakob6eb11da2016-01-17 22:36:36 +0100149 # conflicting Python instances are active at the same time (even when they
150 # are of the same version).
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200151
Wenzel Jakobbcd31822015-10-12 23:57:20 +0200152 # Windows is not affected by this issue since it handles DLL imports
153 # differently. The solution for Linux and Mac OS is simple: we just don't
154 # link against the Python library. The resulting shared library will have
155 # missing symbols, but that's perfectly fine -- they will be resolved at
156 # import time.
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200157
158 # .SO file extension on Linux/Mac OS
159 set_target_properties(example PROPERTIES SUFFIX ".so")
160
Wenzel Jakob867ae372015-10-15 22:41:25 +0200161 # Optimize for a small binary size
162 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
163 set_target_properties(example PROPERTIES COMPILE_FLAGS "-Os")
164 endif()
165
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200166 # Strip unnecessary sections of the binary on Linux/Mac OS
167 if(APPLE)
168 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
169 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
170 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
171 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
172 endif()
173 else()
174 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
175 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
176 endif()
177 endif()
178endif()
179
180enable_testing()
181set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
Wenzel Jakob6d6fd092015-10-01 17:34:26 +0200182foreach(i RANGE 1 12)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200183 add_test(NAME example${i} COMMAND ${RUN_TEST} example${i})
184endforeach()
Luka Čehovin19af3572015-11-24 21:20:56 +0100185
Wenzel Jakob3350b5e2015-11-24 21:33:18 +0100186if (PYBIND11_INSTALL)
Luka Čehovin19af3572015-11-24 21:20:56 +0100187 install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
188endif()