|
3 | 3 | load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
|
4 | 4 | load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_proc_macro")
|
5 | 5 | load("//test/unit:common.bzl", "assert_argv_contains", "assert_list_contains_adjacent_elements", "assert_list_contains_adjacent_elements_not")
|
| 6 | +load(":wrap.bzl", "wrap") |
| 7 | + |
| 8 | +NOT_WINDOWS = select({ |
| 9 | + "@platforms//os:linux": [], |
| 10 | + "@platforms//os:macos": [], |
| 11 | + "//conditions:default": ["@platforms//:incompatible"], |
| 12 | +}) |
| 13 | + |
| 14 | +ENABLE_PIPELINING = { |
| 15 | + "@//rust/settings:pipelined_compilation": True, |
| 16 | +} |
6 | 17 |
|
7 | 18 | def _second_lib_test_impl(ctx):
|
8 | 19 | env = analysistest.begin(ctx)
|
@@ -67,9 +78,6 @@ def _bin_test_impl(ctx):
|
67 | 78 |
|
68 | 79 | return analysistest.end(env)
|
69 | 80 |
|
70 |
| -ENABLE_PIPELINING = { |
71 |
| - "@//rust/settings:pipelined_compilation": True, |
72 |
| -} |
73 | 81 | bin_test = analysistest.make(_bin_test_impl, config_settings = ENABLE_PIPELINING)
|
74 | 82 | second_lib_test = analysistest.make(_second_lib_test_impl, config_settings = ENABLE_PIPELINING)
|
75 | 83 |
|
@@ -101,26 +109,87 @@ def _pipelined_compilation_test():
|
101 | 109 | deps = [":second"],
|
102 | 110 | )
|
103 | 111 |
|
104 |
| - NOT_WINDOWS = select({ |
105 |
| - "@platforms//os:linux": [], |
106 |
| - "@platforms//os:macos": [], |
107 |
| - "//conditions:default": ["@platforms//:incompatible"], |
108 |
| - }) |
109 | 112 | second_lib_test(name = "second_lib_test", target_under_test = ":second", target_compatible_with = NOT_WINDOWS)
|
110 | 113 | bin_test(name = "bin_test", target_under_test = ":bin", target_compatible_with = NOT_WINDOWS)
|
111 | 114 |
|
| 115 | +def _custom_rule_test_impl(ctx): |
| 116 | + env = analysistest.begin(ctx) |
| 117 | + tut = analysistest.target_under_test(env) |
| 118 | + |
| 119 | + # This is the metadata-generating action. It should depend on metadata for the library and, if generate_metadata is set |
| 120 | + # also depend on metadata for 'wrapper'. |
| 121 | + rust_action = [act for act in tut.actions if act.mnemonic == "RustcMetadata"][0] |
| 122 | + |
| 123 | + metadata_inputs = [i for i in rust_action.inputs.to_list() if i.path.endswith(".rmeta")] |
| 124 | + rlib_inputs = [i for i in rust_action.inputs.to_list() if i.path.endswith(".rlib")] |
| 125 | + |
| 126 | + seen_wrapper_metadata = False |
| 127 | + seen_to_wrap = False |
| 128 | + for mi in metadata_inputs: |
| 129 | + if "libwrapper" in mi.path: |
| 130 | + seen_wrapper_metadata = True |
| 131 | + if "libto_wrap" in mi.path: |
| 132 | + seen_to_wrap = True |
| 133 | + |
| 134 | + seen_wrapper_rlib = True |
| 135 | + for ri in rlib_inputs: |
| 136 | + if "libwrapper" in ri.path: |
| 137 | + seen_wrapper_rlib = True |
| 138 | + |
| 139 | + if ctx.attr.generate_metadata: |
| 140 | + asserts.true(env, seen_wrapper_metadata, "expected dependency on metadata for 'wrapper' but not found") |
| 141 | + else: |
| 142 | + asserts.true(env, seen_wrapper_rlib, "expected dependency on rlib for 'wrapper' but not found") |
| 143 | + |
| 144 | + asserts.true(env, seen_to_wrap, "expected dependency on metadata for 'to_wrap' but not found") |
| 145 | + |
| 146 | + return analysistest.end(env) |
| 147 | + |
| 148 | +custom_rule_test = analysistest.make(_custom_rule_test_impl, attrs = {"generate_metadata": attr.bool()}, config_settings = ENABLE_PIPELINING) |
| 149 | + |
| 150 | +def _custom_rule_test(generate_metadata, prefix): |
| 151 | + rust_library( |
| 152 | + name = "to_wrap" + prefix, |
| 153 | + crate_name = "to_wrap", |
| 154 | + srcs = ["custom_rule_test/to_wrap.rs"], |
| 155 | + edition = "2021", |
| 156 | + ) |
| 157 | + wrap( |
| 158 | + name = "wrapper" + prefix, |
| 159 | + crate_name = "wrapper", |
| 160 | + target = ":to_wrap" + prefix, |
| 161 | + generate_metadata = generate_metadata, |
| 162 | + ) |
| 163 | + rust_library( |
| 164 | + name = "uses_wrapper" + prefix, |
| 165 | + srcs = ["custom_rule_test/uses_wrapper.rs"], |
| 166 | + deps = [":wrapper" + prefix], |
| 167 | + edition = "2021", |
| 168 | + ) |
| 169 | + |
| 170 | + custom_rule_test( |
| 171 | + name = "custom_rule_test" + prefix, |
| 172 | + generate_metadata = generate_metadata, |
| 173 | + target_compatible_with = NOT_WINDOWS, |
| 174 | + target_under_test = ":uses_wrapper" + prefix, |
| 175 | + ) |
| 176 | + |
112 | 177 | def pipelined_compilation_test_suite(name):
|
113 | 178 | """Entry-point macro called from the BUILD file.
|
114 | 179 |
|
115 | 180 | Args:
|
116 | 181 | name: Name of the macro.
|
117 | 182 | """
|
118 | 183 | _pipelined_compilation_test()
|
| 184 | + _custom_rule_test(generate_metadata = True, prefix = "_with_metadata") |
| 185 | + _custom_rule_test(generate_metadata = False, prefix = "_without_metadata") |
119 | 186 |
|
120 | 187 | native.test_suite(
|
121 | 188 | name = name,
|
122 | 189 | tests = [
|
123 | 190 | ":bin_test",
|
124 | 191 | ":second_lib_test",
|
| 192 | + ":custom_rule_test_with_metadata", |
| 193 | + ":custom_rule_test_without_metadata", |
125 | 194 | ],
|
126 | 195 | )
|
0 commit comments