Skip to content

Commit 116ddcf

Browse files
authored
Merge branch 'main' into silence-compile-warnings
2 parents 0e5403a + 3f6daa8 commit 116ddcf

15 files changed

Lines changed: 209 additions & 12 deletions

File tree

.bazelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ sphinxdocs
3333
tests/integration/compile_pip_requirements/bazel-compile_pip_requirements
3434
tests/integration/local_toolchains/bazel-local_toolchains
3535
tests/integration/py_cc_toolchain_registered/bazel-py_cc_toolchain_registered
36+
tests/integration/toolchain_target_settings/bazel-module_under_test

.bazelrc.deleted_packages

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ common --deleted_packages=tests/integration/local_toolchains
3636
common --deleted_packages=tests/integration/pip_parse
3737
common --deleted_packages=tests/integration/pip_parse/empty
3838
common --deleted_packages=tests/integration/py_cc_toolchain_registered
39+
common --deleted_packages=tests/integration/toolchain_target_settings
3940
common --deleted_packages=tests/modules/another_module
4041
common --deleted_packages=tests/modules/other
4142
common --deleted_packages=tests/modules/other/nspkg_delta

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ END_UNRELEASED_TEMPLATE
6868

6969
{#v0-0-0-added}
7070
### Added
71+
* (toolchain) Added {obj}`python.override.toolchain_target_settings` to allow
72+
adding `config_setting` labels to all registered toolchains.
7173
* (windows) Full venv support for Windows is available. Set
7274
{obj}`--venvs_site_packages=yes` to enable.
7375
* (runfiles) Added a pathlib-compatible API: {obj}`Runfiles.root()`

python/private/pypi/whl_library.bzl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -546,26 +546,28 @@ def _whl_library_impl(rctx):
546546
rctx.file("MODULE.bazel")
547547
rctx.file("REPO.bazel")
548548

549+
# BUILD files interfere with globbing and Bazel package boundaries.
550+
_remove_files(rctx, "BUILD", "BUILD.bazel")
551+
rctx.file("BUILD.bazel", build_file_contents)
552+
553+
if enable_pipstar and enable_pipstar_extract:
554+
if hasattr(rctx, "repo_metadata"):
555+
return rctx.repo_metadata(reproducible = True)
556+
557+
return None
558+
559+
def _remove_files(rctx, *basenames):
549560
paths = list(rctx.path(".").readdir())
550561
for _ in range(10000000):
551562
if not paths:
552563
break
553564
path = paths.pop()
554565

555-
# BUILD files interfere with globbing and Bazel package boundaries.
556-
if path.basename in ("BUILD", "BUILD.bazel"):
566+
if path.basename in basenames:
557567
rctx.delete(path)
558568
elif path.is_dir:
559569
paths.extend(path.readdir())
560570

561-
rctx.file("BUILD.bazel", build_file_contents)
562-
563-
if enable_pipstar and enable_pipstar_extract:
564-
if hasattr(rctx, "repo_metadata"):
565-
return rctx.repo_metadata(reproducible = True)
566-
567-
return None
568-
569571
def _generate_entry_point_contents(
570572
module,
571573
attribute,

python/private/python.bzl

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ def _python_impl(module_ctx):
403403
# the PLATFORMS global for this toolchain
404404
toolchain_platform_keys = {}
405405

406+
# Extra target_settings to add to every registered toolchain, e.g. for
407+
# gating the default toolchains behind a custom config_setting.
408+
global_add_target_settings = py.config.add_target_settings
409+
406410
# Split the toolchain info into separate objects so they can be passed onto
407411
# the repository rule.
408412
for entry in toolchain_impls:
@@ -414,7 +418,7 @@ def _python_impl(module_ctx):
414418

415419
# The target_settings attribute may not be present for users
416420
# patching python/versions.bzl.
417-
toolchain_ts_map[key] = getattr(entry.platform, "target_settings", [])
421+
toolchain_ts_map[key] = getattr(entry.platform, "target_settings", []) + global_add_target_settings
418422
toolchain_platform_keys[key] = entry.platform_name
419423
toolchain_python_versions[key] = entry.full_python_version
420424

@@ -702,6 +706,9 @@ def _process_global_overrides(*, tag, default, _fail = fail):
702706

703707
default["minor_mapping"] = tag.minor_mapping
704708

709+
if tag.add_target_settings:
710+
default["add_target_settings"] = list(tag.add_target_settings)
711+
705712
forwarded_attrs = sorted(AUTH_ATTRS) + [
706713
"base_url",
707714
"register_all_versions",
@@ -809,6 +816,7 @@ def _get_toolchain_config(*, modules, _fail = fail):
809816
)
810817

811818
register_all_versions = default.pop("register_all_versions", False)
819+
add_target_settings = default.pop("add_target_settings", [])
812820
kwargs = default.pop("kwargs", {})
813821

814822
versions = {}
@@ -834,6 +842,7 @@ def _get_toolchain_config(*, modules, _fail = fail):
834842
minor_mapping = minor_mapping,
835843
default = default,
836844
register_all_versions = register_all_versions,
845+
add_target_settings = add_target_settings,
837846
)
838847

839848
def _compute_default_python_version(mctx):
@@ -1099,6 +1108,30 @@ _override = tag_class(
10991108
:::
11001109
""",
11011110
attrs = {
1111+
"add_target_settings": attr.string_list(
1112+
mandatory = False,
1113+
doc = """\
1114+
A list of `config_setting` labels to add to the `target_settings` of every
1115+
toolchain registered by this module extension. This is useful for creating
1116+
separate "families" of toolchains gated behind custom build settings.
1117+
1118+
For example, to ensure the default prebuilt toolchains are only resolved when
1119+
a `prebuilt` config setting is active:
1120+
1121+
```starlark
1122+
python.override(
1123+
add_target_settings = ["@@//:python_toolchain_family_prebuilt"],
1124+
)
1125+
```
1126+
1127+
These settings are appended to the `target_settings` of all toolchains
1128+
registered by the extension, including any that already have settings
1129+
from `python.single_version_platform_override`.
1130+
1131+
:::{versionadded} VERSION_NEXT_FEATURE
1132+
:::
1133+
""",
1134+
),
11021135
"available_python_versions": attr.string_list(
11031136
mandatory = False,
11041137
doc = """\

tests/integration/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ rules_python_integration_test(
8888
py_main = "custom_commands_test.py",
8989
)
9090

91+
rules_python_integration_test(
92+
name = "toolchain_target_settings_test",
93+
py_main = "toolchain_target_settings_test.py",
94+
)
95+
9196
py_library(
9297
name = "runner_lib",
9398
srcs = ["runner.py"],
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
common --lockfile_mode=off
2+
test --test_output=errors
3+
build --enable_runfiles
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2+
load("@rules_python//python:py_test.bzl", "py_test")
3+
4+
# A flag to select which "family" of toolchains to use.
5+
string_flag(
6+
name = "family",
7+
build_setting_default = "prebuilt",
8+
values = [
9+
"prebuilt",
10+
"custom",
11+
],
12+
)
13+
14+
# Matches when the "prebuilt" family is selected.
15+
# This is referenced in MODULE.bazel's python.override(add_target_settings=...).
16+
config_setting(
17+
name = "is_prebuilt",
18+
flag_values = {
19+
":family": "prebuilt",
20+
},
21+
)
22+
23+
# Matches when the "custom" family is selected.
24+
# No toolchains use this setting, so selecting it should produce an error.
25+
config_setting(
26+
name = "is_custom",
27+
flag_values = {
28+
":family": "custom",
29+
},
30+
)
31+
32+
# This target selects the "prebuilt" family via config_settings transition.
33+
# Since python.override(add_target_settings = ["@@//:is_prebuilt"]) gates
34+
# the default toolchains, this should succeed: the flag matches, the config_setting
35+
# is satisfied, and the default 3.13 toolchain resolves.
36+
py_test(
37+
name = "prebuilt_test",
38+
srcs = ["main.py"],
39+
config_settings = {
40+
"//:family": "prebuilt",
41+
},
42+
main = "main.py",
43+
)
44+
45+
# This target selects the "custom" family via config_settings transition.
46+
# No toolchains have target_settings = [":is_custom"], so toolchain resolution
47+
# should fail -- the default toolchains are gated behind ":is_prebuilt" and
48+
# won't match.
49+
py_test(
50+
name = "custom_no_toolchain_test",
51+
srcs = ["main.py"],
52+
config_settings = {
53+
"//:family": "custom",
54+
},
55+
main = "main.py",
56+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module(name = "module_under_test")
2+
3+
bazel_dep(name = "rules_python", version = "0.0.0")
4+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
5+
6+
local_path_override(
7+
module_name = "rules_python",
8+
path = "../../..",
9+
)
10+
11+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
12+
python.toolchain(python_version = "3.13")
13+
14+
# Gate ALL default-registered toolchains behind the "prebuilt" config setting.
15+
# This prevents them from being a silent fallback when a different toolchain
16+
# family is requested.
17+
python.override(
18+
add_target_settings = ["@@//:is_prebuilt"],
19+
)
20+
21+
# Register //:family as a transition setting so py_binary/py_test
22+
# config_settings can set it.
23+
config = use_extension("@rules_python//python/extensions:config.bzl", "config")
24+
config.add_transition_setting(setting = "//:family")

tests/integration/toolchain_target_settings/REPO.bazel

Whitespace-only changes.

0 commit comments

Comments
 (0)