xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/unit/stdlib/stdlib.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Unittest to verify contents and ordering of rust stdlib in rust_library() CcInfo"""
2
3load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4load("//rust:defs.bzl", "rust_library")
5
6def _categorize_library(name):
7    """Given an rlib name, guess if it's std, core, or alloc."""
8    if "std" in name:
9        return "std"
10    if "core" in name:
11        return "core"
12    if "alloc" in name:
13        return "alloc"
14    if "compiler_builtins" in name:
15        return "compiler_builtins"
16    return "other"
17
18def _dedup_preserving_order(list):
19    """Given a list, deduplicate its elements preserving order."""
20    r = []
21    seen = {}
22    for e in list:
23        if e in seen:
24            continue
25        seen[e] = 1
26        r.append(e)
27    return r
28
29def _stdlibs(tut):
30    """Given a target, return the list of its standard rust libraries."""
31    libs = [
32        lib.static_library
33        for li in tut[CcInfo].linking_context.linker_inputs.to_list()
34        for lib in li.libraries
35    ]
36    stdlibs = [lib for lib in libs if (lib and tut.label.name not in lib.basename)]
37    return stdlibs
38
39def _libstd_ordering_test_impl(ctx):
40    env = analysistest.begin(ctx)
41    tut = analysistest.target_under_test(env)
42    stdlib_categories = [_categorize_library(lib.basename) for lib in _stdlibs(tut)]
43    set_to_check = _dedup_preserving_order([lib for lib in stdlib_categories if lib != "other"])
44    asserts.equals(env, ["std", "core", "compiler_builtins", "alloc"], set_to_check)
45    return analysistest.end(env)
46
47libstd_ordering_test = analysistest.make(_libstd_ordering_test_impl)
48
49def _libstd_panic_test_impl(ctx):
50    # The libraries panic_unwind and panic_abort are alternatives.
51    # Check that they don't occur together.
52    env = analysistest.begin(ctx)
53    tut = analysistest.target_under_test(env)
54    stdlibs = _stdlibs(tut)
55    has_panic_unwind = [lib for lib in stdlibs if "panic_unwind" in lib.basename]
56    if has_panic_unwind:
57        has_panic_abort = [lib for lib in stdlibs if "panic_abort" in lib.basename]
58        asserts.false(env, has_panic_abort)
59
60    return analysistest.end(env)
61
62libstd_panic_test = analysistest.make(_libstd_panic_test_impl)
63
64def _native_dep_test():
65    rust_library(
66        name = "some_rlib",
67        srcs = ["some_rlib.rs"],
68        edition = "2018",
69    )
70
71    libstd_ordering_test(
72        name = "libstd_ordering_test",
73        target_under_test = ":some_rlib",
74    )
75
76    libstd_panic_test(
77        name = "libstd_panic_test",
78        target_under_test = ":some_rlib",
79    )
80
81def stdlib_suite(name):
82    """Entry-point macro called from the BUILD file.
83
84    Args:
85        name: Name of the macro.
86    """
87    _native_dep_test()
88
89    native.test_suite(
90        name = name,
91        tests = [
92            ":libstd_ordering_test",
93        ],
94    )
95