xref: /aosp_15_r20/external/bazelbuild-rules_cc/tools/migration/ctoolchain_compare.bzl (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1*eed53cd4SHONG Yifan"""A test rule that compares two CToolchains in proto format."""
2*eed53cd4SHONG Yifan
3*eed53cd4SHONG Yifandef _impl(ctx):
4*eed53cd4SHONG Yifan    toolchain_config_proto = ctx.actions.declare_file(ctx.label.name + "_toolchain_config.proto")
5*eed53cd4SHONG Yifan    ctx.actions.write(
6*eed53cd4SHONG Yifan        toolchain_config_proto,
7*eed53cd4SHONG Yifan        ctx.attr.toolchain_config[CcToolchainConfigInfo].proto,
8*eed53cd4SHONG Yifan    )
9*eed53cd4SHONG Yifan
10*eed53cd4SHONG Yifan    script = ("%s --before='%s' --after='%s' --toolchain_identifier='%s'" % (
11*eed53cd4SHONG Yifan        ctx.executable._comparator.short_path,
12*eed53cd4SHONG Yifan        ctx.file.crosstool.short_path,
13*eed53cd4SHONG Yifan        toolchain_config_proto.short_path,
14*eed53cd4SHONG Yifan        ctx.attr.toolchain_identifier,
15*eed53cd4SHONG Yifan    ))
16*eed53cd4SHONG Yifan    test_executable = ctx.actions.declare_file(ctx.label.name)
17*eed53cd4SHONG Yifan    ctx.actions.write(test_executable, script, is_executable = True)
18*eed53cd4SHONG Yifan
19*eed53cd4SHONG Yifan    runfiles = ctx.runfiles(files = [toolchain_config_proto, ctx.file.crosstool])
20*eed53cd4SHONG Yifan    runfiles = runfiles.merge(ctx.attr._comparator[DefaultInfo].default_runfiles)
21*eed53cd4SHONG Yifan
22*eed53cd4SHONG Yifan    return DefaultInfo(runfiles = runfiles, executable = test_executable)
23*eed53cd4SHONG Yifan
24*eed53cd4SHONG Yifancc_toolchains_compare_test = rule(
25*eed53cd4SHONG Yifan    implementation = _impl,
26*eed53cd4SHONG Yifan    attrs = {
27*eed53cd4SHONG Yifan        "crosstool": attr.label(
28*eed53cd4SHONG Yifan            mandatory = True,
29*eed53cd4SHONG Yifan            allow_single_file = True,
30*eed53cd4SHONG Yifan            doc = "Location of the CROSSTOOL file",
31*eed53cd4SHONG Yifan        ),
32*eed53cd4SHONG Yifan        "toolchain_config": attr.label(
33*eed53cd4SHONG Yifan            mandatory = True,
34*eed53cd4SHONG Yifan            providers = [CcToolchainConfigInfo],
35*eed53cd4SHONG Yifan            doc = ("Starlark rule that replaces the CROSSTOOL file functionality " +
36*eed53cd4SHONG Yifan                   "for the CToolchain with the given identifier"),
37*eed53cd4SHONG Yifan        ),
38*eed53cd4SHONG Yifan        "toolchain_identifier": attr.string(
39*eed53cd4SHONG Yifan            mandatory = True,
40*eed53cd4SHONG Yifan            doc = "identifier of the CToolchain that is being compared",
41*eed53cd4SHONG Yifan        ),
42*eed53cd4SHONG Yifan        "_comparator": attr.label(
43*eed53cd4SHONG Yifan            default = ":ctoolchain_comparator",
44*eed53cd4SHONG Yifan            executable = True,
45*eed53cd4SHONG Yifan            cfg = "exec",
46*eed53cd4SHONG Yifan        ),
47*eed53cd4SHONG Yifan    },
48*eed53cd4SHONG Yifan    test = True,
49*eed53cd4SHONG Yifan)
50