blob: 82c0785ca45fb9da1faa638a0269d77ee3d01c02 [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 Jakobbcd31822015-10-12 23:57:20 +020012# Add a CMake parameter for choosing a desired Python version
13set(PYBIND_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
14
15# Set a default build configuration if none is specified. 'MinSizeRel' produces the smallest binaries
Wenzel Jakob38bd7112015-07-05 20:05:44 +020016if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
17 message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
18 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
19 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
20 "MinSizeRel" "RelWithDebInfo")
21endif()
Wenzel Jakobbcd31822015-10-12 23:57:20 +020022string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
Wenzel Jakob3b806d42015-10-11 16:29:35 +020023
Wenzel Jakob38bd7112015-07-05 20:05:44 +020024set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6)
Wenzel Jakob3b806d42015-10-11 16:29:35 +020025find_package(PythonLibs ${PYBIND_PYTHON_VERSION} REQUIRED)
26find_package(PythonInterp ${PYBIND_PYTHON_VERSION} REQUIRED)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020027
Wenzel Jakob38bd7112015-07-05 20:05:44 +020028if (UNIX)
Wenzel Jakobbcd31822015-10-12 23:57:20 +020029 # Enable C++11 mode
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020030 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020031
32 # Enable link time optimization and set the default symbol
33 # visibility to hidden (very important to obtain small binaries)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020034 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020035 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto")
Wenzel Jakob38bd7112015-07-05 20:05:44 +020036 endif()
37endif()
38
39# Compile with compiler warnings turned on
40if(MSVC)
41 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
42 string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
43 else()
44 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
45 endif()
46else()
47 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
48endif()
49
Wenzel Jakobbcd31822015-10-12 23:57:20 +020050# Include path for Python header files
51include_directories(${PYTHON_INCLUDE_DIR})
Wenzel Jakob38bd7112015-07-05 20:05:44 +020052
Wenzel Jakobbcd31822015-10-12 23:57:20 +020053# Include path for pybind11 header files
54include_directories(include)
55
56# Create the binding library
Wenzel Jakob38bd7112015-07-05 20:05:44 +020057add_library(example SHARED
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020058 include/pybind11/cast.h
59 include/pybind11/common.h
60 include/pybind11/operators.h
61 include/pybind11/pybind11.h
62 include/pybind11/pytypes.h
63 include/pybind11/typeid.h
64 include/pybind11/numpy.h
Wenzel Jakob38bd7112015-07-05 20:05:44 +020065 example/example.cpp
66 example/example1.cpp
67 example/example2.cpp
68 example/example3.cpp
69 example/example4.cpp
70 example/example5.cpp
71 example/example6.cpp
72 example/example7.cpp
73 example/example8.cpp
74 example/example9.cpp
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020075 example/example10.cpp
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020076 example/example11.cpp
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020077 example/example12.cpp
Wenzel Jakob38bd7112015-07-05 20:05:44 +020078)
79
Wenzel Jakobbcd31822015-10-12 23:57:20 +020080# Don't add a 'lib' prefix to the shared library
Wenzel Jakob38bd7112015-07-05 20:05:44 +020081set_target_properties(example PROPERTIES PREFIX "")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020082
Wenzel Jakob607654f2015-10-13 23:58:10 +020083# Always write the output file directly into the 'example' directory (even on MSVC)
84set(CompilerFlags
85 LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_RELEASE LIBRARY_OUTPUT_DIRECTORY_DEBUG
86 LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO
87 RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_RELEASE RUNTIME_OUTPUT_DIRECTORY_DEBUG
88 RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO)
89
90foreach(CompilerFlag ${CompilerFlags})
91 set_target_properties(example PROPERTIES ${CompilerFlag} ${PROJECT_SOURCE_DIR}/example)
92endforeach()
Wenzel Jakob38bd7112015-07-05 20:05:44 +020093
94if (WIN32)
95 if (MSVC)
Wenzel Jakobbcd31822015-10-12 23:57:20 +020096 # Enforce size-based optimization and link time code generation
97 # on MSVC (~30% smaller binaries in experiments). /bigobj is needed
98 # for bigger binding projects due to the limit to 64k addressable sections
99 # /MP enables multithreaded builds (relevant when there are many files).
Wenzel Jakob02f770d2015-09-01 21:38:20 +0200100 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200101 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
102 endif()
103
104 # .PYD file extension on Windows
105 set_target_properties(example PROPERTIES SUFFIX ".pyd")
106
107 # Link against the Python shared library
108 target_link_libraries(example ${PYTHON_LIBRARY})
109elseif (UNIX)
110 # It's quite common to have multiple copies of the same Python version
111 # installed on one's system. E.g.: one copy from the OS and another copy
112 # that's statically linked into an application like Blender or Maya.
113 # If we link our plugin library against the OS Python here and import it
114 # into Blender or Maya later on, this will cause segfaults when multiple
115 # conflicting Python instances are active at the same time.
116
Wenzel Jakobbcd31822015-10-12 23:57:20 +0200117 # Windows is not affected by this issue since it handles DLL imports
118 # 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.
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200122
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 ".")
129 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
130 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
131 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/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_SOURCE_DIR}/example/example.so)
136 endif()
137 endif()
138endif()
139
140enable_testing()
141set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
Wenzel Jakob6d6fd092015-10-01 17:34:26 +0200142foreach(i RANGE 1 12)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200143 add_test(NAME example${i} COMMAND ${RUN_TEST} example${i})
144endforeach()