|
1 | 1 | """Unittests for the --experimental_use_cc_common_link build setting."""
|
2 | 2 |
|
3 | 3 | load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
|
| 4 | +load("@bazel_skylib//rules:write_file.bzl", "write_file") |
4 | 5 | load("@rules_cc//cc:defs.bzl", "cc_library")
|
5 | 6 | load(
|
6 | 7 | "@rules_rust//rust:defs.bzl",
|
7 | 8 | "rust_binary",
|
8 | 9 | "rust_shared_library",
|
9 | 10 | "rust_test",
|
10 | 11 | )
|
| 12 | +load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain") |
11 | 13 |
|
12 | 14 | DepActionsInfo = provider(
|
13 | 15 | "Contains information about dependencies actions.",
|
@@ -46,6 +48,31 @@ use_cc_common_link_on_target = rule(
|
46 | 48 | },
|
47 | 49 | )
|
48 | 50 |
|
| 51 | +def _with_collect_dep_actions_impl(ctx): |
| 52 | + # return [ctx.attr.target[DepActionsInfo], ctx.attr.target[OutputGroupInfo]] |
| 53 | + return [ctx.attr.target[DepActionsInfo]] |
| 54 | + |
| 55 | +with_collect_dep_actions = rule( |
| 56 | + implementation = _with_collect_dep_actions_impl, |
| 57 | + attrs = { |
| 58 | + "target": attr.label( |
| 59 | + aspects = [collect_dep_actions_aspect], |
| 60 | + ), |
| 61 | + }, |
| 62 | +) |
| 63 | + |
| 64 | +def _with_exec_cfg_impl(ctx): |
| 65 | + return [ctx.attr.target[DepActionsInfo]] |
| 66 | + |
| 67 | +with_exec_cfg = rule( |
| 68 | + implementation = _with_exec_cfg_impl, |
| 69 | + attrs = { |
| 70 | + "target": attr.label( |
| 71 | + cfg = "exec", |
| 72 | + ), |
| 73 | + }, |
| 74 | +) |
| 75 | + |
49 | 76 | def _outputs_object_file(action):
|
50 | 77 | object_files = [output for output in action.outputs.to_list() if output.extension in ("o", "obj")]
|
51 | 78 | return len(object_files) > 0
|
@@ -192,24 +219,185 @@ def _cc_common_link_test_targets():
|
192 | 219 | target_under_test = ":bin_with_cc_common_link",
|
193 | 220 | )
|
194 | 221 |
|
| 222 | + return [ |
| 223 | + "use_cc_common_link_on_binary", |
| 224 | + "use_cc_common_link_on_binary_with_pdb", |
| 225 | + "use_cc_common_link_on_test", |
| 226 | + "use_cc_common_link_on_crate_test", |
| 227 | + "use_cc_common_link_on_cdylib", |
| 228 | + "custom_malloc_on_binary_test", |
| 229 | + ] |
| 230 | + |
| 231 | +_RUSTC_FLAGS_CODEGEN_UNITS = 2 |
| 232 | +_SETTINGS_CODEGEN_UNITS = 3 |
| 233 | +_PER_CRATE_RUSTC_FLAG_CODEGEN_UNITS = 4 |
| 234 | +_EXTRA_RUSTC_FLAG_CODEGEN_UNITS = 5 |
| 235 | +_EXTRA_RUSTC_FLAGS_CODEGEN_UNITS = 6 |
| 236 | +_EXTRA_EXEC_RUSTC_FLAG_CODEGEN_UNITS = 7 |
| 237 | +_EXTRA_EXEC_RUSTC_FLAGS_CODEGEN_UNITS = 8 |
| 238 | +_TOOLCHAIN_EXTRA_RUSTC_FLAGS_CODEGEN_UNITS = 9 |
| 239 | +_TOOLCHAIN_EXTRA_EXEC_RUSTC_FLAGS_CODEGEN_UNITS = 10 |
| 240 | +_TOOLCHAIN_EXTRA_RUSTC_FLAGS_FOR_CRATE_TYPES = 11 |
| 241 | + |
| 242 | +def _codegen_units_test_impl(ctx): |
| 243 | + env = analysistest.begin(ctx) |
| 244 | + tut = analysistest.target_under_test(env) |
| 245 | + registered_actions = tut[DepActionsInfo].actions |
| 246 | + |
| 247 | + rustc_actions = [action for action in registered_actions if action.mnemonic == "Rustc"] |
| 248 | + asserts.equals(env, 1, len(rustc_actions)) |
| 249 | + rustc_action = rustc_actions[0] |
| 250 | + |
| 251 | + actual = sorted([arg for arg in rustc_action.argv if arg.startswith("-Ccodegen-units=")]) |
| 252 | + expected = sorted(["-Ccodegen-units=%s" % codegen_units for codegen_units in ctx.attr.expected_codegen_units]) |
| 253 | + |
| 254 | + asserts.equals(env, expected, actual) |
| 255 | + |
| 256 | + return analysistest.end(env) |
| 257 | + |
| 258 | +codegen_units_test = analysistest.make( |
| 259 | + _codegen_units_test_impl, |
| 260 | + config_settings = { |
| 261 | + # By default, don't use cc_common.link to link. |
| 262 | + str(Label("@rules_rust//rust/settings:experimental_use_cc_common_link")): False, |
| 263 | + # Set `-Ccodegen-units` in various different ways. |
| 264 | + str(Label("@rules_rust//rust/settings:codegen_units")): _SETTINGS_CODEGEN_UNITS, |
| 265 | + # Empty prefix (before the `@`) means the flag is applied to all crates. |
| 266 | + str(Label("@rules_rust//rust/settings:experimental_per_crate_rustc_flag")): ["@-Ccodegen-units=%s" % _PER_CRATE_RUSTC_FLAG_CODEGEN_UNITS], |
| 267 | + str(Label("@rules_rust//rust/settings:extra_rustc_flag")): ["-Ccodegen-units=%s" % _EXTRA_RUSTC_FLAG_CODEGEN_UNITS], |
| 268 | + str(Label("@rules_rust//rust/settings:extra_rustc_flags")): ["-Ccodegen-units=%s" % _EXTRA_RUSTC_FLAGS_CODEGEN_UNITS], |
| 269 | + str(Label("@rules_rust//rust/settings:extra_exec_rustc_flag")): ["-Ccodegen-units=%s" % _EXTRA_EXEC_RUSTC_FLAG_CODEGEN_UNITS], |
| 270 | + str(Label("@rules_rust//rust/settings:extra_exec_rustc_flags")): ["-Ccodegen-units=%s" % _EXTRA_EXEC_RUSTC_FLAGS_CODEGEN_UNITS], |
| 271 | + "//command_line_option:extra_toolchains": ["//unit:codegen_units_toolchain"], |
| 272 | + }, |
| 273 | + attrs = {"expected_codegen_units": attr.int_list()}, |
| 274 | +) |
| 275 | + |
| 276 | +def _codegen_units_test_targets(): |
| 277 | + # Targets under test. |
| 278 | + rust_binary( |
| 279 | + name = "codegen_units_bin", |
| 280 | + srcs = ["bin.rs"], |
| 281 | + edition = "2018", |
| 282 | + rustc_flags = ["-Ccodegen-units=%s" % _RUSTC_FLAGS_CODEGEN_UNITS], |
| 283 | + ) |
| 284 | + use_cc_common_link_on_target( |
| 285 | + name = "codegen_units_bin_with_cc_common_link", |
| 286 | + target = ":codegen_units_bin", |
| 287 | + ) |
| 288 | + with_collect_dep_actions( |
| 289 | + name = "codegen_units_bin_with_collect_dep_actions", |
| 290 | + target = ":codegen_units_bin", |
| 291 | + ) |
| 292 | + with_exec_cfg( |
| 293 | + name = "codegen_units_exec_bin_with_cc_common_link", |
| 294 | + target = ":codegen_units_bin_with_cc_common_link", |
| 295 | + ) |
| 296 | + with_exec_cfg( |
| 297 | + name = "codegen_units_exec_bin_with_collect_dep_actions", |
| 298 | + target = ":codegen_units_bin_with_collect_dep_actions", |
| 299 | + ) |
| 300 | + |
| 301 | + # Fake toolchain along with its dependencies. |
| 302 | + write_file( |
| 303 | + name = "mock_rustc", |
| 304 | + out = "mock_rustc.exe", |
| 305 | + ) |
| 306 | + write_file( |
| 307 | + name = "stdlib_src_self_contained", |
| 308 | + out = "self-contained/something.o", |
| 309 | + ) |
| 310 | + rust_stdlib_filegroup( |
| 311 | + name = "std_libs", |
| 312 | + srcs = ["self-contained/something.o"], |
| 313 | + ) |
| 314 | + write_file( |
| 315 | + name = "mock_rustdoc", |
| 316 | + out = "mock_rustdoc.exe", |
| 317 | + ) |
| 318 | + rust_toolchain( |
| 319 | + name = "codegen_units_toolchain_impl", |
| 320 | + binary_ext = "", |
| 321 | + dylib_ext = ".so", |
| 322 | + exec_triple = "x86_64-unknown-none", |
| 323 | + target_triple = "x86_64-unknown-none", |
| 324 | + rust_doc = ":mock_rustdoc", |
| 325 | + rust_std = ":std_libs", |
| 326 | + rustc = ":mock_rustc", |
| 327 | + staticlib_ext = ".a", |
| 328 | + stdlib_linkflags = [], |
| 329 | + extra_rustc_flags = ["-Ccodegen-units=%s" % _TOOLCHAIN_EXTRA_RUSTC_FLAGS_CODEGEN_UNITS], |
| 330 | + extra_exec_rustc_flags = ["-Ccodegen-units=%s" % _TOOLCHAIN_EXTRA_EXEC_RUSTC_FLAGS_CODEGEN_UNITS], |
| 331 | + extra_rustc_flags_for_crate_types = {"bin": ["-Ccodegen-units=%s" % _TOOLCHAIN_EXTRA_RUSTC_FLAGS_FOR_CRATE_TYPES]}, |
| 332 | + visibility = ["//visibility:public"], |
| 333 | + ) |
| 334 | + native.toolchain( |
| 335 | + name = "codegen_units_toolchain", |
| 336 | + toolchain = ":codegen_units_toolchain_impl", |
| 337 | + toolchain_type = "@rules_rust//rust:toolchain", |
| 338 | + ) |
| 339 | + |
| 340 | + # Tests |
| 341 | + codegen_units_test( |
| 342 | + name = "codegen_units_filtered", |
| 343 | + target_under_test = ":codegen_units_bin_with_cc_common_link", |
| 344 | + # When using cc_common.link, we expect an explicit `-Ccodegen-units=1`. |
| 345 | + expected_codegen_units = [1], |
| 346 | + ) |
| 347 | + codegen_units_test( |
| 348 | + name = "codegen_units_filtered_exec", |
| 349 | + target_under_test = ":codegen_units_exec_bin_with_cc_common_link", |
| 350 | + # When using cc_common.link, we expect an explicit `-Ccodegen-units=1`. |
| 351 | + expected_codegen_units = [1], |
| 352 | + ) |
| 353 | + codegen_units_test( |
| 354 | + name = "codegen_units_not_filtered", |
| 355 | + target_under_test = ":codegen_units_bin_with_collect_dep_actions", |
| 356 | + # When not using cc_common.link, all `-Ccodegen-units` flags added in |
| 357 | + # various ways should remain unchanged. |
| 358 | + expected_codegen_units = [ |
| 359 | + _RUSTC_FLAGS_CODEGEN_UNITS, |
| 360 | + _SETTINGS_CODEGEN_UNITS, |
| 361 | + _PER_CRATE_RUSTC_FLAG_CODEGEN_UNITS, |
| 362 | + _EXTRA_RUSTC_FLAG_CODEGEN_UNITS, |
| 363 | + _EXTRA_RUSTC_FLAGS_CODEGEN_UNITS, |
| 364 | + _TOOLCHAIN_EXTRA_RUSTC_FLAGS_CODEGEN_UNITS, |
| 365 | + _TOOLCHAIN_EXTRA_RUSTC_FLAGS_FOR_CRATE_TYPES, |
| 366 | + ], |
| 367 | + ) |
| 368 | + codegen_units_test( |
| 369 | + name = "codegen_units_not_filtered_exec", |
| 370 | + target_under_test = ":codegen_units_exec_bin_with_collect_dep_actions", |
| 371 | + # When not using cc_common.link, all `-Ccodegen-units` flags added in |
| 372 | + # various ways should remain unchanged. |
| 373 | + expected_codegen_units = [ |
| 374 | + _RUSTC_FLAGS_CODEGEN_UNITS, |
| 375 | + _SETTINGS_CODEGEN_UNITS, |
| 376 | + _EXTRA_EXEC_RUSTC_FLAG_CODEGEN_UNITS, |
| 377 | + _EXTRA_EXEC_RUSTC_FLAGS_CODEGEN_UNITS, |
| 378 | + _TOOLCHAIN_EXTRA_EXEC_RUSTC_FLAGS_CODEGEN_UNITS, |
| 379 | + _TOOLCHAIN_EXTRA_RUSTC_FLAGS_FOR_CRATE_TYPES, |
| 380 | + ], |
| 381 | + ) |
| 382 | + |
| 383 | + return [ |
| 384 | + "codegen_units_filtered", |
| 385 | + "codegen_units_filtered_exec", |
| 386 | + "codegen_units_not_filtered", |
| 387 | + "codegen_units_not_filtered_exec", |
| 388 | + ] |
| 389 | + |
195 | 390 | def cc_common_link_test_suite(name, **kwargs):
|
196 | 391 | """Entry-point macro called from the BUILD file.
|
197 | 392 |
|
198 | 393 | Args:
|
199 | 394 | name: Name of the macro.
|
200 | 395 | **kwargs: Additional keyword arguments,
|
201 | 396 | """
|
202 |
| - _cc_common_link_test_targets() |
203 |
| - |
204 | 397 | native.test_suite(
|
205 | 398 | name = name,
|
206 |
| - tests = [ |
207 |
| - "use_cc_common_link_on_binary", |
208 |
| - "use_cc_common_link_on_binary_with_pdb", |
209 |
| - "use_cc_common_link_on_test", |
210 |
| - "use_cc_common_link_on_crate_test", |
211 |
| - "use_cc_common_link_on_cdylib", |
212 |
| - "custom_malloc_on_binary_test", |
213 |
| - ], |
| 399 | + tests = |
| 400 | + _cc_common_link_test_targets() + |
| 401 | + _codegen_units_test_targets(), |
214 | 402 | **kwargs
|
215 | 403 | )
|
0 commit comments