blob: 48d3cc7e8402ae1b6ead2b69bfe80ebdf93640a7 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001# CMakeLists.txt -- Build system for the pybind11 modules
Wenzel Jakob38bd7112015-07-05 20:05:44 +02002#
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})
Dean Moldovan00a30092016-08-15 13:41:44 +020020option(PYBIND11_WERROR "Report all warnings as errors" OFF)
Wenzel Jakob3350b5e2015-11-24 21:33:18 +010021
Wenzel Jakobbcd31822015-10-12 23:57:20 +020022# Add a CMake parameter for choosing a desired Python version
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020023set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020024
Dean Moldovan928fff62016-05-22 19:48:47 +020025list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/tools")
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010026set(Python_ADDITIONAL_VERSIONS 3.4 3.5 3.6 3.7)
Dean Moldovan928fff62016-05-22 19:48:47 +020027find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
Wenzel Jakobfaaee1c2015-11-30 10:17:01 +010028
Dean Moldovanc3c27c42016-05-28 11:08:16 +020029include(CheckCXXCompilerFlag)
30
31if(NOT MSVC AND NOT PYBIND11_CPP_STANDARD)
32 check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
33 check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
34
35 if (HAS_CPP14_FLAG)
36 set(PYBIND11_CPP_STANDARD -std=c++14)
37 elseif (HAS_CPP11_FLAG)
38 set(PYBIND11_CPP_STANDARD -std=c++11)
39 else()
40 message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
41 endif()
42
43 set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
Dean Moldovanb62a8962016-08-25 22:35:15 +020044 "C++ standard flag, e.g. -std=c++11 or -std=c++14. Defaults to latest available." FORCE)
Dean Moldovanc3c27c42016-05-28 11:08:16 +020045endif()
46
Dean Moldovan4563e9a2016-05-22 22:23:18 +020047# Cache variables so pybind11_add_module can be used in parent projects
48set(PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include" CACHE INTERNAL "")
49set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "")
Dean Moldovan03d6a512016-05-25 13:39:32 +020050set(PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "")
Dean Moldovan4563e9a2016-05-22 22:23:18 +020051set(PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "")
52set(PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "")
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020053
Dean Moldovan4563e9a2016-05-22 22:23:18 +020054# Build a Python extension module:
55# pybind11_add_module(<name> source1 [source2 ...])
56#
57function(pybind11_add_module target_name)
58 add_library(${target_name} MODULE ${ARGN})
Trygve Laugstøl9119f132016-08-01 09:14:54 +020059 target_include_directories(${target_name}
60 PRIVATE ${PYBIND11_INCLUDE_DIR}
Wenzel Jakob21608602016-08-02 02:19:35 +020061 PRIVATE ${PYTHON_INCLUDE_DIRS})
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020062
Dean Moldovan4563e9a2016-05-22 22:23:18 +020063 # The prefix and extension are provided by FindPythonLibsNew.cmake
64 set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}")
65 set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}")
Wenzel Jakobbcd31822015-10-12 23:57:20 +020066
Boris Schälingfc19c192016-05-29 16:56:15 +020067 if(WIN32 OR CYGWIN)
Wenzel Jakob67a63922016-05-29 12:35:16 +020068 # Link against the Python shared library on Windows
Dean Moldovan9fb50c52016-05-27 21:42:43 +020069 target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES})
Dean Moldovan4563e9a2016-05-22 22:23:18 +020070 elseif(APPLE)
Wenzel Jakob67a63922016-05-29 12:35:16 +020071 # It's quite common to have multiple copies of the same Python version
72 # installed on one's system. E.g.: one copy from the OS and another copy
73 # that's statically linked into an application like Blender or Maya.
74 # If we link our plugin library against the OS Python here and import it
75 # into Blender or Maya later on, this will cause segfaults when multiple
76 # conflicting Python instances are active at the same time (even when they
77 # are of the same version).
78
79 # Windows is not affected by this issue since it handles DLL imports
80 # differently. The solution for Linux and Mac OS is simple: we just don't
81 # link against the Python library. The resulting shared library will have
82 # missing symbols, but that's perfectly fine -- they will be resolved at
83 # import time.
84
Dean Moldovan4563e9a2016-05-22 22:23:18 +020085 target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup")
86 endif()
87
Dean Moldovan4563e9a2016-05-22 22:23:18 +020088 if(NOT MSVC)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020089 # Make sure C++11/14 are enabled
Dean Moldovanc3c27c42016-05-28 11:08:16 +020090 target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
Dean Moldovan4563e9a2016-05-22 22:23:18 +020091
Dean Moldovan9fb50c52016-05-27 21:42:43 +020092 # Enable link time optimization and set the default symbol
93 # visibility to hidden (very important to obtain small binaries)
Dean Moldovan4563e9a2016-05-22 22:23:18 +020094 string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
95 if (NOT ${U_CMAKE_BUILD_TYPE} MATCHES DEBUG)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020096 # Check for Link Time Optimization support (GCC/Clang)
97 check_cxx_compiler_flag("-flto" HAS_LTO_FLAG)
Wenzel Jakobc48da922016-05-29 12:45:35 +020098 if(HAS_LTO_FLAG AND NOT CYGWIN)
Dean Moldovan9fb50c52016-05-27 21:42:43 +020099 target_compile_options(${target_name} PRIVATE -flto)
100 endif()
101
102 # Intel equivalent to LTO is called IPO
103 if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
104 check_cxx_compiler_flag("-ipo" HAS_IPO_FLAG)
105 if(HAS_IPO_FLAG)
106 target_compile_options(${target_name} PRIVATE -ipo)
107 endif()
108 endif()
109
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200110 # Default symbol visibility
111 target_compile_options(${target_name} PRIVATE "-fvisibility=hidden")
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200112
113 # Strip unnecessary sections of the binary on Linux/Mac OS
114 if(CMAKE_STRIP)
115 if(APPLE)
116 add_custom_command(TARGET ${target_name} POST_BUILD
117 COMMAND ${CMAKE_STRIP} -u -r $<TARGET_FILE:${target_name}>)
118 else()
119 add_custom_command(TARGET ${target_name} POST_BUILD
120 COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>)
121 endif()
122 endif()
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200123 endif()
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200124 elseif(MSVC)
125 # /MP enables multithreaded builds (relevant when there are many files), /bigobj is
126 # needed for bigger binding projects due to the limit to 64k addressable sections
127 target_compile_options(${target_name} PRIVATE /MP /bigobj)
128
129 # Enforce link time code generation on MSVC, except in debug mode
130 target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/GL>)
Wenzel Jakob67a63922016-05-29 12:35:16 +0200131
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200132 # Fancy generator expressions don't work with linker flags, for reasons unknown
133 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELEASE /LTCG)
134 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_MINSIZEREL /LTCG)
135 set_property(TARGET ${target_name} APPEND_STRING PROPERTY LINK_FLAGS_RELWITHDEBINFO /LTCG)
136 endif()
137endfunction()
138
139# Compile with compiler warnings turned on
Wenzel Jakob67a63922016-05-29 12:35:16 +0200140function(pybind11_enable_warnings target_name)
Dean Moldovan9fb50c52016-05-27 21:42:43 +0200141 if(MSVC)
142 target_compile_options(${target_name} PRIVATE /W4)
143 else()
Wenzel Jakobc48da922016-05-29 12:45:35 +0200144 target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion)
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200145 endif()
Dean Moldovan00a30092016-08-15 13:41:44 +0200146
147 if(PYBIND11_WERROR)
148 if(MSVC)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200149 target_compile_options(${target_name} PRIVATE /WX)
Dean Moldovan00a30092016-08-15 13:41:44 +0200150 else()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200151 target_compile_options(${target_name} PRIVATE -Werror)
Dean Moldovan00a30092016-08-15 13:41:44 +0200152 endif()
153 endif()
Dean Moldovan4563e9a2016-05-22 22:23:18 +0200154endfunction()
Wenzel Jakobe44e56f2016-04-30 22:59:58 +0200155
Wenzel Jakobdac38582016-09-29 21:30:00 +0200156set(PYBIND11_HEADERS
157 include/pybind11/attr.h
158 include/pybind11/cast.h
159 include/pybind11/chrono.h
160 include/pybind11/common.h
161 include/pybind11/complex.h
162 include/pybind11/descr.h
163 include/pybind11/eigen.h
164 include/pybind11/eval.h
165 include/pybind11/functional.h
166 include/pybind11/numpy.h
167 include/pybind11/operators.h
168 include/pybind11/pybind11.h
169 include/pybind11/pytypes.h
170 include/pybind11/stl.h
171 include/pybind11/stl_bind.h
172 include/pybind11/typeid.h
173)
174string(REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR}/include/"
175 PYBIND11_HEADERS "${PYBIND11_HEADERS}")
176
Dean Moldovan8c6b0b82016-05-23 00:12:37 +0200177if (PYBIND11_TEST)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200178 add_subdirectory(tests)
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200179endif()
180
Wenzel Jakob3350b5e2015-11-24 21:33:18 +0100181if (PYBIND11_INSTALL)
Dean Moldovan8c6b0b82016-05-23 00:12:37 +0200182 install(FILES ${PYBIND11_HEADERS} DESTINATION include/pybind11)
Luka Čehovin19af3572015-11-24 21:20:56 +0100183endif()