blob: 7fc23db9be2b1fd3076ec5e9b136ce1fefef2665 [file] [log] [blame]
Tim Windelschmidtcb065e72024-07-15 19:20:06 +02001From 01a53a95a8fa20f2bf5ec6357baa457e7b2bc9f2 Mon Sep 17 00:00:00 2001
Tim Windelschmidt223609c2024-01-12 22:59:20 +01002From: Tim Windelschmidt <tim@monogon.tech>
3Date: Fri, 12 Jan 2024 15:44:41 +0100
4Subject: [PATCH] Improve reproducibility
5
Serge Bazanski4f00f902023-12-19 13:54:04 +01006Fixes a few issues with rules_rust/rustc reproducibility when the same code is
7being built in slightly different BuildConfigurations.
8
9Even if BuildConfigurations differ only by insignificant (to rules_rust)
10configuration flags, the resulting output directory will be different (keyed by
11an 'ST-hash' which is generated from the configuration).
12
13Unfortunately, rust/rules_rust really likes to embed bazel-out/<dir>/bin paths
14into the binaries by default, thus embedding the ST-hash, thus leading to
15different bit-for-bit binaries when built across two slightly different
16configs.
17
18We 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 Windelschmidt223609c2024-01-12 22:59:20 +010026---
27 proto/prost/private/prost.bzl | 4 +++-
28 proto/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 | 7 +++++--
33 6 files changed, 17 insertions(+), 8 deletions(-)
Serge Bazanski4f00f902023-12-19 13:54:04 +010034
35diff --git a/proto/prost/private/prost.bzl b/proto/prost/private/prost.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +020036index 38bd7b4f..645a520f 100644
Serge Bazanski4f00f902023-12-19 13:54:04 +010037--- a/proto/prost/private/prost.bzl
38+++ b/proto/prost/private/prost.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +020039@@ -131,7 +131,8 @@ def _compile_rust(ctx, attr, crate_name, src, deps, edition):
Serge Bazanski4f00f902023-12-19 13:54:04 +010040 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 Bazanski4f00f902023-12-19 13:54:04 +010045+ output_hash = repr(hash(src_path + ".prost"))
Tim Windelschmidt223609c2024-01-12 22:59:20 +010046
Serge Bazanski4f00f902023-12-19 13:54:04 +010047 lib_name = "{prefix}{name}-{lib_hash}{extension}".format(
48 prefix = "lib",
49diff --git a/proto/protobuf/proto.bzl b/proto/protobuf/proto.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +020050index 959d0c1c..00dff5d0 100644
Serge Bazanski4f00f902023-12-19 13:54:04 +010051--- a/proto/protobuf/proto.bzl
52+++ b/proto/protobuf/proto.bzl
Tim Windelschmidt223609c2024-01-12 22:59:20 +010053@@ -187,7 +187,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
Serge Bazanski4f00f902023-12-19 13:54:04 +010054 srcs.append(lib_rs)
Tim Windelschmidt223609c2024-01-12 22:59:20 +010055
Serge Bazanski4f00f902023-12-19 13:54:04 +010056 # 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,
62diff --git a/rust/private/clippy.bzl b/rust/private/clippy.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +020063index ef3ec2f8..cab2e6bf 100644
Serge Bazanski4f00f902023-12-19 13:54:04 +010064--- a/rust/private/clippy.bzl
65+++ b/rust/private/clippy.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +020066@@ -132,7 +132,7 @@ def _clippy_aspect_impl(target, ctx):
Serge Bazanski4f00f902023-12-19 13:54:04 +010067 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,
75diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +020076index afe1f129..ee73ee44 100644
Serge Bazanski4f00f902023-12-19 13:54:04 +010077--- a/rust/private/rust.bzl
78+++ b/rust/private/rust.bzl
Tim Windelschmidt223609c2024-01-12 22:59:20 +010079@@ -159,7 +159,7 @@ def _rust_library_common(ctx, crate_type):
Serge Bazanski4f00f902023-12-19 13:54:04 +010080 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 Windelschmidt223609c2024-01-12 22:59:20 +010085
86 rust_lib_name = determine_lib_name(
87 crate_name,
Tim Windelschmidtcb065e72024-07-15 19:20:06 +020088@@ -309,7 +309,7 @@ def _rust_test_impl(ctx):
Serge Bazanski4f00f902023-12-19 13:54:04 +010089 # Target is building the crate in `test` config
90 crate = ctx.attr.crate[rust_common.crate_info] if rust_common.crate_info in ctx.attr.crate else ctx.attr.crate[rust_common.test_crate_info].crate
Tim Windelschmidt223609c2024-01-12 22:59:20 +010091
Serge Bazanski4f00f902023-12-19 13:54:04 +010092- 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 Windelschmidtcb065e72024-07-15 19:20:06 +020097@@ -368,7 +368,7 @@ def _rust_test_impl(ctx):
Serge Bazanski4f00f902023-12-19 13:54:04 +010098 crate_root = crate_root_src(ctx.attr.name, ctx.files.srcs, crate_root_type)
Tim Windelschmidt223609c2024-01-12 22:59:20 +010099 srcs, crate_root = transform_sources(ctx, ctx.files.srcs, crate_root)
100
Serge Bazanski4f00f902023-12-19 13:54:04 +0100101- 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,
106diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +0200107index 6dbb258b..bb5e0b05 100644
Serge Bazanski4f00f902023-12-19 13:54:04 +0100108--- a/rust/private/rustc.bzl
109+++ b/rust/private/rustc.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +0200110@@ -965,6 +965,10 @@ def construct_arguments(
Serge Bazanski4f00f902023-12-19 13:54:04 +0100111 if remap_path_prefix != None:
112 rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(remap_path_prefix))
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100113
Serge Bazanski4f00f902023-12-19 13:54:04 +0100114+ # 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 Windelschmidtcb065e72024-07-15 19:20:06 +0200118 emit_without_paths = []
119 for kind in emit:
120 if kind == "link" and crate_info.type == "bin" and crate_info.output != None:
Serge Bazanski4f00f902023-12-19 13:54:04 +0100121diff --git a/rust/private/utils.bzl b/rust/private/utils.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +0200122index 57a3fe7a..315f45b6 100644
Serge Bazanski4f00f902023-12-19 13:54:04 +0100123--- a/rust/private/utils.bzl
124+++ b/rust/private/utils.bzl
Tim Windelschmidtcb065e72024-07-15 19:20:06 +0200125@@ -186,7 +186,7 @@ def abs(value):
Serge Bazanski4f00f902023-12-19 13:54:04 +0100126 return -value
127 return value
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100128
Serge Bazanski4f00f902023-12-19 13:54:04 +0100129-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 Windelschmidt223609c2024-01-12 22:59:20 +0100132
Serge Bazanski4f00f902023-12-19 13:54:04 +0100133 Args:
Tim Windelschmidtcb065e72024-07-15 19:20:06 +0200134@@ -197,8 +197,11 @@ def determine_output_hash(crate_root, label):
Serge Bazanski4f00f902023-12-19 13:54:04 +0100135 str: A string representation of the hash.
136 """
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100137
Serge Bazanski4f00f902023-12-19 13:54:04 +0100138+ # Remove any unstable BuildConfiguration derived dir fragments to unify
139+ # hashes between different configs.
140+ crate_root_path = crate_root.path.replace(bin_dir.path, 'bin')
141 # Take the absolute value of hash() since it could be negative.
142- h = abs(hash(crate_root.path) + hash(repr(label)))
143+ h = abs(hash(crate_root_path) + hash(repr(label)))
144 return repr(h)
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100145
Serge Bazanski4f00f902023-12-19 13:54:04 +0100146 def get_preferred_artifact(library_to_link, use_pic):
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100147--
Tim Windelschmidtcb065e72024-07-15 19:20:06 +02001482.44.1
Tim Windelschmidt223609c2024-01-12 22:59:20 +0100149