File: rmake.rs

package info (click to toggle)
rustc 1.90.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 925,928 kB
  • sloc: xml: 158,148; javascript: 19,781; sh: 19,174; python: 15,732; ansic: 13,096; cpp: 7,181; asm: 4,376; makefile: 697; lisp: 176; sql: 15
file content (283 lines) | stat: -rw-r--r-- 10,789 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// This test case makes sure that two identical invocations of the compiler
// (i.e. same code base, same compile-flags, same compiler-versions, etc.)
// produce the same output. In the past, symbol names of monomorphized functions
// were not deterministic (which we want to avoid).
//
// The test tries to exercise as many different paths into symbol name
// generation as possible:
//
// - regular functions
// - generic functions
// - methods
// - statics
// - closures
// - enum variant constructors
// - tuple struct constructors
// - drop glue
// - FnOnce adapters
// - Trait object shims
// - Fn Pointer shims
// See https://github.com/rust-lang/rust/pull/32293
// Tracking Issue: https://github.com/rust-lang/rust/issues/129080

//@ ignore-cross-compile (linker binary needs to run)

use run_make_support::{
    bin_name, cwd, diff, is_darwin, is_windows, regex, rfs, run_in_tmpdir, rust_lib_name, rustc,
};

fn main() {
    // Smoke tests. Simple flags, build should be reproducible.
    eprintln!("smoke_test => None");
    smoke_test(None);
    eprintln!("smoke_test => SmokeFlag::Debug");
    smoke_test(Some(SmokeFlag::Debug));
    eprintln!("smoke_test => SmokeFlag::Opt");
    smoke_test(Some(SmokeFlag::Opt));

    // Builds should be reproducible even through custom library search paths
    // or remap path prefixes.
    eprintln!("paths_test => PathsFlag::Link");
    paths_test(PathsFlag::Link);
    eprintln!("paths_test => PathsFlag::Remap");
    paths_test(PathsFlag::Remap);

    // Builds should be reproducible even if each build is done in a different directory,
    // with both --remap-path-prefix and -Z remap-cwd-prefix.

    // FIXME(Oneirical): Building with crate type set to `bin` AND having -Cdebuginfo=2
    // (or `-g`, the shorthand form) enabled will cause reproducibility failures.
    // See https://github.com/rust-lang/rust/issues/89911

    if !is_darwin() && !is_windows() {
        // FIXME(Oneirical): Bin builds are not reproducible on non-Linux targets.
        eprintln!("diff_dir_test => Bin, Path");
        diff_dir_test(CrateType::Bin, RemapType::Path);
    }

    eprintln!("diff_dir_test => Rlib, Path");
    diff_dir_test(CrateType::Rlib, RemapType::Path);

    // FIXME(Oneirical): This specific case would fail on Linux, should -Cdebuginfo=2
    // be added.
    // FIXME(Oneirical): Bin builds are not reproducible on non-Linux targets.
    // See https://github.com/rust-lang/rust/issues/89911
    if !is_darwin() && !is_windows() {
        eprintln!("diff_dir_test => Bin, Cwd false");
        diff_dir_test(CrateType::Bin, RemapType::Cwd { is_empty: false });
    }

    eprintln!("diff_dir_test => Rlib, Cwd false");
    diff_dir_test(CrateType::Rlib, RemapType::Cwd { is_empty: false });
    eprintln!("diff_dir_test => Rlib, Cwd true");
    diff_dir_test(CrateType::Rlib, RemapType::Cwd { is_empty: true });

    eprintln!("final extern test");
    // Builds should be reproducible when using the --extern flag.
    run_in_tmpdir(|| {
        rustc().input("reproducible-build-aux.rs").run();
        rustc()
            .input("reproducible-build.rs")
            .crate_type("rlib")
            .extern_("reproducible_build_aux", rust_lib_name("reproducible_build_aux"))
            .run();
        rfs::copy(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
        rfs::copy(rust_lib_name("reproducible_build_aux"), rust_lib_name("bar"));
        rustc()
            .input("reproducible-build.rs")
            .crate_type("rlib")
            .extern_("reproducible_build_aux", rust_lib_name("bar"))
            .run();
        assert!(rfs::read(rust_lib_name("foo")) == rfs::read(rust_lib_name("reproducible_build")))
    });
}

#[track_caller]
fn smoke_test(flag: Option<SmokeFlag>) {
    run_in_tmpdir(|| {
        rustc().input("linker.rs").opt().run();
        rustc().input("reproducible-build-aux.rs").run();
        let mut compiler1 = rustc();
        let mut compiler2 = rustc();
        if let Some(flag) = flag {
            match flag {
                SmokeFlag::Debug => {
                    compiler1.arg("-g");
                    compiler2.arg("-g");
                }
                SmokeFlag::Opt => {
                    compiler1.opt();
                    compiler2.opt();
                }
            };
        };
        compiler1
            .input("reproducible-build.rs")
            .linker(&cwd().join(bin_name("linker")).display().to_string())
            .run();
        compiler2
            .input("reproducible-build.rs")
            .linker(&cwd().join(bin_name("linker")).display().to_string())
            .run();

        #[cfg(not(target_os = "aix"))]
        {
            diff().actual_file("linker-arguments1").expected_file("linker-arguments2").run();
        }
        #[cfg(target_os = "aix")]
        {
            // The AIX link command includes an additional argument
            // that specifies the file containing exported symbols, e.g.,
            // -bE:/tmp/rustcO6hxkY/list.exp. In this example, the part of the
            // directory name "rustcO6hxkY" is randomly generated to ensure that
            // different linking processes do not collide. For the purpose
            // of comparing link arguments, the randomly generated part is
            // replaced with a placeholder.
            let content1 =
                std::fs::read_to_string("linker-arguments1").expect("Failed to read file");
            let content2 =
                std::fs::read_to_string("linker-arguments2").expect("Failed to read file");

            // Define the regex for the directory name containing the random substring.
            let re = regex::Regex::new(r"rustc[a-zA-Z0-9]{6}/list\.exp").expect("Invalid regex");

            // Compare link commands with random strings replaced by placeholders.
            assert!(
                re.replace_all(&content1, "rustcXXXXXX/list.exp").to_string()
                    == re.replace_all(&content2, "rustcXXXXXX/list.exp").to_string()
            );
        }
    });
}

#[track_caller]
fn paths_test(flag: PathsFlag) {
    run_in_tmpdir(|| {
        rustc().input("reproducible-build-aux.rs").run();
        let mut compiler1 = rustc();
        let mut compiler2 = rustc();
        match flag {
            PathsFlag::Link => {
                compiler1.library_search_path("a");
                compiler2.library_search_path("b");
            }
            PathsFlag::Remap => {
                compiler1.arg("--remap-path-prefix=/a=/c");
                compiler2.arg("--remap-path-prefix=/b=/c");
            }
        }
        compiler1.input("reproducible-build.rs").crate_type("rlib").run();
        rfs::rename(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
        compiler2.input("reproducible-build.rs").crate_type("rlib").run();
        assert!(rfs::read(rust_lib_name("foo")) == rfs::read(rust_lib_name("reproducible_build")))
    });
}

#[track_caller]
fn diff_dir_test(crate_type: CrateType, remap_type: RemapType) {
    run_in_tmpdir(|| {
        let base_dir = cwd();
        rustc().input("reproducible-build-aux.rs").run();
        rfs::create_dir("test");
        rfs::copy("reproducible-build.rs", "test/reproducible-build.rs");
        let mut compiler1 = rustc();
        let mut compiler2 = rustc();
        match crate_type {
            CrateType::Bin => {
                compiler1.crate_type("bin");
                compiler2.crate_type("bin");
            }
            CrateType::Rlib => {
                compiler1.crate_type("rlib");
                compiler2.crate_type("rlib");
            }
        }
        match remap_type {
            RemapType::Path => {
                compiler1.arg(&format!("--remap-path-prefix={}=/b", cwd().display()));
                compiler2
                    .arg(format!("--remap-path-prefix={}=/b", base_dir.join("test").display()));
            }
            RemapType::Cwd { is_empty } => {
                // FIXME(Oneirical): Building with crate type set to `bin` AND having -Cdebuginfo=2
                // (or `-g`, the shorthand form) enabled will cause reproducibility failures
                // for multiple platforms.
                // See https://github.com/rust-lang/rust/issues/89911
                // FIXME(#129117): Windows rlib + `-Cdebuginfo=2` + `-Z remap-cwd-prefix=.` seems
                // to be unreproducible.
                if !matches!(crate_type, CrateType::Bin) && !is_windows() {
                    compiler1.arg("-Cdebuginfo=2");
                    compiler2.arg("-Cdebuginfo=2");
                }
                if is_empty {
                    compiler1.arg("-Zremap-cwd-prefix=");
                    compiler2.arg("-Zremap-cwd-prefix=");
                } else {
                    compiler1.arg("-Zremap-cwd-prefix=.");
                    compiler2.arg("-Zremap-cwd-prefix=.");
                }
            }
        }
        compiler1.input("reproducible-build.rs").run();
        match crate_type {
            CrateType::Bin => {
                rfs::rename(bin_name("reproducible-build"), bin_name("foo"));
            }
            CrateType::Rlib => {
                rfs::rename(rust_lib_name("reproducible_build"), rust_lib_name("foo"));
            }
        }
        std::env::set_current_dir("test").unwrap();
        compiler2
            .input("reproducible-build.rs")
            .library_search_path(&base_dir)
            .out_dir(&base_dir)
            .run();
        std::env::set_current_dir(&base_dir).unwrap();
        match crate_type {
            CrateType::Bin => {
                #[cfg(not(target_os = "aix"))]
                {
                    assert!(
                        rfs::read(bin_name("reproducible-build")) == rfs::read(bin_name("foo"))
                    );
                }
                #[cfg(target_os = "aix")]
                {
                    // At the 4th-byte offset, the AIX XCOFF file header defines a
                    // 4-byte timestamp. Nullify the timestamp before performing a
                    // binary comparison.
                    let mut file1 = rfs::read(bin_name("reproducible-build"));
                    let mut file2 = rfs::read(bin_name("foo"));
                    assert!(file1[4..8].fill(0x00) == file2[4..8].fill(0x00));
                };
            }
            CrateType::Rlib => {
                assert!(
                    rfs::read(rust_lib_name("foo"))
                        == rfs::read(rust_lib_name("reproducible_build"))
                );
            }
        }
    });
}

enum SmokeFlag {
    Debug,
    Opt,
}

enum PathsFlag {
    Link,
    Remap,
}

enum CrateType {
    Bin,
    Rlib,
}

enum RemapType {
    Path,
    Cwd { is_empty: bool },
}