blob: 2fa32e81a802d8e94dccd6526974abc5e686cb4f [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
17# Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
Wenzel Jakob38bd7112015-07-05 20:05:44 +020018if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
19 message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
20 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
21 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
22 "MinSizeRel" "RelWithDebInfo")
23endif()
Wenzel Jakobbcd31822015-10-12 23:57:20 +020024string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
Wenzel Jakob3b806d42015-10-11 16:29:35 +020025
Wenzel Jakob38bd7112015-07-05 20:05:44 +020026set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6)
Wenzel Jakobb1b71402015-10-18 16:48:30 +020027find_package(PythonLibs ${PYBIND11_PYTHON_VERSION} REQUIRED)
28find_package(PythonInterp ${PYBIND11_PYTHON_VERSION} REQUIRED)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020029
Wenzel Jakobf2331662015-11-28 14:24:44 +010030if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
31 # Enable C++11 mode on C++ / Clang
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020032 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020033
34 # Enable link time optimization and set the default symbol
35 # visibility to hidden (very important to obtain small binaries)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020036 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020037 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto")
Wenzel Jakob38bd7112015-07-05 20:05:44 +020038 endif()
39endif()
40
41# Compile with compiler warnings turned on
42if(MSVC)
43 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
44 string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
45 else()
46 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
47 endif()
Wenzel Jakobf2331662015-11-28 14:24:44 +010048elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
Wenzel Jakob38bd7112015-07-05 20:05:44 +020049 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
50endif()
51
Wenzel Jakobbcd31822015-10-12 23:57:20 +020052# Include path for Python header files
53include_directories(${PYTHON_INCLUDE_DIR})
Wenzel Jakob38bd7112015-07-05 20:05:44 +020054
Wenzel Jakobbcd31822015-10-12 23:57:20 +020055# Include path for pybind11 header files
56include_directories(include)
57
Luka Čehovin19af3572015-11-24 21:20:56 +010058set(PYBIND11_HEADERS
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020059 include/pybind11/cast.h
60 include/pybind11/common.h
61 include/pybind11/operators.h
62 include/pybind11/pybind11.h
63 include/pybind11/pytypes.h
64 include/pybind11/typeid.h
65 include/pybind11/numpy.h
Luka Čehovin19af3572015-11-24 21:20:56 +010066)
67
68# Create the binding library
69add_library(example SHARED
70 ${PYBIND11_HEADERS}
Wenzel Jakob38bd7112015-07-05 20:05:44 +020071 example/example.cpp
72 example/example1.cpp
73 example/example2.cpp
74 example/example3.cpp
75 example/example4.cpp
76 example/example5.cpp
77 example/example6.cpp
78 example/example7.cpp
79 example/example8.cpp
80 example/example9.cpp
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020081 example/example10.cpp
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020082 example/example11.cpp
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020083 example/example12.cpp
Wenzel Jakob38bd7112015-07-05 20:05:44 +020084)
85
Wenzel Jakobbcd31822015-10-12 23:57:20 +020086# Don't add a 'lib' prefix to the shared library
Wenzel Jakob38bd7112015-07-05 20:05:44 +020087set_target_properties(example PROPERTIES PREFIX "")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020088
Wenzel Jakob607654f2015-10-13 23:58:10 +020089# Always write the output file directly into the 'example' directory (even on MSVC)
90set(CompilerFlags
91 LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG
92 LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
93 RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG
94 RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO)
95
96foreach(CompilerFlag ${CompilerFlags})
97 set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
98endforeach()
Wenzel Jakob38bd7112015-07-05 20:05:44 +020099
100if (WIN32)
101 if (MSVC)
Wenzel Jakobbcd31822015-10-12 23:57:20 +0200102 # Enforce size-based optimization and link time code generation
103 # on MSVC (~30% smaller binaries in experiments). /bigobj is needed
104 # for bigger binding projects due to the limit to 64k addressable sections
105 # /MP enables multithreaded builds (relevant when there are many files).
Wenzel Jakob02f770d2015-09-01 21:38:20 +0200106 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200107 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
108 endif()
109
110 # .PYD file extension on Windows
111 set_target_properties(example PROPERTIES SUFFIX ".pyd")
112
113 # Link against the Python shared library
114 target_link_libraries(example ${PYTHON_LIBRARY})
115elseif (UNIX)
116 # It's quite common to have multiple copies of the same Python version
117 # installed on one's system. E.g.: one copy from the OS and another copy
118 # that's statically linked into an application like Blender or Maya.
119 # If we link our plugin library against the OS Python here and import it
120 # into Blender or Maya later on, this will cause segfaults when multiple
121 # conflicting Python instances are active at the same time.
122
Wenzel Jakobbcd31822015-10-12 23:57:20 +0200123 # Windows is not affected by this issue since it handles DLL imports
124 # differently. The solution for Linux and Mac OS is simple: we just don't
125 # link against the Python library. The resulting shared library will have
126 # missing symbols, but that's perfectly fine -- they will be resolved at
127 # import time.
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200128
129 # .SO file extension on Linux/Mac OS
130 set_target_properties(example PROPERTIES SUFFIX ".so")
131
Wenzel Jakob867ae372015-10-15 22:41:25 +0200132 # Optimize for a small binary size
133 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
134 set_target_properties(example PROPERTIES COMPILE_FLAGS "-Os")
135 endif()
136
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200137 # Strip unnecessary sections of the binary on Linux/Mac OS
138 if(APPLE)
139 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
140 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
141 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
142 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
143 endif()
144 else()
145 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
146 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
147 endif()
148 endif()
149endif()
150
151enable_testing()
152set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
Wenzel Jakob6d6fd092015-10-01 17:34:26 +0200153foreach(i RANGE 1 12)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200154 add_test(NAME example${i} COMMAND ${RUN_TEST} example${i})
155endforeach()
Luka Čehovin19af3572015-11-24 21:20:56 +0100156
Wenzel Jakob3350b5e2015-11-24 21:33:18 +0100157if (PYBIND11_INSTALL)
Luka Čehovin19af3572015-11-24 21:20:56 +0100158 install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
159endif()