Skip to content

Commit 09109e3

Browse files
authored
internal(pystar): make starlark impl (mostly) loadable (#1422)
This just makes the files able to get passed the loading stage under Bazel 7+. This mostly involves fixing load statements, but also exposed a couple places where py_internal needs some small changes. * Also renames files to better distinguish rule vs macro vs Bazel-specific. This makes it easier to patch them within Google and more clear about which file is doing what. Work towards #1069
1 parent 6cb77cd commit 09109e3

20 files changed

+178
-83
lines changed

python/private/common/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2023 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.

python/private/common/attributes.bzl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@
1313
# limitations under the License.
1414
"""Attributes for Python rules."""
1515

16-
load(":common/cc/cc_info.bzl", _CcInfo = "CcInfo")
17-
load(":common/python/common.bzl", "union_attrs")
18-
load(":common/python/providers.bzl", "PyInfo")
16+
load(":common.bzl", "union_attrs")
17+
load(":providers.bzl", "PyInfo")
18+
load(":py_internal.bzl", "py_internal")
1919
load(
20-
":common/python/semantics.bzl",
20+
":semantics.bzl",
2121
"DEPS_ATTR_ALLOW_RULES",
2222
"PLATFORMS_LOCATION",
2323
"SRCS_ATTR_ALLOW_FILES",
2424
"TOOLS_REPO",
2525
)
2626

27-
PackageSpecificationInfo = _builtins.toplevel.PackageSpecificationInfo
27+
# TODO: Load CcInfo from rules_cc
28+
_CcInfo = CcInfo
29+
_PackageSpecificationInfo = py_internal.PackageSpecificationInfo
2830

2931
_STAMP_VALUES = [-1, 0, 1]
3032

@@ -89,7 +91,7 @@ NATIVE_RULES_ALLOWLIST_ATTRS = {
8991
fragment = "py",
9092
name = "native_rules_allowlist",
9193
),
92-
providers = [PackageSpecificationInfo],
94+
providers = [_PackageSpecificationInfo],
9395
),
9496
}
9597

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2023 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+
"""PYTHON RULE IMPLEMENTATION ONLY: Do not use outside of the rule implementations and their tests.
15+
16+
Adapter for accessing Bazel's internal cc_helper.
17+
18+
These may change at any time and are closely coupled to the rule implementation.
19+
"""
20+
21+
load(":py_internal.bzl", "py_internal")
22+
23+
cc_helper = py_internal.cc_helper

python/private/common/common.bzl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@
1313
# limitations under the License.
1414
"""Various things common to Bazel and Google rule implementations."""
1515

16-
load(":common/cc/cc_helper.bzl", "cc_helper")
16+
load(":cc_helper.bzl", "cc_helper")
17+
load(":providers.bzl", "PyInfo")
18+
load(":py_internal.bzl", "py_internal")
1719
load(
18-
":common/python/providers.bzl",
19-
"PyInfo",
20-
)
21-
load(
22-
":common/python/semantics.bzl",
20+
":semantics.bzl",
2321
"NATIVE_RULES_MIGRATION_FIX_CMD",
2422
"NATIVE_RULES_MIGRATION_HELP_URL",
2523
"TOOLS_REPO",
2624
)
2725

28-
_testing = _builtins.toplevel.testing
29-
_platform_common = _builtins.toplevel.platform_common
30-
_coverage_common = _builtins.toplevel.coverage_common
31-
_py_builtins = _builtins.internal.py_builtins
32-
PackageSpecificationInfo = _builtins.toplevel.PackageSpecificationInfo
26+
_testing = testing
27+
_platform_common = platform_common
28+
_coverage_common = coverage_common
29+
_py_builtins = py_internal
30+
PackageSpecificationInfo = py_internal.PackageSpecificationInfo
3331

3432
TOOLCHAIN_TYPE = "@" + TOOLS_REPO + "//tools/python:toolchain_type"
3533

python/private/common/common_bazel.bzl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
# limitations under the License.
1414
"""Common functions that are specific to Bazel rule implementation"""
1515

16-
load(":common/cc/cc_common.bzl", _cc_common = "cc_common")
17-
load(":common/cc/cc_info.bzl", _CcInfo = "CcInfo")
18-
load(":common/paths.bzl", "paths")
19-
load(":common/python/common.bzl", "is_bool")
20-
load(":common/python/providers.bzl", "PyCcLinkParamsProvider")
16+
load("@bazel_skylib//lib:paths.bzl", "paths")
17+
load(":common.bzl", "is_bool")
18+
load(":providers.bzl", "PyCcLinkParamsProvider")
19+
load(":py_internal.bzl", "py_internal")
2120

22-
_py_builtins = _builtins.internal.py_builtins
21+
# TODO: Load cc_common from rules_cc
22+
_cc_common = cc_common
23+
24+
# TODO: Load CcInfo from rules_cc
25+
_CcInfo = CcInfo
26+
27+
_py_builtins = py_internal
2328

2429
def collect_cc_info(ctx, extra_deps = []):
2530
"""Collect C++ information from dependencies for Bazel.

python/private/common/providers.bzl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
# limitations under the License.
1414
"""Providers for Python rules."""
1515

16-
load(":common/python/semantics.bzl", "TOOLS_REPO")
16+
load(":semantics.bzl", "TOOLS_REPO")
1717

18-
_CcInfo = _builtins.toplevel.CcInfo
18+
# TODO: load CcInfo from rules_cc
19+
_CcInfo = CcInfo
1920

20-
# NOTE: This is copied to PyRuntimeInfo.java
2121
DEFAULT_STUB_SHEBANG = "#!/usr/bin/env python3"
2222

23-
# NOTE: This is copied to PyRuntimeInfo.java
2423
DEFAULT_BOOTSTRAP_TEMPLATE = "@" + TOOLS_REPO + "//tools/python:python_bootstrap_template.txt"
2524
_PYTHON_VERSION_VALUES = ["PY2", "PY3"]
2625

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414
"""Implementation of macro-half of py_binary rule."""
1515

16-
load(":common/python/common_bazel.bzl", "convert_legacy_create_init_to_int")
17-
load(":common/python/py_binary_bazel.bzl", py_binary_rule = "py_binary")
16+
load(":common_bazel.bzl", "convert_legacy_create_init_to_int")
17+
load(":py_binary_rule_bazel.bzl", py_binary_rule = "py_binary")
1818

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

python/private/common/py_binary_bazel.bzl renamed to python/private/common/py_binary_rule_bazel.bzl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
# limitations under the License.
1414
"""Rule implementation of py_binary for Bazel."""
1515

16-
load(":common/python/attributes.bzl", "AGNOSTIC_BINARY_ATTRS")
16+
load(":attributes.bzl", "AGNOSTIC_BINARY_ATTRS")
1717
load(
18-
":common/python/py_executable_bazel.bzl",
18+
":py_executable_bazel.bzl",
1919
"create_executable_rule",
2020
"py_executable_bazel_impl",
2121
)
22-
load(":common/python/semantics.bzl", "TOOLS_REPO")
22+
load(":semantics.bzl", "TOOLS_REPO")
2323

2424
_PY_TEST_ATTRS = {
2525
"_collect_cc_coverage": attr.label(

python/private/common/py_executable.bzl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@
1313
# limitations under the License.
1414
"""Common functionality between test/binary executables."""
1515

16-
load(":common/cc/cc_common.bzl", _cc_common = "cc_common")
17-
load(":common/cc/cc_helper.bzl", "cc_helper")
1816
load(
19-
":common/python/attributes.bzl",
17+
":attributes.bzl",
2018
"AGNOSTIC_EXECUTABLE_ATTRS",
2119
"COMMON_ATTRS",
2220
"PY_SRCS_ATTRS",
2321
"SRCS_VERSION_ALL_VALUES",
2422
"create_srcs_attr",
2523
"create_srcs_version_attr",
2624
)
25+
load(":cc_helper.bzl", "cc_helper")
2726
load(
28-
":common/python/common.bzl",
27+
":common.bzl",
2928
"TOOLCHAIN_TYPE",
3029
"check_native_allowed",
3130
"collect_imports",
@@ -38,19 +37,23 @@ load(
3837
"union_attrs",
3938
)
4039
load(
41-
":common/python/providers.bzl",
40+
":providers.bzl",
4241
"PyCcLinkParamsProvider",
4342
"PyRuntimeInfo",
4443
)
44+
load(":py_internal.bzl", "py_internal")
4545
load(
46-
":common/python/semantics.bzl",
46+
":semantics.bzl",
4747
"ALLOWED_MAIN_EXTENSIONS",
4848
"BUILD_DATA_SYMLINK_PATH",
4949
"IS_BAZEL",
5050
"PY_RUNTIME_ATTR_NAME",
5151
)
5252

53-
_py_builtins = _builtins.internal.py_builtins
53+
# TODO: Load cc_common from rules_cc
54+
_cc_common = cc_common
55+
56+
_py_builtins = py_internal
5457

5558
# Non-Google-specific attributes for executables
5659
# These attributes are for rules that accept Python sources.
@@ -677,9 +680,8 @@ def is_stamping_enabled(ctx, semantics):
677680
def _is_tool_config(ctx):
678681
# NOTE: The is_tool_configuration() function is only usable by builtins.
679682
# See https://github.com/bazelbuild/bazel/issues/14444 for the FR for
680-
# a more public API. Outside of builtins, ctx.bin_dir.path can be
681-
# checked for `/host/` or `-exec-`.
682-
return ctx.configuration.is_tool_configuration()
683+
# a more public API. Until that's available, py_internal to the rescue.
684+
return py_internal.is_tool_configuration(ctx)
683685

684686
def _create_providers(
685687
*,

python/private/common/py_executable_bazel.bzl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,26 @@
1313
# limitations under the License.
1414
"""Implementation for Bazel Python executable."""
1515

16-
load(":common/paths.bzl", "paths")
17-
load(":common/python/attributes_bazel.bzl", "IMPORTS_ATTRS")
16+
load("@bazel_skylib//lib:paths.bzl", "paths")
17+
load(":attributes_bazel.bzl", "IMPORTS_ATTRS")
1818
load(
19-
":common/python/common.bzl",
19+
":common.bzl",
2020
"create_binary_semantics_struct",
2121
"create_cc_details_struct",
2222
"create_executable_result_struct",
2323
"union_attrs",
2424
)
25-
load(":common/python/common_bazel.bzl", "collect_cc_info", "get_imports", "maybe_precompile")
26-
load(":common/python/providers.bzl", "DEFAULT_STUB_SHEBANG")
25+
load(":common_bazel.bzl", "collect_cc_info", "get_imports", "maybe_precompile")
26+
load(":providers.bzl", "DEFAULT_STUB_SHEBANG")
2727
load(
28-
":common/python/py_executable.bzl",
28+
":py_executable.bzl",
2929
"create_base_executable_rule",
3030
"py_executable_base_impl",
3131
)
32-
load(":common/python/semantics.bzl", "TOOLS_REPO")
32+
load(":py_internal.bzl", "py_internal")
33+
load(":semantics.bzl", "TOOLS_REPO")
3334

34-
_py_builtins = _builtins.internal.py_builtins
35+
_py_builtins = py_internal
3536
_EXTERNAL_PATH_PREFIX = "external"
3637
_ZIP_RUNFILES_DIRECTORY_NAME = "runfiles"
3738

0 commit comments

Comments
 (0)