4 use std::process::{self, Command};
6 use crate::rustc_info::get_file_name;
7 use crate::utils::{spawn_and_wait, try_hard_link};
8 use crate::SysrootKind;
10 pub(crate) fn build_sysroot(
12 sysroot_kind: SysrootKind,
14 cg_clif_dylib: String,
18 if target_dir.exists() {
19 fs::remove_dir_all(target_dir).unwrap();
21 fs::create_dir_all(target_dir.join("bin")).unwrap();
22 fs::create_dir_all(target_dir.join("lib")).unwrap();
25 for file in ["cg_clif", "cg_clif_build_sysroot"] {
27 Path::new("target").join(channel).join(get_file_name(file, "bin")),
28 target_dir.join("bin").join(get_file_name(file, "bin")),
33 // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
36 Path::new("target").join(channel).join(&cg_clif_dylib),
37 target_dir.join("bin").join(cg_clif_dylib),
41 Path::new("target").join(channel).join(&cg_clif_dylib),
42 target_dir.join("lib").join(cg_clif_dylib),
46 // Build and copy cargo wrapper
47 let mut build_cargo_wrapper_cmd = Command::new("rustc");
48 build_cargo_wrapper_cmd
49 .arg("scripts/cargo.rs")
51 .arg(target_dir.join("cargo"))
53 spawn_and_wait(build_cargo_wrapper_cmd);
55 let default_sysroot = crate::rustc_info::get_default_sysroot();
57 let rustlib = target_dir.join("lib").join("rustlib");
58 let host_rustlib_lib = rustlib.join(host_triple).join("lib");
59 let target_rustlib_lib = rustlib.join(target_triple).join("lib");
60 fs::create_dir_all(&host_rustlib_lib).unwrap();
61 fs::create_dir_all(&target_rustlib_lib).unwrap();
63 if target_triple == "x86_64-pc-windows-gnu" {
64 if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
66 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
67 to compile a sysroot for it.",
71 for file in fs::read_dir(
72 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
76 let file = file.unwrap().path();
77 if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
78 continue; // only copy object files
80 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
85 SysrootKind::None => {} // Nothing to do
86 SysrootKind::Llvm => {
87 for file in fs::read_dir(
88 default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
92 let file = file.unwrap().path();
93 let file_name_str = file.file_name().unwrap().to_str().unwrap();
94 if file_name_str.contains("rustc_")
95 || file_name_str.contains("chalk")
96 || file_name_str.contains("tracing")
97 || file_name_str.contains("regex")
99 // These are large crates that are part of the rustc-dev component and are not
100 // necessary to run regular programs.
103 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
106 if target_triple != host_triple {
107 for file in fs::read_dir(
108 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
112 let file = file.unwrap().path();
113 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
117 SysrootKind::Clif => {
118 build_clif_sysroot_for_triple(channel, target_dir, target_triple);
120 if host_triple != target_triple {
121 build_clif_sysroot_for_triple(channel, target_dir, host_triple);
124 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
126 for file in fs::read_dir(host_rustlib_lib).unwrap() {
127 let file = file.unwrap().path();
128 if file.file_name().unwrap().to_str().unwrap().contains("std-") {
129 try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
136 fn build_clif_sysroot_for_triple(channel: &str, target_dir: &Path, triple: &str) {
137 let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
140 fs::read_to_string("config.txt").unwrap().lines().any(|line| line.trim() == "keep_sysroot");
142 // Cleanup the target dir with the exception of build scripts and the incremental cache
143 for dir in ["build", "deps", "examples", "native"] {
144 if build_dir.join(dir).exists() {
145 fs::remove_dir_all(build_dir.join(dir)).unwrap();
151 let mut build_cmd = Command::new("cargo");
152 build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
153 let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
154 if channel == "release" {
155 build_cmd.arg("--release");
156 rustflags.push_str(" -Zmir-opt-level=3");
158 build_cmd.env("RUSTFLAGS", rustflags);
161 env::current_dir().unwrap().join(target_dir).join("bin").join("cg_clif_build_sysroot"),
163 // FIXME Enable incremental again once rust-lang/rust#74946 is fixed
164 build_cmd.env("CARGO_INCREMENTAL", "0").env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
165 spawn_and_wait(build_cmd);
167 // Copy all relevant files to the sysroot
169 fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
172 let entry = entry.unwrap();
173 if let Some(ext) = entry.path().extension() {
174 if ext == "rmeta" || ext == "d" || ext == "dSYM" {
182 target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),