From 7c5e7cfcef3ec262b5164bf6dcd09cd558fe57ba Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Tue, 29 Oct 2024 20:32:35 -0700 Subject: [PATCH] refactor: fold bazel-specific code into main files (#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. --- docs/BUILD.bazel | 6 +-- docs/conf.py | 4 +- python/BUILD.bazel | 6 +-- python/private/BUILD.bazel | 39 +++++++-------- python/private/attributes.bzl | 16 +++++++ python/private/attributes_bazel.bzl | 30 ------------ ...ry_macro_bazel.bzl => py_binary_macro.bzl} | 2 +- ...nary_rule_bazel.bzl => py_binary_rule.bzl} | 0 python/private/py_executable_bazel.bzl | 2 +- python/private/py_library.bzl | 4 +- ...y_macro_bazel.bzl => py_library_macro.bzl} | 4 +- python/private/py_library_rule.bzl | 32 +++++++++++++ python/private/py_library_rule_bazel.bzl | 47 ------------------- ...test_macro_bazel.bzl => py_test_macro.bzl} | 2 +- ...y_test_rule_bazel.bzl => py_test_rule.bzl} | 2 +- python/py_binary.bzl | 2 +- python/py_library.bzl | 2 +- python/py_test.bzl | 2 +- 18 files changed, 85 insertions(+), 117 deletions(-) delete mode 100644 python/private/attributes_bazel.bzl rename python/private/{py_binary_macro_bazel.bzl => py_binary_macro.bzl} (92%) rename python/private/{py_binary_rule_bazel.bzl => py_binary_rule.bzl} (100%) rename python/private/{py_library_macro_bazel.bzl => py_library_macro.bzl} (77%) create mode 100644 python/private/py_library_rule.bzl delete mode 100644 python/private/py_library_rule_bazel.bzl rename python/private/{py_test_macro_bazel.bzl => py_test_macro.bzl} (93%) rename python/private/{py_test_rule_bazel.bzl => py_test_rule.bzl} (97%) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index d16a27e74f..a4b6a5a440 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -97,11 +97,11 @@ sphinx_stardocs( "//python/cc:py_cc_toolchain_bzl", "//python/cc:py_cc_toolchain_info_bzl", "//python/entry_points:py_console_script_binary_bzl", - "//python/private:py_binary_rule_bazel_bzl", + "//python/private:py_binary_rule_bzl", "//python/private:py_cc_toolchain_rule_bzl", - "//python/private:py_library_rule_bazel_bzl", + "//python/private:py_library_rule_bzl", "//python/private:py_runtime_rule_bzl", - "//python/private:py_test_rule_bazel_bzl", + "//python/private:py_test_rule_bzl", "//python/private/api:py_common_api_bzl", ] + ([ # Bazel 6 + Stardoc isn't able to parse something about the python bzlmod extension diff --git a/docs/conf.py b/docs/conf.py index 9d3378241e..4c8e4a2a6b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -69,9 +69,9 @@ "api/python/defs": "/api/rules_python/python/defs.html", "api/python/index": "/api/rules_python/python/index.html", "api/python/py_runtime_info": "/api/rules_python/python/py_runtime_info.html", - "api/python/private/common/py_library_rule_bazel": "/api/rules_python/python/private/py_library_rule_bazel.html", + "api/python/private/common/py_library_rule_bazel": "/api/rules_python/python/private/py_library_rule.html", "api/python/private/common/py_test_rule_bazel": "/api/rules_python/python/private/py_test_rule_bazel.html", - "api/python/private/common/py_binary_rule_bazel": "/api/rules_python/python/private/py_binary_rule_bazel.html", + "api/python/private/common/py_binary_rule_bazel": "/api/rules_python/python/private/py_binary_rule.html", "api/python/private/common/py_runtime_rule": "/api/rules_python/python/private/py_runtime_rule.html", "api/python/extensions/pip": "/api/rules_python/python/extensions/pip.html", "api/python/extensions/python": "/api/rules_python/python/extensions/python.html", diff --git a/python/BUILD.bazel b/python/BUILD.bazel index f2f3374db3..f3b5b136a7 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -124,7 +124,7 @@ bzl_library( name = "py_binary_bzl", srcs = ["py_binary.bzl"], deps = [ - "//python/private:py_binary_macro_bazel_bzl", + "//python/private:py_binary_macro_bzl", "//python/private:register_extension_info_bzl", "//python/private:util_bzl", "@rules_python_internal//:rules_python_config_bzl", @@ -178,7 +178,7 @@ bzl_library( name = "py_library_bzl", srcs = ["py_library.bzl"], deps = [ - "//python/private:py_library_macro_bazel_bzl", + "//python/private:py_library_macro_bzl", "//python/private:register_extension_info_bzl", "//python/private:util_bzl", "@rules_python_internal//:rules_python_config_bzl", @@ -219,7 +219,7 @@ bzl_library( name = "py_test_bzl", srcs = ["py_test.bzl"], deps = [ - "//python/private:py_test_macro_bazel_bzl", + "//python/private:py_test_macro_bzl", "//python/private:register_extension_info_bzl", "//python/private:util_bzl", "@rules_python_internal//:rules_python_config_bzl", diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index d3b9bf4aad..7741e553ce 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -51,12 +51,6 @@ filegroup( visibility = ["//python:__pkg__"], ) -bzl_library( - name = "attributes_bazel_bzl", - srcs = ["attributes_bazel.bzl"], - deps = ["//python/private:rules_cc_srcs_bzl"], -) - bzl_library( name = "attributes_bzl", srcs = ["attributes.bzl"], @@ -260,17 +254,17 @@ bzl_library( ) bzl_library( - name = "py_binary_macro_bazel_bzl", - srcs = ["py_binary_macro_bazel.bzl"], + name = "py_binary_macro_bzl", + srcs = ["py_binary_macro.bzl"], deps = [ ":common_bzl", - ":py_binary_rule_bazel_bzl", + ":py_binary_rule_bzl", ], ) bzl_library( - name = "py_binary_rule_bazel_bzl", - srcs = ["py_binary_rule_bazel.bzl"], + name = "py_binary_rule_bzl", + srcs = ["py_binary_rule.bzl"], deps = [ ":attributes_bzl", ":py_executable_bazel_bzl", @@ -345,7 +339,7 @@ bzl_library( name = "py_executable_bazel_bzl", srcs = ["py_executable_bazel.bzl"], deps = [ - ":attributes_bazel_bzl", + ":attributes_bzl", ":common_bazel_bzl", ":common_bzl", ":py_executable_bzl", @@ -420,16 +414,15 @@ bzl_library( ) bzl_library( - name = "py_library_macro_bazel_bzl", - srcs = ["py_library_macro_bazel.bzl"], - deps = [":py_library_rule_bazel_bzl"], + name = "py_library_macro_bzl", + srcs = ["py_library_macro.bzl"], + deps = [":py_library_rule_bzl"], ) bzl_library( - name = "py_library_rule_bazel_bzl", - srcs = ["py_library_rule_bazel.bzl"], + name = "py_library_rule_bzl", + srcs = ["py_library_rule.bzl"], deps = [ - ":attributes_bazel_bzl", ":common_bazel_bzl", ":common_bzl", ":py_library_bzl", @@ -502,17 +495,17 @@ bzl_library( ) bzl_library( - name = "py_test_macro_bazel_bzl", - srcs = ["py_test_macro_bazel.bzl"], + name = "py_test_macro_bzl", + srcs = ["py_test_macro.bzl"], deps = [ ":common_bazel_bzl", - ":py_test_rule_bazel_bzl", + ":py_test_rule_bzl", ], ) bzl_library( - name = "py_test_rule_bazel_bzl", - srcs = ["py_test_rule_bazel.bzl"], + name = "py_test_rule_bzl", + srcs = ["py_test_rule.bzl"], deps = [ ":attributes_bzl", ":common_bzl", diff --git a/python/private/attributes.bzl b/python/private/attributes.bzl index 424a2c5ad6..a863e195bb 100644 --- a/python/private/attributes.bzl +++ b/python/private/attributes.bzl @@ -270,6 +270,22 @@ COMMON_ATTRS = union_attrs( allow_none = True, ) +IMPORTS_ATTRS = { + "imports": attr.string_list( + doc = """ +List of import directories to be added to the PYTHONPATH. + +Subject to "Make variable" substitution. These import directories will be added +for this rule and all rules that depend on it (note: not the rules this rule +depends on. Each directory will be added to `PYTHONPATH` by `py_binary` rules +that depend on this rule. The strings are repo-runfiles-root relative, + +Absolute paths (paths that start with `/`) and paths that references a path +above the execution root are not allowed and will result in an error. +""", + ), +} + _MaybeBuiltinPyInfo = [[BuiltinPyInfo]] if BuiltinPyInfo != None else [] # Attributes common to rules accepting Python sources and deps. diff --git a/python/private/attributes_bazel.bzl b/python/private/attributes_bazel.bzl deleted file mode 100644 index f87245d6ff..0000000000 --- a/python/private/attributes_bazel.bzl +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright 2022 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Attributes specific to the Bazel implementation of the Python rules.""" - -IMPORTS_ATTRS = { - "imports": attr.string_list( - doc = """ -List of import directories to be added to the PYTHONPATH. - -Subject to "Make variable" substitution. These import directories will be added -for this rule and all rules that depend on it (note: not the rules this rule -depends on. Each directory will be added to `PYTHONPATH` by `py_binary` rules -that depend on this rule. The strings are repo-runfiles-root relative, - -Absolute paths (paths that start with `/`) and paths that references a path -above the execution root are not allowed and will result in an error. -""", - ), -} diff --git a/python/private/py_binary_macro_bazel.bzl b/python/private/py_binary_macro.bzl similarity index 92% rename from python/private/py_binary_macro_bazel.bzl rename to python/private/py_binary_macro.bzl index a6c4e97dac..83b3c18677 100644 --- a/python/private/py_binary_macro_bazel.bzl +++ b/python/private/py_binary_macro.bzl @@ -14,7 +14,7 @@ """Implementation of macro-half of py_binary rule.""" load(":common_bazel.bzl", "convert_legacy_create_init_to_int") -load(":py_binary_rule_bazel.bzl", py_binary_rule = "py_binary") +load(":py_binary_rule.bzl", py_binary_rule = "py_binary") def py_binary(**kwargs): convert_legacy_create_init_to_int(kwargs) diff --git a/python/private/py_binary_rule_bazel.bzl b/python/private/py_binary_rule.bzl similarity index 100% rename from python/private/py_binary_rule_bazel.bzl rename to python/private/py_binary_rule.bzl diff --git a/python/private/py_executable_bazel.bzl b/python/private/py_executable_bazel.bzl index 53206bdbfd..6f9c0947a3 100644 --- a/python/private/py_executable_bazel.bzl +++ b/python/private/py_executable_bazel.bzl @@ -15,7 +15,7 @@ load("@bazel_skylib//lib:dicts.bzl", "dicts") load("@bazel_skylib//lib:paths.bzl", "paths") -load(":attributes_bazel.bzl", "IMPORTS_ATTRS") +load(":attributes.bzl", "IMPORTS_ATTRS") load( ":common.bzl", "create_binary_semantics_struct", diff --git a/python/private/py_library.bzl b/python/private/py_library.bzl index 1bc96b5e4b..6a65038e8a 100644 --- a/python/private/py_library.bzl +++ b/python/private/py_library.bzl @@ -11,13 +11,14 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""Implementation of py_library rule.""" +"""Common code for implementing py_library rules.""" load("@bazel_skylib//lib:dicts.bzl", "dicts") load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load( ":attributes.bzl", "COMMON_ATTRS", + "IMPORTS_ATTRS", "PY_SRCS_ATTRS", "PrecompileAttr", "REQUIRED_EXEC_GROUPS", @@ -50,6 +51,7 @@ _py_builtins = py_internal LIBRARY_ATTRS = union_attrs( COMMON_ATTRS, PY_SRCS_ATTRS, + IMPORTS_ATTRS, create_srcs_version_attr(values = SRCS_VERSION_ALL_VALUES), create_srcs_attr(mandatory = False), { diff --git a/python/private/py_library_macro_bazel.bzl b/python/private/py_library_macro.bzl similarity index 77% rename from python/private/py_library_macro_bazel.bzl rename to python/private/py_library_macro.bzl index b4f51eff1d..981253d63a 100644 --- a/python/private/py_library_macro_bazel.bzl +++ b/python/private/py_library_macro.bzl @@ -13,7 +13,9 @@ # limitations under the License. """Implementation of macro-half of py_library rule.""" -load(":py_library_rule_bazel.bzl", py_library_rule = "py_library") +load(":py_library_rule.bzl", py_library_rule = "py_library") +# A wrapper macro is used to avoid any user-observable changes between a +# rule and macro. It also makes generator_function look as expected. def py_library(**kwargs): py_library_rule(**kwargs) diff --git a/python/private/py_library_rule.bzl b/python/private/py_library_rule.bzl new file mode 100644 index 0000000000..ed64716122 --- /dev/null +++ b/python/private/py_library_rule.bzl @@ -0,0 +1,32 @@ +# Copyright 2022 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Implementation of py_library rule.""" + +load(":common.bzl", "create_library_semantics_struct") +load(":common_bazel.bzl", "collect_cc_info", "get_imports", "maybe_precompile") +load(":py_library.bzl", "create_py_library_rule", "py_library_impl") + +def _py_library_impl_with_semantics(ctx): + return py_library_impl( + ctx, + semantics = create_library_semantics_struct( + get_imports = get_imports, + maybe_precompile = maybe_precompile, + get_cc_info_for_library = collect_cc_info, + ), + ) + +py_library = create_py_library_rule( + implementation = _py_library_impl_with_semantics, +) diff --git a/python/private/py_library_rule_bazel.bzl b/python/private/py_library_rule_bazel.bzl deleted file mode 100644 index 453abcb816..0000000000 --- a/python/private/py_library_rule_bazel.bzl +++ /dev/null @@ -1,47 +0,0 @@ -# Copyright 2022 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Implementation of py_library for Bazel.""" - -load(":attributes_bazel.bzl", "IMPORTS_ATTRS") -load(":common.bzl", "create_library_semantics_struct", "union_attrs") -load(":common_bazel.bzl", "collect_cc_info", "get_imports", "maybe_precompile") -load( - ":py_library.bzl", - "LIBRARY_ATTRS", - "create_py_library_rule", - bazel_py_library_impl = "py_library_impl", -) - -_BAZEL_LIBRARY_ATTRS = union_attrs( - LIBRARY_ATTRS, - IMPORTS_ATTRS, -) - -def create_library_semantics_bazel(): - return create_library_semantics_struct( - get_imports = get_imports, - maybe_precompile = maybe_precompile, - get_cc_info_for_library = collect_cc_info, - ) - -def _py_library_impl(ctx): - return bazel_py_library_impl( - ctx, - semantics = create_library_semantics_bazel(), - ) - -py_library = create_py_library_rule( - implementation = _py_library_impl, - attrs = _BAZEL_LIBRARY_ATTRS, -) diff --git a/python/private/py_test_macro_bazel.bzl b/python/private/py_test_macro.bzl similarity index 93% rename from python/private/py_test_macro_bazel.bzl rename to python/private/py_test_macro.bzl index 24b78fef96..1f9330f8e5 100644 --- a/python/private/py_test_macro_bazel.bzl +++ b/python/private/py_test_macro.bzl @@ -14,7 +14,7 @@ """Implementation of macro-half of py_test rule.""" load(":common_bazel.bzl", "convert_legacy_create_init_to_int") -load(":py_test_rule_bazel.bzl", py_test_rule = "py_test") +load(":py_test_rule.bzl", py_test_rule = "py_test") def py_test(**kwargs): convert_legacy_create_init_to_int(kwargs) diff --git a/python/private/py_test_rule_bazel.bzl b/python/private/py_test_rule.bzl similarity index 97% rename from python/private/py_test_rule_bazel.bzl rename to python/private/py_test_rule.bzl index 369360d90f..64d5f21f81 100644 --- a/python/private/py_test_rule_bazel.bzl +++ b/python/private/py_test_rule.bzl @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -"""Rule implementation of py_test for Bazel.""" +"""Implementation of py_test rule.""" load("@bazel_skylib//lib:dicts.bzl", "dicts") load(":attributes.bzl", "AGNOSTIC_TEST_ATTRS") diff --git a/python/py_binary.bzl b/python/py_binary.bzl index 349610865f..c7d57dab49 100644 --- a/python/py_binary.bzl +++ b/python/py_binary.bzl @@ -15,7 +15,7 @@ """Public entry point for py_binary.""" load("@rules_python_internal//:rules_python_config.bzl", "config") -load("//python/private:py_binary_macro_bazel.bzl", _starlark_py_binary = "py_binary") +load("//python/private:py_binary_macro.bzl", _starlark_py_binary = "py_binary") load("//python/private:register_extension_info.bzl", "register_extension_info") load("//python/private:util.bzl", "add_migration_tag") diff --git a/python/py_library.bzl b/python/py_library.bzl index 4ec1da4b27..12354a7deb 100644 --- a/python/py_library.bzl +++ b/python/py_library.bzl @@ -15,7 +15,7 @@ """Public entry point for py_library.""" load("@rules_python_internal//:rules_python_config.bzl", "config") -load("//python/private:py_library_macro_bazel.bzl", _starlark_py_library = "py_library") +load("//python/private:py_library_macro.bzl", _starlark_py_library = "py_library") load("//python/private:register_extension_info.bzl", "register_extension_info") load("//python/private:util.bzl", "add_migration_tag") diff --git a/python/py_test.bzl b/python/py_test.bzl index 2aa93ff54b..7f6626e0e5 100644 --- a/python/py_test.bzl +++ b/python/py_test.bzl @@ -15,7 +15,7 @@ """Public entry point for py_test.""" load("@rules_python_internal//:rules_python_config.bzl", "config") -load("//python/private:py_test_macro_bazel.bzl", _starlark_py_test = "py_test") +load("//python/private:py_test_macro.bzl", _starlark_py_test = "py_test") load("//python/private:register_extension_info.bzl", "register_extension_info") load("//python/private:util.bzl", "add_migration_tag")