blob: 5d255ea88db63f93f35db703702a3ea3dfec73aa [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)
25 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-unsequenced")
26 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)
72 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL")
73 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
74 endif()
75
76 # .PYD file extension on Windows
77 set_target_properties(example PROPERTIES SUFFIX ".pyd")
78
79 # Link against the Python shared library
80 target_link_libraries(example ${PYTHON_LIBRARY})
81elseif (UNIX)
82 # It's quite common to have multiple copies of the same Python version
83 # installed on one's system. E.g.: one copy from the OS and another copy
84 # that's statically linked into an application like Blender or Maya.
85 # If we link our plugin library against the OS Python here and import it
86 # into Blender or Maya later on, this will cause segfaults when multiple
87 # conflicting Python instances are active at the same time.
88
89 # Windows does not seem to be affected by this issue. The solution for Linux
90 # and Mac OS is simple: we just don't link against the Python library. The
91 # resulting shared library will have missing symbols, but that's perfectly
92 # fine -- they will be resolved at import time.
93
94 # .SO file extension on Linux/Mac OS
95 set_target_properties(example PROPERTIES SUFFIX ".so")
96
97 # Strip unnecessary sections of the binary on Linux/Mac OS
98 if(APPLE)
99 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
100 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
101 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
102 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
103 endif()
104 else()
105 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
106 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
107 endif()
108 endif()
109endif()
110
111enable_testing()
112set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
113foreach(i RANGE 1 7)
114 add_test(NAME example${i} COMMAND ${RUN_TEST} example${i})
115endforeach()