]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/build_sysroot.rs
Merge commit '7d53619064ab7045c383644cb445052d2a3d46db' into sync_cg_clif-2023-02-09
[rust.git] / compiler / rustc_codegen_cranelift / build_system / build_sysroot.rs
1 use std::fs;
2 use std::path::{Path, PathBuf};
3 use std::process::{self, Command};
4
5 use super::path::{Dirs, RelPath};
6 use super::rustc_info::{get_file_name, get_rustc_version, get_toolchain_name};
7 use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler};
8 use super::SysrootKind;
9
10 static DIST_DIR: RelPath = RelPath::DIST;
11 static BIN_DIR: RelPath = RelPath::DIST.join("bin");
12 static LIB_DIR: RelPath = RelPath::DIST.join("lib");
13
14 pub(crate) fn build_sysroot(
15     dirs: &Dirs,
16     channel: &str,
17     sysroot_kind: SysrootKind,
18     cg_clif_dylib_src: &Path,
19     bootstrap_host_compiler: &Compiler,
20     target_triple: String,
21 ) -> Compiler {
22     eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
23
24     DIST_DIR.ensure_fresh(dirs);
25     BIN_DIR.ensure_exists(dirs);
26     LIB_DIR.ensure_exists(dirs);
27
28     let is_native = bootstrap_host_compiler.triple == target_triple;
29
30     // Copy the backend
31     let cg_clif_dylib_path = if cfg!(windows) {
32         // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
33         // binaries.
34         BIN_DIR
35     } else {
36         LIB_DIR
37     }
38     .to_path(dirs)
39     .join(cg_clif_dylib_src.file_name().unwrap());
40     try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
41
42     // Build and copy rustc and cargo wrappers
43     let wrapper_base_name = get_file_name("____", "bin");
44     let toolchain_name = get_toolchain_name();
45     for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] {
46         let wrapper_name = wrapper_base_name.replace("____", wrapper);
47
48         let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
49         build_cargo_wrapper_cmd
50             .env("TOOLCHAIN_NAME", toolchain_name.clone())
51             .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
52             .arg("-o")
53             .arg(DIST_DIR.to_path(dirs).join(wrapper_name))
54             .arg("-Cstrip=debuginfo");
55         spawn_and_wait(build_cargo_wrapper_cmd);
56     }
57
58     let host = build_sysroot_for_triple(
59         dirs,
60         channel,
61         bootstrap_host_compiler.clone(),
62         &cg_clif_dylib_path,
63         sysroot_kind,
64     );
65     host.install_into_sysroot(&DIST_DIR.to_path(dirs));
66
67     if !is_native {
68         build_sysroot_for_triple(
69             dirs,
70             channel,
71             {
72                 let mut bootstrap_target_compiler = bootstrap_host_compiler.clone();
73                 bootstrap_target_compiler.triple = target_triple.clone();
74                 bootstrap_target_compiler.set_cross_linker_and_runner();
75                 bootstrap_target_compiler
76             },
77             &cg_clif_dylib_path,
78             sysroot_kind,
79         )
80         .install_into_sysroot(&DIST_DIR.to_path(dirs));
81     }
82
83     // Copy std for the host to the lib dir. This is necessary for the jit mode to find
84     // libstd.
85     for lib in host.libs {
86         let filename = lib.file_name().unwrap().to_str().unwrap();
87         if filename.contains("std-") && !filename.contains(".rlib") {
88             try_hard_link(&lib, LIB_DIR.to_path(dirs).join(lib.file_name().unwrap()));
89         }
90     }
91
92     let mut target_compiler = {
93         let dirs: &Dirs = &dirs;
94         let rustc_clif =
95             RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustc-clif"));
96         let rustdoc_clif =
97             RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustdoc-clif"));
98
99         Compiler {
100             cargo: bootstrap_host_compiler.cargo.clone(),
101             rustc: rustc_clif.clone(),
102             rustdoc: rustdoc_clif.clone(),
103             rustflags: String::new(),
104             rustdocflags: String::new(),
105             triple: target_triple,
106             runner: vec![],
107         }
108     };
109     if !is_native {
110         target_compiler.set_cross_linker_and_runner();
111     }
112     target_compiler
113 }
114
115 struct SysrootTarget {
116     triple: String,
117     libs: Vec<PathBuf>,
118 }
119
120 impl SysrootTarget {
121     fn install_into_sysroot(&self, sysroot: &Path) {
122         if self.libs.is_empty() {
123             return;
124         }
125
126         let target_rustlib_lib = sysroot.join("lib").join("rustlib").join(&self.triple).join("lib");
127         fs::create_dir_all(&target_rustlib_lib).unwrap();
128
129         for lib in &self.libs {
130             try_hard_link(lib, target_rustlib_lib.join(lib.file_name().unwrap()));
131         }
132     }
133 }
134
135 pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot");
136 pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot");
137 pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version");
138 pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src");
139 pub(crate) static STANDARD_LIBRARY: CargoProject =
140     CargoProject::new(&BUILD_SYSROOT, "build_sysroot");
141 pub(crate) static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup");
142
143 #[must_use]
144 fn build_sysroot_for_triple(
145     dirs: &Dirs,
146     channel: &str,
147     compiler: Compiler,
148     cg_clif_dylib_path: &Path,
149     sysroot_kind: SysrootKind,
150 ) -> SysrootTarget {
151     match sysroot_kind {
152         SysrootKind::None => build_rtstartup(dirs, &compiler)
153             .unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }),
154         SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler),
155         SysrootKind::Clif => {
156             build_clif_sysroot_for_triple(dirs, channel, compiler, &cg_clif_dylib_path)
157         }
158     }
159 }
160
161 #[must_use]
162 fn build_llvm_sysroot_for_triple(compiler: Compiler) -> SysrootTarget {
163     let default_sysroot = super::rustc_info::get_default_sysroot(&compiler.rustc);
164
165     let mut target_libs = SysrootTarget { triple: compiler.triple, libs: vec![] };
166
167     for entry in fs::read_dir(
168         default_sysroot.join("lib").join("rustlib").join(&target_libs.triple).join("lib"),
169     )
170     .unwrap()
171     {
172         let entry = entry.unwrap();
173         if entry.file_type().unwrap().is_dir() {
174             continue;
175         }
176         let file = entry.path();
177         let file_name_str = file.file_name().unwrap().to_str().unwrap();
178         if (file_name_str.contains("rustc_")
179             && !file_name_str.contains("rustc_std_workspace_")
180             && !file_name_str.contains("rustc_demangle"))
181             || file_name_str.contains("chalk")
182             || file_name_str.contains("tracing")
183             || file_name_str.contains("regex")
184         {
185             // These are large crates that are part of the rustc-dev component and are not
186             // necessary to run regular programs.
187             continue;
188         }
189         target_libs.libs.push(file);
190     }
191
192     target_libs
193 }
194
195 #[must_use]
196 fn build_clif_sysroot_for_triple(
197     dirs: &Dirs,
198     channel: &str,
199     mut compiler: Compiler,
200     cg_clif_dylib_path: &Path,
201 ) -> SysrootTarget {
202     match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
203         Err(e) => {
204             eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
205             eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
206             process::exit(1);
207         }
208         Ok(source_version) => {
209             let rustc_version = get_rustc_version(&compiler.rustc);
210             if source_version != rustc_version {
211                 eprintln!("The patched sysroot source is outdated");
212                 eprintln!("Source version: {}", source_version.trim());
213                 eprintln!("Rustc version:  {}", rustc_version.trim());
214                 eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source");
215                 process::exit(1);
216             }
217         }
218     }
219
220     let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] };
221
222     if let Some(rtstartup_target_libs) = build_rtstartup(dirs, &compiler) {
223         rtstartup_target_libs.install_into_sysroot(&RTSTARTUP_SYSROOT.to_path(dirs));
224
225         target_libs.libs.extend(rtstartup_target_libs.libs);
226     }
227
228     let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel);
229
230     if !super::config::get_bool("keep_sysroot") {
231         // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
232         // recompilation as they are not affected by changes in cg_clif.
233         remove_dir_if_exists(&build_dir.join("deps"));
234     }
235
236     // Build sysroot
237     let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
238     rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
239     // Necessary for MinGW to find rsbegin.o and rsend.o
240     rustflags
241         .push_str(&format!(" --sysroot={}", RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap()));
242     if channel == "release" {
243         rustflags.push_str(" -Zmir-opt-level=3");
244     }
245     compiler.rustflags += &rustflags;
246     let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
247     if channel == "release" {
248         build_cmd.arg("--release");
249     }
250     build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
251     if compiler.triple.contains("apple") {
252         build_cmd.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed");
253     }
254     spawn_and_wait(build_cmd);
255
256     for entry in fs::read_dir(build_dir.join("deps")).unwrap() {
257         let entry = entry.unwrap();
258         if let Some(ext) = entry.path().extension() {
259             if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
260                 continue;
261             }
262         } else {
263             continue;
264         };
265         target_libs.libs.push(entry.path());
266     }
267
268     target_libs
269 }
270
271 fn build_rtstartup(dirs: &Dirs, compiler: &Compiler) -> Option<SysrootTarget> {
272     if !compiler.triple.ends_with("windows-gnu") {
273         return None;
274     }
275
276     RTSTARTUP_SYSROOT.ensure_fresh(dirs);
277
278     let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup");
279     let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] };
280
281     for file in ["rsbegin", "rsend"] {
282         let obj = RTSTARTUP_SYSROOT.to_path(dirs).join(format!("{file}.o"));
283         let mut build_rtstartup_cmd = Command::new(&compiler.rustc);
284         build_rtstartup_cmd
285             .arg("--target")
286             .arg(&compiler.triple)
287             .arg("--emit=obj")
288             .arg("-o")
289             .arg(&obj)
290             .arg(rtstartup_src.join(format!("{file}.rs")));
291         spawn_and_wait(build_rtstartup_cmd);
292         target_libs.libs.push(obj.clone());
293     }
294
295     Some(target_libs)
296 }