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