Update @pybind11_bazel to version 2.11.1.bzl.2.

It now uses rules_python, which makes it drastically simpler
and also means that hermetic Python toolchains are used by
default. However, we want local Python toolchains to be used
when building wheels, so we have to do a little dance there.

Change-Id: I0dcd55522aca40aae0de0535439b714eeb85911a
Reviewed-on: https://code-review.googlesource.com/c/re2/+/62710
Reviewed-by: Alex Chernyakhovsky <achernya@google.com>
Reviewed-by: Paul Wankadia <junyer@google.com>
This commit is contained in:
Paul Wankadia
2024-02-16 16:30:47 +00:00
parent ed9fc269e2
commit 2aa303b697
2 changed files with 88 additions and 11 deletions

View File

@@ -7,6 +7,7 @@ import setuptools
import setuptools.command.build_ext
import shutil
import sys
import sysconfig
long_description = r"""A drop-in replacement for the re module.
@@ -48,9 +49,6 @@ class BuildExt(setuptools.command.build_ext.build_ext):
if 'GITHUB_ACTIONS' not in os.environ:
return super().build_extension(ext)
# For @pybind11_bazel's `python_configure()`.
os.environ['PYTHON_BIN_PATH'] = sys.executable
cmd = ['bazel', 'build']
try:
cpu = os.environ['BAZEL_CPU']
@@ -63,8 +61,9 @@ class BuildExt(setuptools.command.build_ext.build_ext):
cmd.append(f'--extra_toolchains=@local_config_cc//:cc-toolchain-{cpu}')
except KeyError:
pass
# Register the local Python toolchain with highest priority.
cmd.append('--extra_toolchains=@local_config_python//:py_toolchain')
# Register the local Python toolchains with highest priority.
self.generate_python_toolchains()
cmd.append('--extra_toolchains=//python/toolchains:all')
# Print debug information during toolchain resolution.
cmd.append('--toolchain_resolution_debug=.*')
cmd += ['--compilation_mode=opt', '--', ':all']
@@ -78,6 +77,88 @@ class BuildExt(setuptools.command.build_ext.build_ext):
cmd = ['bazel', 'clean', '--expunge']
self.spawn(cmd)
def generate_python_toolchains(self):
include = sysconfig.get_path('include')
libs = os.path.join(include, '../libs')
os.makedirs('toolchains')
shutil.copytree(include, 'toolchains/include')
try:
shutil.copytree(libs, 'toolchains/libs')
except FileNotFoundError:
# We must not be running on Windows. :)
pass
with open('toolchains/BUILD.bazel', 'x') as file:
file.write(
"""\
load("@rules_python//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
load("@rules_python//python:py_runtime.bzl", "py_runtime")
load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
package(default_visibility = ["//visibility:public"])
toolchain(
name = "py",
toolchain = ":py_toolchain",
toolchain_type = "@rules_python//python:toolchain_type",
)
py_runtime_pair(
name = "py_toolchain",
py3_runtime = ":interpreter",
)
py_runtime(
name = "interpreter",
interpreter_path = "{interpreter_path}",
interpreter_version_info = {{
"major": "{major}",
"minor": "{minor}",
}},
python_version = "PY3",
)
toolchain(
name = "py_cc",
toolchain = ":py_cc_toolchain",
toolchain_type = "@rules_python//python/cc:toolchain_type",
)
py_cc_toolchain(
name = "py_cc_toolchain",
headers = ":headers",
libs = ":libraries",
python_version = "{major}.{minor}",
)
cc_library(
name = "headers",
hdrs = glob(["include/**/*.h"]),
includes = ["include"],
deps = select({{
"@platforms//os:windows": [":interface_library"],
"//conditions:default": [],
}}),
)
cc_import(
name = "interface_library",
interface_library = select({{
"@platforms//os:windows": "libs/python{major}{minor}.lib",
"//conditions:default": None,
}}),
system_provided = True,
)
# Not actually necessary for our purposes. :)
cc_library(
name = "libraries",
)
""".format(interpreter_path=sys.executable,
major=sys.version_info.major,
minor=sys.version_info.minor))
def options():
bdist_wheel = {}