Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: platform_transition_filegroup #55

Merged
merged 2 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 24 additions & 0 deletions docs/transitions.md
Original file line number Diff line number Diff line change
@@ -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 | |


38 changes: 31 additions & 7 deletions lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -96,7 +90,37 @@ bzl_library(

bzl_library(
name = "diff_test",
srcs = ["diff_test.bzl"],
srcs = ["diff_test.bzl"], # keep
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this comment for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without it, running Gazelle in this repo removes the file.

Gazelle seems to have some (new?) heuristics for avoiding "test code" being included, so files ending with "_test.bzl" are a problem. I had to totally revert the changes Gazelle makes to the lib/private/BUILD.bazel file where it removes a bunch...

visibility = ["//visibility:public"],
deps = ["//lib/private:diff_test"],
)

bzl_library(
name = "copy_file",
srcs = ["copy_file.bzl"],
visibility = ["//visibility:public"],
deps = ["//lib/private:copy_file"],
)

bzl_library(
name = "repositories",
srcs = ["repositories.bzl"],
visibility = ["//visibility:public"],
deps = [
"//lib/private:jq_toolchain",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
)

bzl_library(
name = "transitions",
srcs = ["transitions.bzl"],
visibility = ["//visibility:public"],
)

bzl_library(
name = "windows_utils",
srcs = ["windows_utils.bzl"],
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions lib/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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"],
)
44 changes: 44 additions & 0 deletions lib/transitions.bzl
Original file line number Diff line number Diff line change
@@ -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.",
)