Skip to content

Commit 49b21ce

Browse files
authored
feat, refactor(pystar): bzl_library for packaging.bzl; fix pystar doc building and py_wheel (bazel-contrib#1432)
Changed `py_wheel` to load `py_binary` instead of using `native.py_binary`. This caused the doc generation to fail because of the additional loads(), so the doc libraries were refactored to represent the additional loads. This adds `//python:packaging_bzl` as a public target. Docs were failing to build because Stardoc wasn't able to process the value `cc_helper.use_cpp_toolchains()` returned. For some reason, under Stardoc, the value is some mocked out value that it can't handle. This is easily fixed by just using the regular way for referencing an optional toolchain; the labels the two use are the same. Work towards bazel-contrib#1069
1 parent 48daf52 commit 49b21ce

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

.bazelignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ bazel-out
88
bazel-testlogs
99
# Prevent the convenience symlinks within the examples from being
1010
# treated as directories with valid BUILD files for the main repo.
11+
# Any directory with a WORKSPACE in it should be added here, with
12+
# an entry like `bazel-{workspacename}`
1113
examples/bzlmod/bazel-bzlmod
1214
examples/bzlmod/other_module/bazel-other_module
1315
examples/bzlmod_build_file_generation/bazel-bzlmod_build_file_generation
1416
examples/pip_parse/bazel-pip_parse
1517
examples/py_proto_library/bazel-py_proto_library
18+
tests/ignore_root_user_error/bazel-ignore_root_user_error

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ A brief description of the categories of changes:
4848
https://github.com/indygreg/python-build-standalone/releases/tag/20230826.
4949
* (gazelle) New `# gazelle:python_generation_mode file` directive to support
5050
generating one `py_library` per file.
51-
5251
* (python_repository) Support `netrc` and `auth_patterns` attributes to enable
5352
authentication against private HTTP hosts serving Python toolchain binaries.
53+
* `//python:packaging_bzl` added, a `bzl_library` for the Starlark
54+
files `//python:packaging.bzl` requires.
5455

5556
### Removed
5657

docs/BUILD.bazel

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,6 @@ bzl_library(
7474
],
7575
)
7676

77-
bzl_library(
78-
name = "packaging_bzl",
79-
srcs = [
80-
"//python:packaging.bzl",
81-
"//python/private:py_package.bzl",
82-
"//python/private:py_wheel.bzl",
83-
"//python/private:stamp.bzl",
84-
"//python/private:util.bzl",
85-
],
86-
deps = [
87-
"//python/private:util_bzl",
88-
],
89-
)
90-
9177
# TODO: Stardoc does not guarantee consistent outputs accross platforms (Unix/Windows).
9278
# As a result we do not build or test docs on Windows.
9379
_NOT_WINDOWS = select({
@@ -144,7 +130,7 @@ stardoc(
144130
out = "packaging.md_",
145131
input = "//python:packaging.bzl",
146132
target_compatible_with = _NOT_WINDOWS,
147-
deps = [":packaging_bzl"],
133+
deps = ["//python:packaging_bzl"],
148134
)
149135

150136
stardoc(

python/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ bzl_library(
7070
],
7171
)
7272

73+
bzl_library(
74+
name = "packaging_bzl",
75+
srcs = ["packaging.bzl"],
76+
deps = [
77+
":py_binary_bzl",
78+
"//python/private:py_package.bzl",
79+
"//python/private:py_wheel_bzl",
80+
"//python/private:stamp_bzl",
81+
"//python/private:util_bzl",
82+
],
83+
)
84+
7385
bzl_library(
7486
name = "proto_bzl",
7587
srcs = [

python/packaging.bzl

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

1515
"""Public API for for building wheels."""
1616

17+
load("//python:py_binary.bzl", "py_binary")
1718
load("//python/private:py_package.bzl", "py_package_lib")
1819
load("//python/private:py_wheel.bzl", _PyWheelInfo = "PyWheelInfo", _py_wheel = "py_wheel")
1920
load("//python/private:util.bzl", "copy_propagating_kwargs")
@@ -167,7 +168,7 @@ def py_wheel(name, twine = None, publish_args = [], **kwargs):
167168

168169
# TODO: use py_binary from //python:defs.bzl after our stardoc setup is less brittle
169170
# buildifier: disable=native-py
170-
native.py_binary(
171+
py_binary(
171172
name = "{}.publish".format(name),
172173
srcs = [twine_main],
173174
args = twine_args,

python/private/BUILD.bazel

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ bzl_library(
106106
],
107107
)
108108

109+
bzl_library(
110+
name = "py_package_bzl",
111+
srcs = ["py_package.bzl"],
112+
visibility = ["//:__subpackages__"],
113+
)
114+
115+
bzl_library(
116+
name = "py_wheel_bzl",
117+
srcs = ["py_wheel.bzl"],
118+
visibility = ["//:__subpackages__"],
119+
deps = [
120+
":py_package_bzl",
121+
":stamp_bzl",
122+
],
123+
)
124+
125+
bzl_library(
126+
name = "stamp_bzl",
127+
srcs = ["stamp.bzl"],
128+
visibility = ["//:__subpackages__"],
129+
)
130+
109131
# @bazel_tools can't define bzl_library itself, so we just put a wrapper around it.
110132
bzl_library(
111133
name = "bazel_tools_bzl",
@@ -130,7 +152,7 @@ exports_files(
130152
"util.bzl",
131153
"py_cc_toolchain_rule.bzl",
132154
],
133-
visibility = ["//docs:__pkg__"],
155+
visibility = ["//:__subpackages__"],
134156
)
135157

136158
# Used to determine the use of `--stamp` in Starlark rules

python/private/common/py_executable.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,20 @@ load(
4949
"BUILD_DATA_SYMLINK_PATH",
5050
"IS_BAZEL",
5151
"PY_RUNTIME_ATTR_NAME",
52+
"TOOLS_REPO",
5253
)
5354

5455
# TODO: Load cc_common from rules_cc
5556
_cc_common = cc_common
5657

5758
_py_builtins = py_internal
5859

60+
# Bazel 5.4 doesn't have config_common.toolchain_type
61+
_CC_TOOLCHAINS = [config_common.toolchain_type(
62+
"@" + TOOLS_REPO + "//tools/cpp:toolchain_type",
63+
mandatory = False,
64+
)] if hasattr(config_common, "toolchain_type") else []
65+
5966
# Non-Google-specific attributes for executables
6067
# These attributes are for rules that accept Python sources.
6168
EXECUTABLE_ATTRS = union_attrs(
@@ -810,7 +817,7 @@ def create_base_executable_rule(*, attrs, fragments = [], **kwargs):
810817
return rule(
811818
# TODO: add ability to remove attrs, i.e. for imports attr
812819
attrs = dicts.add(EXECUTABLE_ATTRS, attrs),
813-
toolchains = [TOOLCHAIN_TYPE] + (cc_helper.use_cpp_toolchain() if cc_helper else []),
820+
toolchains = [TOOLCHAIN_TYPE] + _CC_TOOLCHAINS,
814821
fragments = fragments,
815822
**kwargs
816823
)

0 commit comments

Comments
 (0)