| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 1 | From b05e8fb3ae3a385d5615a8e9753f4510c4ca8d79 Mon Sep 17 00:00:00 2001 |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame] | 2 | From: Serge Bazanski <serge@monogon.tech> |
| 3 | Date: Wed, 25 Sep 2024 02:38:50 +0200 |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 4 | Subject: [PATCH 3/4] Improve reproducibility |
| Tim Windelschmidt | 223609c | 2024-01-12 22:59:20 +0100 | [diff] [blame] | 5 | |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 6 | Fixes a few issues with rules_rust/rustc reproducibility when the same code is |
| 7 | being built in slightly different BuildConfigurations. |
| 8 | |
| 9 | Even if BuildConfigurations differ only by insignificant (to rules_rust) |
| 10 | configuration flags, the resulting output directory will be different (keyed by |
| 11 | an 'ST-hash' which is generated from the configuration). |
| 12 | |
| 13 | Unfortunately, rust/rules_rust really likes to embed bazel-out/<dir>/bin paths |
| 14 | into the binaries by default, thus embedding the ST-hash, thus leading to |
| 15 | different bit-for-bit binaries when built across two slightly different |
| 16 | configs. |
| 17 | |
| 18 | We fix this by doing two changes: |
| 19 | |
| 20 | 1. We override the codegen metadata hash suffix to not depend on the ST-hash |
| 21 | directory name. Otherwise, built rlibs will have a numeric .XXX suffix that |
| 22 | changes depending on the ST-hash. We have to do this separately for prost |
| 23 | codegen, too. |
| 24 | 2. We add a remap path option to rustc that replaces bazel-out/<dir>/bin/ with |
| 25 | bin/. |
| Tim Windelschmidt | 223609c | 2024-01-12 22:59:20 +0100 | [diff] [blame] | 26 | --- |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 27 | extensions/prost/private/prost.bzl | 3 ++- |
| 28 | extensions/protobuf/proto.bzl | 2 +- |
| 29 | rust/private/clippy.bzl | 2 +- |
| 30 | rust/private/rust.bzl | 6 +++--- |
| 31 | rust/private/rustc.bzl | 4 ++++ |
| 32 | rust/private/utils.bzl | 8 ++++++-- |
| 33 | 6 files changed, 17 insertions(+), 8 deletions(-) |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 34 | |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 35 | diff --git a/extensions/prost/private/prost.bzl b/extensions/prost/private/prost.bzl |
| 36 | index 6d9d4969..8dad0961 100644 |
| 37 | --- a/extensions/prost/private/prost.bzl |
| 38 | +++ b/extensions/prost/private/prost.bzl |
| 39 | @@ -164,7 +164,8 @@ def _compile_rust( |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 40 | A DepVariantInfo provider. |
| 41 | """ |
| 42 | toolchain = ctx.toolchains["@rules_rust//rust:toolchain_type"] |
| 43 | - output_hash = repr(hash(src.path + ".prost")) |
| 44 | + src_path = src.path.replace(ctx.bin_dir.path, 'bin') |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 45 | + output_hash = repr(hash(src_path + ".prost")) |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 46 | |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 47 | lib_name = "{prefix}{name}-{lib_hash}{extension}".format( |
| 48 | prefix = "lib", |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 49 | diff --git a/extensions/protobuf/proto.bzl b/extensions/protobuf/proto.bzl |
| 50 | index 56025417..7c025b11 100644 |
| 51 | --- a/extensions/protobuf/proto.bzl |
| 52 | +++ b/extensions/protobuf/proto.bzl |
| Tim Windelschmidt | 223609c | 2024-01-12 22:59:20 +0100 | [diff] [blame] | 53 | @@ -187,7 +187,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 54 | srcs.append(lib_rs) |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 55 | |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 56 | # And simulate rust_library behavior |
| 57 | - output_hash = determine_output_hash(lib_rs, ctx.label) |
| 58 | + output_hash = determine_output_hash(ctx.bin_dir, lib_rs, ctx.label) |
| 59 | rust_lib = ctx.actions.declare_file("%s/lib%s-%s.rlib" % ( |
| 60 | output_dir, |
| 61 | crate_name, |
| 62 | diff --git a/rust/private/clippy.bzl b/rust/private/clippy.bzl |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 63 | index 8c172ae0..efdba8fc 100644 |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 64 | --- a/rust/private/clippy.bzl |
| 65 | +++ b/rust/private/clippy.bzl |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 66 | @@ -138,7 +138,7 @@ def _clippy_aspect_impl(target, ctx): |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 67 | dep_info = dep_info, |
| 68 | linkstamp_outs = linkstamp_outs, |
| 69 | ambiguous_libs = ambiguous_libs, |
| 70 | - output_hash = determine_output_hash(crate_info.root, ctx.label), |
| 71 | + output_hash = determine_output_hash(ctx.bin_dir, crate_info.root, ctx.label), |
| 72 | rust_flags = [], |
| 73 | out_dir = out_dir, |
| 74 | build_env_files = build_env_files, |
| 75 | diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 76 | index 2717d3f5..be277010 100644 |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 77 | --- a/rust/private/rust.bzl |
| 78 | +++ b/rust/private/rust.bzl |
| Tim Windelschmidt | 223609c | 2024-01-12 22:59:20 +0100 | [diff] [blame] | 79 | @@ -159,7 +159,7 @@ def _rust_library_common(ctx, crate_type): |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 80 | if crate_type in ["cdylib", "staticlib"]: |
| 81 | output_hash = None |
| 82 | else: |
| 83 | - output_hash = determine_output_hash(crate_root, ctx.label) |
| 84 | + output_hash = determine_output_hash(ctx.bin_dir, crate_root, ctx.label) |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 85 | |
| Tim Windelschmidt | 223609c | 2024-01-12 22:59:20 +0100 | [diff] [blame] | 86 | rust_lib_name = determine_lib_name( |
| 87 | crate_name, |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 88 | @@ -321,7 +321,7 @@ def _rust_test_impl(ctx): |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame] | 89 | ) |
| 90 | else: |
| 91 | crate_name = crate.name |
| 92 | - output_hash = determine_output_hash(crate.root, ctx.label) |
| 93 | + output_hash = determine_output_hash(ctx.bin_dir, crate.root, ctx.label) |
| 94 | output = ctx.actions.declare_file( |
| 95 | "test-%s/%s%s" % ( |
| 96 | output_hash, |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 97 | @@ -388,7 +388,7 @@ def _rust_test_impl(ctx): |
| Tim Windelschmidt | 5d0f634 | 2024-09-25 03:35:00 +0200 | [diff] [blame] | 98 | ctx.label.name + toolchain.binary_ext, |
| 99 | ) |
| 100 | else: |
| 101 | - output_hash = determine_output_hash(crate_root, ctx.label) |
| 102 | + output_hash = determine_output_hash(ctx.bin_dir, crate_root, ctx.label) |
| 103 | output = ctx.actions.declare_file( |
| 104 | "test-%s/%s%s" % ( |
| 105 | output_hash, |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 106 | diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 107 | index 2bc6ce19..73e472f5 100644 |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 108 | --- a/rust/private/rustc.bzl |
| 109 | +++ b/rust/private/rustc.bzl |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 110 | @@ -972,6 +972,10 @@ def construct_arguments( |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 111 | if remap_path_prefix != None: |
| 112 | rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(remap_path_prefix)) |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 113 | |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 114 | + # Replace unstable bindir path (based on ST-hash which is in turn based on |
| 115 | + # build configuration) with a stable bin/ path. |
| 116 | + rustc_flags.add("--remap-path-prefix={}=bin".format(ctx.bin_dir.path)) |
| 117 | + |
| Tim Windelschmidt | cb065e7 | 2024-07-15 19:20:06 +0200 | [diff] [blame] | 118 | emit_without_paths = [] |
| 119 | for kind in emit: |
| 120 | if kind == "link" and crate_info.type == "bin" and crate_info.output != None: |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 121 | diff --git a/rust/private/utils.bzl b/rust/private/utils.bzl |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 122 | index 7b11c10d..41f7d2db 100644 |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 123 | --- a/rust/private/utils.bzl |
| 124 | +++ b/rust/private/utils.bzl |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 125 | @@ -176,7 +176,7 @@ def get_lib_name_for_windows(lib): |
| 126 | |
| 127 | return libname |
| 128 | |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 129 | -def determine_output_hash(crate_root, label): |
| 130 | +def determine_output_hash(bin_dir, crate_root, label): |
| 131 | """Generates a hash of the crate root file's path. |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 132 | |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 133 | Args: |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 134 | @@ -187,8 +187,12 @@ def determine_output_hash(crate_root, label): |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 135 | str: A string representation of the hash. |
| 136 | """ |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 137 | |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 138 | + # Remove any unstable BuildConfiguration derived dir fragments to unify |
| 139 | + # hashes between different configs. |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 140 | + crate_root_path = crate_root.path.replace(bin_dir.path, "bin") |
| 141 | + |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 142 | # Take the absolute value of hash() since it could be negative. |
| 143 | - h = abs(hash(crate_root.path) + hash(repr(label))) |
| 144 | + h = abs(hash(crate_root_path) + hash(repr(label))) |
| 145 | return repr(h) |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 146 | |
| Serge Bazanski | 4f00f90 | 2023-12-19 13:54:04 +0100 | [diff] [blame] | 147 | def get_preferred_artifact(library_to_link, use_pic): |
| Tim Windelschmidt | d634975 | 2025-01-09 06:43:26 +0100 | [diff] [blame^] | 148 | -- |
| 149 | 2.47.0 |
| Tim Windelschmidt | 223609c | 2024-01-12 22:59:20 +0100 | [diff] [blame] | 150 | |