aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/FindCatch.cmake10
-rw-r--r--tools/FindPythonLibsNew.cmake66
-rw-r--r--tools/JoinPaths.cmake23
-rwxr-xr-xtools/check-style.sh6
-rw-r--r--tools/codespell_ignore_lines_from_errors.py39
-rw-r--r--tools/libsize.py6
-rwxr-xr-xtools/make_changelog.py19
-rw-r--r--tools/pybind11.pc.in7
-rw-r--r--tools/pybind11Common.cmake122
-rw-r--r--tools/pybind11Config.cmake.in21
-rw-r--r--tools/pybind11NewTools.cmake59
-rw-r--r--tools/pybind11Tools.cmake93
-rw-r--r--tools/setup_global.py.in16
-rw-r--r--tools/setup_main.py.in14
14 files changed, 313 insertions, 188 deletions
diff --git a/tools/FindCatch.cmake b/tools/FindCatch.cmake
index 4d6bffcf..5d3fcbfb 100644
--- a/tools/FindCatch.cmake
+++ b/tools/FindCatch.cmake
@@ -9,6 +9,8 @@
# CATCH_INCLUDE_DIR - path to catch.hpp
# CATCH_VERSION - version number
+option(DOWNLOAD_CATCH "Download catch2 if not found")
+
if(NOT Catch_FIND_VERSION)
message(FATAL_ERROR "A version number must be specified.")
elseif(Catch_FIND_REQUIRED)
@@ -34,10 +36,14 @@ endfunction()
function(_download_catch version destination_dir)
message(STATUS "Downloading catch v${version}...")
set(url https://github.com/philsquared/Catch/releases/download/v${version}/catch.hpp)
- file(DOWNLOAD ${url} "${destination_dir}/catch.hpp" STATUS status)
+ file(
+ DOWNLOAD ${url} "${destination_dir}/catch.hpp"
+ STATUS status
+ LOG log)
list(GET status 0 error)
if(error)
- message(FATAL_ERROR "Could not download ${url}")
+ string(REPLACE "\n" "\n " log " ${log}")
+ message(FATAL_ERROR "Could not download URL:\n" " ${url}\n" "Log:\n" "${log}")
endif()
set(CATCH_INCLUDE_DIR
"${destination_dir}"
diff --git a/tools/FindPythonLibsNew.cmake b/tools/FindPythonLibsNew.cmake
index 3605aebc..ce558d4e 100644
--- a/tools/FindPythonLibsNew.cmake
+++ b/tools/FindPythonLibsNew.cmake
@@ -92,7 +92,7 @@ endif()
# Use the Python interpreter to find the libs.
if(NOT PythonLibsNew_FIND_VERSION)
- set(PythonLibsNew_FIND_VERSION "")
+ set(PythonLibsNew_FIND_VERSION "3.6")
endif()
find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} ${_pythonlibs_required}
@@ -112,12 +112,26 @@ endif()
# VERSION. VERSION will typically be like "2.7" on unix, and "27" on windows.
execute_process(
COMMAND
- "${PYTHON_EXECUTABLE}" "-c" "from distutils import sysconfig as s;import sys;import struct;
+ "${PYTHON_EXECUTABLE}" "-c" "
+import sys;import struct;
+import sysconfig as s
+USE_SYSCONFIG = sys.version_info >= (3, 10)
+if not USE_SYSCONFIG:
+ from distutils import sysconfig as ds
print('.'.join(str(v) for v in sys.version_info));
print(sys.prefix);
-print(s.get_python_inc(plat_specific=True));
-print(s.get_python_lib(plat_specific=True));
-print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'));
+if USE_SYSCONFIG:
+ scheme = s.get_default_scheme()
+ if scheme == 'posix_local':
+ # Debian's default scheme installs to /usr/local/ but we want to find headers in /usr/
+ scheme = 'posix_prefix'
+ print(s.get_path('platinclude', scheme))
+ print(s.get_path('platlib'))
+ print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))
+else:
+ print(ds.get_python_inc(plat_specific=True));
+ print(ds.get_python_lib(plat_specific=True));
+ print(ds.get_config_var('EXT_SUFFIX') or ds.get_config_var('SO'));
print(hasattr(sys, 'gettotalrefcount')+0);
print(struct.calcsize('@P'));
print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
@@ -137,26 +151,40 @@ if(NOT _PYTHON_SUCCESS MATCHES 0)
return()
endif()
+option(
+ PYBIND11_PYTHONLIBS_OVERWRITE
+ "Overwrite cached values read from Python library (classic search). Turn off if cross-compiling and manually setting these values."
+ ON)
+# Can manually set values when cross-compiling
+macro(_PYBIND11_GET_IF_UNDEF lst index name)
+ if(PYBIND11_PYTHONLIBS_OVERWRITE OR NOT DEFINED "${name}")
+ list(GET "${lst}" "${index}" "${name}")
+ endif()
+endmacro()
+
# Convert the process output into a list
if(WIN32)
string(REGEX REPLACE "\\\\" "/" _PYTHON_VALUES ${_PYTHON_VALUES})
endif()
string(REGEX REPLACE ";" "\\\\;" _PYTHON_VALUES ${_PYTHON_VALUES})
string(REGEX REPLACE "\n" ";" _PYTHON_VALUES ${_PYTHON_VALUES})
-list(GET _PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
-list(GET _PYTHON_VALUES 1 PYTHON_PREFIX)
-list(GET _PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
-list(GET _PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
-list(GET _PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
-list(GET _PYTHON_VALUES 5 PYTHON_IS_DEBUG)
-list(GET _PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
-list(GET _PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
-list(GET _PYTHON_VALUES 8 PYTHON_LIBDIR)
-list(GET _PYTHON_VALUES 9 PYTHON_MULTIARCH)
+_pybind11_get_if_undef(_PYTHON_VALUES 0 _PYTHON_VERSION_LIST)
+_pybind11_get_if_undef(_PYTHON_VALUES 1 PYTHON_PREFIX)
+_pybind11_get_if_undef(_PYTHON_VALUES 2 PYTHON_INCLUDE_DIR)
+_pybind11_get_if_undef(_PYTHON_VALUES 3 PYTHON_SITE_PACKAGES)
+_pybind11_get_if_undef(_PYTHON_VALUES 4 PYTHON_MODULE_EXTENSION)
+_pybind11_get_if_undef(_PYTHON_VALUES 5 PYTHON_IS_DEBUG)
+_pybind11_get_if_undef(_PYTHON_VALUES 6 PYTHON_SIZEOF_VOID_P)
+_pybind11_get_if_undef(_PYTHON_VALUES 7 PYTHON_LIBRARY_SUFFIX)
+_pybind11_get_if_undef(_PYTHON_VALUES 8 PYTHON_LIBDIR)
+_pybind11_get_if_undef(_PYTHON_VALUES 9 PYTHON_MULTIARCH)
# Make sure the Python has the same pointer-size as the chosen compiler
# Skip if CMAKE_SIZEOF_VOID_P is not defined
-if(CMAKE_SIZEOF_VOID_P AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
+# This should be skipped for (non-Apple) cross-compiles (like EMSCRIPTEN)
+if(NOT CMAKE_CROSSCOMPILING
+ AND CMAKE_SIZEOF_VOID_P
+ AND (NOT "${PYTHON_SIZEOF_VOID_P}" STREQUAL "${CMAKE_SIZEOF_VOID_P}"))
if(PythonLibsNew_FIND_REQUIRED)
math(EXPR _PYTHON_BITS "${PYTHON_SIZEOF_VOID_P} * 8")
math(EXPR _CMAKE_BITS "${CMAKE_SIZEOF_VOID_P} * 8")
@@ -180,7 +208,9 @@ string(REGEX REPLACE "\\\\" "/" PYTHON_PREFIX "${PYTHON_PREFIX}")
string(REGEX REPLACE "\\\\" "/" PYTHON_INCLUDE_DIR "${PYTHON_INCLUDE_DIR}")
string(REGEX REPLACE "\\\\" "/" PYTHON_SITE_PACKAGES "${PYTHON_SITE_PACKAGES}")
-if(CMAKE_HOST_WIN32)
+if(DEFINED PYTHON_LIBRARY)
+ # Don't write to PYTHON_LIBRARY if it's already set
+elseif(CMAKE_HOST_WIN32)
set(PYTHON_LIBRARY "${PYTHON_PREFIX}/libs/python${PYTHON_LIBRARY_SUFFIX}.lib")
# when run in a venv, PYTHON_PREFIX points to it. But the libraries remain in the
@@ -246,7 +276,7 @@ if(NOT PYTHON_DEBUG_LIBRARY)
endif()
set(PYTHON_DEBUG_LIBRARIES "${PYTHON_DEBUG_LIBRARY}")
-find_package_message(PYTHON "Found PythonLibs: ${PYTHON_LIBRARY}"
+find_package_message(PYTHON "Found PythonLibs: ${PYTHON_LIBRARIES}"
"${PYTHON_EXECUTABLE}${PYTHON_VERSION_STRING}")
set(PYTHONLIBS_FOUND TRUE)
diff --git a/tools/JoinPaths.cmake b/tools/JoinPaths.cmake
new file mode 100644
index 00000000..c68d91b8
--- /dev/null
+++ b/tools/JoinPaths.cmake
@@ -0,0 +1,23 @@
+# This module provides function for joining paths
+# known from most languages
+#
+# SPDX-License-Identifier: (MIT OR CC0-1.0)
+# Copyright 2020 Jan Tojnar
+# https://github.com/jtojnar/cmake-snips
+#
+# Modelled after Python’s os.path.join
+# https://docs.python.org/3.7/library/os.path.html#os.path.join
+# Windows not supported
+function(join_paths joined_path first_path_segment)
+ set(temp_path "${first_path_segment}")
+ foreach(current_segment IN LISTS ARGN)
+ if(NOT ("${current_segment}" STREQUAL ""))
+ if(IS_ABSOLUTE "${current_segment}")
+ set(temp_path "${current_segment}")
+ else()
+ set(temp_path "${temp_path}/${current_segment}")
+ endif()
+ endif()
+ endforeach()
+ set(${joined_path} "${temp_path}" PARENT_SCOPE)
+endfunction()
diff --git a/tools/check-style.sh b/tools/check-style.sh
index f7af2a41..6d832523 100755
--- a/tools/check-style.sh
+++ b/tools/check-style.sh
@@ -16,11 +16,11 @@ check_style_errors=0
IFS=$'\n'
-found="$(grep '\<\(if\|for\|while\|catch\)(\|){' $@ -rn --color=always)"
+found="$(grep '\<\(if\|for\|while\|catch\)(\|){' "$@" -rn --color=always)"
if [ -n "$found" ]; then
echo -e '\033[31;01mError: found the following coding style problems:\033[0m'
check_style_errors=1
- echo "$found" | sed -e 's/^/ /'
+ echo "${found//^/ /}"
fi
found="$(awk '
@@ -34,7 +34,7 @@ last && /^\s*{/ {
last=""
}
{ last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" }
-' $(find include -type f) $@)"
+' "$(find include -type f)" "$@")"
if [ -n "$found" ]; then
check_style_errors=1
echo -e '\033[31;01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files:\033[0m'
diff --git a/tools/codespell_ignore_lines_from_errors.py b/tools/codespell_ignore_lines_from_errors.py
new file mode 100644
index 00000000..4ec9add1
--- /dev/null
+++ b/tools/codespell_ignore_lines_from_errors.py
@@ -0,0 +1,39 @@
+"""Simple script for rebuilding .codespell-ignore-lines
+
+Usage:
+
+cat < /dev/null > .codespell-ignore-lines
+pre-commit run --all-files codespell >& /tmp/codespell_errors.txt
+python3 tools/codespell_ignore_lines_from_errors.py /tmp/codespell_errors.txt > .codespell-ignore-lines
+
+git diff to review changes, then commit, push.
+"""
+
+import sys
+from typing import List
+
+
+def run(args: List[str]) -> None:
+ assert len(args) == 1, "codespell_errors.txt"
+ cache = {}
+ done = set()
+ with open(args[0]) as f:
+ lines = f.read().splitlines()
+
+ for line in sorted(lines):
+ i = line.find(" ==> ")
+ if i > 0:
+ flds = line[:i].split(":")
+ if len(flds) >= 2:
+ filename, line_num = flds[:2]
+ if filename not in cache:
+ with open(filename) as f:
+ cache[filename] = f.read().splitlines()
+ supp = cache[filename][int(line_num) - 1]
+ if supp not in done:
+ print(supp)
+ done.add(supp)
+
+
+if __name__ == "__main__":
+ run(args=sys.argv[1:])
diff --git a/tools/libsize.py b/tools/libsize.py
index 589c317f..1ac9afbe 100644
--- a/tools/libsize.py
+++ b/tools/libsize.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-from __future__ import print_function, division
import os
import sys
@@ -15,7 +13,7 @@ lib = sys.argv[1]
save = sys.argv[2]
if not os.path.exists(lib):
- sys.exit("Error: requested file ({}) does not exist".format(lib))
+ sys.exit(f"Error: requested file ({lib}) does not exist")
libsize = os.path.getsize(lib)
@@ -30,7 +28,7 @@ if os.path.exists(save):
if change == 0:
print(" (no change)")
else:
- print(" (change of {:+} bytes = {:+.2%})".format(change, change / oldsize))
+ print(f" (change of {change:+} bytes = {change / oldsize:+.2%})")
else:
print()
diff --git a/tools/make_changelog.py b/tools/make_changelog.py
index 609ce2f1..b5bd8329 100755
--- a/tools/make_changelog.py
+++ b/tools/make_changelog.py
@@ -1,14 +1,11 @@
#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
import re
import ghapi.all
-
from rich import print
from rich.syntax import Syntax
-
ENTRY = re.compile(
r"""
Suggested \s changelog \s entry:
@@ -27,12 +24,17 @@ print()
api = ghapi.all.GhApi(owner="pybind", repo="pybind11")
-issues = api.issues.list_for_repo(labels="needs changelog", state="closed")
+issues_pages = ghapi.page.paged(
+ api.issues.list_for_repo, labels="needs changelog", state="closed"
+)
+issues = (issue for page in issues_pages for issue in page)
missing = []
for issue in issues:
- changelog = ENTRY.findall(issue.body)
- if changelog:
+ changelog = ENTRY.findall(issue.body or "")
+ if not changelog or not changelog[0]:
+ missing.append(issue)
+ else:
(msg,) = changelog
if not msg.startswith("* "):
msg = "* " + msg
@@ -41,12 +43,9 @@ for issue in issues:
msg += f"\n `#{issue.number} <{issue.html_url}>`_"
- print(Syntax(msg, "rst", theme="ansi_light"))
+ print(Syntax(msg, "rst", theme="ansi_light", word_wrap=True))
print()
- else:
- missing.append(issue)
-
if missing:
print()
print("[blue]" + "-" * 30)
diff --git a/tools/pybind11.pc.in b/tools/pybind11.pc.in
new file mode 100644
index 00000000..402f0b35
--- /dev/null
+++ b/tools/pybind11.pc.in
@@ -0,0 +1,7 @@
+prefix=@prefix_for_pc_file@
+includedir=@includedir_for_pc_file@
+
+Name: @PROJECT_NAME@
+Description: Seamless operability between C++11 and Python
+Version: @PROJECT_VERSION@
+Cflags: -I${includedir}
diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake
index 3c05c682..308d1b70 100644
--- a/tools/pybind11Common.cmake
+++ b/tools/pybind11Common.cmake
@@ -5,10 +5,9 @@ Adds the following targets::
pybind11::pybind11 - link to headers and pybind11
pybind11::module - Adds module links
pybind11::embed - Adds embed links
- pybind11::lto - Link time optimizations (manual selection)
- pybind11::thin_lto - Link time optimizations (manual selection)
+ pybind11::lto - Link time optimizations (only if CMAKE_INTERPROCEDURAL_OPTIMIZATION is not set)
+ pybind11::thin_lto - Link time optimizations (only if CMAKE_INTERPROCEDURAL_OPTIMIZATION is not set)
pybind11::python_link_helper - Adds link to Python libraries
- pybind11::python2_no_register - Avoid warning/error with Python 2 + C++14/7
pybind11::windows_extras - MSVC bigobj and mp for building multithreaded
pybind11::opt_size - avoid optimizations that increase code size
@@ -20,7 +19,8 @@ Adds the following functions::
#]======================================================]
# CMake 3.10 has an include_guard command, but we can't use that yet
-if(TARGET pybind11::lto)
+# include_guard(global) (pre-CMake 3.10)
+if(TARGET pybind11::pybind11)
return()
endif()
@@ -65,31 +65,6 @@ set_property(
APPEND
PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11)
-# ----------------------- no register ----------------------
-
-# Workaround for Python 2.7 and C++17 (C++14 as a warning) incompatibility
-# This adds the flags -Wno-register and -Wno-deprecated-register if the compiler
-# is Clang 3.9+ or AppleClang and the compile language is CXX, or /wd5033 for MSVC (all languages,
-# since MSVC didn't recognize COMPILE_LANGUAGE until CMake 3.11+).
-
-add_library(pybind11::python2_no_register INTERFACE IMPORTED ${optional_global})
-set(clang_4plus
- "$<AND:$<CXX_COMPILER_ID:Clang>,$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,3.9>>>")
-set(no_register "$<OR:${clang_4plus},$<CXX_COMPILER_ID:AppleClang>>")
-
-if(MSVC AND CMAKE_VERSION VERSION_LESS 3.11)
- set(cxx_no_register "${no_register}")
-else()
- set(cxx_no_register "$<AND:$<COMPILE_LANGUAGE:CXX>,${no_register}>")
-endif()
-
-set(msvc "$<CXX_COMPILER_ID:MSVC>")
-
-set_property(
- TARGET pybind11::python2_no_register
- PROPERTY INTERFACE_COMPILE_OPTIONS
- "$<${cxx_no_register}:-Wno-register;-Wno-deprecated-register>" "$<${msvc}:/wd5033>")
-
# --------------------------- link helper ---------------------------
add_library(pybind11::python_link_helper IMPORTED INTERFACE ${optional_global})
@@ -115,28 +90,32 @@ endif()
add_library(pybind11::windows_extras IMPORTED INTERFACE ${optional_global})
-if(MSVC)
- # /MP enables multithreaded builds (relevant when there are many files), /bigobj is
- # needed for bigger binding projects due to the limit to 64k addressable sections
+if(MSVC) # That's also clang-cl
+ # /bigobj is needed for bigger binding projects due to the limit to 64k
+ # addressable sections
set_property(
TARGET pybind11::windows_extras
APPEND
- PROPERTY INTERFACE_COMPILE_OPTIONS /bigobj)
+ PROPERTY INTERFACE_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:CXX>:/bigobj>)
- if(CMAKE_VERSION VERSION_LESS 3.11)
- set_property(
- TARGET pybind11::windows_extras
- APPEND
- PROPERTY INTERFACE_COMPILE_OPTIONS $<$<NOT:$<CONFIG:Debug>>:/MP>)
- else()
- # Only set these options for C++ files. This is important so that, for
- # instance, projects that include other types of source files like CUDA
- # .cu files don't get these options propagated to nvcc since that would
- # cause the build to fail.
- set_property(
- TARGET pybind11::windows_extras
- APPEND
- PROPERTY INTERFACE_COMPILE_OPTIONS $<$<NOT:$<CONFIG:Debug>>:$<$<COMPILE_LANGUAGE:CXX>:/MP>>)
+ # /MP enables multithreaded builds (relevant when there are many files) for MSVC
+ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # no Clang no Intel
+ if(CMAKE_VERSION VERSION_LESS 3.11)
+ set_property(
+ TARGET pybind11::windows_extras
+ APPEND
+ PROPERTY INTERFACE_COMPILE_OPTIONS $<$<NOT:$<CONFIG:Debug>>:/MP>)
+ else()
+ # Only set these options for C++ files. This is important so that, for
+ # instance, projects that include other types of source files like CUDA
+ # .cu files don't get these options propagated to nvcc since that would
+ # cause the build to fail.
+ set_property(
+ TARGET pybind11::windows_extras
+ APPEND
+ PROPERTY INTERFACE_COMPILE_OPTIONS
+ $<$<NOT:$<CONFIG:Debug>>:$<$<COMPILE_LANGUAGE:CXX>:/MP>>)
+ endif()
endif()
endif()
@@ -184,11 +163,19 @@ endif()
# --------------------- Python specifics -------------------------
+# CMake 3.27 removes the classic FindPythonInterp if CMP0148 is NEW
+if(CMAKE_VERSION VERSION_LESS "3.27")
+ set(_pybind11_missing_old_python "OLD")
+else()
+ cmake_policy(GET CMP0148 _pybind11_missing_old_python)
+endif()
+
# Check to see which Python mode we are in, new, old, or no python
if(PYBIND11_NOPYTHON)
set(_pybind11_nopython ON)
elseif(
- PYBIND11_FINDPYTHON
+ _pybind11_missing_old_python STREQUAL "NEW"
+ OR PYBIND11_FINDPYTHON
OR Python_FOUND
OR Python2_FOUND
OR Python3_FOUND)
@@ -298,27 +285,50 @@ function(_pybind11_return_if_cxx_and_linker_flags_work result cxxflags linkerfla
endfunction()
function(_pybind11_generate_lto target prefer_thin_lto)
+ if(MINGW)
+ message(STATUS "${target} disabled (problems with undefined symbols for MinGW for now)")
+ return()
+ endif()
+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(cxx_append "")
set(linker_append "")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE)
# Clang Gold plugin does not support -Os; append -O3 to MinSizeRel builds to override it
set(linker_append ";$<$<CONFIG:MinSizeRel>:-O3>")
- elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
+ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT MINGW)
set(cxx_append ";-fno-fat-lto-objects")
endif()
- if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND prefer_thin_lto)
+ if(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le" OR CMAKE_SYSTEM_PROCESSOR MATCHES "mips64")
+ set(NO_FLTO_ARCH TRUE)
+ else()
+ set(NO_FLTO_ARCH FALSE)
+ endif()
+
+ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang"
+ AND prefer_thin_lto
+ AND NOT NO_FLTO_ARCH)
_pybind11_return_if_cxx_and_linker_flags_work(
HAS_FLTO_THIN "-flto=thin${cxx_append}" "-flto=thin${linker_append}"
PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
endif()
- if(NOT HAS_FLTO_THIN)
+ if(NOT HAS_FLTO_THIN AND NOT NO_FLTO_ARCH)
_pybind11_return_if_cxx_and_linker_flags_work(
HAS_FLTO "-flto${cxx_append}" "-flto${linker_append}" PYBIND11_LTO_CXX_FLAGS
PYBIND11_LTO_LINKER_FLAGS)
endif()
+ elseif(CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM")
+ # IntelLLVM equivalent to LTO is called IPO; also IntelLLVM is WIN32/UNIX
+ # WARNING/HELP WANTED: This block of code is currently not covered by pybind11 GitHub Actions!
+ if(WIN32)
+ _pybind11_return_if_cxx_and_linker_flags_work(
+ HAS_INTEL_IPO "-Qipo" "-Qipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
+ else()
+ _pybind11_return_if_cxx_and_linker_flags_work(
+ HAS_INTEL_IPO "-ipo" "-ipo" PYBIND11_LTO_CXX_FLAGS PYBIND11_LTO_LINKER_FLAGS)
+ endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
# Intel equivalent to LTO is called IPO
_pybind11_return_if_cxx_and_linker_flags_work(HAS_INTEL_IPO "-ipo" "-ipo"
@@ -370,11 +380,13 @@ function(_pybind11_generate_lto target prefer_thin_lto)
endif()
endfunction()
-add_library(pybind11::lto IMPORTED INTERFACE ${optional_global})
-_pybind11_generate_lto(pybind11::lto FALSE)
+if(NOT DEFINED CMAKE_INTERPROCEDURAL_OPTIMIZATION)
+ add_library(pybind11::lto IMPORTED INTERFACE ${optional_global})
+ _pybind11_generate_lto(pybind11::lto FALSE)
-add_library(pybind11::thin_lto IMPORTED INTERFACE ${optional_global})
-_pybind11_generate_lto(pybind11::thin_lto TRUE)
+ add_library(pybind11::thin_lto IMPORTED INTERFACE ${optional_global})
+ _pybind11_generate_lto(pybind11::thin_lto TRUE)
+endif()
# ---------------------- pybind11_strip -----------------------------
diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in
index 9921aeb3..5734f437 100644
--- a/tools/pybind11Config.cmake.in
+++ b/tools/pybind11Config.cmake.in
@@ -13,7 +13,7 @@ This module sets the following variables in your project:
``pybind11_VERSION``
pybind11 version in format Major.Minor.Release
``pybind11_VERSION_TYPE``
- pybind11 version type (dev, release)
+ pybind11 version type (``dev*`` or empty for a release)
``pybind11_INCLUDE_DIRS``
Directories where pybind11 and python headers are located.
``pybind11_INCLUDE_DIR``
@@ -51,8 +51,6 @@ complex applications, and they are available in all modes:
Python headers too.
``pybind11::python_link_helper``
Just the "linking" part of ``pybind11:module``, for CMake < 3.15.
-``pybind11::python2_no_register``
- Quiets the warning/error when mixing C++14+ and Python 2, also included in ``pybind11::module``.
``pybind11::thin_lto``
An alternative to ``INTERPROCEDURAL_OPTIMIZATION``.
``pybind11::lto``
@@ -65,7 +63,9 @@ Modes
There are two modes provided; classic, which is built on the old Python
discovery packages in CMake, or the new FindPython mode, which uses FindPython
-from 3.12+ forward (3.15+ _highly_ recommended).
+from 3.12+ forward (3.15+ _highly_ recommended). If you set the minimum or
+maximum version of CMake to 3.27+, then FindPython is the default (since
+FindPythonInterp/FindPythonLibs has been removed via policy `CMP0148`).
New FindPython mode
^^^^^^^^^^^^^^^^^^^
@@ -88,7 +88,7 @@ you can either use the basic targets, or use the FindPython tools:
set_target_properties(MyModule2 PROPERTIES
INTERPROCEDURAL_OPTIMIZATION ON
CXX_VISIBILITY_PRESET ON
- VISIBLITY_INLINES_HIDDEN ON)
+ VISIBILITY_INLINES_HIDDEN ON)
If you build targets yourself, you may be interested in stripping the output
for reduced size; this is the one other feature that the helper function gives you.
@@ -139,7 +139,7 @@ This module defines the following commands to assist with creating Python module
pybind11_add_module(<target>
[STATIC|SHARED|MODULE]
- [THIN_LTO] [OPT_SIZE] [NO_EXTRAS] [WITHOUT_SOBAI]
+ [THIN_LTO] [OPT_SIZE] [NO_EXTRAS] [WITHOUT_SOABI]
<files>...
)
@@ -147,7 +147,7 @@ Add a module and setup all helpers. You can select the type of the library; the
default is ``MODULE``. There are several options:
``OPT_SIZE``
- Optimize for size, even if the ``CMAKE_BUILD_TYPE`` is not ``RelSize``.
+ Optimize for size, even if the ``CMAKE_BUILD_TYPE`` is not ``MinSizeRel``.
``THIN_LTO``
Use thin TLO instead of regular if there's a choice (pybind11's selection
is disabled if ``CMAKE_INTERPROCEDURAL_OPTIMIZATIONS`` is set).
@@ -195,13 +195,14 @@ Using ``find_package`` with version info is not recommended except for release v
.. code-block:: cmake
find_package(pybind11 CONFIG)
- find_package(pybind11 2.0 EXACT CONFIG REQUIRED)
+ find_package(pybind11 2.9 EXACT CONFIG REQUIRED)
#]=============================================================================]
@PACKAGE_INIT@
# Location of pybind11/pybind11.h
-set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@")
+# This will be relative unless explicitly set as absolute
+set(pybind11_INCLUDE_DIR "@pybind11_INCLUDEDIR@")
set(pybind11_LIBRARY "")
set(pybind11_DEFINITIONS USING_pybind11)
@@ -227,6 +228,6 @@ include("${CMAKE_CURRENT_LIST_DIR}/pybind11Common.cmake")
if(NOT pybind11_FIND_QUIETLY)
message(
STATUS
- "Found pybind11: ${pybind11_INCLUDE_DIR} (found version \"${pybind11_VERSION}\" ${pybind11_VERSION_TYPE})"
+ "Found pybind11: ${pybind11_INCLUDE_DIR} (found version \"${pybind11_VERSION}${pybind11_VERSION_TYPE}\")"
)
endif()
diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake
index 18da8be1..7d7424a7 100644
--- a/tools/pybind11NewTools.cmake
+++ b/tools/pybind11NewTools.cmake
@@ -5,6 +5,12 @@
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
+if(CMAKE_VERSION VERSION_LESS 3.12)
+ message(FATAL_ERROR "You cannot use the new FindPython module with CMake < 3.12")
+endif()
+
+include_guard(DIRECTORY)
+
get_property(
is_config
TARGET pybind11::headers
@@ -16,13 +22,7 @@ else()
set(_pybind11_quiet "")
endif()
-if(CMAKE_VERSION VERSION_LESS 3.12)
- message(FATAL_ERROR "You cannot use the new FindPython module with CMake < 3.12")
-endif()
-
-if(NOT Python_FOUND
- AND NOT Python3_FOUND
- AND NOT Python2_FOUND)
+if(NOT Python_FOUND AND NOT Python3_FOUND)
if(NOT DEFINED Python_FIND_IMPLEMENTATIONS)
set(Python_FIND_IMPLEMENTATIONS CPython PyPy)
endif()
@@ -32,7 +32,7 @@ if(NOT Python_FOUND
set(Python_ROOT_DIR "$ENV{pythonLocation}")
endif()
- find_package(Python REQUIRED COMPONENTS Interpreter Development ${_pybind11_quiet})
+ find_package(Python 3.6 REQUIRED COMPONENTS Interpreter Development ${_pybind11_quiet})
# If we are in submodule mode, export the Python targets to global targets.
# If this behavior is not desired, FindPython _before_ pybind11.
@@ -49,19 +49,10 @@ if(Python_FOUND)
set(_Python
Python
CACHE INTERNAL "" FORCE)
-elseif(Python3_FOUND AND NOT Python2_FOUND)
+elseif(Python3_FOUND)
set(_Python
Python3
CACHE INTERNAL "" FORCE)
-elseif(Python2_FOUND AND NOT Python3_FOUND)
- set(_Python
- Python2
- CACHE INTERNAL "" FORCE)
-else()
- message(AUTHOR_WARNING "Python2 and Python3 both present, pybind11 in "
- "PYBIND11_NOPYTHON mode (manually activate to silence warning)")
- set(_pybind11_nopython ON)
- return()
endif()
if(PYBIND11_MASTER_PROJECT)
@@ -82,6 +73,15 @@ if(NOT DEFINED ${_Python}_EXECUTABLE)
endif()
+if(NOT ${_Python}_EXECUTABLE STREQUAL PYBIND11_PYTHON_EXECUTABLE_LAST)
+ # Detect changes to the Python version/binary in subsequent CMake runs, and refresh config if needed
+ unset(PYTHON_IS_DEBUG CACHE)
+ unset(PYTHON_MODULE_EXTENSION CACHE)
+ set(PYBIND11_PYTHON_EXECUTABLE_LAST
+ "${${_Python}_EXECUTABLE}"
+ CACHE INTERNAL "Python executable during the last CMake run")
+endif()
+
if(NOT DEFINED PYTHON_IS_DEBUG)
# Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
execute_process(
@@ -99,7 +99,7 @@ if(NOT DEFINED PYTHON_MODULE_EXTENSION)
execute_process(
COMMAND
"${${_Python}_EXECUTABLE}" "-c"
- "from distutils import sysconfig as s;print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))"
+ "import sys, importlib; s = importlib.import_module('distutils.sysconfig' if sys.version_info < (3, 10) else 'sysconfig'); print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))"
OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION
ERROR_VARIABLE _PYTHON_MODULE_EXTENSION_ERR
OUTPUT_STRIP_TRAILING_WHITESPACE)
@@ -126,7 +126,7 @@ if(PYTHON_IS_DEBUG)
PROPERTY INTERFACE_COMPILE_DEFINITIONS Py_DEBUG)
endif()
-# Check on every access - since Python2 and Python3 could have been used - do nothing in that case.
+# Check on every access - since Python can change - do nothing in that case.
if(DEFINED ${_Python}_INCLUDE_DIRS)
# Only add Python for build - must be added during the import for config
@@ -148,13 +148,6 @@ if(DEFINED ${_Python}_INCLUDE_DIRS)
CACHE INTERNAL "Directories where pybind11 and possibly Python headers are located")
endif()
-if(DEFINED ${_Python}_VERSION AND ${_Python}_VERSION VERSION_LESS 3)
- set_property(
- TARGET pybind11::pybind11
- APPEND
- PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python2_no_register)
-endif()
-
# In CMake 3.18+, you can find these separately, so include an if
if(TARGET ${_Python}::Python)
set_property(
@@ -194,8 +187,6 @@ function(pybind11_add_module target_name)
python_add_library(${target_name} ${lib_type} ${ARG_UNPARSED_ARGUMENTS})
elseif("${_Python}" STREQUAL "Python3")
python3_add_library(${target_name} ${lib_type} ${ARG_UNPARSED_ARGUMENTS})
- elseif("${_Python}" STREQUAL "Python2")
- python2_add_library(${target_name} ${lib_type} ${ARG_UNPARSED_ARGUMENTS})
else()
message(FATAL_ERROR "Cannot detect FindPython version: ${_Python}")
endif()
@@ -212,10 +203,6 @@ function(pybind11_add_module target_name)
target_link_libraries(${target_name} PRIVATE pybind11::windows_extras)
endif()
- if(DEFINED ${_Python}_VERSION AND ${_Python}_VERSION VERSION_LESS 3)
- target_link_libraries(${target_name} PRIVATE pybind11::python2_no_register)
- endif()
-
# -fvisibility=hidden is required to allow multiple modules compiled against
# different pybind versions to work properly, and for some features (e.g.
# py::module_local). We force it on everything inside the `pybind11`
@@ -230,7 +217,7 @@ function(pybind11_add_module target_name)
endif()
# If we don't pass a WITH_SOABI or WITHOUT_SOABI, use our own default handling of extensions
- if(NOT ARG_WITHOUT_SOABI OR NOT "WITH_SOABI" IN_LIST ARG_UNPARSED_ARGUMENTS)
+ if(NOT ARG_WITHOUT_SOABI AND NOT "WITH_SOABI" IN_LIST ARG_UNPARSED_ARGUMENTS)
pybind11_extension(${target_name})
endif()
@@ -246,7 +233,9 @@ function(pybind11_add_module target_name)
endif()
endif()
- if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
+ # Use case-insensitive comparison to match the result of $<CONFIG:cfgs>
+ string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
+ if(NOT MSVC AND NOT "${uppercase_CMAKE_BUILD_TYPE}" MATCHES DEBUG|RELWITHDEBINFO)
# Strip unnecessary sections of the binary on Linux/macOS
pybind11_strip(${target_name})
endif()
diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake
index 32313539..66ad00a4 100644
--- a/tools/pybind11Tools.cmake
+++ b/tools/pybind11Tools.cmake
@@ -5,6 +5,11 @@
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
+# include_guard(global) (pre-CMake 3.10)
+if(TARGET pybind11::python_headers)
+ return()
+endif()
+
# Built-in in CMake 3.5+
include(CMakeParseArguments)
@@ -38,38 +43,32 @@ endif()
# A user can set versions manually too
set(Python_ADDITIONAL_VERSIONS
- "3.10;3.9;3.8;3.7;3.6;3.5;3.4"
+ "3.11;3.10;3.9;3.8;3.7;3.6"
CACHE INTERNAL "")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} MODULE REQUIRED ${_pybind11_quiet})
list(REMOVE_AT CMAKE_MODULE_PATH -1)
+# Makes a normal variable a cached variable
+macro(_PYBIND11_PROMOTE_TO_CACHE NAME)
+ set(_tmp_ptc "${${NAME}}")
+ # CMake 3.21 complains if a cached variable is shadowed by a normal one
+ unset(${NAME})
+ set(${NAME}
+ "${_tmp_ptc}"
+ CACHE INTERNAL "")
+endmacro()
+
# Cache variables so pybind11_add_module can be used in parent projects
-set(PYTHON_INCLUDE_DIRS
- ${PYTHON_INCLUDE_DIRS}
- CACHE INTERNAL "")
-set(PYTHON_LIBRARIES
- ${PYTHON_LIBRARIES}
- CACHE INTERNAL "")
-set(PYTHON_MODULE_PREFIX
- ${PYTHON_MODULE_PREFIX}
- CACHE INTERNAL "")
-set(PYTHON_MODULE_EXTENSION
- ${PYTHON_MODULE_EXTENSION}
- CACHE INTERNAL "")
-set(PYTHON_VERSION_MAJOR
- ${PYTHON_VERSION_MAJOR}
- CACHE INTERNAL "")
-set(PYTHON_VERSION_MINOR
- ${PYTHON_VERSION_MINOR}
- CACHE INTERNAL "")
-set(PYTHON_VERSION
- ${PYTHON_VERSION}
- CACHE INTERNAL "")
-set(PYTHON_IS_DEBUG
- "${PYTHON_IS_DEBUG}"
- CACHE INTERNAL "")
+_pybind11_promote_to_cache(PYTHON_INCLUDE_DIRS)
+_pybind11_promote_to_cache(PYTHON_LIBRARIES)
+_pybind11_promote_to_cache(PYTHON_MODULE_PREFIX)
+_pybind11_promote_to_cache(PYTHON_MODULE_EXTENSION)
+_pybind11_promote_to_cache(PYTHON_VERSION_MAJOR)
+_pybind11_promote_to_cache(PYTHON_VERSION_MINOR)
+_pybind11_promote_to_cache(PYTHON_VERSION)
+_pybind11_promote_to_cache(PYTHON_IS_DEBUG)
if(PYBIND11_MASTER_PROJECT)
if(PYTHON_MODULE_EXTENSION MATCHES "pypy")
@@ -116,25 +115,37 @@ if(PYTHON_IS_DEBUG)
PROPERTY INTERFACE_COMPILE_DEFINITIONS Py_DEBUG)
endif()
-set_property(
- TARGET pybind11::module
- APPEND
- PROPERTY
- INTERFACE_LINK_LIBRARIES pybind11::python_link_helper
- "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES}>>")
+# The <3.11 code here does not support release/debug builds at the same time, like on vcpkg
+if(CMAKE_VERSION VERSION_LESS 3.11)
+ set_property(
+ TARGET pybind11::module
+ APPEND
+ PROPERTY
+ INTERFACE_LINK_LIBRARIES
+ pybind11::python_link_helper
+ "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES}>>"
+ )
-if(PYTHON_VERSION VERSION_LESS 3)
set_property(
- TARGET pybind11::pybind11
+ TARGET pybind11::embed
APPEND
- PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python2_no_register)
+ PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
+else()
+ # The IMPORTED INTERFACE library here is to ensure that "debug" and "release" get processed outside
+ # of a generator expression - https://gitlab.kitware.com/cmake/cmake/-/issues/18424, as they are
+ # target_link_library keywords rather than real libraries.
+ add_library(pybind11::_ClassicPythonLibraries IMPORTED INTERFACE)
+ target_link_libraries(pybind11::_ClassicPythonLibraries INTERFACE ${PYTHON_LIBRARIES})
+ target_link_libraries(
+ pybind11::module
+ INTERFACE
+ pybind11::python_link_helper
+ "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:pybind11::_ClassicPythonLibraries>")
+
+ target_link_libraries(pybind11::embed INTERFACE pybind11::pybind11
+ pybind11::_ClassicPythonLibraries)
endif()
-set_property(
- TARGET pybind11::embed
- APPEND
- PROPERTY INTERFACE_LINK_LIBRARIES pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES}>)
-
function(pybind11_extension name)
# The prefix and extension are provided by FindPythonLibsNew.cmake
set_target_properties(${name} PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
@@ -201,7 +212,9 @@ function(pybind11_add_module target_name)
endif()
endif()
- if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
+ # Use case-insensitive comparison to match the result of $<CONFIG:cfgs>
+ string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
+ if(NOT MSVC AND NOT "${uppercase_CMAKE_BUILD_TYPE}" MATCHES DEBUG|RELWITHDEBINFO)
pybind11_strip(${target_name})
endif()
diff --git a/tools/setup_global.py.in b/tools/setup_global.py.in
index 4cf040b2..885ac5c7 100644
--- a/tools/setup_global.py.in
+++ b/tools/setup_global.py.in
@@ -1,17 +1,11 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
# Setup script for pybind11-global (in the sdist or in tools/setup_global.py in the repository)
# This package is targeted for easy use from CMake.
-import contextlib
import glob
import os
import re
-import shutil
-import subprocess
-import sys
-import tempfile
# Setuptools has to be before distutils
from setuptools import setup
@@ -33,8 +27,11 @@ class InstallHeadersNested(install_headers):
main_headers = glob.glob("pybind11/include/pybind11/*.h")
detail_headers = glob.glob("pybind11/include/pybind11/detail/*.h")
+eigen_headers = glob.glob("pybind11/include/pybind11/eigen/*.h")
+stl_headers = glob.glob("pybind11/include/pybind11/stl/*.h")
cmake_files = glob.glob("pybind11/share/cmake/pybind11/*.cmake")
-headers = main_headers + detail_headers
+pkgconfig_files = glob.glob("pybind11/share/pkgconfig/*.pc")
+headers = main_headers + detail_headers + stl_headers + eigen_headers
cmdclass = {"install_headers": InstallHeadersNested}
$extra_cmd
@@ -56,8 +53,11 @@ setup(
headers=headers,
data_files=[
(base + "share/cmake/pybind11", cmake_files),
+ (base + "share/pkgconfig", pkgconfig_files),
(base + "include/pybind11", main_headers),
(base + "include/pybind11/detail", detail_headers),
+ (base + "include/pybind11/eigen", eigen_headers),
+ (base + "include/pybind11/stl", stl_headers),
],
cmdclass=cmdclass,
)
diff --git a/tools/setup_main.py.in b/tools/setup_main.py.in
index 2231a08f..6358cc7b 100644
--- a/tools/setup_main.py.in
+++ b/tools/setup_main.py.in
@@ -1,5 +1,4 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
# Setup script (in the sdist or in tools/setup_main.py in the repository)
@@ -16,13 +15,19 @@ setup(
"pybind11",
"pybind11.include.pybind11",
"pybind11.include.pybind11.detail",
+ "pybind11.include.pybind11.eigen",
+ "pybind11.include.pybind11.stl",
"pybind11.share.cmake.pybind11",
+ "pybind11.share.pkgconfig",
],
package_data={
- "pybind11": ["py.typed", "*.pyi"],
+ "pybind11": ["py.typed"],
"pybind11.include.pybind11": ["*.h"],
"pybind11.include.pybind11.detail": ["*.h"],
+ "pybind11.include.pybind11.eigen": ["*.h"],
+ "pybind11.include.pybind11.stl": ["*.h"],
"pybind11.share.cmake.pybind11": ["*.cmake"],
+ "pybind11.share.pkgconfig": ["*.pc"],
},
extras_require={
"global": ["pybind11_global==$version"]
@@ -30,6 +35,9 @@ setup(
entry_points={
"console_scripts": [
"pybind11-config = pybind11.__main__:main",
+ ],
+ "pipx.run": [
+ "pybind11 = pybind11.__main__:main",
]
},
cmdclass=cmdclass