]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/build_sysroot.rs
Auto merge of #96906 - tbu-:pr_stabilize_to_ipv4_mapped, r=dtolnay
[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::rustc_info::{get_file_name, get_rustc_version};
6 use super::utils::{spawn_and_wait, try_hard_link};
7 use super::SysrootKind;
8
9 pub(crate) fn build_sysroot(
10     channel: &str,
11     sysroot_kind: SysrootKind,
12     target_dir: &Path,
13     cg_clif_build_dir: PathBuf,
14     host_triple: &str,
15     target_triple: &str,
16 ) {
17     if target_dir.exists() {
18         fs::remove_dir_all(target_dir).unwrap();
19     }
20     fs::create_dir_all(target_dir.join("bin")).unwrap();
21     fs::create_dir_all(target_dir.join("lib")).unwrap();
22
23     // Copy the backend
24     let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
25     let cg_clif_dylib_path = target_dir
26         .join(if cfg!(windows) {
27             // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
28             // binaries.
29             "bin"
30         } else {
31             "lib"
32         })
33         .join(&cg_clif_dylib);
34     try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path);
35
36     // Build and copy rustc and cargo wrappers
37     for wrapper in ["rustc-clif", "cargo-clif"] {
38         let mut build_cargo_wrapper_cmd = Command::new("rustc");
39         build_cargo_wrapper_cmd
40             .arg(PathBuf::from("scripts").join(format!("{wrapper}.rs")))
41             .arg("-o")
42             .arg(target_dir.join(wrapper))
43             .arg("-g");
44         spawn_and_wait(build_cargo_wrapper_cmd);
45     }
46
47     let default_sysroot = super::rustc_info::get_default_sysroot();
48
49     let rustlib = target_dir.join("lib").join("rustlib");
50     let host_rustlib_lib = rustlib.join(host_triple).join("lib");
51     let target_rustlib_lib = rustlib.join(target_triple).join("lib");
52     fs::create_dir_all(&host_rustlib_lib).unwrap();
53     fs::create_dir_all(&target_rustlib_lib).unwrap();
54
55     if target_triple == "x86_64-pc-windows-gnu" {
56         if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
57             eprintln!(
58                 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
59                 to compile a sysroot for it.",
60             );
61             process::exit(1);
62         }
63         for file in fs::read_dir(
64             default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
65         )
66         .unwrap()
67         {
68             let file = file.unwrap().path();
69             if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
70                 continue; // only copy object files
71             }
72             try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
73         }
74     }
75
76     match sysroot_kind {
77         SysrootKind::None => {} // Nothing to do
78         SysrootKind::Llvm => {
79             for file in fs::read_dir(
80                 default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
81             )
82             .unwrap()
83             {
84                 let file = file.unwrap().path();
85                 let file_name_str = file.file_name().unwrap().to_str().unwrap();
86                 if (file_name_str.contains("rustc_")
87                     && !file_name_str.contains("rustc_std_workspace_")
88                     && !file_name_str.contains("rustc_demangle"))
89                     || file_name_str.contains("chalk")
90                     || file_name_str.contains("tracing")
91                     || file_name_str.contains("regex")
92                 {
93                     // These are large crates that are part of the rustc-dev component and are not
94                     // necessary to run regular programs.
95                     continue;
96                 }
97                 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
98             }
99
100             if target_triple != host_triple {
101                 for file in fs::read_dir(
102                     default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
103                 )
104                 .unwrap()
105                 {
106                     let file = file.unwrap().path();
107                     try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
108                 }
109             }
110         }
111         SysrootKind::Clif => {
112             build_clif_sysroot_for_triple(
113                 channel,
114                 target_dir,
115                 host_triple,
116                 &cg_clif_dylib_path,
117                 None,
118             );
119
120             if host_triple != target_triple {
121                 // When cross-compiling it is often necessary to manually pick the right linker
122                 let linker = if target_triple == "aarch64-unknown-linux-gnu" {
123                     Some("aarch64-linux-gnu-gcc")
124                 } else {
125                     None
126                 };
127                 build_clif_sysroot_for_triple(
128                     channel,
129                     target_dir,
130                     target_triple,
131                     &cg_clif_dylib_path,
132                     linker,
133                 );
134             }
135
136             // Copy std for the host to the lib dir. This is necessary for the jit mode to find
137             // libstd.
138             for file in fs::read_dir(host_rustlib_lib).unwrap() {
139                 let file = file.unwrap().path();
140                 let filename = file.file_name().unwrap().to_str().unwrap();
141                 if filename.contains("std-") && !filename.contains(".rlib") {
142                     try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
143                 }
144             }
145         }
146     }
147 }
148
149 fn build_clif_sysroot_for_triple(
150     channel: &str,
151     target_dir: &Path,
152     triple: &str,
153     cg_clif_dylib_path: &Path,
154     linker: Option<&str>,
155 ) {
156     match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) {
157         Err(e) => {
158             eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
159             eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
160             process::exit(1);
161         }
162         Ok(source_version) => {
163             let rustc_version = get_rustc_version();
164             if source_version != rustc_version {
165                 eprintln!("The patched sysroot source is outdated");
166                 eprintln!("Source version: {}", source_version.trim());
167                 eprintln!("Rustc version:  {}", rustc_version.trim());
168                 eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source");
169                 process::exit(1);
170             }
171         }
172     }
173
174     let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
175
176     if !super::config::get_bool("keep_sysroot") {
177         // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
178         // recompilation as they are not affected by changes in cg_clif.
179         if build_dir.join("deps").exists() {
180             fs::remove_dir_all(build_dir.join("deps")).unwrap();
181         }
182     }
183
184     // Build sysroot
185     let mut build_cmd = Command::new("cargo");
186     build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
187     let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
188     rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
189     if channel == "release" {
190         build_cmd.arg("--release");
191         rustflags.push_str(" -Zmir-opt-level=3");
192     }
193     if let Some(linker) = linker {
194         use std::fmt::Write;
195         write!(rustflags, " -Clinker={}", linker).unwrap();
196     }
197     build_cmd.env("RUSTFLAGS", rustflags);
198     build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
199     spawn_and_wait(build_cmd);
200
201     // Copy all relevant files to the sysroot
202     for entry in
203         fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
204             .unwrap()
205     {
206         let entry = entry.unwrap();
207         if let Some(ext) = entry.path().extension() {
208             if ext == "rmeta" || ext == "d" || ext == "dSYM" {
209                 continue;
210             }
211         } else {
212             continue;
213         };
214         try_hard_link(
215             entry.path(),
216             target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
217         );
218     }
219 }