diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 7ef3c18..ebb6307 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -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() diff --git a/docs/transitions.md b/docs/transitions.md new file mode 100644 index 0000000..1c48a8a --- /dev/null +++ b/docs/transitions.md @@ -0,0 +1,24 @@ + + +Rules for working with transitions. + + + +## platform_transition_filegroup + +
+platform_transition_filegroup(name, srcs, target_platform) ++ +Transitions the srcs to use the provided platform. The filegroup will contain artifacts for the target platform. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | +| srcs | The input to be transitioned to the target platform. | List of labels | optional | [] | +| target_platform | The target platform to transition the srcs. | Label | required | | + + diff --git a/lib/BUILD.bazel b/lib/BUILD.bazel index c2db6cd..45f0a54 100644 --- a/lib/BUILD.bazel +++ b/lib/BUILD.bazel @@ -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"], +) diff --git a/lib/tests/BUILD.bazel b/lib/tests/BUILD.bazel index 632bdd2..f6eba70 100644 --- a/lib/tests/BUILD.bazel +++ b/lib/tests/BUILD.bazel @@ -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"], +) diff --git a/lib/tests/transitions/BUILD.bazel b/lib/tests/transitions/BUILD.bazel new file mode 100644 index 0000000..d764145 --- /dev/null +++ b/lib/tests/transitions/BUILD.bazel @@ -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", +) diff --git a/lib/tests/transitions/linux_arm.txt b/lib/tests/transitions/linux_arm.txt new file mode 100644 index 0000000..d4a07bc --- /dev/null +++ b/lib/tests/transitions/linux_arm.txt @@ -0,0 +1 @@ +Linux ARMv7 \ No newline at end of file diff --git a/lib/tests/transitions/linux_x86.txt b/lib/tests/transitions/linux_x86.txt new file mode 100644 index 0000000..9fdb20b --- /dev/null +++ b/lib/tests/transitions/linux_x86.txt @@ -0,0 +1 @@ +Linux x86 \ No newline at end of file diff --git a/lib/transitions.bzl b/lib/transitions.bzl new file mode 100644 index 0000000..e964c14 --- /dev/null +++ b/lib/transitions.bzl @@ -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.", +)