|
| 1 | +"""Builds an instrumented Rust standard library for a rules_rust toolchain. |
| 2 | +
|
| 3 | +rules_rust consumes prebuilt standard-library rlibs but does not implement Cargo's |
| 4 | +-Zbuild-std. This rule invokes Cargo as a Bazel action instead of reproducing |
| 5 | +Rust's internal crate graph as Bazel targets (which would introduce a toolchain |
| 6 | +cycle). |
| 7 | +""" |
| 8 | + |
| 9 | +load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup") |
| 10 | + |
| 11 | +# rustc_lib contains host compiler libraries plus prebuilt target metadata. A |
| 12 | +# replacement toolchain must discard that metadata while retaining the runtime |
| 13 | +# archive needed when rustc links sanitizer-instrumented execution tools. |
| 14 | +def _compiler_runtime_files_impl(ctx): |
| 15 | + sanitizer_runtime = "_rt.{}.a".format(ctx.attr.sanitizer) |
| 16 | + return [DefaultInfo(files = depset([ |
| 17 | + file |
| 18 | + for file in ctx.files.src |
| 19 | + if "/lib/rustlib/" not in file.path or file.basename.endswith(sanitizer_runtime) |
| 20 | + ]))] |
| 21 | + |
| 22 | +compiler_runtime_files = rule( |
| 23 | + implementation = _compiler_runtime_files_impl, |
| 24 | + attrs = { |
| 25 | + "sanitizer": attr.string(mandatory = True), |
| 26 | + "src": attr.label(mandatory = True, allow_files = True), |
| 27 | + }, |
| 28 | +) |
| 29 | + |
| 30 | +# Bazel must know the output names before Cargo assigns metadata hashes. Keep the |
| 31 | +# expected -Zbuild-std closure in one place and copy each artifact to a stable name. |
| 32 | +_STDLIB_CRATES = [ |
| 33 | + "addr2line", |
| 34 | + "adler2", |
| 35 | + "alloc", |
| 36 | + "cfg_if", |
| 37 | + "compiler_builtins", |
| 38 | + "core", |
| 39 | + "getopts", |
| 40 | + "gimli", |
| 41 | + "hashbrown", |
| 42 | + "libc", |
| 43 | + "memchr", |
| 44 | + "miniz_oxide", |
| 45 | + "object", |
| 46 | + "panic_abort", |
| 47 | + "panic_unwind", |
| 48 | + "proc_macro", |
| 49 | + "rustc_demangle", |
| 50 | + "rustc_literal_escaper", |
| 51 | + "rustc_std_workspace_alloc", |
| 52 | + "rustc_std_workspace_core", |
| 53 | + "rustc_std_workspace_std", |
| 54 | + "std", |
| 55 | + "std_detect", |
| 56 | + "test", |
| 57 | + "unwind", |
| 58 | +] |
| 59 | + |
| 60 | +def _rust_build_std_impl(ctx): |
| 61 | + outputs = [ |
| 62 | + ctx.actions.declare_file("{}/lib{}_{}.rlib".format(ctx.label.name, crate, crate)) |
| 63 | + for crate in _STDLIB_CRATES |
| 64 | + ] |
| 65 | + |
| 66 | + output_dir = outputs[0].dirname |
| 67 | + ctx.actions.run( |
| 68 | + mnemonic = "RustBuildStd", |
| 69 | + progress_message = "Building the Rust standard library with {} instrumentation".format(ctx.attr.sanitizer), |
| 70 | + executable = ctx.executable._build_std, |
| 71 | + arguments = [ |
| 72 | + ctx.executable.rustc.dirname.removesuffix("/bin"), |
| 73 | + ctx.file.rust_src_manifest.dirname, |
| 74 | + output_dir + "-target", |
| 75 | + output_dir, |
| 76 | + output_dir + "-work", |
| 77 | + ctx.attr.target_triple, |
| 78 | + ctx.attr.sanitizer, |
| 79 | + ] + _STDLIB_CRATES, |
| 80 | + inputs = depset( |
| 81 | + ctx.files.rust_src + ctx.files.rust_toolchain_files, |
| 82 | + ), |
| 83 | + outputs = outputs, |
| 84 | + tools = [ctx.executable.cargo, ctx.executable.rustc], |
| 85 | + ) |
| 86 | + |
| 87 | + return [DefaultInfo(files = depset(outputs))] |
| 88 | + |
| 89 | +rust_build_std = rule( |
| 90 | + implementation = _rust_build_std_impl, |
| 91 | + attrs = { |
| 92 | + "cargo": attr.label(mandatory = True, executable = True, allow_files = True, cfg = "exec"), |
| 93 | + "rustc": attr.label(mandatory = True, executable = True, allow_files = True, cfg = "exec"), |
| 94 | + "rust_src": attr.label(mandatory = True, allow_files = True), |
| 95 | + "rust_src_manifest": attr.label(mandatory = True, allow_single_file = True), |
| 96 | + "rust_toolchain_files": attr.label_list(mandatory = True, allow_files = True), |
| 97 | + "sanitizer": attr.string(mandatory = True), |
| 98 | + "target_triple": attr.string(mandatory = True), |
| 99 | + "_build_std": attr.label( |
| 100 | + default = Label("//build/rust:build_std.sh"), |
| 101 | + executable = True, |
| 102 | + allow_single_file = True, |
| 103 | + cfg = "exec", |
| 104 | + ), |
| 105 | + }, |
| 106 | +) |
| 107 | + |
| 108 | +def instrumented_rust_std( |
| 109 | + name, |
| 110 | + sanitizer, |
| 111 | + target_triple, |
| 112 | + rust_tools, |
| 113 | + rust_src, |
| 114 | + tags = None, |
| 115 | + target_compatible_with = None, |
| 116 | + visibility = None): |
| 117 | + """Defines a rules_rust standard library instrumented by `sanitizer`.""" |
| 118 | + rust_build_std( |
| 119 | + name = name + "_build", |
| 120 | + cargo = rust_tools + "//:cargo", |
| 121 | + rustc = rust_tools + "//:rustc", |
| 122 | + rust_src = rust_src + "//:rust_src", |
| 123 | + rust_src_manifest = rust_src + "//:library/Cargo.toml", |
| 124 | + rust_toolchain_files = [rust_tools + "//:rustc_lib"], |
| 125 | + sanitizer = sanitizer, |
| 126 | + target_triple = target_triple, |
| 127 | + tags = tags, |
| 128 | + target_compatible_with = target_compatible_with, |
| 129 | + ) |
| 130 | + rust_stdlib_filegroup( |
| 131 | + name = name, |
| 132 | + srcs = [":" + name + "_build"], |
| 133 | + tags = tags, |
| 134 | + target_compatible_with = target_compatible_with, |
| 135 | + visibility = visibility, |
| 136 | + ) |
0 commit comments