pybind11Tools.cmake revision 14299:2fbea9df56d2
111730Sar4jc@virginia.edu# tools/pybind11Tools.cmake -- Build system for the pybind11 modules 211730Sar4jc@virginia.edu# 311730Sar4jc@virginia.edu# Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch> 411730Sar4jc@virginia.edu# 511730Sar4jc@virginia.edu# All rights reserved. Use of this source code is governed by a 611730Sar4jc@virginia.edu# BSD-style license that can be found in the LICENSE file. 711730Sar4jc@virginia.edu 811730Sar4jc@virginia.educmake_minimum_required(VERSION 2.8.12) 911730Sar4jc@virginia.edu 1011730Sar4jc@virginia.edu# Add a CMake parameter for choosing a desired Python version 1111730Sar4jc@virginia.eduif(NOT PYBIND11_PYTHON_VERSION) 1211730Sar4jc@virginia.edu set(PYBIND11_PYTHON_VERSION "" CACHE STRING "Python version to use for compiling modules") 1311730Sar4jc@virginia.eduendif() 1411730Sar4jc@virginia.edu 1511730Sar4jc@virginia.eduset(Python_ADDITIONAL_VERSIONS 3.7 3.6 3.5 3.4) 1611730Sar4jc@virginia.edufind_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED) 1711730Sar4jc@virginia.edu 1811730Sar4jc@virginia.eduinclude(CheckCXXCompilerFlag) 1911730Sar4jc@virginia.eduinclude(CMakeParseArguments) 2011730Sar4jc@virginia.edu 2111730Sar4jc@virginia.eduif(NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD) 2211730Sar4jc@virginia.edu if(NOT MSVC) 2311730Sar4jc@virginia.edu check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG) 2411730Sar4jc@virginia.edu 2511730Sar4jc@virginia.edu if (HAS_CPP14_FLAG) 2611730Sar4jc@virginia.edu set(PYBIND11_CPP_STANDARD -std=c++14) 2711730Sar4jc@virginia.edu else() 2811730Sar4jc@virginia.edu check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG) 2911730Sar4jc@virginia.edu if (HAS_CPP11_FLAG) 3011730Sar4jc@virginia.edu set(PYBIND11_CPP_STANDARD -std=c++11) 3111730Sar4jc@virginia.edu else() 3211730Sar4jc@virginia.edu message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!") 3311730Sar4jc@virginia.edu endif() 3411730Sar4jc@virginia.edu endif() 3511730Sar4jc@virginia.edu elseif(MSVC) 3611730Sar4jc@virginia.edu set(PYBIND11_CPP_STANDARD /std:c++14) 3711730Sar4jc@virginia.edu endif() 3811730Sar4jc@virginia.edu 3911730Sar4jc@virginia.edu set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING 4011730Sar4jc@virginia.edu "C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE) 4111730Sar4jc@virginia.eduendif() 4211730Sar4jc@virginia.edu 4311730Sar4jc@virginia.edu# Checks whether the given CXX/linker flags can compile and link a cxx file. cxxflags and 4411730Sar4jc@virginia.edu# linkerflags are lists of flags to use. The result variable is a unique variable name for each set 4511730Sar4jc@virginia.edu# of flags: the compilation result will be cached base on the result variable. If the flags work, 4611730Sar4jc@virginia.edu# sets them in cxxflags_out/linkerflags_out internal cache variables (in addition to ${result}). 4711730Sar4jc@virginia.edufunction(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerflags cxxflags_out linkerflags_out) 4811730Sar4jc@virginia.edu set(CMAKE_REQUIRED_LIBRARIES ${linkerflags}) 4911730Sar4jc@virginia.edu check_cxx_compiler_flag("${cxxflags}" ${result}) 5011730Sar4jc@virginia.edu if (${result}) 5111730Sar4jc@virginia.edu set(${cxxflags_out} "${cxxflags}" CACHE INTERNAL "" FORCE) 5211730Sar4jc@virginia.edu set(${linkerflags_out} "${linkerflags}" CACHE INTERNAL "" FORCE) 5311730Sar4jc@virginia.edu endif() 5411730Sar4jc@virginia.eduendfunction() 5511730Sar4jc@virginia.edu 5611730Sar4jc@virginia.edu# Internal: find the appropriate link time optimization flags for this compiler 5711730Sar4jc@virginia.edufunction(_pybind11_add_lto_flags target_name prefer_thin_lto) 5811730Sar4jc@virginia.edu if (NOT DEFINED PYBIND11_LTO_CXX_FLAGS) 5911730Sar4jc@virginia.edu set(PYBIND11_LTO_CXX_FLAGS "" CACHE INTERNAL "") 6011730Sar4jc@virginia.edu set(PYBIND11_LTO_LINKER_FLAGS "" CACHE INTERNAL "") 6111730Sar4jc@virginia.edu 6211730Sar4jc@virginia.edu if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") 6311730Sar4jc@virginia.edu set(cxx_append "") 6411730Sar4jc@virginia.edu set(linker_append "") 6511730Sar4jc@virginia.edu if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE) 6611730Sar4jc@virginia.edu # Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it 6711730Sar4jc@virginia.edu set(linker_append ";$<$<CONFIG:MinSizeRel>:-O3>") 6811730Sar4jc@virginia.edu elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU") 6911730Sar4jc@virginia.edu set(cxx_append ";-fno-fat-lto-objects") 7011730Sar4jc@virginia.edu endif() 7111730Sar4jc@virginia.edu 7211730Sar4jc@virginia.edu if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND prefer_thin_lto) 7311730Sar4jc@virginia.edu _pybind11_return_if_cxx_and_linker_flags_work(HAS_FLTO_THIN 7411730Sar4jc@virginia.edu "-flto=thin${cxx_append}" "-flto=thin${linker_append}" 7511730Sar4jc@virginia.edu PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) 7611730Sar4jc@virginia.edu endif() 7711730Sar4jc@virginia.edu 7811730Sar4jc@virginia.edu if (NOT HAS_FLTO_THIN) 7911730Sar4jc@virginia.edu _pybind11_return_if_cxx_and_linker_flags_work(HAS_FLTO 8011730Sar4jc@virginia.edu "-flto${cxx_append}" "-flto${linker_append}" 8111730Sar4jc@virginia.edu PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) 8211730Sar4jc@virginia.edu endif() 8311730Sar4jc@virginia.edu elseif (CMAKE_CXX_COMPILER_ID MATCHES "Intel") 8411730Sar4jc@virginia.edu # Intel equivalent to LTO is called IPO 8511730Sar4jc@virginia.edu _pybind11_return_if_cxx_and_linker_flags_work(HAS_INTEL_IPO 8611730Sar4jc@virginia.edu "-ipo" "-ipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) 8711730Sar4jc@virginia.edu elseif(MSVC) 8811730Sar4jc@virginia.edu # cmake only interprets libraries as linker flags when they start with a - (otherwise it 8911730Sar4jc@virginia.edu # converts /LTCG to \LTCG as if it was a Windows path). Luckily MSVC supports passing flags 9011730Sar4jc@virginia.edu # with - instead of /, even if it is a bit non-standard: 9111730Sar4jc@virginia.edu _pybind11_return_if_cxx_and_linker_flags_work(HAS_MSVC_GL_LTCG 9211730Sar4jc@virginia.edu "/GL" "-LTCG" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS) 9311730Sar4jc@virginia.edu endif() 9411730Sar4jc@virginia.edu 9511730Sar4jc@virginia.edu if (PYBIND11_LTO_CXX_FLAGS) 9611730Sar4jc@virginia.edu message(STATUS "LTO enabled") 9711730Sar4jc@virginia.edu else() 9811730Sar4jc@virginia.edu message(STATUS "LTO disabled (not supported by the compiler and/or linker)") 9911730Sar4jc@virginia.edu endif() 10011730Sar4jc@virginia.edu endif() 10111730Sar4jc@virginia.edu 10211730Sar4jc@virginia.edu # Enable LTO flags if found, except for Debug builds 10311730Sar4jc@virginia.edu if (PYBIND11_LTO_CXX_FLAGS) 10411730Sar4jc@virginia.edu target_compile_options(${target_name} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:${PYBIND11_LTO_CXX_FLAGS}>") 10511730Sar4jc@virginia.edu endif() 10611730Sar4jc@virginia.edu if (PYBIND11_LTO_LINKER_FLAGS) 10711730Sar4jc@virginia.edu target_link_libraries(${target_name} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:${PYBIND11_LTO_LINKER_FLAGS}>") 10811730Sar4jc@virginia.edu endif() 10911730Sar4jc@virginia.eduendfunction() 11011730Sar4jc@virginia.edu 11111730Sar4jc@virginia.edu# Build a Python extension module: 11211730Sar4jc@virginia.edu# pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL] 11311730Sar4jc@virginia.edu# [NO_EXTRAS] [SYSTEM] [THIN_LTO] source1 [source2 ...]) 11411730Sar4jc@virginia.edu# 11511730Sar4jc@virginia.edufunction(pybind11_add_module target_name) 11611730Sar4jc@virginia.edu set(options MODULE SHARED EXCLUDE_FROM_ALL NO_EXTRAS SYSTEM THIN_LTO) 11711730Sar4jc@virginia.edu cmake_parse_arguments(ARG "${options}" "" "" ${ARGN}) 11811730Sar4jc@virginia.edu 11911730Sar4jc@virginia.edu if(ARG_MODULE AND ARG_SHARED) 12011730Sar4jc@virginia.edu message(FATAL_ERROR "Can't be both MODULE and SHARED") 12111730Sar4jc@virginia.edu elseif(ARG_SHARED) 12211730Sar4jc@virginia.edu set(lib_type SHARED) 12311730Sar4jc@virginia.edu else() 12411730Sar4jc@virginia.edu set(lib_type MODULE) 12511730Sar4jc@virginia.edu endif() 12611730Sar4jc@virginia.edu 12711730Sar4jc@virginia.edu if(ARG_EXCLUDE_FROM_ALL) 12811730Sar4jc@virginia.edu set(exclude_from_all EXCLUDE_FROM_ALL) 12911730Sar4jc@virginia.edu endif() 13011730Sar4jc@virginia.edu 13111730Sar4jc@virginia.edu add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS}) 13211730Sar4jc@virginia.edu 13311730Sar4jc@virginia.edu if(ARG_SYSTEM) 13411730Sar4jc@virginia.edu set(inc_isystem SYSTEM) 13511730Sar4jc@virginia.edu endif() 13611730Sar4jc@virginia.edu 13711730Sar4jc@virginia.edu target_include_directories(${target_name} ${inc_isystem} 13811730Sar4jc@virginia.edu PRIVATE ${PYBIND11_INCLUDE_DIR} # from project CMakeLists.txt 13911730Sar4jc@virginia.edu PRIVATE ${pybind11_INCLUDE_DIR} # from pybind11Config 14011730Sar4jc@virginia.edu PRIVATE ${PYTHON_INCLUDE_DIRS}) 14111730Sar4jc@virginia.edu 14211730Sar4jc@virginia.edu # Python debug libraries expose slightly different objects 14311730Sar4jc@virginia.edu # https://docs.python.org/3.6/c-api/intro.html#debugging-builds 14411730Sar4jc@virginia.edu # https://stackoverflow.com/questions/39161202/how-to-work-around-missing-pymodule-create2-in-amd64-win-python35-d-lib 14511730Sar4jc@virginia.edu if(PYTHON_IS_DEBUG) 14611730Sar4jc@virginia.edu target_compile_definitions(${target_name} PRIVATE Py_DEBUG) 14711730Sar4jc@virginia.edu endif() 14811730Sar4jc@virginia.edu 14911730Sar4jc@virginia.edu # The prefix and extension are provided by FindPythonLibsNew.cmake 15011730Sar4jc@virginia.edu set_target_properties(${target_name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}") 15111730Sar4jc@virginia.edu set_target_properties(${target_name} PROPERTIES SUFFIX "${PYTHON_MODULE_EXTENSION}") 15211730Sar4jc@virginia.edu 15311730Sar4jc@virginia.edu # -fvisibility=hidden is required to allow multiple modules compiled against 15411730Sar4jc@virginia.edu # different pybind versions to work properly, and for some features (e.g. 15511730Sar4jc@virginia.edu # py::module_local). We force it on everything inside the `pybind11` 15611730Sar4jc@virginia.edu # namespace; also turning it on for a pybind module compilation here avoids 15711730Sar4jc@virginia.edu # potential warnings or issues from having mixed hidden/non-hidden types. 15811730Sar4jc@virginia.edu set_target_properties(${target_name} PROPERTIES CXX_VISIBILITY_PRESET "hidden") 15911730Sar4jc@virginia.edu set_target_properties(${target_name} PROPERTIES CUDA_VISIBILITY_PRESET "hidden") 16011730Sar4jc@virginia.edu 16111730Sar4jc@virginia.edu if(WIN32 OR CYGWIN) 16211730Sar4jc@virginia.edu # Link against the Python shared library on Windows 16311730Sar4jc@virginia.edu target_link_libraries(${target_name} PRIVATE ${PYTHON_LIBRARIES}) 16411730Sar4jc@virginia.edu elseif(APPLE) 16511730Sar4jc@virginia.edu # It's quite common to have multiple copies of the same Python version 16611730Sar4jc@virginia.edu # installed on one's system. E.g.: one copy from the OS and another copy 16711730Sar4jc@virginia.edu # that's statically linked into an application like Blender or Maya. 16811730Sar4jc@virginia.edu # If we link our plugin library against the OS Python here and import it 16911730Sar4jc@virginia.edu # into Blender or Maya later on, this will cause segfaults when multiple 17011730Sar4jc@virginia.edu # conflicting Python instances are active at the same time (even when they 17111730Sar4jc@virginia.edu # are of the same version). 17211730Sar4jc@virginia.edu 17311730Sar4jc@virginia.edu # Windows is not affected by this issue since it handles DLL imports 17411730Sar4jc@virginia.edu # differently. The solution for Linux and Mac OS is simple: we just don't 17511730Sar4jc@virginia.edu # link against the Python library. The resulting shared library will have 17611730Sar4jc@virginia.edu # missing symbols, but that's perfectly fine -- they will be resolved at 17711730Sar4jc@virginia.edu # import time. 17811730Sar4jc@virginia.edu 17911730Sar4jc@virginia.edu target_link_libraries(${target_name} PRIVATE "-undefined dynamic_lookup") 18011730Sar4jc@virginia.edu 18111730Sar4jc@virginia.edu if(ARG_SHARED) 18211730Sar4jc@virginia.edu # Suppress CMake >= 3.0 warning for shared libraries 18311730Sar4jc@virginia.edu set_target_properties(${target_name} PROPERTIES MACOSX_RPATH ON) 18411730Sar4jc@virginia.edu endif() 18511730Sar4jc@virginia.edu endif() 18611730Sar4jc@virginia.edu 18711730Sar4jc@virginia.edu # Make sure C++11/14 are enabled 18811730Sar4jc@virginia.edu if(CMAKE_VERSION VERSION_LESS 3.3) 18911730Sar4jc@virginia.edu target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD}) 19011730Sar4jc@virginia.edu else() 19111730Sar4jc@virginia.edu target_compile_options(${target_name} PUBLIC $<$<COMPILE_LANGUAGE:CXX>:${PYBIND11_CPP_STANDARD}>) 19211730Sar4jc@virginia.edu endif() 19311730Sar4jc@virginia.edu 19411730Sar4jc@virginia.edu if(ARG_NO_EXTRAS) 19511730Sar4jc@virginia.edu return() 19611730Sar4jc@virginia.edu endif() 19711730Sar4jc@virginia.edu 19811730Sar4jc@virginia.edu _pybind11_add_lto_flags(${target_name} ${ARG_THIN_LTO}) 19911730Sar4jc@virginia.edu 20011730Sar4jc@virginia.edu if (NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) 20111730Sar4jc@virginia.edu # Strip unnecessary sections of the binary on Linux/Mac OS 20211730Sar4jc@virginia.edu if(CMAKE_STRIP) 20311730Sar4jc@virginia.edu if(APPLE) 20411730Sar4jc@virginia.edu add_custom_command(TARGET ${target_name} POST_BUILD 20511730Sar4jc@virginia.edu COMMAND ${CMAKE_STRIP} -x $<TARGET_FILE:${target_name}>) 20611730Sar4jc@virginia.edu else() 20711730Sar4jc@virginia.edu add_custom_command(TARGET ${target_name} POST_BUILD 20811730Sar4jc@virginia.edu COMMAND ${CMAKE_STRIP} $<TARGET_FILE:${target_name}>) 20911730Sar4jc@virginia.edu endif() 21011730Sar4jc@virginia.edu endif() 21111730Sar4jc@virginia.edu endif() 21211730Sar4jc@virginia.edu 21311730Sar4jc@virginia.edu if(MSVC) 21411730Sar4jc@virginia.edu # /MP enables multithreaded builds (relevant when there are many files), /bigobj is 21511730Sar4jc@virginia.edu # needed for bigger binding projects due to the limit to 64k addressable sections 21611730Sar4jc@virginia.edu target_compile_options(${target_name} PRIVATE /bigobj) 21711730Sar4jc@virginia.edu if(CMAKE_VERSION VERSION_LESS 3.11) 21811730Sar4jc@virginia.edu target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MP>) 21911730Sar4jc@virginia.edu else() 22011730Sar4jc@virginia.edu # Only set these options for C++ files. This is important so that, for 22111730Sar4jc@virginia.edu # instance, projects that include other types of source files like CUDA 22211730Sar4jc@virginia.edu # .cu files don't get these options propagated to nvcc since that would 22311730Sar4jc@virginia.edu # cause the build to fail. 22411730Sar4jc@virginia.edu target_compile_options(${target_name} PRIVATE $<$<NOT:$<CONFIG:Debug>>:$<$<COMPILE_LANGUAGE:CXX>:/MP>>) 22511730Sar4jc@virginia.edu endif() 22611730Sar4jc@virginia.edu endif() 22711730Sar4jc@virginia.eduendfunction() 22811730Sar4jc@virginia.edu