Skip to content

Commit 096133e

Browse files
authored
feat: platform_transition_filegroup (#55)
* feat: platform_transition_filegroup Lifted from https://github.com/aspect-build/gcc-toolchain/pull/8/files See also https://github.com/fmeum/rules_meta/blob/main/meta/internal/meta.bzl#L4 * test: add tests for transition filegroup
1 parent 229fcfb commit 096133e

File tree

8 files changed

+154
-7
lines changed

8 files changed

+154
-7
lines changed

docs/BUILD.bazel

+5
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ stardoc_with_diff_test(
5353
bzl_library_target = "//lib:output_files",
5454
)
5555

56+
stardoc_with_diff_test(
57+
name = "transitions",
58+
bzl_library_target = "//lib:transitions",
59+
)
60+
5661
update_docs()

docs/transitions.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2+
3+
Rules for working with transitions.
4+
5+
<a id="#platform_transition_filegroup"></a>
6+
7+
## platform_transition_filegroup
8+
9+
<pre>
10+
platform_transition_filegroup(<a href="#platform_transition_filegroup-name">name</a>, <a href="#platform_transition_filegroup-srcs">srcs</a>, <a href="#platform_transition_filegroup-target_platform">target_platform</a>)
11+
</pre>
12+
13+
Transitions the srcs to use the provided platform. The filegroup will contain artifacts for the target platform.
14+
15+
**ATTRIBUTES**
16+
17+
18+
| Name | Description | Type | Mandatory | Default |
19+
| :------------- | :------------- | :------------- | :------------- | :------------- |
20+
| <a id="platform_transition_filegroup-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
21+
| <a id="platform_transition_filegroup-srcs"></a>srcs | The input to be transitioned to the target platform. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
22+
| <a id="platform_transition_filegroup-target_platform"></a>target_platform | The target platform to transition the srcs. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
23+
24+

lib/BUILD.bazel

+13-7
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ bzl_library(
4343
deps = ["//lib/private:utils"],
4444
)
4545

46-
bzl_library(
47-
name = "windows_utils",
48-
srcs = ["windows_utils.bzl"],
49-
visibility = ["//visibility:public"],
50-
)
51-
5246
bzl_library(
5347
name = "jq",
5448
srcs = ["jq.bzl"],
@@ -96,7 +90,19 @@ bzl_library(
9690

9791
bzl_library(
9892
name = "diff_test",
99-
srcs = ["diff_test.bzl"],
93+
srcs = ["diff_test.bzl"], # keep
10094
visibility = ["//visibility:public"],
10195
deps = ["//lib/private:diff_test"],
10296
)
97+
98+
bzl_library(
99+
name = "transitions",
100+
srcs = ["transitions.bzl"],
101+
visibility = ["//visibility:public"],
102+
)
103+
104+
bzl_library(
105+
name = "windows_utils",
106+
srcs = ["windows_utils.bzl"],
107+
visibility = ["//visibility:public"],
108+
)

lib/tests/BUILD.bazel

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"tests for libs"
22

3+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
34
load("@bazel_skylib//rules:write_file.bzl", "write_file")
45
load("//lib:expand_make_vars.bzl", "expand_template")
56
load(":expand_make_vars_test.bzl", "expand_make_vars_test_suite")
@@ -40,3 +41,9 @@ sh_test(
4041
name = "expand_template_test",
4142
srcs = ["expand_template"],
4243
)
44+
45+
bzl_library(
46+
name = "generate_outputs",
47+
srcs = ["generate_outputs.bzl"],
48+
visibility = ["//visibility:public"],
49+
)

lib/tests/transitions/BUILD.bazel

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
load("//lib:transitions.bzl", "platform_transition_filegroup")
2+
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
3+
4+
platform(
5+
name = "armv7_linux",
6+
constraint_values = [
7+
"@platforms//os:linux",
8+
"@platforms//cpu:armv7",
9+
],
10+
)
11+
12+
platform(
13+
name = "x86_64_linux",
14+
constraint_values = [
15+
"@platforms//os:linux",
16+
"@platforms//cpu:x86_64",
17+
],
18+
)
19+
20+
config_setting(
21+
name = "is_x86",
22+
constraint_values = [
23+
"@platforms//cpu:x86_64",
24+
],
25+
)
26+
27+
# Simple test fixture that produces something different depending on the
28+
# target platform.
29+
filegroup(
30+
name = "platform_description",
31+
srcs = select({
32+
":is_x86": ["linux_x86.txt"],
33+
"//conditions:default": ["linux_arm.txt"],
34+
}),
35+
)
36+
37+
platform_transition_filegroup(
38+
name = "for_x86",
39+
srcs = ["platform_description"],
40+
target_platform = ":x86_64_linux",
41+
)
42+
43+
platform_transition_filegroup(
44+
name = "for_arm",
45+
srcs = ["platform_description"],
46+
target_platform = ":armv7_linux",
47+
)
48+
49+
diff_test(
50+
name = "test_x86",
51+
file1 = "for_x86",
52+
file2 = "linux_x86.txt",
53+
)
54+
55+
diff_test(
56+
name = "test_arm",
57+
file1 = "for_arm",
58+
file2 = "linux_arm.txt",
59+
)

lib/tests/transitions/linux_arm.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Linux ARMv7

lib/tests/transitions/linux_x86.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Linux x86

lib/transitions.bzl

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"Rules for working with transitions."
2+
3+
def _transition_platform_impl(_, attr):
4+
return {"//command_line_option:platforms": str(attr.target_platform)}
5+
6+
# Transition from any input configuration to one that includes the
7+
# --platforms command-line flag.
8+
_transition_platform = transition(
9+
implementation = _transition_platform_impl,
10+
inputs = [],
11+
outputs = ["//command_line_option:platforms"],
12+
)
13+
14+
def _platform_transition_filegroup_impl(ctx):
15+
files = []
16+
runfiles = ctx.runfiles()
17+
for src in ctx.attr.srcs:
18+
files.append(src[DefaultInfo].files)
19+
20+
runfiles = runfiles.merge_all([src[DefaultInfo].default_runfiles for src in ctx.attr.srcs])
21+
return [DefaultInfo(
22+
files = depset(transitive = files),
23+
runfiles = runfiles,
24+
)]
25+
26+
platform_transition_filegroup = rule(
27+
_platform_transition_filegroup_impl,
28+
attrs = {
29+
# Required to Opt-in to the transitions feature.
30+
"_allowlist_function_transition": attr.label(
31+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
32+
),
33+
"target_platform": attr.label(
34+
doc = "The target platform to transition the srcs.",
35+
mandatory = True,
36+
),
37+
"srcs": attr.label_list(
38+
allow_empty = False,
39+
cfg = _transition_platform,
40+
doc = "The input to be transitioned to the target platform.",
41+
),
42+
},
43+
doc = "Transitions the srcs to use the provided platform. The filegroup will contain artifacts for the target platform.",
44+
)

0 commit comments

Comments
 (0)