Skip to content

Commit cf49241

Browse files
committed
massive refactor of reproducible-build test
1 parent 6f554ec commit cf49241

File tree

1 file changed

+140
-152
lines changed
  • tests/run-make/reproducible-build

1 file changed

+140
-152
lines changed

tests/run-make/reproducible-build/rmake.rs

+140-152
Original file line numberDiff line numberDiff line change
@@ -30,193 +30,181 @@
3030
use run_make_support::{bin_name, cwd, diff, rfs, run_in_tmpdir, rust_lib_name, rustc};
3131

3232
fn main() {
33-
run_in_tmpdir(|| {
34-
rustc().input("linker.rs").opt().run();
35-
rustc().input("reproducible-build-aux.rs").run();
36-
rustc()
37-
.input("reproducible-build.rs")
38-
.linker(&cwd().join(bin_name("linker")).display().to_string())
39-
.run();
40-
rustc()
41-
.input("reproducible-build.rs")
42-
.linker(&cwd().join(bin_name("linker")).display().to_string())
43-
.run();
44-
diff().actual_file("linker-arguments1").expected_file("linker-arguments2").run();
45-
});
46-
47-
run_in_tmpdir(|| {
48-
rustc().input("linker.rs").opt().run();
49-
rustc().arg("-g").input("reproducible-build-aux.rs").run();
50-
rustc()
51-
.arg("-g")
52-
.input("reproducible-build.rs")
53-
.linker(&cwd().join(bin_name("linker")).display().to_string())
54-
.run();
55-
rustc()
56-
.arg("-g")
57-
.input("reproducible-build.rs")
58-
.linker(&cwd().join(bin_name("linker")).display().to_string())
59-
.run();
60-
diff().actual_file("linker-arguments1").expected_file("linker-arguments2").run();
61-
});
33+
// Smoke tests. Simple flags, build should be reproducible.
34+
smoke_test(None);
35+
smoke_test(Some(SmokeFlag::Debug));
36+
smoke_test(Some(SmokeFlag::Opt));
6237

63-
run_in_tmpdir(|| {
64-
rustc().input("linker.rs").opt().run();
65-
rustc().opt().input("reproducible-build-aux.rs").run();
66-
rustc()
67-
.opt()
68-
.input("reproducible-build.rs")
69-
.linker(&cwd().join(bin_name("linker")).display().to_string())
70-
.run();
71-
rustc()
72-
.opt()
73-
.input("reproducible-build.rs")
74-
.linker(&cwd().join(bin_name("linker")).display().to_string())
75-
.run();
76-
diff().actual_file("linker-arguments1").expected_file("linker-arguments2").run();
77-
});
38+
// Builds should be reproducible even through custom library search paths
39+
// or remap path prefixes.
40+
paths_test(PathsFlag::Link);
41+
paths_test(PathsFlag::Remap);
7842

79-
run_in_tmpdir(|| {
80-
rustc().input("reproducible-build-aux.rs").run();
81-
rustc().input("reproducible-build.rs").crate_type("rlib").library_search_path("b").run();
82-
rfs::copy(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
83-
rustc().input("reproducible-build.rs").crate_type("rlib").library_search_path("a").run();
84-
assert_eq!(rfs::read(rust_lib_name("reproducible_build")), rfs::read(rust_lib_name("foo")));
85-
});
43+
// Builds should be reproducible even if each build is done in a different directory,
44+
// with both --remap-path-prefix and -Z remap-cwd-prefix.
45+
diff_dir_test(CrateType::Bin, RemapType::Path);
46+
diff_dir_test(CrateType::Rlib, RemapType::Path);
47+
// FIXME(Oneirical): this specific case fails the final assertion.
48+
// diff_dir_test(CrateType::Bin, RemapType::Cwd { is_empty: false });
49+
diff_dir_test(CrateType::Rlib, RemapType::Cwd { is_empty: false });
50+
diff_dir_test(CrateType::Rlib, RemapType::Cwd { is_empty: true });
8651

52+
// Builds should be reproducible when using the --extern flag.
8753
run_in_tmpdir(|| {
8854
rustc().input("reproducible-build-aux.rs").run();
8955
rustc()
9056
.input("reproducible-build.rs")
9157
.crate_type("rlib")
92-
.arg("--remap-path-prefix=/a=/c")
58+
.extern_("reproducible_build_aux", rust_lib_name("reproducible_build_aux"))
9359
.run();
9460
rfs::copy(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
61+
rfs::copy(rust_lib_name("reproducible_build_aux"), rust_lib_name("bar"));
9562
rustc()
9663
.input("reproducible-build.rs")
9764
.crate_type("rlib")
98-
.arg("--remap-path-prefix=/b=/c")
65+
.extern_("reproducible_build_aux", rust_lib_name("bar"))
9966
.run();
100-
assert_eq!(rfs::read(rust_lib_name("reproducible_build")), rfs::read(rust_lib_name("foo")));
67+
assert_eq!(rfs::read(rust_lib_name("foo")), rfs::read(rust_lib_name("reproducible_build")));
10168
});
69+
}
10270

71+
#[track_caller]
72+
fn smoke_test(flag: Option<SmokeFlag>) {
10373
run_in_tmpdir(|| {
74+
rustc().input("linker.rs").opt().run();
10475
rustc().input("reproducible-build-aux.rs").run();
105-
rfs::create_dir("test");
106-
rfs::copy("reproducible-build.rs", "test/reproducible-build.rs");
107-
rustc()
76+
let mut compiler1 = rustc();
77+
let mut compiler2 = rustc();
78+
if let Some(flag) = flag {
79+
match flag {
80+
SmokeFlag::Debug => {
81+
compiler1.arg("-g");
82+
compiler2.arg("-g");
83+
}
84+
SmokeFlag::Opt => {
85+
compiler1.opt();
86+
compiler2.opt();
87+
}
88+
};
89+
};
90+
compiler1
10891
.input("reproducible-build.rs")
109-
.crate_type("bin")
110-
.arg(&format!("--remap-path-prefix={}=/b", cwd().display()))
92+
.linker(&cwd().join(bin_name("linker")).display().to_string())
11193
.run();
112-
eprintln!("{:#?}", rfs::shallow_find_dir_entries(cwd()));
113-
rfs::copy(bin_name("reproducible_build"), bin_name("foo"));
114-
rustc()
115-
.input("test/reproducible-build.rs")
116-
.crate_type("bin")
117-
.arg("--remap-path-prefix=/test=/b")
94+
compiler2
95+
.input("reproducible-build.rs")
96+
.linker(&cwd().join(bin_name("linker")).display().to_string())
11897
.run();
119-
assert_eq!(rfs::read(bin_name("reproducible_build")), rfs::read(bin_name("foo")));
98+
diff().actual_file("linker-arguments1").expected_file("linker-arguments2").run();
12099
});
100+
}
121101

102+
#[track_caller]
103+
fn paths_test(flag: PathsFlag) {
122104
run_in_tmpdir(|| {
123105
rustc().input("reproducible-build-aux.rs").run();
124-
rfs::create_dir("test");
125-
rfs::copy("reproducible-build.rs", "test/reproducible-build.rs");
126-
rustc()
127-
.input("reproducible-build.rs")
128-
.crate_type("rlib")
129-
.arg(&format!("--remap-path-prefix={}=/b", cwd().display()))
130-
.run();
131-
rfs::copy(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
132-
rustc()
133-
.input("test/reproducible-build.rs")
134-
.crate_type("rlib")
135-
.arg("--remap-path-prefix=/test=/b")
136-
.run();
106+
let mut compiler1 = rustc();
107+
let mut compiler2 = rustc();
108+
match flag {
109+
PathsFlag::Link => {
110+
compiler1.library_search_path("a");
111+
compiler2.library_search_path("b");
112+
}
113+
PathsFlag::Remap => {
114+
compiler1.arg("--remap-path-prefix=/a=/c");
115+
compiler2.arg("--remap-path-prefix=/b=/c");
116+
}
117+
}
118+
compiler1.input("reproducible-build.rs").crate_type("rlib").run();
119+
rfs::rename(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
120+
compiler2.input("reproducible-build.rs").crate_type("rlib").run();
137121
assert_eq!(rfs::read(rust_lib_name("reproducible_build")), rfs::read(rust_lib_name("foo")));
138122
});
123+
}
139124

125+
#[track_caller]
126+
fn diff_dir_test(crate_type: CrateType, remap_type: RemapType) {
140127
run_in_tmpdir(|| {
128+
let base_dir = cwd();
141129
rustc().input("reproducible-build-aux.rs").run();
142130
rfs::create_dir("test");
143131
rfs::copy("reproducible-build.rs", "test/reproducible-build.rs");
144-
rustc()
132+
let mut compiler1 = rustc();
133+
let mut compiler2 = rustc();
134+
match crate_type {
135+
CrateType::Bin => {
136+
compiler1.crate_type("bin");
137+
compiler2.crate_type("bin");
138+
}
139+
CrateType::Rlib => {
140+
compiler1.crate_type("rlib");
141+
compiler2.crate_type("rlib");
142+
}
143+
}
144+
match remap_type {
145+
RemapType::Path => {
146+
compiler1.arg(&format!("--remap-path-prefix={}=/b", cwd().display()));
147+
compiler2
148+
.arg(format!("--remap-path-prefix={}=/b", base_dir.join("test").display()));
149+
}
150+
RemapType::Cwd { is_empty } => {
151+
compiler1.arg("-g");
152+
compiler2.arg("-g");
153+
if is_empty {
154+
compiler1.arg("-Zremap-cwd-prefix=");
155+
compiler2.arg("-Zremap-cwd-prefix=");
156+
} else {
157+
compiler1.arg("-Zremap-cwd-prefix=.");
158+
compiler2.arg("-Zremap-cwd-prefix=.");
159+
}
160+
}
161+
}
162+
compiler1.input("reproducible-build.rs").run();
163+
match crate_type {
164+
CrateType::Bin => {
165+
rfs::rename(bin_name("reproducible-build"), bin_name("foo"));
166+
}
167+
CrateType::Rlib => {
168+
rfs::rename(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
169+
}
170+
}
171+
std::env::set_current_dir("test").unwrap();
172+
compiler2
145173
.input("reproducible-build.rs")
146-
.crate_type("bin")
147-
.arg("-Zremap-path-prefix=.")
148-
.arg("-Cdebuginfo=2")
149-
.run();
150-
rfs::copy(bin_name("reproducible_build"), bin_name("first"));
151-
rustc()
152-
.input("test/reproducible-build.rs")
153-
.crate_type("bin")
154-
.arg("-Zremap-path-prefix=.")
155-
.arg("-Cdebuginfo=2")
156-
.run();
157-
assert_eq!(rfs::read(bin_name("first")), rfs::read(bin_name("reproducible_build")));
174+
.library_search_path(&base_dir)
175+
.out_dir(&base_dir)
176+
.run();
177+
std::env::set_current_dir(&base_dir).unwrap();
178+
match crate_type {
179+
CrateType::Bin => {
180+
assert!(rfs::read(bin_name("reproducible-build")) == rfs::read(bin_name("foo")));
181+
}
182+
CrateType::Rlib => {
183+
assert_eq!(
184+
rfs::read(rust_lib_name("foo")),
185+
rfs::read(rust_lib_name("reproducible_build"))
186+
);
187+
}
188+
}
158189
});
190+
}
159191

160-
run_in_tmpdir(|| {
161-
rustc().input("reproducible-build-aux.rs").run();
162-
rfs::create_dir("test");
163-
rfs::copy("reproducible-build.rs", "test/reproducible-build.rs");
164-
rustc()
165-
.input("reproducible-build.rs")
166-
.crate_type("rlib")
167-
.arg("-Zremap-path-prefix=.")
168-
.arg("-Cdebuginfo=2")
169-
.run();
170-
rfs::copy("reproducible_build", "first");
171-
rustc()
172-
.input("test/reproducible-build.rs")
173-
.crate_type("rlib")
174-
.arg("-Zremap-path-prefix=.")
175-
.arg("-Cdebuginfo=2")
176-
.run();
177-
assert_eq!(
178-
rfs::read(rust_lib_name("first")),
179-
rfs::read(rust_lib_name("reproducible_build"))
180-
);
181-
});
192+
enum SmokeFlag {
193+
Debug,
194+
Opt,
195+
}
182196

183-
run_in_tmpdir(|| {
184-
rustc().input("reproducible-build-aux.rs").run();
185-
rfs::create_dir("test");
186-
rfs::copy("reproducible-build.rs", "test/reproducible-build.rs");
187-
rustc()
188-
.input("reproducible-build.rs")
189-
.crate_type("rlib")
190-
.arg("-Zremap-path-prefix=")
191-
.arg("-Cdebuginfo=2")
192-
.run();
193-
rfs::copy(rust_lib_name("reproducible_build"), rust_lib_name("first"));
194-
rustc()
195-
.input("test/reproducible-build.rs")
196-
.crate_type("rlib")
197-
.arg("-Zremap-path-prefix=")
198-
.arg("-Cdebuginfo=2")
199-
.run();
200-
assert_eq!(
201-
rfs::read(rust_lib_name("first")),
202-
rfs::read(rust_lib_name("reproducible_build"))
203-
);
204-
});
197+
enum PathsFlag {
198+
Link,
199+
Remap,
200+
}
205201

206-
run_in_tmpdir(|| {
207-
rustc().input("reproducible-build-aux.rs").run();
208-
rustc()
209-
.input("reproducible-build.rs")
210-
.crate_type("rlib")
211-
.extern_("reproducible_build_aux", rust_lib_name("reproducible_build_aux"))
212-
.run();
213-
rfs::copy(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
214-
rfs::copy(rust_lib_name("reproducible_build_aux"), rust_lib_name("bar"));
215-
rustc()
216-
.input("reproducible-build.rs")
217-
.crate_type("rlib")
218-
.extern_("reproducible_build_aux", rust_lib_name("bar"))
219-
.run();
220-
assert_eq!(rfs::read(rust_lib_name("foo")), rfs::read(rust_lib_name("reproducible_build")));
221-
});
202+
enum CrateType {
203+
Bin,
204+
Rlib,
205+
}
206+
207+
enum RemapType {
208+
Path,
209+
Cwd { is_empty: bool },
222210
}

0 commit comments

Comments
 (0)