blob: 68020501320ae9dbefd79a05857c1e8863c4bf26 [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)
Wenzel Jakob67a63922016-05-29 12:35:16 +020016endif()
Dean Moldovan8c6b0b82016-05-23 00:12:37 +020017
Dean Moldovan49720f02016-05-26 22:53:38 +020018option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT})
Wenzel Jakob67a63922016-05-29 12:35:16 +020019option(PYBIND11_TEST "Build pybind11 test suite?" ${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})
Trygve Laugstøl9119f132016-08-01 09:14:54 +020058 target_include_directories(${target_name}
59 PRIVATE ${PYBIND11_INCLUDE_DIR}
Wenzel Jakob21608602016-08-02 02:19:35 +020060 PRIVATE ${PYTHON_INCLUDE_DIRS})
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020061
Dean Moldovan4563e9a2016-05-22 22:23:18 +020062 # The prefix and extension are provided by FindPythonLibsNew.cmake
63 set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
64 set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020065
Boris Schälingfc19c192016-05-29 16:56:15 +020066 if(WIN32 OR CYGWIN)
Wenzel Jakob67a63922016-05-29 12:35:16 +020067 # Link against the Python shared library on Windows
Dean Moldovan9fb50c52016-05-27 21:42:43 +020068 target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
Dean Moldovan4563e9a2016-05-22 22:23:18 +020069 elseif(APPLE)
Wenzel Jakob67a63922016-05-29 12:35:16 +020070 # It's quite common to have multiple copies of the same Python version
71 # installed on one's system. E.g.: one copy from the OS and another copy
72 # that's statically linked into an application like Blender or Maya.
73 # If we link our plugin library against the OS Python here and import it
74 # into Blender or Maya later on, this will cause segfaults when multiple
75 # conflicting Python instances are active at the same time (even when they
76 # are of the same version).
77
78 # Windows is not affected by this issue since it handles DLL imports
79 # differently. The solution for Linux and Mac OS is simple: we just don't
80 # link against the Python library. The resulting shared library will have
81 # missing symbols, but that's perfectly fine -- they will be resolved at
82 # import time.
83
Dean Moldovan4563e9a2016-05-22 22:23:18 +020084 target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
85 endif()
86
Dean Moldovan4563e9a2016-05-22 22:23:18 +020087 if(NOT MSVC)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020088 # Make sure C++11/14 are enabled
Dean Moldovanc3c27c42016-05-28 11:08:16 +020089 target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
Dean Moldovan4563e9a2016-05-22 22:23:18 +020090
Dean Moldovan9fb50c52016-05-27 21:42:43 +020091 # Enable link time optimization and set the default symbol
92 # visibility to hidden (very important to obtain small binaries)
Dean Moldovan4563e9a2016-05-22 22:23:18 +020093 string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
94 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020095 # Check for Link Time Optimization support (GCC/Clang)
96 check_cxx_compiler_flag("-flto" HAS_LTO_FLAG)
Wenzel Jakobc48da922016-05-29 12:45:35 +020097 if(HAS_LTO_FLAG AND NOT CYGWIN)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020098 target_compile_options(${target_name} PRIVATE -flto)
99 endif()
100
101 # Intel equivalent to LTO is called IPO
102 if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
103 check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG)
104 if(HAS_IPO_FLAG)
105 target_compile_options(${target_name} PRIVATE -ipo)
106 endif()
107 endif()
108
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200109 # Default symbol visibility
110 target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200111
112 # Strip unnecessary sections of the binary on Linux/Mac OS
113 if(CMAKE_STRIP)
114 if(APPLE)
115 add_custom_command(TARGET ${target_name} POST_BUILD
116 COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>)
117 else()
118 add_custom_command(TARGET ${target_name} POST_BUILD
119 COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
120 endif()
121 endif()
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200122 endif()
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200123 elseif(MSVC)
124 # /MP enables multithreaded builds (relevant when there are many files), /bigobj is
125 # needed for bigger binding projects due to the limit to 64k addressable sections
126 target_compile_options(${target_name} PRIVATE /MP /bigobj)
127
128 # Enforce link time code generation on MSVC, except in debug mode
129 target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>)
Wenzel Jakob67a63922016-05-29 12:35:16 +0200130
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200131 # Fancy generator expressions don't work with linker flags, for reasons unknown
132 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG)
133 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG)
134 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG)
135 endif()
136endfunction()
137
138# Compile with compiler warnings turned on
Wenzel Jakob67a63922016-05-29 12:35:16 +0200139function(pybind11_enable_warnings target_name)
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200140 if(MSVC)
141 target_compile_options(${target_name} PRIVATE /W4)
142 else()
Wenzel Jakobc48da922016-05-29 12:45:35 +0200143 target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion)
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200144 endif()
145endfunction()
Wenzel Jakobe44e56f2016-04-30 22:59:58 +0200146
Dean Moldovan8c6b0b82016-05-23 00:12:37 +0200147if (PYBIND11_TEST)
148 enable_testing()
149 add_subdirectory(example)
Dean Moldovan52ae7b12016-06-01 23:28:44 +0200150 add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION> DEPENDS example)
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200151endif()
152
Wenzel Jakob3350b5e2015-11-24 21:33:18 +0100153if (PYBIND11_INSTALL)
Dean Moldovan8c6b0b82016-05-23 00:12:37 +0200154 set(PYBIND11_HEADERS
155 include/pybind11/attr.h
156 include/pybind11/cast.h
157 include/pybind11/common.h
158 include/pybind11/complex.h
159 include/pybind11/descr.h
160 include/pybind11/eigen.h
161 include/pybind11/functional.h
162 include/pybind11/numpy.h
163 include/pybind11/operators.h
164 include/pybind11/pybind11.h
165 include/pybind11/pytypes.h
166 include/pybind11/stl.h
167 include/pybind11/stl_bind.h
168 include/pybind11/typeid.h
169 )
170
171 install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
Luka Čehovin19af3572015-11-24 21:20:56 +0100172endif()