77import setuptools .command .build_ext
88import shutil
99import sys
10+ import sysconfig
1011
1112long_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
82163def options ():
83164 bdist_wheel = {}
0 commit comments