blob: 574f9fc37f81c088dcfcf336bf32e97e32fa542f [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)
Wenzel Jakob57082212015-09-04 23:42:12 +020020find_package(PythonLibs REQUIRED)
21find_package(PythonInterp REQUIRED)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020022
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 Jakoba2f6fde2015-10-01 16:46:03 +020064 example/example12.cpp
Wenzel Jakob38bd7112015-07-05 20:05:44 +020065)
66
67set_target_properties(example PROPERTIES PREFIX "")
68set_target_properties(example PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/example)
69
70if (WIN32)
71 if (MSVC)
72 # Enforce size-based optimization and link time code generation on MSVC (~30% smaller binaries in experiments)
Wenzel Jakob02f770d2015-09-01 21:38:20 +020073 # /bigobj is needed for bigger binding projects due to the limit to 64k addressable sections
74 # /MP enables multithreaded builds
75 set_target_properties(example PROPERTIES COMPILE_FLAGS "/Os /GL /MP /bigobj")
Wenzel Jakob38bd7112015-07-05 20:05:44 +020076 set_target_properties(example PROPERTIES LINK_FLAGS "/LTCG")
77 endif()
78
79 # .PYD file extension on Windows
80 set_target_properties(example PROPERTIES SUFFIX ".pyd")
81
82 # Link against the Python shared library
83 target_link_libraries(example ${PYTHON_LIBRARY})
84elseif (UNIX)
85 # It's quite common to have multiple copies of the same Python version
86 # installed on one's system. E.g.: one copy from the OS and another copy
87 # that's statically linked into an application like Blender or Maya.
88 # If we link our plugin library against the OS Python here and import it
89 # into Blender or Maya later on, this will cause segfaults when multiple
90 # conflicting Python instances are active at the same time.
91
92 # Windows does not seem to be affected by this issue. The solution for Linux
93 # and Mac OS is simple: we just don't link against the Python library. The
94 # resulting shared library will have missing symbols, but that's perfectly
95 # fine -- they will be resolved at import time.
96
97 # .SO file extension on Linux/Mac OS
98 set_target_properties(example PROPERTIES SUFFIX ".so")
99
100 # Strip unnecessary sections of the binary on Linux/Mac OS
101 if(APPLE)
102 set_target_properties(example PROPERTIES MACOSX_RPATH ".")
103 set_target_properties(example PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
104 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
105 add_custom_command(TARGET example POST_BUILD COMMAND strip -u -r ${PROJECT_SOURCE_DIR}/example/example.so)
106 endif()
107 else()
108 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
109 add_custom_command(TARGET example POST_BUILD COMMAND strip ${PROJECT_SOURCE_DIR}/example/example.so)
110 endif()
111 endif()
112endif()
113
114enable_testing()
115set(RUN_TEST ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/example/run_test.py)
Wenzel Jakob6d6fd092015-10-01 17:34:26 +0200116foreach(i RANGE 1 12)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200117 add_test(NAME example${i} COMMAND ${RUN_TEST} example${i})
118endforeach()