]> git.lizzy.rs Git - rust.git/blob - build_system/build_sysroot.rs
Allow preserving the old sysroot
[rust.git] / build_system / build_sysroot.rs
1 use crate::utils::spawn_and_wait;
2 use crate::utils::try_hard_link;
3 use crate::SysrootKind;
4 use std::fs;
5 use std::path::Path;
6 use std::process::{self, Command};
7
8 pub(crate) fn build_sysroot(
9     channel: &str,
10     sysroot_kind: SysrootKind,
11     target_dir: &Path,
12     cg_clif_dylib: String,
13     host_triple: &str,
14     target_triple: &str,
15 ) {
16     if target_dir.exists() {
17         fs::remove_dir_all(target_dir).unwrap();
18     }
19     fs::create_dir_all(target_dir.join("bin")).unwrap();
20     fs::create_dir_all(target_dir.join("lib")).unwrap();
21
22     // Copy the backend
23     for file in ["cg_clif", "cg_clif_build_sysroot"] {
24         try_hard_link(
25             Path::new("target").join(channel).join(file),
26             target_dir.join("bin").join(file),
27         );
28     }
29
30     try_hard_link(
31         Path::new("target").join(channel).join(&cg_clif_dylib),
32         target_dir.join("lib").join(cg_clif_dylib),
33     );
34
35     // Copy supporting files
36     try_hard_link("rust-toolchain", target_dir.join("rust-toolchain"));
37     try_hard_link("scripts/config.sh", target_dir.join("config.sh"));
38     try_hard_link("scripts/cargo.sh", target_dir.join("cargo.sh"));
39
40     let default_sysroot = crate::rustc_info::get_default_sysroot();
41
42     let rustlib = target_dir.join("lib").join("rustlib");
43     let host_rustlib_lib = rustlib.join(host_triple).join("lib");
44     let target_rustlib_lib = rustlib.join(target_triple).join("lib");
45     fs::create_dir_all(&host_rustlib_lib).unwrap();
46     fs::create_dir_all(&target_rustlib_lib).unwrap();
47
48     if target_triple == "x86_64-pc-windows-gnu" {
49         if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
50             eprintln!(
51                 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
52                 to compile a sysroot for it.",
53             );
54             process::exit(1);
55         }
56         for file in fs::read_dir(
57             default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
58         )
59         .unwrap()
60         {
61             let file = file.unwrap().path();
62             if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
63                 continue; // only copy object files
64             }
65             try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
66         }
67     }
68
69     match sysroot_kind {
70         SysrootKind::None => {} // Nothing to do
71         SysrootKind::Llvm => {
72             for file in fs::read_dir(
73                 default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
74             )
75             .unwrap()
76             {
77                 let file = file.unwrap().path();
78                 let file_name_str = file.file_name().unwrap().to_str().unwrap();
79                 if file_name_str.contains("rustc_")
80                     || file_name_str.contains("chalk")
81                     || file_name_str.contains("tracing")
82                     || file_name_str.contains("regex")
83                 {
84                     // These are large crates that are part of the rustc-dev component and are not
85                     // necessary to run regular programs.
86                     continue;
87                 }
88                 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
89             }
90
91             if target_triple != host_triple {
92                 for file in fs::read_dir(
93                     default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
94                 )
95                 .unwrap()
96                 {
97                     let file = file.unwrap().path();
98                     try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
99                 }
100             }
101         }
102         SysrootKind::Clif => {
103             build_clif_sysroot_for_triple(channel, target_dir, target_triple);
104
105             if host_triple != target_triple {
106                 build_clif_sysroot_for_triple(channel, target_dir, host_triple);
107             }
108
109             // Copy std for the host to the lib dir. This is necessary for the jit mode to find
110             // libstd.
111             for file in fs::read_dir(host_rustlib_lib).unwrap() {
112                 let file = file.unwrap().path();
113                 if file.file_name().unwrap().to_str().unwrap().contains("std-") {
114                     try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
115                 }
116             }
117         }
118     }
119 }
120
121 fn build_clif_sysroot_for_triple(channel: &str, target_dir: &Path, triple: &str) {
122     let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
123
124     let keep_sysroot =
125         fs::read_to_string("config.txt").unwrap().lines().any(|line| line.trim() == "keep_sysroot");
126     if !keep_sysroot {
127         // Cleanup the target dir with the exception of build scripts and the incremental cache
128         for dir in ["build", "deps", "examples", "native"] {
129             if build_dir.join(dir).exists() {
130                 fs::remove_dir_all(build_dir.join(dir)).unwrap();
131             }
132         }
133     }
134
135     // Build sysroot
136     let mut build_cmd = Command::new("cargo");
137     build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
138     let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
139     if channel == "release" {
140         build_cmd.arg("--release");
141         rustflags.push_str(" -Zmir-opt-level=3");
142     }
143     build_cmd.env("RUSTFLAGS", rustflags);
144     build_cmd
145         .env("RUSTC", target_dir.join("bin").join("cg_clif_build_sysroot").canonicalize().unwrap());
146     // FIXME Enable incremental again once rust-lang/rust#74946 is fixed
147     build_cmd.env("CARGO_INCREMENTAL", "0").env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
148     spawn_and_wait(build_cmd);
149
150     // Copy all relevant files to the sysroot
151     for entry in
152         fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
153             .unwrap()
154     {
155         let entry = entry.unwrap();
156         if let Some(ext) = entry.path().extension() {
157             if ext == "rmeta" || ext == "d" || ext == "dSYM" {
158                 continue;
159             }
160         } else {
161             continue;
162         };
163         try_hard_link(
164             entry.path(),
165             target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
166         );
167     }
168 }