Skip to content

Commit 7c5e7cf

Browse files
authored
refactor: fold bazel-specific code into main files (bazel-contrib#2358)
This removes most of the `*_bazel.bzl` files and either renames them or folds them into the "regular" files. (common_bazel.bzl and py_executable_bazel.bzl turned out to be a bit more involved, so are left for a future change). This is part of simplifying the implementation and removing the complicated/convoluted "plugin" style design that was inherited from the original Bazel code to accommodate various Google changes.
1 parent 3e552df commit 7c5e7cf

18 files changed

+85
-117
lines changed

docs/BUILD.bazel

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ sphinx_stardocs(
9797
"//python/cc:py_cc_toolchain_bzl",
9898
"//python/cc:py_cc_toolchain_info_bzl",
9999
"//python/entry_points:py_console_script_binary_bzl",
100-
"//python/private:py_binary_rule_bazel_bzl",
100+
"//python/private:py_binary_rule_bzl",
101101
"//python/private:py_cc_toolchain_rule_bzl",
102-
"//python/private:py_library_rule_bazel_bzl",
102+
"//python/private:py_library_rule_bzl",
103103
"//python/private:py_runtime_rule_bzl",
104-
"//python/private:py_test_rule_bazel_bzl",
104+
"//python/private:py_test_rule_bzl",
105105
"//python/private/api:py_common_api_bzl",
106106
] + ([
107107
# Bazel 6 + Stardoc isn't able to parse something about the python bzlmod extension

docs/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
6969
"api/python/defs": "/api/rules_python/python/defs.html",
7070
"api/python/index": "/api/rules_python/python/index.html",
7171
"api/python/py_runtime_info": "/api/rules_python/python/py_runtime_info.html",
72-
"api/python/private/common/py_library_rule_bazel": "/api/rules_python/python/private/py_library_rule_bazel.html",
72+
"api/python/private/common/py_library_rule_bazel": "/api/rules_python/python/private/py_library_rule.html",
7373
"api/python/private/common/py_test_rule_bazel": "/api/rules_python/python/private/py_test_rule_bazel.html",
74-
"api/python/private/common/py_binary_rule_bazel": "/api/rules_python/python/private/py_binary_rule_bazel.html",
74+
"api/python/private/common/py_binary_rule_bazel": "/api/rules_python/python/private/py_binary_rule.html",
7575
"api/python/private/common/py_runtime_rule": "/api/rules_python/python/private/py_runtime_rule.html",
7676
"api/python/extensions/pip": "/api/rules_python/python/extensions/pip.html",
7777
"api/python/extensions/python": "/api/rules_python/python/extensions/python.html",

python/BUILD.bazel

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ bzl_library(
124124
name = "py_binary_bzl",
125125
srcs = ["py_binary.bzl"],
126126
deps = [
127-
"//python/private:py_binary_macro_bazel_bzl",
127+
"//python/private:py_binary_macro_bzl",
128128
"//python/private:register_extension_info_bzl",
129129
"//python/private:util_bzl",
130130
"@rules_python_internal//:rules_python_config_bzl",
@@ -178,7 +178,7 @@ bzl_library(
178178
name = "py_library_bzl",
179179
srcs = ["py_library.bzl"],
180180
deps = [
181-
"//python/private:py_library_macro_bazel_bzl",
181+
"//python/private:py_library_macro_bzl",
182182
"//python/private:register_extension_info_bzl",
183183
"//python/private:util_bzl",
184184
"@rules_python_internal//:rules_python_config_bzl",
@@ -219,7 +219,7 @@ bzl_library(
219219
name = "py_test_bzl",
220220
srcs = ["py_test.bzl"],
221221
deps = [
222-
"//python/private:py_test_macro_bazel_bzl",
222+
"//python/private:py_test_macro_bzl",
223223
"//python/private:register_extension_info_bzl",
224224
"//python/private:util_bzl",
225225
"@rules_python_internal//:rules_python_config_bzl",

python/private/BUILD.bazel

+16-23
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ filegroup(
5151
visibility = ["//python:__pkg__"],
5252
)
5353

54-
bzl_library(
55-
name = "attributes_bazel_bzl",
56-
srcs = ["attributes_bazel.bzl"],
57-
deps = ["//python/private:rules_cc_srcs_bzl"],
58-
)
59-
6054
bzl_library(
6155
name = "attributes_bzl",
6256
srcs = ["attributes.bzl"],
@@ -260,17 +254,17 @@ bzl_library(
260254
)
261255

262256
bzl_library(
263-
name = "py_binary_macro_bazel_bzl",
264-
srcs = ["py_binary_macro_bazel.bzl"],
257+
name = "py_binary_macro_bzl",
258+
srcs = ["py_binary_macro.bzl"],
265259
deps = [
266260
":common_bzl",
267-
":py_binary_rule_bazel_bzl",
261+
":py_binary_rule_bzl",
268262
],
269263
)
270264

271265
bzl_library(
272-
name = "py_binary_rule_bazel_bzl",
273-
srcs = ["py_binary_rule_bazel.bzl"],
266+
name = "py_binary_rule_bzl",
267+
srcs = ["py_binary_rule.bzl"],
274268
deps = [
275269
":attributes_bzl",
276270
":py_executable_bazel_bzl",
@@ -345,7 +339,7 @@ bzl_library(
345339
name = "py_executable_bazel_bzl",
346340
srcs = ["py_executable_bazel.bzl"],
347341
deps = [
348-
":attributes_bazel_bzl",
342+
":attributes_bzl",
349343
":common_bazel_bzl",
350344
":common_bzl",
351345
":py_executable_bzl",
@@ -420,16 +414,15 @@ bzl_library(
420414
)
421415

422416
bzl_library(
423-
name = "py_library_macro_bazel_bzl",
424-
srcs = ["py_library_macro_bazel.bzl"],
425-
deps = [":py_library_rule_bazel_bzl"],
417+
name = "py_library_macro_bzl",
418+
srcs = ["py_library_macro.bzl"],
419+
deps = [":py_library_rule_bzl"],
426420
)
427421

428422
bzl_library(
429-
name = "py_library_rule_bazel_bzl",
430-
srcs = ["py_library_rule_bazel.bzl"],
423+
name = "py_library_rule_bzl",
424+
srcs = ["py_library_rule.bzl"],
431425
deps = [
432-
":attributes_bazel_bzl",
433426
":common_bazel_bzl",
434427
":common_bzl",
435428
":py_library_bzl",
@@ -502,17 +495,17 @@ bzl_library(
502495
)
503496

504497
bzl_library(
505-
name = "py_test_macro_bazel_bzl",
506-
srcs = ["py_test_macro_bazel.bzl"],
498+
name = "py_test_macro_bzl",
499+
srcs = ["py_test_macro.bzl"],
507500
deps = [
508501
":common_bazel_bzl",
509-
":py_test_rule_bazel_bzl",
502+
":py_test_rule_bzl",
510503
],
511504
)
512505

513506
bzl_library(
514-
name = "py_test_rule_bazel_bzl",
515-
srcs = ["py_test_rule_bazel.bzl"],
507+
name = "py_test_rule_bzl",
508+
srcs = ["py_test_rule.bzl"],
516509
deps = [
517510
":attributes_bzl",
518511
":common_bzl",

python/private/attributes.bzl

+16
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,22 @@ COMMON_ATTRS = union_attrs(
270270
allow_none = True,
271271
)
272272

273+
IMPORTS_ATTRS = {
274+
"imports": attr.string_list(
275+
doc = """
276+
List of import directories to be added to the PYTHONPATH.
277+
278+
Subject to "Make variable" substitution. These import directories will be added
279+
for this rule and all rules that depend on it (note: not the rules this rule
280+
depends on. Each directory will be added to `PYTHONPATH` by `py_binary` rules
281+
that depend on this rule. The strings are repo-runfiles-root relative,
282+
283+
Absolute paths (paths that start with `/`) and paths that references a path
284+
above the execution root are not allowed and will result in an error.
285+
""",
286+
),
287+
}
288+
273289
_MaybeBuiltinPyInfo = [[BuiltinPyInfo]] if BuiltinPyInfo != None else []
274290

275291
# Attributes common to rules accepting Python sources and deps.

python/private/attributes_bazel.bzl

-30
This file was deleted.

python/private/py_binary_macro_bazel.bzl renamed to python/private/py_binary_macro.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Implementation of macro-half of py_binary rule."""
1515

1616
load(":common_bazel.bzl", "convert_legacy_create_init_to_int")
17-
load(":py_binary_rule_bazel.bzl", py_binary_rule = "py_binary")
17+
load(":py_binary_rule.bzl", py_binary_rule = "py_binary")
1818

1919
def py_binary(**kwargs):
2020
convert_legacy_create_init_to_int(kwargs)

python/private/py_executable_bazel.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
load("@bazel_skylib//lib:dicts.bzl", "dicts")
1717
load("@bazel_skylib//lib:paths.bzl", "paths")
18-
load(":attributes_bazel.bzl", "IMPORTS_ATTRS")
18+
load(":attributes.bzl", "IMPORTS_ATTRS")
1919
load(
2020
":common.bzl",
2121
"create_binary_semantics_struct",

python/private/py_library.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
"""Implementation of py_library rule."""
14+
"""Common code for implementing py_library rules."""
1515

1616
load("@bazel_skylib//lib:dicts.bzl", "dicts")
1717
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
1818
load(
1919
":attributes.bzl",
2020
"COMMON_ATTRS",
21+
"IMPORTS_ATTRS",
2122
"PY_SRCS_ATTRS",
2223
"PrecompileAttr",
2324
"REQUIRED_EXEC_GROUPS",
@@ -50,6 +51,7 @@ _py_builtins = py_internal
5051
LIBRARY_ATTRS = union_attrs(
5152
COMMON_ATTRS,
5253
PY_SRCS_ATTRS,
54+
IMPORTS_ATTRS,
5355
create_srcs_version_attr(values = SRCS_VERSION_ALL_VALUES),
5456
create_srcs_attr(mandatory = False),
5557
{

python/private/py_library_macro_bazel.bzl renamed to python/private/py_library_macro.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
# limitations under the License.
1414
"""Implementation of macro-half of py_library rule."""
1515

16-
load(":py_library_rule_bazel.bzl", py_library_rule = "py_library")
16+
load(":py_library_rule.bzl", py_library_rule = "py_library")
1717

18+
# A wrapper macro is used to avoid any user-observable changes between a
19+
# rule and macro. It also makes generator_function look as expected.
1820
def py_library(**kwargs):
1921
py_library_rule(**kwargs)

python/private/py_library_rule.bzl

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Implementation of py_library rule."""
15+
16+
load(":common.bzl", "create_library_semantics_struct")
17+
load(":common_bazel.bzl", "collect_cc_info", "get_imports", "maybe_precompile")
18+
load(":py_library.bzl", "create_py_library_rule", "py_library_impl")
19+
20+
def _py_library_impl_with_semantics(ctx):
21+
return py_library_impl(
22+
ctx,
23+
semantics = create_library_semantics_struct(
24+
get_imports = get_imports,
25+
maybe_precompile = maybe_precompile,
26+
get_cc_info_for_library = collect_cc_info,
27+
),
28+
)
29+
30+
py_library = create_py_library_rule(
31+
implementation = _py_library_impl_with_semantics,
32+
)

python/private/py_library_rule_bazel.bzl

-47
This file was deleted.

python/private/py_test_macro_bazel.bzl renamed to python/private/py_test_macro.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""Implementation of macro-half of py_test rule."""
1515

1616
load(":common_bazel.bzl", "convert_legacy_create_init_to_int")
17-
load(":py_test_rule_bazel.bzl", py_test_rule = "py_test")
17+
load(":py_test_rule.bzl", py_test_rule = "py_test")
1818

1919
def py_test(**kwargs):
2020
convert_legacy_create_init_to_int(kwargs)

python/private/py_test_rule_bazel.bzl renamed to python/private/py_test_rule.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
"""Rule implementation of py_test for Bazel."""
14+
"""Implementation of py_test rule."""
1515

1616
load("@bazel_skylib//lib:dicts.bzl", "dicts")
1717
load(":attributes.bzl", "AGNOSTIC_TEST_ATTRS")

python/py_binary.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Public entry point for py_binary."""
1616

1717
load("@rules_python_internal//:rules_python_config.bzl", "config")
18-
load("//python/private:py_binary_macro_bazel.bzl", _starlark_py_binary = "py_binary")
18+
load("//python/private:py_binary_macro.bzl", _starlark_py_binary = "py_binary")
1919
load("//python/private:register_extension_info.bzl", "register_extension_info")
2020
load("//python/private:util.bzl", "add_migration_tag")
2121

python/py_library.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Public entry point for py_library."""
1616

1717
load("@rules_python_internal//:rules_python_config.bzl", "config")
18-
load("//python/private:py_library_macro_bazel.bzl", _starlark_py_library = "py_library")
18+
load("//python/private:py_library_macro.bzl", _starlark_py_library = "py_library")
1919
load("//python/private:register_extension_info.bzl", "register_extension_info")
2020
load("//python/private:util.bzl", "add_migration_tag")
2121

python/py_test.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Public entry point for py_test."""
1616

1717
load("@rules_python_internal//:rules_python_config.bzl", "config")
18-
load("//python/private:py_test_macro_bazel.bzl", _starlark_py_test = "py_test")
18+
load("//python/private:py_test_macro.bzl", _starlark_py_test = "py_test")
1919
load("//python/private:register_extension_info.bzl", "register_extension_info")
2020
load("//python/private:util.bzl", "add_migration_tag")
2121

0 commit comments

Comments
 (0)