Skip to content

Commit 668a551

Browse files
authored
refactor: use common_labels.bzl for labels used across files (#3263)
Cleanup after the PR introducing the common labels file. Testing related labels (those starting with `//tests`) are left in `tests/support/support.bzl`. Only labels that are used in two or more files are moved into common_labels. This avoids obscuring otherwise simple assignments like defaults for attributes. It also acts as a signal that, if something is in common_labels.bzl, be ware it's used in multiple places. Only non-testing related labels (those not under `//tests`) are moved into common_labels.
1 parent 029a4dc commit 668a551

33 files changed

+106
-89
lines changed

python/BUILD.bazel

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ filegroup(
5656
bzl_library(
5757
name = "current_py_toolchain_bzl",
5858
srcs = ["current_py_toolchain.bzl"],
59+
deps = ["//python/private:toolchain_types_bzl"],
5960
)
6061

6162
bzl_library(
@@ -91,11 +92,9 @@ bzl_library(
9192
deps = [
9293
":py_binary_bzl",
9394
"//python/private:bzlmod_enabled_bzl",
94-
"//python/private:py_package.bzl",
95+
"//python/private:py_package_bzl",
9596
"//python/private:py_wheel_bzl",
96-
"//python/private:stamp_bzl",
9797
"//python/private:util_bzl",
98-
"//python/private:version.bzl",
9998
"@bazel_skylib//rules:native_binary",
10099
],
101100
)
@@ -215,7 +214,6 @@ bzl_library(
215214
deps = [
216215
"//python/private:py_runtime_info_bzl",
217216
"//python/private:reexports_bzl",
218-
"//python/private:util_bzl",
219217
"@rules_python_internal//:rules_python_config_bzl",
220218
],
221219
)

python/extensions/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ bzl_library(
4646
visibility = ["//:__subpackages__"],
4747
deps = [
4848
"//python/private:internal_config_repo_bzl",
49+
"//python/private/pypi:deps_bzl",
4950
],
5051
)

python/private/BUILD.bazel

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ bzl_library(
6666
deps = [
6767
":attr_builders_bzl",
6868
":common_bzl",
69+
":common_labels_bzl",
6970
":enum_bzl",
7071
":flags_bzl",
7172
":py_info_bzl",
@@ -356,6 +357,7 @@ bzl_library(
356357
name = "py_cc_toolchain_rule_bzl",
357358
srcs = ["py_cc_toolchain_rule.bzl"],
358359
deps = [
360+
":common_labels.bzl",
359361
":py_cc_toolchain_info_bzl",
360362
":rules_cc_srcs_bzl",
361363
":util_bzl",
@@ -390,6 +392,7 @@ bzl_library(
390392
srcs = ["py_exec_tools_toolchain.bzl"],
391393
deps = [
392394
":common_bzl",
395+
":common_labels_bzl",
393396
":py_exec_tools_info_bzl",
394397
":sentinel_bzl",
395398
":toolchain_types_bzl",
@@ -405,6 +408,7 @@ bzl_library(
405408
":attributes_bzl",
406409
":cc_helper_bzl",
407410
":common_bzl",
411+
":common_labels_bzl",
408412
":flags_bzl",
409413
":precompile_bzl",
410414
":py_cc_link_params_info_bzl",
@@ -456,6 +460,7 @@ bzl_library(
456460
deps = [
457461
":attributes_bzl",
458462
":common_bzl",
463+
":common_labels_bzl",
459464
":flags_bzl",
460465
":normalize_name_bzl",
461466
":precompile_bzl",
@@ -479,6 +484,7 @@ bzl_library(
479484
name = "py_library_rule_bzl",
480485
srcs = ["py_library_rule.bzl"],
481486
deps = [
487+
":common_labels_bzl",
482488
":py_library_bzl",
483489
],
484490
)
@@ -522,6 +528,7 @@ bzl_library(
522528
srcs = ["py_runtime_rule.bzl"],
523529
deps = [
524530
":attributes_bzl",
531+
":common_labels_bzl",
525532
":flags_bzl",
526533
":py_internal_bzl",
527534
":py_runtime_info_bzl",
@@ -545,6 +552,7 @@ bzl_library(
545552
name = "py_runtime_pair_rule_bzl",
546553
srcs = ["py_runtime_pair_rule.bzl"],
547554
deps = [
555+
":common_labels_bzl",
548556
"//python:py_runtime_bzl",
549557
"//python:py_runtime_info_bzl",
550558
"@bazel_skylib//rules:common_settings",
@@ -591,6 +599,7 @@ bzl_library(
591599
":py_package_bzl",
592600
":stamp_bzl",
593601
":transition_labels_bzl",
602+
":version_bzl",
594603
],
595604
)
596605

@@ -661,7 +670,7 @@ bzl_library(
661670
name = "transition_labels_bzl",
662671
srcs = ["transition_labels.bzl"],
663672
deps = [
664-
"common_labels_bzl",
673+
":common_labels_bzl",
665674
"@bazel_skylib//lib:collections",
666675
"@rules_python_internal//:extra_transition_settings_bzl",
667676
],

python/private/attributes.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts")
1717
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1818
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
1919
load(":attr_builders.bzl", "attrb")
20+
load(":common_labels.bzl", "labels")
2021
load(":enum.bzl", "enum")
2122
load(":flags.bzl", "PrecompileFlag", "PrecompileSourceRetentionFlag")
2223
load(":py_info.bzl", "PyInfo")
@@ -370,11 +371,11 @@ files that may be needed at run time belong in `data`.
370371
doc = "Defunct, unused, does nothing.",
371372
),
372373
"_precompile_flag": lambda: attrb.Label(
373-
default = "//python/config_settings:precompile",
374+
default = labels.PRECOMPILE,
374375
providers = [BuildSettingInfo],
375376
),
376377
"_precompile_source_retention_flag": lambda: attrb.Label(
377-
default = "//python/config_settings:precompile_source_retention",
378+
default = labels.PRECOMPILE_SOURCE_RETENTION,
378379
providers = [BuildSettingInfo],
379380
),
380381
# Force enabling auto exec groups, see

python/private/common_labels.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ labels = struct(
99
BOOTSTRAP_IMPL = str(Label("//python/config_settings:bootstrap_impl")),
1010
EXEC_TOOLS_TOOLCHAIN = str(Label("//python/config_settings:exec_tools_toolchain")),
1111
PIP_ENV_MARKER_CONFIG = str(Label("//python/config_settings:pip_env_marker_config")),
12-
PIP_WHL_MUSLC_VERSION = str(Label("//python/config_settings:pip_whl_muslc_version")),
12+
NONE = str(Label("//python:none")),
1313
PIP_WHL = str(Label("//python/config_settings:pip_whl")),
1414
PIP_WHL_GLIBC_VERSION = str(Label("//python/config_settings:pip_whl_glibc_version")),
15+
PIP_WHL_MUSLC_VERSION = str(Label("//python/config_settings:pip_whl_muslc_version")),
1516
PIP_WHL_OSX_ARCH = str(Label("//python/config_settings:pip_whl_osx_arch")),
1617
PIP_WHL_OSX_VERSION = str(Label("//python/config_settings:pip_whl_osx_version")),
1718
PRECOMPILE = str(Label("//python/config_settings:precompile")),
1819
PRECOMPILE_SOURCE_RETENTION = str(Label("//python/config_settings:precompile_source_retention")),
20+
PYC_COLLECTION = str(Label("//python/config_settings:pyc_collection")),
1921
PYTHON_SRC = str(Label("//python/bin:python_src")),
2022
PYTHON_VERSION = str(Label("//python/config_settings:python_version")),
2123
PYTHON_VERSION_MAJOR_MINOR = str(Label("//python/config_settings:python_version_major_minor")),
@@ -24,4 +26,5 @@ labels = struct(
2426
REPL_DEP = str(Label("//python/bin:repl_dep")),
2527
VENVS_SITE_PACKAGES = str(Label("//python/config_settings:venvs_site_packages")),
2628
VENVS_USE_DECLARE_SYMLINK = str(Label("//python/config_settings:venvs_use_declare_symlink")),
29+
VISIBLE_FOR_TESTING = str(Label("//python/private:visible_for_testing")),
2730
)

python/private/py_cc_toolchain_rule.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ https://github.com/bazel-contrib/rules_python/issues/824 is considered done.
2020

2121
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
2222
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
23+
load(":common_labels.bzl", "labels")
2324
load(":py_cc_toolchain_info.bzl", "PyCcToolchainInfo")
2425

2526
def _py_cc_toolchain_impl(ctx):
@@ -70,7 +71,7 @@ py_cc_toolchain = rule(
7071
mandatory = True,
7172
),
7273
"_visible_for_testing": attr.label(
73-
default = "//python/private:visible_for_testing",
74+
default = labels.VISIBLE_FOR_TESTING,
7475
),
7576
},
7677
doc = """\

python/private/py_exec_tools_toolchain.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load("@bazel_skylib//lib:paths.bzl", "paths")
1818
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
19+
load(":common_labels.bzl", "labels")
1920
load(":py_exec_tools_info.bzl", "PyExecToolsInfo")
2021
load(":sentinel.bzl", "SentinelInfo")
2122
load(":toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
@@ -89,7 +90,7 @@ so that the toolchain `py_runtime` field can be correctly forwarded.
8990
doc = "See {obj}`PyExecToolsInfo.precompiler`",
9091
),
9192
"_visible_for_testing": attr.label(
92-
default = "//python/private:visible_for_testing",
93+
default = labels.VISIBLE_FOR_TESTING,
9394
),
9495
},
9596
)

python/private/py_executable.bzl

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ load(
5151
"runfiles_root_path",
5252
"target_platform_has_any_constraint",
5353
)
54+
load(":common_labels.bzl", "labels")
5455
load(":flags.bzl", "BootstrapImplFlag", "VenvsUseDeclareSymlinkFlag")
5556
load(":precompile.bzl", "maybe_precompile")
5657
load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo")
@@ -60,18 +61,12 @@ load(":py_internal.bzl", "py_internal")
6061
load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo")
6162
load(":reexports.bzl", "BuiltinPyInfo", "BuiltinPyRuntimeInfo")
6263
load(":rule_builders.bzl", "ruleb")
63-
load(
64-
":toolchain_types.bzl",
65-
"EXEC_TOOLS_TOOLCHAIN_TYPE",
66-
"TARGET_TOOLCHAIN_TYPE",
67-
TOOLCHAIN_TYPE = "TARGET_TOOLCHAIN_TYPE",
68-
)
64+
load(":toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE", TOOLCHAIN_TYPE = "TARGET_TOOLCHAIN_TYPE")
6965
load(":transition_labels.bzl", "TRANSITION_LABELS")
7066

7167
_py_builtins = py_internal
7268
_EXTERNAL_PATH_PREFIX = "external"
7369
_ZIP_RUNFILES_DIRECTORY_NAME = "runfiles"
74-
_PYTHON_VERSION_FLAG = str(Label("//python/config_settings:python_version"))
7570

7671
# Non-Google-specific attributes for executables
7772
# These attributes are for rules that accept Python sources.
@@ -192,7 +187,7 @@ accepting arbitrary Python versions.
192187
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
193188
),
194189
"_bootstrap_impl_flag": lambda: attrb.Label(
195-
default = "//python/config_settings:bootstrap_impl",
190+
default = labels.BOOTSTRAP_IMPL,
196191
providers = [BuildSettingInfo],
197192
),
198193
"_bootstrap_template": lambda: attrb.Label(
@@ -222,10 +217,10 @@ accepting arbitrary Python versions.
222217
default = TARGET_TOOLCHAIN_TYPE,
223218
),
224219
"_python_version_flag": lambda: attrb.Label(
225-
default = "//python/config_settings:python_version",
220+
default = labels.PYTHON_VERSION,
226221
),
227222
"_venvs_use_declare_symlink_flag": lambda: attrb.Label(
228-
default = "//python/config_settings:venvs_use_declare_symlink",
223+
default = labels.VENVS_USE_DECLARE_SYMLINK,
229224
providers = [BuildSettingInfo],
230225
),
231226
"_windows_constraints": lambda: attrb.LabelList(
@@ -1910,7 +1905,7 @@ def _transition_executable_impl(settings, attr):
19101905
apply_config_settings_attr(settings, attr)
19111906

19121907
if attr.python_version and attr.python_version not in ("PY2", "PY3"):
1913-
settings[_PYTHON_VERSION_FLAG] = attr.python_version
1908+
settings[labels.PYTHON_VERSION] = attr.python_version
19141909
return settings
19151910

19161911
def create_executable_rule(*, attrs, **kwargs):
@@ -1961,8 +1956,8 @@ def create_executable_rule_builder(implementation, **kwargs):
19611956
],
19621957
cfg = dict(
19631958
implementation = _transition_executable_impl,
1964-
inputs = TRANSITION_LABELS + [_PYTHON_VERSION_FLAG],
1965-
outputs = TRANSITION_LABELS + [_PYTHON_VERSION_FLAG],
1959+
inputs = TRANSITION_LABELS + [labels.PYTHON_VERSION],
1960+
outputs = TRANSITION_LABELS + [labels.PYTHON_VERSION],
19661961
),
19671962
**kwargs
19681963
)

python/private/py_library.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ load(
4040
"get_imports",
4141
"runfiles_root_path",
4242
)
43+
load(":common_labels.bzl", "labels")
4344
load(":flags.bzl", "AddSrcsToRunfilesFlag", "PrecompileFlag", "VenvsSitePackages")
4445
load(":normalize_name.bzl", "normalize_name")
4546
load(":precompile.bzl", "maybe_precompile")
@@ -102,7 +103,7 @@ and that only one package version will be included.
102103
""",
103104
),
104105
"_add_srcs_to_runfiles_flag": lambda: attrb.Label(
105-
default = "//python/config_settings:add_srcs_to_runfiles",
106+
default = labels.ADD_SRCS_TO_RUNFILES,
106107
),
107108
},
108109
)

python/private/py_runtime_pair_rule.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1818
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
19+
load(":common_labels.bzl", "labels")
1920
load(":reexports.bzl", "BuiltinPyRuntimeInfo")
2021
load(":util.bzl", "IS_BAZEL_7_OR_HIGHER")
2122

@@ -94,7 +95,7 @@ The runtime to use for Python 3 targets. Must have `python_version` set to
9495
""",
9596
),
9697
"_visible_for_testing": attr.label(
97-
default = "//python/private:visible_for_testing",
98+
default = labels.VISIBLE_FOR_TESTING,
9899
),
99100
},
100101
fragments = ["py"],

0 commit comments

Comments
 (0)