Skip to content

Commit

Permalink
refactor: fold bazel-specific code into main files (#2358)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rickeylev authored Oct 30, 2024
1 parent 3e552df commit 7c5e7cf
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 117 deletions.
6 changes: 3 additions & 3 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
39 changes: 16 additions & 23 deletions python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions python/private/attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 0 additions & 30 deletions python/private/attributes_bazel.bzl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion python/private/py_executable_bazel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion python/private/py_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
32 changes: 32 additions & 0 deletions python/private/py_library_rule.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
47 changes: 0 additions & 47 deletions python/private/py_library_rule_bazel.bzl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion python/py_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion python/py_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion python/py_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 7c5e7cf

Please sign in to comment.