|
57 | 57 |
|
58 | 58 | # TODO(marco): Separate each rule into its own file. |
59 | 59 |
|
| 60 | +def _get_pkg_name(ctx): |
| 61 | + """Extract the Cargo package name from a 'crate-name=...' tag. |
| 62 | +
|
| 63 | + crate_universe adds this tag to vendored crates. Returns empty string if |
| 64 | + not found, in which case _create_crate_info defaults to the crate name. |
| 65 | + """ |
| 66 | + for tag in getattr(ctx.attr, "tags", []): |
| 67 | + if tag.startswith("crate-name="): |
| 68 | + return tag[len("crate-name="):] |
| 69 | + return "" |
| 70 | + |
60 | 71 | def _assert_no_deprecated_attributes(_ctx): |
61 | 72 | """Forces a failure if any deprecated attributes were specified |
62 | 73 |
|
@@ -245,6 +256,9 @@ def _rust_library_common(ctx, crate_type): |
245 | 256 | compile_data = depset(compile_data), |
246 | 257 | compile_data_targets = depset(ctx.attr.compile_data), |
247 | 258 | owner = ctx.label, |
| 259 | + version = getattr(ctx.attr, "version", "0.0.0"), |
| 260 | + source = getattr(ctx.attr, "source", "Local"), |
| 261 | + pkg_name = _get_pkg_name(ctx), |
248 | 262 | cfgs = _collect_cfgs(ctx, toolchain, crate_root, crate_type, crate_is_test = False), |
249 | 263 | ), |
250 | 264 | ) |
@@ -309,6 +323,9 @@ def _rust_binary_impl(ctx): |
309 | 323 | compile_data = depset(compile_data), |
310 | 324 | compile_data_targets = depset(ctx.attr.compile_data), |
311 | 325 | owner = ctx.label, |
| 326 | + version = getattr(ctx.attr, "version", "0.0.0"), |
| 327 | + source = getattr(ctx.attr, "source", "Local"), |
| 328 | + pkg_name = _get_pkg_name(ctx), |
312 | 329 | cfgs = _collect_cfgs(ctx, toolchain, crate_root, ctx.attr.crate_type, crate_is_test = False), |
313 | 330 | ), |
314 | 331 | ) |
@@ -431,6 +448,9 @@ def _rust_test_impl(ctx): |
431 | 448 | compile_data_targets = compile_data_targets, |
432 | 449 | wrapped_crate_type = crate.type, |
433 | 450 | owner = ctx.label, |
| 451 | + version = getattr(ctx.attr, "version", "0.0.0"), |
| 452 | + source = getattr(ctx.attr, "source", "Local"), |
| 453 | + pkg_name = _get_pkg_name(ctx), |
434 | 454 | cfgs = _collect_cfgs(ctx, toolchain, crate.root, crate_type, crate_is_test = True), |
435 | 455 | ) |
436 | 456 | else: |
@@ -487,6 +507,9 @@ def _rust_test_impl(ctx): |
487 | 507 | compile_data = depset(compile_data), |
488 | 508 | compile_data_targets = depset(ctx.attr.compile_data), |
489 | 509 | owner = ctx.label, |
| 510 | + version = getattr(ctx.attr, "version", "0.0.0"), |
| 511 | + source = getattr(ctx.attr, "source", "Local"), |
| 512 | + pkg_name = _get_pkg_name(ctx), |
490 | 513 | cfgs = _collect_cfgs(ctx, toolchain, crate_root, crate_type, crate_is_test = True), |
491 | 514 | ) |
492 | 515 |
|
@@ -605,6 +628,9 @@ RUSTC_ATTRS = { |
605 | 628 | "_always_enable_metadata_output_groups": attr.label( |
606 | 629 | default = Label("//rust/settings:always_enable_metadata_output_groups"), |
607 | 630 | ), |
| 631 | + "_auditable": attr.label( |
| 632 | + default = Label("//rust/settings:auditable"), |
| 633 | + ), |
608 | 634 | "_error_format": attr.label( |
609 | 635 | default = Label("//rust/settings:error_format"), |
610 | 636 | ), |
@@ -791,6 +817,10 @@ _COMMON_ATTRS = { |
791 | 817 | # "name": attr.string( |
792 | 818 | # doc = "This name will also be used as the name of the crate built by this rule.", |
793 | 819 | # `), |
| 820 | + "source": attr.string( |
| 821 | + doc = "The source of this crate (e.g. 'crates.io', 'git', 'local', 'registry'). Used for cargo-auditable dependency tracking.", |
| 822 | + default = "Local", |
| 823 | + ), |
794 | 824 | "srcs": attr.label_list( |
795 | 825 | doc = dedent("""\ |
796 | 826 | List of Rust `.rs` source files used to build the library. |
@@ -1082,9 +1112,17 @@ _rust_shared_library_transition = transition( |
1082 | 1112 | ], |
1083 | 1113 | ) |
1084 | 1114 |
|
| 1115 | +_AUDITABLE_INJECTOR_ATTR = { |
| 1116 | + "auditable_injector": attr.label( |
| 1117 | + doc = "The auditable_injector tool for embedding cargo-auditable metadata. " + |
| 1118 | + "Set to @rules_rust//tools/auditable:auditable_injector to enable.", |
| 1119 | + cfg = "exec", |
| 1120 | + ), |
| 1121 | +} |
| 1122 | + |
1085 | 1123 | rust_shared_library = rule( |
1086 | 1124 | implementation = _rust_shared_library_impl, |
1087 | | - attrs = _COMMON_ATTRS | _PLATFORM_ATTRS | _EXPERIMENTAL_USE_CC_COMMON_LINK_ATTRS, |
| 1125 | + attrs = _COMMON_ATTRS | _PLATFORM_ATTRS | _EXPERIMENTAL_USE_CC_COMMON_LINK_ATTRS | _AUDITABLE_INJECTOR_ATTR, |
1088 | 1126 | fragments = ["cpp"], |
1089 | 1127 | cfg = _rust_shared_library_transition, |
1090 | 1128 | toolchains = [ |
@@ -1207,7 +1245,7 @@ _rust_binary_transition = transition( |
1207 | 1245 | rust_binary = rule( |
1208 | 1246 | implementation = _rust_binary_impl, |
1209 | 1247 | provides = COMMON_PROVIDERS, |
1210 | | - attrs = _COMMON_ATTRS | _RUST_BINARY_ATTRS | _PLATFORM_ATTRS, |
| 1248 | + attrs = _COMMON_ATTRS | _RUST_BINARY_ATTRS | _PLATFORM_ATTRS | _AUDITABLE_INJECTOR_ATTR, |
1211 | 1249 | executable = True, |
1212 | 1250 | fragments = ["cpp"], |
1213 | 1251 | cfg = _rust_binary_transition, |
|
0 commit comments