@@ -34,6 +34,7 @@ load(":coverage_deps.bzl", "coverage_dep")
34
34
load (":full_version.bzl" , "full_version" )
35
35
load (":internal_config_repo.bzl" , "internal_config_repo" )
36
36
load (":repo_utils.bzl" , "REPO_DEBUG_ENV_VAR" , "repo_utils" )
37
+ load (":text_util.bzl" , "render" )
37
38
load (
38
39
":toolchains_repo.bzl" ,
39
40
"host_toolchain" ,
@@ -246,20 +247,6 @@ def _python_repository_impl(rctx):
246
247
247
248
python_bin = "python.exe" if ("windows" in platform ) else "bin/python3"
248
249
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
-
263
250
if "linux" in platform :
264
251
# Workaround around https://github.com/indygreg/python-build-standalone/issues/231
265
252
for url in urls :
@@ -281,6 +268,8 @@ def _python_repository_impl(rctx):
281
268
rctx .delete ("share/terminfo" )
282
269
break
283
270
271
+ glob_include = []
272
+ glob_exclude = []
284
273
if rctx .attr .ignore_root_user_error or "windows" in platform :
285
274
glob_exclude += [
286
275
# These pycache files are created on first use of the associated python files.
@@ -295,148 +284,42 @@ def _python_repository_impl(rctx):
295
284
glob_include += [
296
285
"*.exe" ,
297
286
"*.dll" ,
298
- "bin/**" ,
299
287
"DLLs/**" ,
300
- "extensions/**" ,
301
- "include/**" ,
302
288
"Lib/**" ,
303
- "libs/**" ,
304
289
"Scripts/**" ,
305
- "share/**" ,
306
290
"tcl/**" ,
307
291
]
308
292
else :
309
- glob_include += [
310
- "bin/**" ,
311
- "extensions/**" ,
312
- "include/**" ,
293
+ glob_include .append (
313
294
"lib/**" ,
314
- "libs/**" ,
315
- "share/**" ,
316
- ]
295
+ )
317
296
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
330
299
else :
331
- coverage_attr_text = " # coverage_tool attribute not supported by this Bazel version"
300
+ coverage_tool = rctx . attr . coverage_tool
332
301
333
302
build_content = """\
334
- # Generated by python/repositories .bzl
303
+ # Generated by python/private/python_repositories .bzl
335
304
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")
340
306
341
307
package(default_visibility = ["//visibility:public"])
342
308
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},
429
316
)
430
317
""" .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 ),
440
323
)
441
324
rctx .delete ("python" )
442
325
rctx .symlink (python_bin , "python" )
0 commit comments