xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/generated_inputs/input_from_different_cfg.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""A custom rule that generates a .rs file in a different configuration."""
2
3def _change_cfg_impl(_settings, _attr):
4    return {"//test/generated_inputs:change_cfg": True}
5
6change_cfg_transition = transition(
7    implementation = _change_cfg_impl,
8    inputs = [],
9    outputs = ["//test/generated_inputs:change_cfg"],
10)
11
12def _input_from_different_cfg_impl(ctx):
13    rs_file = ctx.actions.declare_file(ctx.label.name + ".rs")
14
15    ctx.actions.write(
16        output = rs_file,
17        content = """
18pub fn generated_fn() -> String {
19    "Generated".to_owned()
20}
21
22#[cfg(test)]
23mod tests {
24    #[test]
25    fn test_generated() {
26        assert_eq!(super::generated_fn(), "Generated".to_owned());
27    }
28}
29""",
30    )
31
32    return OutputGroupInfo(generated_file = depset([rs_file]))
33
34input_from_different_cfg = rule(
35    implementation = _input_from_different_cfg_impl,
36    attrs = {
37        "_allowlist_function_transition": attr.label(
38            default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"),
39        ),
40    },
41    cfg = change_cfg_transition,
42)
43