File: rmake.rs

package info (click to toggle)
rustc 1.89.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 906,624 kB
  • sloc: xml: 158,148; python: 34,888; javascript: 19,595; sh: 19,221; ansic: 13,046; cpp: 7,144; asm: 4,376; makefile: 692; lisp: 174; sql: 15
file content (131 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download | duplicates (15)
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
//! Regression test for rust-lang/rust#70924. Check that if we add the `rust-src` component in
//! between two incremental compiles, that the compiler doesn't ICE on the second invocation.
//!
//! This test uses symbolic links to save testing time.
//!
//! The way this test works is that, for every prefix in `root/lib/rustlib/src`, link all of prefix
//! parent content, then remove the prefix, then loop on the next prefix. This way, we basically
//! create a copy of the context around `root/lib/rustlib/src`, and can freely add/remove the src
//! component itself.

//@ ignore-cross-compile
// Reason: test needs to run.

//@ needs-symlink
// Reason: test needs symlink to create stub directories and files.

use std::path::Path;

use run_make_support::rfs::read_dir_entries;
use run_make_support::{bare_rustc, path, rfs, run};

#[derive(Debug, Copy, Clone)]
struct Symlink<'a, 'b> {
    src_dir: &'a Path,
    dst_dir: &'b Path,
}

fn shallow_symlink_dir<'a, 'b>(Symlink { src_dir, dst_dir }: Symlink<'a, 'b>) {
    eprintln!(
        "shallow_symlink_dir: src_dir={} -> dst_dir={}",
        src_dir.display(),
        dst_dir.display()
    );

    read_dir_entries(src_dir, |src_path| {
        let src_metadata = rfs::symlink_metadata(src_path);
        let filename = src_path.file_name().unwrap();
        if src_metadata.is_dir() {
            rfs::symlink_dir(src_path, dst_dir.join(filename));
        } else if src_metadata.is_file() {
            rfs::symlink_file(src_path, dst_dir.join(filename));
        } else if src_metadata.is_symlink() {
            rfs::copy_symlink(src_path, dst_dir.join(filename));
        }
    });
}

fn recreate_dir(path: &Path) {
    rfs::recursive_remove(path);
    rfs::create_dir(path);
}

fn main() {
    let sysroot = bare_rustc().print("sysroot").run().stdout_utf8();
    let sysroot = sysroot.trim();
    let sysroot = path(sysroot);

    let incr = path("incr");

    let fakeroot = path("fakeroot");
    rfs::create_dir(&fakeroot);

    shallow_symlink_dir(Symlink { src_dir: &sysroot, dst_dir: &fakeroot });
    recreate_dir(&fakeroot.join("lib"));

    shallow_symlink_dir(Symlink { src_dir: &sysroot.join("lib"), dst_dir: &fakeroot.join("lib") });
    recreate_dir(&fakeroot.join("lib").join("rustlib"));

    shallow_symlink_dir(Symlink {
        src_dir: &sysroot.join("lib").join("rustlib"),
        dst_dir: &fakeroot.join("lib").join("rustlib"),
    });
    recreate_dir(&fakeroot.join("lib").join("rustlib").join("src"));

    shallow_symlink_dir(Symlink {
        src_dir: &sysroot.join("lib").join("rustlib").join("src"),
        dst_dir: &fakeroot.join("lib").join("rustlib").join("src"),
    });

    rfs::recursive_remove(&fakeroot.join("lib").join("rustlib").join("src").join("rust"));

    let run_incr_rustc = || {
        bare_rustc()
            .sysroot(&fakeroot)
            .arg("-C")
            .arg(format!("incremental={}", incr.to_str().unwrap()))
            .input("main.rs")
            .run();
    };

    // Run rustc w/ incremental once...
    run_incr_rustc();

    // NOTE: the Makefile version of this used `$SYSROOT/lib/rustlib/src/rust/src/libstd/lib.rs`,
    // but that actually got moved around and reorganized over the years. As of Dec 2024, the
    // rust-src component is more like (specific for our purposes):
    //
    // ```
    // $SYSROOT/lib/rustlib/src/rust/
    //     library/std/src/lib.rs
    //     src/
    // ```
    rfs::create_dir_all(
        &fakeroot
            .join("lib")
            .join("rustlib")
            .join("src")
            .join("rust")
            .join("library")
            .join("std")
            .join("src"),
    );
    rfs::write(
        &fakeroot
            .join("lib")
            .join("rustlib")
            .join("src")
            .join("rust")
            .join("library")
            .join("std")
            .join("src")
            .join("lib.rs"),
        b"",
    );

    // ... and a second time.
    run_incr_rustc();

    // Basic sanity check that the compiled binary can run.
    run("main");
}