xref: /aosp_15_r20/external/bazelbuild-rules_rust/rust/private/transitions.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan"""Internal transition implementations for core Rust rules"""
2*d4726bddSHONG Yifan
3*d4726bddSHONG Yifanload("//rust:defs.bzl", "rust_common")
4*d4726bddSHONG Yifan
5*d4726bddSHONG Yifandef _import_macro_dep_bootstrap_transition(_settings, _attr):
6*d4726bddSHONG Yifan    """The implementation of the `import_macro_dep_bootstrap_transition` transition.
7*d4726bddSHONG Yifan
8*d4726bddSHONG Yifan    This transition modifies the config to start using the fake macro
9*d4726bddSHONG Yifan    implementation, so that the macro itself can be bootstrapped without
10*d4726bddSHONG Yifan    creating a dependency cycle, even while every Rust target has an implicit
11*d4726bddSHONG Yifan    dependency on the "import" macro (either real or fake).
12*d4726bddSHONG Yifan
13*d4726bddSHONG Yifan    Args:
14*d4726bddSHONG Yifan        _settings (dict): a dict {String:Object} of all settings declared in the
15*d4726bddSHONG Yifan            inputs parameter to `transition()`.
16*d4726bddSHONG Yifan        _attr (dict): A dict of attributes and values of the rule to which the
17*d4726bddSHONG Yifan            transition is attached.
18*d4726bddSHONG Yifan
19*d4726bddSHONG Yifan    Returns:
20*d4726bddSHONG Yifan        dict: A dict of new build settings values to apply.
21*d4726bddSHONG Yifan    """
22*d4726bddSHONG Yifan    return {"@rules_rust//rust/settings:use_real_import_macro": False}
23*d4726bddSHONG Yifan
24*d4726bddSHONG Yifanimport_macro_dep_bootstrap_transition = transition(
25*d4726bddSHONG Yifan    implementation = _import_macro_dep_bootstrap_transition,
26*d4726bddSHONG Yifan    inputs = [],
27*d4726bddSHONG Yifan    outputs = ["@rules_rust//rust/settings:use_real_import_macro"],
28*d4726bddSHONG Yifan)
29*d4726bddSHONG Yifan
30*d4726bddSHONG Yifandef _alias_with_import_macro_bootstrapping_mode_impl(ctx):
31*d4726bddSHONG Yifan    actual = ctx.attr.actual[0]
32*d4726bddSHONG Yifan    return [actual[rust_common.crate_info], actual[rust_common.dep_info]]
33*d4726bddSHONG Yifan
34*d4726bddSHONG Yifanalias_with_import_macro_bootstrapping_mode = rule(
35*d4726bddSHONG Yifan    implementation = _alias_with_import_macro_bootstrapping_mode_impl,
36*d4726bddSHONG Yifan    doc = "Alias-like rule to build the `actual` with `use_real_import_macro` setting disabled. Not to be used outside of the import macro bootstrap.",
37*d4726bddSHONG Yifan    attrs = {
38*d4726bddSHONG Yifan        # Using `actual` so tooling such as rust analyzer aspect traverses the target.
39*d4726bddSHONG Yifan        "actual": attr.label(
40*d4726bddSHONG Yifan            doc = "The target this alias refers to.",
41*d4726bddSHONG Yifan            cfg = import_macro_dep_bootstrap_transition,
42*d4726bddSHONG Yifan            mandatory = True,
43*d4726bddSHONG Yifan        ),
44*d4726bddSHONG Yifan        "_allowlist_function_transition": attr.label(
45*d4726bddSHONG Yifan            default = Label("//tools/allowlists/function_transition_allowlist"),
46*d4726bddSHONG Yifan        ),
47*d4726bddSHONG Yifan    },
48*d4726bddSHONG Yifan)
49