Skip to content

Commit c7f09c0

Browse files
authored
Adding select statements for build script environment and data attributes in bzlmod (#3511)
An implementation to bring some select statement functionality which are available in [repository rules](https://github.com/bazelbuild/rules_rust/blob/f04b2a6f9dc3f26e29563867155da8577b7203ad/crate_universe/private/crate.bzl#L258) but not bzlmod. Since a dict of dicts is not supported, I've opted for a dict of json string which gets decoded on the other end.
1 parent 608af09 commit c7f09c0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

crate_universe/extensions.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,22 @@ def _crate_impl(module_ctx):
874874
repo_specific_annotations = {}
875875
for annotation_tag in mod.tags.annotation:
876876
annotation_dict = structs.to_dict(annotation_tag)
877+
if annotation_dict["build_script_data_select"]:
878+
annotation_dict["build_script_data"] = struct(
879+
common = annotation_dict["build_script_data"],
880+
selects = annotation_dict["build_script_data_select"],
881+
)
882+
annotation_dict.pop("build_script_data_select")
883+
if annotation_dict["build_script_env_select"]:
884+
annotation_dict["build_script_env"] = struct(
885+
common = annotation_dict["build_script_env"],
886+
selects = {
887+
k: json.decode(v)
888+
for k, v in annotation_dict["build_script_env_select"].items()
889+
},
890+
)
891+
annotation_dict.pop("build_script_env_select")
892+
877893
repositories = annotation_dict.pop("repositories")
878894
crate = annotation_dict.pop("crate")
879895

@@ -1103,12 +1119,18 @@ _annotation = tag_class(
11031119
"build_script_data_glob": attr.string_list(
11041120
doc = "A list of glob patterns to add to a crate's `cargo_build_script::data` attribute",
11051121
),
1122+
"build_script_data_select": attr.string_list_dict(
1123+
doc = "A list of labels to add to a crate's `cargo_build_script::data` attribute. Keys should be the platform triplet. Value should be a list of labels.",
1124+
),
11061125
"build_script_deps": _relative_label_list(
11071126
doc = "A list of labels to add to a crate's `cargo_build_script::deps` attribute.",
11081127
),
11091128
"build_script_env": attr.string_dict(
11101129
doc = "Additional environment variables to set on a crate's `cargo_build_script::env` attribute.",
11111130
),
1131+
"build_script_env_select": attr.string_dict(
1132+
doc = "Additional environment variables to set on a crate's `cargo_build_script::env` attribute. Key should be the platform triplet. Value should be a JSON encoded dictionary mapping variable names to values, for example `{\"FOO\": \"bar\"}`.",
1133+
),
11121134
"build_script_link_deps": _relative_label_list(
11131135
doc = "A list of labels to add to a crate's `cargo_build_script::link_deps` attribute.",
11141136
),

0 commit comments

Comments
 (0)