blob: b1f4436b7361c7f3706f205b15043d7d4b47b131 [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
Dean Moldovan4563e9a2016-05-22 22:23:18 +02008cmake_minimum_required(VERSION 2.8.12)
Wenzel Jakob38bd7112015-07-05 20:05:44 +02009
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020010project(pybind11)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020011
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020012# Check if pybind11 is being used directly or via add_subdirectory
Dean Moldovan49720f02016-05-26 22:53:38 +020013set(PYBIND11_MASTER_PROJECT OFF)
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020014if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
Dean Moldovan49720f02016-05-26 22:53:38 +020015 set(PYBIND11_MASTER_PROJECT ON)
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020016endif ()
17
Dean Moldovan49720f02016-05-26 22:53:38 +020018option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
19option(PYBIND11_TEST "Build tests?" ${PYBIND11_MASTER_PROJECT})
Wenzel Jakob3350b5e2015-11-24 21:33:18 +010020
Wenzel Jakobbcd31822015-10-12 23:57:20 +020021# Add a CMake parameter for choosing a desired Python version
Wenzel Jakobb1b71402015-10-18 16:48:30 +020022set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling the example application")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020023
Dean Moldovan928fff62016-05-22 19:48:47 +020024list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010025set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
Dean Moldovan928fff62016-05-22 19:48:47 +020026find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
Wenzel Jakobfaaee1c2015-11-30 10:17:01 +010027
Dean Moldovanc3c27c42016-05-28 11:08:16 +020028include(CheckCXXCompilerFlag)
29
30if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD)
31 check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
32 check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
33
34 if (HAS_CPP14_FLAG)
35 set(PYBIND11_CPP_STANDARD -std=c++14)
36 elseif (HAS_CPP11_FLAG)
37 set(PYBIND11_CPP_STANDARD -std=c++11)
38 else()
39 message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
40 endif()
41
42 set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
43 "C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available.")
44endif()
45
Dean Moldovan4563e9a2016-05-22 22:23:18 +020046# Cache variables so pybind11_add_module can be used in parent projects
47set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
48set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
Dean Moldovan03d6a512016-05-25 13:39:32 +020049set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
Dean Moldovan4563e9a2016-05-22 22:23:18 +020050set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
51set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020052
Dean Moldovan4563e9a2016-05-22 22:23:18 +020053# Build a Python extension module:
54# pybind11_add_module(<name> source1 [source2 ...])
55#
56function(pybind11_add_module target_name)
57 add_library(${target_name} MODULE ${ARGN})
58 target_include_directories(${target_name} PUBLIC ${PYBIND11_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS})
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020059
Dean Moldovan4563e9a2016-05-22 22:23:18 +020060 # The prefix and extension are provided by FindPythonLibsNew.cmake
61 set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
62 set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020063
Dean Moldovan4563e9a2016-05-22 22:23:18 +020064 # It's quite common to have multiple copies of the same Python version
65 # installed on one's system. E.g.: one copy from the OS and another copy
66 # that's statically linked into an application like Blender or Maya.
67 # If we link our plugin library against the OS Python here and import it
68 # into Blender or Maya later on, this will cause segfaults when multiple
69 # conflicting Python instances are active at the same time (even when they
70 # are of the same version).
71
72 # Windows is not affected by this issue since it handles DLL imports
73 # differently. The solution for Linux and Mac OS is simple: we just don't
74 # link against the Python library. The resulting shared library will have
75 # missing symbols, but that's perfectly fine -- they will be resolved at
76 # import time.
77 if(MSVC)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020078 target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
Dean Moldovan4563e9a2016-05-22 22:23:18 +020079 elseif(APPLE)
80 # Make sure OS X does not have any issues with missing symbols
Dean Moldovan4563e9a2016-05-22 22:23:18 +020081 target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
82 endif()
83
Dean Moldovan4563e9a2016-05-22 22:23:18 +020084 if(NOT MSVC)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020085 # Make sure C++11/14 are enabled
Dean Moldovanc3c27c42016-05-28 11:08:16 +020086 target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
Dean Moldovan4563e9a2016-05-22 22:23:18 +020087
Dean Moldovan9fb50c52016-05-27 21:42:43 +020088 # Enable link time optimization and set the default symbol
89 # visibility to hidden (very important to obtain small binaries)
Dean Moldovan4563e9a2016-05-22 22:23:18 +020090 string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
91 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020092 # Check for Link Time Optimization support (GCC/Clang)
93 check_cxx_compiler_flag("-flto" HAS_LTO_FLAG)
94 if(HAS_LTO_FLAG)
95 target_compile_options(${target_name} PRIVATE -flto)
96 endif()
97
98 # Intel equivalent to LTO is called IPO
99 if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
100 check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG)
101 if(HAS_IPO_FLAG)
102 target_compile_options(${target_name} PRIVATE -ipo)
103 endif()
104 endif()
105
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200106 # Default symbol visibility
107 target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200108
109 # Strip unnecessary sections of the binary on Linux/Mac OS
110 if(CMAKE_STRIP)
111 if(APPLE)
112 add_custom_command(TARGET ${target_name} POST_BUILD
113 COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>)
114 else()
115 add_custom_command(TARGET ${target_name} POST_BUILD
116 COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
117 endif()
118 endif()
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200119 endif()
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200120 elseif(MSVC)
121 # /MP enables multithreaded builds (relevant when there are many files), /bigobj is
122 # needed for bigger binding projects due to the limit to 64k addressable sections
123 target_compile_options(${target_name} PRIVATE /MP /bigobj)
124
125 # Enforce link time code generation on MSVC, except in debug mode
126 target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>)
127 # Fancy generator expressions don't work with linker flags, for reasons unknown
128 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG)
129 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG)
130 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG)
131 endif()
132endfunction()
133
134# Compile with compiler warnings turned on
135function(pybind11_turn_on_warnings target_name)
136 if(MSVC)
137 target_compile_options(${target_name} PRIVATE /W4)
138 else()
139 target_compile_options(${target_name} PRIVATE -Wall -Wextra)
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200140 endif()
141endfunction()
Wenzel Jakobe44e56f2016-04-30 22:59:58 +0200142
Dean Moldovan8c6b0b82016-05-23 00:12:37 +0200143if (PYBIND11_TEST)
144 enable_testing()
145 add_subdirectory(example)
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200146endif()
147
Wenzel Jakob3350b5e2015-11-24 21:33:18 +0100148if (PYBIND11_INSTALL)
Dean Moldovan8c6b0b82016-05-23 00:12:37 +0200149 set(PYBIND11_HEADERS
150 include/pybind11/attr.h
151 include/pybind11/cast.h
152 include/pybind11/common.h
153 include/pybind11/complex.h
154 include/pybind11/descr.h
155 include/pybind11/eigen.h
156 include/pybind11/functional.h
157 include/pybind11/numpy.h
158 include/pybind11/operators.h
159 include/pybind11/pybind11.h
160 include/pybind11/pytypes.h
161 include/pybind11/stl.h
162 include/pybind11/stl_bind.h
163 include/pybind11/typeid.h
164 )
165
166 install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
Luka Čehovin19af3572015-11-24 21:20:56 +0100167endif()