Skip to content

Commit 2aa303b

Browse files
committed
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>
1 parent ed9fc26 commit 2aa303b

2 files changed

Lines changed: 88 additions & 11 deletions

File tree

MODULE.bazel

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ bazel_dep(name = "platforms", version = "0.0.8")
1414
bazel_dep(name = "apple_support", version = "1.12.0", repo_name = "build_bazel_apple_support")
1515
bazel_dep(name = "rules_cc", version = "0.0.9")
1616
bazel_dep(name = "abseil-cpp", version = "20240116.0", repo_name = "com_google_absl")
17-
bazel_dep(name = "rules_python", version = "0.29.0")
18-
bazel_dep(name = "pybind11_bazel", version = "2.11.1.bzl.1")
17+
bazel_dep(name = "rules_python", version = "0.31.0")
18+
bazel_dep(name = "pybind11_bazel", version = "2.11.1.bzl.2")
1919

2020
# This is a temporary hack for `x64_x86_windows`.
2121
# TODO(junyer): Remove whenever no longer needed.
2222
cc_configure = use_extension("@bazel_tools//tools/cpp:cc_configure.bzl", "cc_configure_extension")
2323
use_repo(cc_configure, "local_config_cc")
2424

25-
python_configure = use_extension("@pybind11_bazel//:python_configure.bzl", "extension")
26-
python_configure.toolchain(python_version = "3") # ignored when non-root module
27-
use_repo(python_configure, "local_config_python", "pybind11")
28-
2925
# These dependencies will be ignored when the `re2` module is not
3026
# the root module (or when `--ignore_dev_dependency` is enabled).
3127
bazel_dep(name = "google_benchmark", version = "1.8.3", dev_dependency = True)

python/setup.py

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import setuptools.command.build_ext
88
import shutil
99
import sys
10+
import sysconfig
1011

1112
long_description = r"""A drop-in replacement for the re module.
1213
@@ -48,9 +49,6 @@ def build_extension(self, ext):
4849
if 'GITHUB_ACTIONS' not in os.environ:
4950
return super().build_extension(ext)
5051

51-
# For @pybind11_bazel's `python_configure()`.
52-
os.environ['PYTHON_BIN_PATH'] = sys.executable
53-
5452
cmd = ['bazel', 'build']
5553
try:
5654
cpu = os.environ['BAZEL_CPU']
@@ -63,8 +61,9 @@ def build_extension(self, ext):
6361
cmd.append(f'--extra_toolchains=@local_config_cc//:cc-toolchain-{cpu}')
6462
except KeyError:
6563
pass
66-
# Register the local Python toolchain with highest priority.
67-
cmd.append('--extra_toolchains=@local_config_python//:py_toolchain')
64+
# Register the local Python toolchains with highest priority.
65+
self.generate_python_toolchains()
66+
cmd.append('--extra_toolchains=//python/toolchains:all')
6867
# Print debug information during toolchain resolution.
6968
cmd.append('--toolchain_resolution_debug=.*')
7069
cmd += ['--compilation_mode=opt', '--', ':all']
@@ -78,6 +77,88 @@ def build_extension(self, ext):
7877
cmd = ['bazel', 'clean', '--expunge']
7978
self.spawn(cmd)
8079

80+
def generate_python_toolchains(self):
81+
include = sysconfig.get_path('include')
82+
libs = os.path.join(include, '../libs')
83+
84+
os.makedirs('toolchains')
85+
shutil.copytree(include, 'toolchains/include')
86+
try:
87+
shutil.copytree(libs, 'toolchains/libs')
88+
except FileNotFoundError:
89+
# We must not be running on Windows. :)
90+
pass
91+
92+
with open('toolchains/BUILD.bazel', 'x') as file:
93+
file.write(
94+
"""\
95+
load("@rules_python//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
96+
load("@rules_python//python:py_runtime.bzl", "py_runtime")
97+
load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
98+
99+
package(default_visibility = ["//visibility:public"])
100+
101+
toolchain(
102+
name = "py",
103+
toolchain = ":py_toolchain",
104+
toolchain_type = "@rules_python//python:toolchain_type",
105+
)
106+
107+
py_runtime_pair(
108+
name = "py_toolchain",
109+
py3_runtime = ":interpreter",
110+
)
111+
112+
py_runtime(
113+
name = "interpreter",
114+
interpreter_path = "{interpreter_path}",
115+
interpreter_version_info = {{
116+
"major": "{major}",
117+
"minor": "{minor}",
118+
}},
119+
python_version = "PY3",
120+
)
121+
122+
toolchain(
123+
name = "py_cc",
124+
toolchain = ":py_cc_toolchain",
125+
toolchain_type = "@rules_python//python/cc:toolchain_type",
126+
)
127+
128+
py_cc_toolchain(
129+
name = "py_cc_toolchain",
130+
headers = ":headers",
131+
libs = ":libraries",
132+
python_version = "{major}.{minor}",
133+
)
134+
135+
cc_library(
136+
name = "headers",
137+
hdrs = glob(["include/**/*.h"]),
138+
includes = ["include"],
139+
deps = select({{
140+
"@platforms//os:windows": [":interface_library"],
141+
"//conditions:default": [],
142+
}}),
143+
)
144+
145+
cc_import(
146+
name = "interface_library",
147+
interface_library = select({{
148+
"@platforms//os:windows": "libs/python{major}{minor}.lib",
149+
"//conditions:default": None,
150+
}}),
151+
system_provided = True,
152+
)
153+
154+
# Not actually necessary for our purposes. :)
155+
cc_library(
156+
name = "libraries",
157+
)
158+
""".format(interpreter_path=sys.executable,
159+
major=sys.version_info.major,
160+
minor=sys.version_info.minor))
161+
81162

82163
def options():
83164
bdist_wheel = {}

0 commit comments

Comments
 (0)