blob: a0ceccf649e9ca3ce57329b7441f4c5b29d0e30c [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
10project(pybind)
11
12if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
13 message(STATUS "Setting build type to 'MinSizeRel' as none was specified.")
14 set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
15 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
16 "MinSizeRel" "RelWithDebInfo")
17endif()
18
Wenzel Jakob3b806d42015-10-11 16:29:35 +020019set(PYBIND_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
20
Wenzel Jakob38bd7112015-07-05 20:05:44 +020021set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6)
Wenzel Jakob3b806d42015-10-11 16:29:35 +020022find_package(PythonLibs ${PYBIND_PYTHON_VERSION} REQUIRED)
23find_package(PythonInterp ${PYBIND_PYTHON_VERSION} REQUIRED)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020024
25string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
26if (UNIX)
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020027 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Wenzel Jakob38bd7112015-07-05 20:05:44 +020028 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020029 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -flto")
Wenzel Jakob38bd7112015-07-05 20:05:44 +020030 endif()
31endif()
32
33# Compile with compiler warnings turned on
34if(MSVC)
35 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
36 string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
37 else()
38 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
39 endif()
40else()
41 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
42endif()
43
44include_directories(${PYTHON_INCLUDE_DIR} include)
45
46add_library(example SHARED
47 include/pybind/cast.h
48 include/pybind/common.h
Wenzel Jakob38bd7112015-07-05 20:05:44 +020049 include/pybind/operators.h
50 include/pybind/pybind.h
51 include/pybind/pytypes.h
52 include/pybind/typeid.h
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020053 include/pybind/numpy.h
Wenzel Jakob38bd7112015-07-05 20:05:44 +020054 example/example.cpp
55 example/example1.cpp
56 example/example2.cpp
57 example/example3.cpp
58 example/example4.cpp
59 example/example5.cpp
60 example/example6.cpp
61 example/example7.cpp
62 example/example8.cpp
63 example/example9.cpp
Wenzel Jakobd4258ba2015-07-26 16:33:49 +020064 example/example10.cpp
Wenzel Jakoba576e6a2015-07-29 17:51:54 +020065 example/example11.cpp
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020066 example/example12.cpp
Wenzel Jakob38bd7112015-07-05 20:05:44 +020067)
68
69set_target_properties(example PROPERTIES PREFIX "")
70set_target_properties(example PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/example)
71
72if (WIN32)
73 if (MSVC)
74 # Enforce size-based optimization and link time code generation on MSVC (~30% smaller binaries in experiments)
Wenzel Jakob02f770d2015-09-01 21:38:20 +020075 # /bigobj is needed for bigger binding projects due to the limit to 64k addressable sections
76 # /MP enables multithreaded builds
77 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
Wenzel Jakob38bd7112015-07-05 20:05:44 +020078 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
79 endif()
80
81 # .PYD file extension on Windows
82 set_target_properties(example PROPERTIES SUFFIX ".pyd")
83
84 # Link against the Python shared library
85 target_link_libraries(example ${PYTHON_LIBRARY})
86elseif (UNIX)
87 # It's quite common to have multiple copies of the same Python version
88 # installed on one's system. E.g.: one copy from the OS and another copy
89 # that's statically linked into an application like Blender or Maya.
90 # If we link our plugin library against the OS Python here and import it
91 # into Blender or Maya later on, this will cause segfaults when multiple
92 # conflicting Python instances are active at the same time.
93
94 # Windows does not seem to be affected by this issue. The solution for Linux
95 # and Mac OS is simple: we just don't link against the Python library. The
96 # resulting shared library will have missing symbols, but that's perfectly
97 # fine -- they will be resolved at import time.
98
99 # .SO file extension on Linux/Mac OS
100 set_target_properties(example PROPERTIES SUFFIX ".so")
101
102 # Strip unnecessary sections of the binary on Linux/Mac OS
103 if(APPLE)
104 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
105 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
106 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
107 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
108 endif()
109 else()
110 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
111 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
112 endif()
113 endif()
114endif()
115
116enable_testing()
117set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
Wenzel Jakob6d6fd092015-10-01 17:34:26 +0200118foreach(i RANGE 1 12)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200119 add_test(NAME example${i} COMMAND ${RUN_TEST} example${i})
120endforeach()