@@ -34,6 +34,7 @@ load(":coverage_deps.bzl", "coverage_dep")
3434load (":full_version.bzl" , "full_version" )
3535load (":internal_config_repo.bzl" , "internal_config_repo" )
3636load (":repo_utils.bzl" , "REPO_DEBUG_ENV_VAR" , "repo_utils" )
37+ load (":text_util.bzl" , "render" )
3738load (
3839 ":toolchains_repo.bzl" ,
3940 "host_toolchain" ,
@@ -246,20 +247,6 @@ def _python_repository_impl(rctx):
246247
247248 python_bin = "python.exe" if ("windows" in platform ) else "bin/python3"
248249
249- glob_include = []
250- glob_exclude = [
251- "**/* *" , # Bazel does not support spaces in file names.
252- # Unused shared libraries. `python` executable and the `:libpython` target
253- # depend on `libpython{python_version}.so.1.0`.
254- "lib/libpython{python_version}.so" .format (python_version = python_short_version ),
255- # static libraries
256- "lib/**/*.a" ,
257- # tests for the standard libraries.
258- "lib/python{python_version}/**/test/**" .format (python_version = python_short_version ),
259- "lib/python{python_version}/**/tests/**" .format (python_version = python_short_version ),
260- "**/__pycache__/*.pyc.*" , # During pyc creation, temp files named *.pyc.NNN are created
261- ]
262-
263250 if "linux" in platform :
264251 # Workaround around https://github.com/indygreg/python-build-standalone/issues/231
265252 for url in urls :
@@ -281,6 +268,8 @@ def _python_repository_impl(rctx):
281268 rctx .delete ("share/terminfo" )
282269 break
283270
271+ glob_include = []
272+ glob_exclude = []
284273 if rctx .attr .ignore_root_user_error or "windows" in platform :
285274 glob_exclude += [
286275 # These pycache files are created on first use of the associated python files.
@@ -295,148 +284,42 @@ def _python_repository_impl(rctx):
295284 glob_include += [
296285 "*.exe" ,
297286 "*.dll" ,
298- "bin/**" ,
299287 "DLLs/**" ,
300- "extensions/**" ,
301- "include/**" ,
302288 "Lib/**" ,
303- "libs/**" ,
304289 "Scripts/**" ,
305- "share/**" ,
306290 "tcl/**" ,
307291 ]
308292 else :
309- glob_include += [
310- "bin/**" ,
311- "extensions/**" ,
312- "include/**" ,
293+ glob_include .append (
313294 "lib/**" ,
314- "libs/**" ,
315- "share/**" ,
316- ]
295+ )
317296
318- if rctx .attr .coverage_tool :
319- if "windows" in platform :
320- coverage_tool = None
321- else :
322- coverage_tool = '"{}"' .format (rctx .attr .coverage_tool )
323-
324- coverage_attr_text = """\
325- coverage_tool = select({{
326- ":coverage_enabled": {coverage_tool},
327- "//conditions:default": None
328- }}),
329- """ .format (coverage_tool = coverage_tool )
297+ if "windows" in platform :
298+ coverage_tool = None
330299 else :
331- coverage_attr_text = " # coverage_tool attribute not supported by this Bazel version"
300+ coverage_tool = rctx . attr . coverage_tool
332301
333302 build_content = """\
334- # Generated by python/repositories .bzl
303+ # Generated by python/private/python_repositories .bzl
335304
336- load("@rules_python//python:py_runtime.bzl", "py_runtime")
337- load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
338- load("@rules_python//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
339- load("@rules_python//python/private:py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
305+ load("@rules_python//python/private:hermetic_runtime_repo_setup.bzl", "define_hermetic_runtime_toolchain_impl")
340306
341307package(default_visibility = ["//visibility:public"])
342308
343- filegroup(
344- name = "files",
345- srcs = glob(
346- include = {glob_include},
347- # Platform-agnostic filegroup can't match on all patterns.
348- allow_empty = True,
349- exclude = {glob_exclude},
350- ),
351- )
352-
353- cc_import(
354- name = "interface",
355- interface_library = "libs/python{python_version_nodot}.lib",
356- system_provided = True,
357- )
358-
359- filegroup(
360- name = "includes",
361- srcs = glob(["include/**/*.h"]),
362- )
363-
364- cc_library(
365- name = "python_headers",
366- deps = select({{
367- "@bazel_tools//src/conditions:windows": [":interface"],
368- "//conditions:default": None,
369- }}),
370- hdrs = [":includes"],
371- includes = [
372- "include",
373- "include/python{python_version}",
374- "include/python{python_version}m",
375- ],
376- )
377-
378- cc_library(
379- name = "libpython",
380- hdrs = [":includes"],
381- srcs = select({{
382- "@platforms//os:windows": ["python3.dll", "libs/python{python_version_nodot}.lib"],
383- "@platforms//os:macos": ["lib/libpython{python_version}.dylib"],
384- "@platforms//os:linux": ["lib/libpython{python_version}.so", "lib/libpython{python_version}.so.1.0"],
385- }}),
386- )
387-
388- exports_files(["python", "{python_path}"])
389-
390- # Used to only download coverage toolchain when the coverage is collected by
391- # bazel.
392- config_setting(
393- name = "coverage_enabled",
394- values = {{"collect_code_coverage": "true"}},
395- visibility = ["//visibility:private"],
396- )
397-
398- py_runtime(
399- name = "py3_runtime",
400- files = [":files"],
401- {coverage_attr}
402- interpreter = "{python_path}",
403- interpreter_version_info = {{
404- "major": "{interpreter_version_info_major}",
405- "minor": "{interpreter_version_info_minor}",
406- "micro": "{interpreter_version_info_micro}",
407- }},
408- python_version = "PY3",
409- implementation_name = 'cpython',
410- pyc_tag = "cpython-{interpreter_version_info_major}{interpreter_version_info_minor}",
411- )
412-
413- py_runtime_pair(
414- name = "python_runtimes",
415- py2_runtime = None,
416- py3_runtime = ":py3_runtime",
417- )
418-
419- py_cc_toolchain(
420- name = "py_cc_toolchain",
421- headers = ":python_headers",
422- libs = ":libpython",
423- python_version = "{python_version}",
424- )
425-
426- py_exec_tools_toolchain(
427- name = "py_exec_tools_toolchain",
428- precompiler = "@rules_python//tools/precompiler:precompiler",
309+ define_hermetic_runtime_toolchain_impl(
310+ name = "define_runtime",
311+ extra_files_glob_include = {extra_files_glob_include},
312+ extra_files_glob_exclude = {extra_files_glob_exclude},
313+ python_version = {python_version},
314+ python_bin = {python_bin},
315+ coverage_tool = {coverage_tool},
429316)
430317""" .format (
431- glob_exclude = repr (glob_exclude ),
432- glob_include = repr (glob_include ),
433- python_path = python_bin ,
434- python_version = python_short_version ,
435- python_version_nodot = python_short_version .replace ("." , "" ),
436- coverage_attr = coverage_attr_text ,
437- interpreter_version_info_major = python_version_info [0 ],
438- interpreter_version_info_minor = python_version_info [1 ],
439- interpreter_version_info_micro = python_version_info [2 ],
318+ extra_files_glob_exclude = render .list (glob_exclude ),
319+ extra_files_glob_include = render .list (glob_include ),
320+ python_bin = render .str (python_bin ),
321+ python_version = render .str (rctx .attr .python_version ),
322+ coverage_tool = render .str (coverage_tool ),
440323 )
441324 rctx .delete ("python" )
442325 rctx .symlink (python_bin , "python" )
0 commit comments