Skip to content

Commit 354f7a2

Browse files
committed
add a new macro for setting up aliases
1 parent 4e61031 commit 354f7a2

File tree

6 files changed

+121
-8
lines changed

6 files changed

+121
-8
lines changed

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2024 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+
15+
"""pkg_aliases is a macro to generate aliases for selecting the right wheel for the right target platform.
16+
17+
This is used in bzlmod and non-bzlmod setups."""
18+
19+
load(
20+
":labels.bzl",
21+
#"DATA_LABEL",
22+
#"DIST_INFO_LABEL",
23+
#"PY_LIBRARY_IMPL_LABEL",
24+
"PY_LIBRARY_PUBLIC_LABEL",
25+
#"WHEEL_FILE_IMPL_LABEL",
26+
#"WHEEL_FILE_PUBLIC_LABEL",
27+
)
28+
29+
def pkg_aliases(
30+
*,
31+
name,
32+
actual,
33+
native = native):
34+
"""Create aliases for an actual package.
35+
36+
Args:
37+
name: {type}`str` The name of the package.
38+
actual: {type}`dict[Label, str]` The config settings for the package
39+
mapping to repositories.
40+
native: {type}`struct` used in unit tests
41+
"""
42+
_ = actual # buildifier: @unused
43+
native.alias(
44+
name = name,
45+
actual = ":" + PY_LIBRARY_PUBLIC_LABEL,
46+
)

python/private/pypi/render_pkg_aliases.bzl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,17 @@ def _render_whl_library_alias(
117117
**kwargs
118118
)
119119

120+
def _render_pkg_aliases(name, actual):
121+
return """pkg_aliases(
122+
name = "{name}",
123+
actual = {actual},
124+
)""".format(name = name, actual = repr(actual))
125+
120126
def _render_common_aliases(*, name, aliases, extra_aliases = [], group_name = None):
121127
lines = [
122-
"""load("@bazel_skylib//lib:selects.bzl", "selects")""",
128+
"""\
129+
load("@bazel_skylib//lib:selects.bzl", "selects")
130+
load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")""",
123131
"""package(default_visibility = ["//visibility:public"])""",
124132
]
125133

@@ -140,9 +148,9 @@ def _render_common_aliases(*, name, aliases, extra_aliases = [], group_name = No
140148
))
141149

142150
lines.append(
143-
render.alias(
151+
_render_pkg_aliases(
144152
name = name,
145-
actual = repr(":pkg"),
153+
actual = ":pkg",
146154
),
147155
)
148156
lines.extend(

tests/pypi/pkg_aliases/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load(":pkg_aliases_test.bzl", "pkg_aliases_test_suite")
2+
3+
pkg_aliases_test_suite(name = "pkg_aliases_tests")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2024 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+
15+
"""pkg_aliases tests"""
16+
17+
load("@rules_testing//lib:test_suite.bzl", "test_suite")
18+
load(
19+
"//python/private/pypi:pkg_aliases.bzl",
20+
"pkg_aliases",
21+
) # buildifier: disable=bzl-visibility
22+
23+
_tests = []
24+
25+
def _test_empty(env):
26+
actual = []
27+
pkg_aliases(
28+
name = "foo",
29+
actual = "repo",
30+
native = struct(
31+
alias = lambda **kwargs: actual.append(kwargs),
32+
),
33+
)
34+
35+
want = [
36+
# buildifier: disable=unsorted-dict-items
37+
{
38+
"name": "foo",
39+
"actual": ":pkg",
40+
},
41+
]
42+
43+
env.expect.that_collection(actual).contains_exactly(want)
44+
45+
_tests.append(_test_empty)
46+
47+
def pkg_aliases_test_suite(name):
48+
"""Create the test suite.
49+
50+
Args:
51+
name: the name of the test suite
52+
"""
53+
test_suite(name = name, basic_tests = _tests)

tests/pypi/render_pkg_aliases/render_pkg_aliases_test.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ def _test_legacy_aliases(env):
7575
want_key = "foo/BUILD.bazel"
7676
want_content = """\
7777
load("@bazel_skylib//lib:selects.bzl", "selects")
78+
load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")
7879
7980
package(default_visibility = ["//visibility:public"])
8081
81-
alias(
82+
pkg_aliases(
8283
name = "foo",
8384
actual = ":pkg",
8485
)
@@ -120,6 +121,7 @@ def _test_bzlmod_aliases(env):
120121
want_key = "bar_baz/BUILD.bazel"
121122
want_content = """\
122123
load("@bazel_skylib//lib:selects.bzl", "selects")
124+
load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")
123125
124126
package(default_visibility = ["//visibility:public"])
125127
@@ -140,7 +142,7 @@ If the value is missing, then the "default" Python version is being used,
140142
which has a "null" version value and will not match version constraints.
141143
\"\"\"
142144
143-
alias(
145+
pkg_aliases(
144146
name = "bar_baz",
145147
actual = ":pkg",
146148
)
@@ -222,6 +224,7 @@ def _test_bzlmod_aliases_with_no_default_version(env):
222224
want_key = "bar_baz/BUILD.bazel"
223225
want_content = """\
224226
load("@bazel_skylib//lib:selects.bzl", "selects")
227+
load("@rules_python//python/private/pypi:pkg_aliases.bzl", "pkg_aliases")
225228
226229
package(default_visibility = ["//visibility:public"])
227230
@@ -243,7 +246,7 @@ If the value is missing, then the "default" Python version is being used,
243246
which has a "null" version value and will not match version constraints.
244247
\"\"\"
245248
246-
alias(
249+
pkg_aliases(
247250
name = "bar_baz",
248251
actual = ":pkg",
249252
)

0 commit comments

Comments
 (0)