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
This commit is contained in:
Alex Eagle 2022-03-30 21:04:14 -07:00 committed by GitHub
parent 229fcfb881
commit 096133e5d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 154 additions and 7 deletions

View File

@ -53,4 +53,9 @@ stardoc_with_diff_test(
bzl_library_target = "//lib:output_files",
)
stardoc_with_diff_test(
name = "transitions",
bzl_library_target = "//lib:transitions",
)
update_docs()

24
docs/transitions.md Normal file
View File

@ -0,0 +1,24 @@
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
Rules for working with transitions.
<a id="#platform_transition_filegroup"></a>
## platform_transition_filegroup
<pre>
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>)
</pre>
Transitions the srcs to use the provided platform. The filegroup will contain artifacts for the target platform.
**ATTRIBUTES**
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <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 | |
| <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 | [] |
| <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 | |

View File

@ -43,12 +43,6 @@ bzl_library(
deps = ["//lib/private:utils"],
)
bzl_library(
name = "windows_utils",
srcs = ["windows_utils.bzl"],
visibility = ["//visibility:public"],
)
bzl_library(
name = "jq",
srcs = ["jq.bzl"],
@ -96,7 +90,19 @@ bzl_library(
bzl_library(
name = "diff_test",
srcs = ["diff_test.bzl"],
srcs = ["diff_test.bzl"], # keep
visibility = ["//visibility:public"],
deps = ["//lib/private:diff_test"],
)
bzl_library(
name = "transitions",
srcs = ["transitions.bzl"],
visibility = ["//visibility:public"],
)
bzl_library(
name = "windows_utils",
srcs = ["windows_utils.bzl"],
visibility = ["//visibility:public"],
)

View File

@ -1,5 +1,6 @@
"tests for libs"
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//lib:expand_make_vars.bzl", "expand_template")
load(":expand_make_vars_test.bzl", "expand_make_vars_test_suite")
@ -40,3 +41,9 @@ sh_test(
name = "expand_template_test",
srcs = ["expand_template"],
)
bzl_library(
name = "generate_outputs",
srcs = ["generate_outputs.bzl"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,59 @@
load("//lib:transitions.bzl", "platform_transition_filegroup")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
platform(
name = "armv7_linux",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:armv7",
],
)
platform(
name = "x86_64_linux",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
)
config_setting(
name = "is_x86",
constraint_values = [
"@platforms//cpu:x86_64",
],
)
# Simple test fixture that produces something different depending on the
# target platform.
filegroup(
name = "platform_description",
srcs = select({
":is_x86": ["linux_x86.txt"],
"//conditions:default": ["linux_arm.txt"],
}),
)
platform_transition_filegroup(
name = "for_x86",
srcs = ["platform_description"],
target_platform = ":x86_64_linux",
)
platform_transition_filegroup(
name = "for_arm",
srcs = ["platform_description"],
target_platform = ":armv7_linux",
)
diff_test(
name = "test_x86",
file1 = "for_x86",
file2 = "linux_x86.txt",
)
diff_test(
name = "test_arm",
file1 = "for_arm",
file2 = "linux_arm.txt",
)

View File

@ -0,0 +1 @@
Linux ARMv7

View File

@ -0,0 +1 @@
Linux x86

44
lib/transitions.bzl Normal file
View File

@ -0,0 +1,44 @@
"Rules for working with transitions."
def _transition_platform_impl(_, attr):
return {"//command_line_option:platforms": str(attr.target_platform)}
# Transition from any input configuration to one that includes the
# --platforms command-line flag.
_transition_platform = transition(
implementation = _transition_platform_impl,
inputs = [],
outputs = ["//command_line_option:platforms"],
)
def _platform_transition_filegroup_impl(ctx):
files = []
runfiles = ctx.runfiles()
for src in ctx.attr.srcs:
files.append(src[DefaultInfo].files)
runfiles = runfiles.merge_all([src[DefaultInfo].default_runfiles for src in ctx.attr.srcs])
return [DefaultInfo(
files = depset(transitive = files),
runfiles = runfiles,
)]
platform_transition_filegroup = rule(
_platform_transition_filegroup_impl,
attrs = {
# Required to Opt-in to the transitions feature.
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
"target_platform": attr.label(
doc = "The target platform to transition the srcs.",
mandatory = True,
),
"srcs": attr.label_list(
allow_empty = False,
cfg = _transition_platform,
doc = "The input to be transitioned to the target platform.",
),
},
doc = "Transitions the srcs to use the provided platform. The filegroup will contain artifacts for the target platform.",
)