1 use crate::utils::spawn_and_wait;
2 use crate::utils::try_hard_link;
3 use crate::SysrootKind;
6 use std::process::{self, Command};
8 pub(crate) fn build_sysroot(
10 sysroot_kind: SysrootKind,
12 cg_clif_dylib: String,
16 if target_dir.exists() {
17 fs::remove_dir_all(target_dir).unwrap();
19 fs::create_dir_all(target_dir.join("bin")).unwrap();
20 fs::create_dir_all(target_dir.join("lib")).unwrap();
23 for file in ["cg_clif", "cg_clif_build_sysroot"] {
25 Path::new("target").join(channel).join(file),
26 target_dir.join("bin").join(file),
31 Path::new("target").join(channel).join(&cg_clif_dylib),
32 target_dir.join("lib").join(cg_clif_dylib),
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"));
40 let default_sysroot = crate::rustc_info::get_default_sysroot();
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();
48 if target_triple == "x86_64-pc-windows-gnu" {
49 if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
51 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
52 to compile a sysroot for it.",
56 for file in fs::read_dir(
57 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
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
65 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
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"),
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")
84 // These are large crates that are part of the rustc-dev component and are not
85 // necessary to run regular programs.
88 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
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"),
97 let file = file.unwrap().path();
98 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
102 SysrootKind::Clif => {
103 build_clif_sysroot_for_triple(channel, target_dir, target_triple);
105 if host_triple != target_triple {
106 build_clif_sysroot_for_triple(channel, target_dir, host_triple);
109 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
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()));
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);
124 // FIXME add option to skip this
125 // Cleanup the target dir with the exception of build scripts and the incremental cache
126 for dir in ["build", "deps", "examples", "native"] {
127 if build_dir.join(dir).exists() {
128 fs::remove_dir_all(build_dir.join(dir)).unwrap();
133 let mut build_cmd = Command::new("cargo");
134 build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
135 let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
136 if channel == "release" {
137 build_cmd.arg("--release");
138 rustflags.push_str(" -Zmir-opt-level=3");
140 build_cmd.env("RUSTFLAGS", rustflags);
142 .env("RUSTC", target_dir.join("bin").join("cg_clif_build_sysroot").canonicalize().unwrap());
143 // FIXME Enable incremental again once rust-lang/rust#74946 is fixed
144 build_cmd.env("CARGO_INCREMENTAL", "0").env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
145 spawn_and_wait(build_cmd);
147 // Copy all relevant files to the sysroot
149 fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
152 let entry = entry.unwrap();
153 if let Some(ext) = entry.path().extension() {
154 if ext == "rmeta" || ext == "d" || ext == "dSYM" {
162 target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),