]> git.lizzy.rs Git - rust.git/blob - build_system/build_sysroot.rs
Fix compilation for AArch64
[rust.git] / build_system / build_sysroot.rs
1 use std::env;
2 use std::fs;
3 use std::path::Path;
4 use std::process::{self, Command};
5
6 use crate::rustc_info::get_file_name;
7 use crate::utils::{spawn_and_wait, try_hard_link};
8 use crate::SysrootKind;
9
10 pub(crate) fn build_sysroot(
11     channel: &str,
12     sysroot_kind: SysrootKind,
13     target_dir: &Path,
14     cg_clif_dylib: String,
15     host_triple: &str,
16     target_triple: &str,
17 ) {
18     if target_dir.exists() {
19         fs::remove_dir_all(target_dir).unwrap();
20     }
21     fs::create_dir_all(target_dir.join("bin")).unwrap();
22     fs::create_dir_all(target_dir.join("lib")).unwrap();
23
24     // Copy the backend
25     for file in ["cg_clif", "cg_clif_build_sysroot"] {
26         try_hard_link(
27             Path::new("target").join(channel).join(get_file_name(file, "bin")),
28             target_dir.join("bin").join(get_file_name(file, "bin")),
29         );
30     }
31
32     if cfg!(windows) {
33         // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
34         // binaries.
35         try_hard_link(
36             Path::new("target").join(channel).join(&cg_clif_dylib),
37             target_dir.join("bin").join(cg_clif_dylib),
38         );
39     } else {
40         try_hard_link(
41             Path::new("target").join(channel).join(&cg_clif_dylib),
42             target_dir.join("lib").join(cg_clif_dylib),
43         );
44     }
45
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")
50         .arg("-o")
51         .arg(target_dir.join("cargo"))
52         .arg("-g");
53     spawn_and_wait(build_cargo_wrapper_cmd);
54
55     let default_sysroot = crate::rustc_info::get_default_sysroot();
56
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();
62
63     if target_triple == "x86_64-pc-windows-gnu" {
64         if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
65             eprintln!(
66                 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
67                 to compile a sysroot for it.",
68             );
69             process::exit(1);
70         }
71         for file in fs::read_dir(
72             default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
73         )
74         .unwrap()
75         {
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
79             }
80             try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
81         }
82     }
83
84     match sysroot_kind {
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"),
89             )
90             .unwrap()
91             {
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")
98                 {
99                     // These are large crates that are part of the rustc-dev component and are not
100                     // necessary to run regular programs.
101                     continue;
102                 }
103                 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
104             }
105
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"),
109                 )
110                 .unwrap()
111                 {
112                     let file = file.unwrap().path();
113                     try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
114                 }
115             }
116         }
117         SysrootKind::Clif => {
118             build_clif_sysroot_for_triple(channel, target_dir, host_triple, None);
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(channel, target_dir, target_triple, linker);
128             }
129
130             // Copy std for the host to the lib dir. This is necessary for the jit mode to find
131             // libstd.
132             for file in fs::read_dir(host_rustlib_lib).unwrap() {
133                 let file = file.unwrap().path();
134                 if file.file_name().unwrap().to_str().unwrap().contains("std-") {
135                     try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
136                 }
137             }
138         }
139     }
140 }
141
142 fn build_clif_sysroot_for_triple(
143     channel: &str,
144     target_dir: &Path,
145     triple: &str,
146     linker: Option<&str>,
147 ) {
148     let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
149
150     let keep_sysroot =
151         fs::read_to_string("config.txt").unwrap().lines().any(|line| line.trim() == "keep_sysroot");
152     if !keep_sysroot {
153         // Cleanup the target dir with the exception of build scripts and the incremental cache
154         for dir in ["build", "deps", "examples", "native"] {
155             if build_dir.join(dir).exists() {
156                 fs::remove_dir_all(build_dir.join(dir)).unwrap();
157             }
158         }
159     }
160
161     // Build sysroot
162     let mut build_cmd = Command::new("cargo");
163     build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
164     let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
165     if channel == "release" {
166         build_cmd.arg("--release");
167         rustflags.push_str(" -Zmir-opt-level=3");
168     }
169     if let Some(linker) = linker {
170         use std::fmt::Write;
171         write!(rustflags, " -Clinker={}", linker).unwrap();
172     }
173     build_cmd.env("RUSTFLAGS", rustflags);
174     build_cmd.env(
175         "RUSTC",
176         env::current_dir().unwrap().join(target_dir).join("bin").join("cg_clif_build_sysroot"),
177     );
178     // FIXME Enable incremental again once rust-lang/rust#74946 is fixed
179     build_cmd.env("CARGO_INCREMENTAL", "0").env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
180     spawn_and_wait(build_cmd);
181
182     // Copy all relevant files to the sysroot
183     for entry in
184         fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
185             .unwrap()
186     {
187         let entry = entry.unwrap();
188         if let Some(ext) = entry.path().extension() {
189             if ext == "rmeta" || ext == "d" || ext == "dSYM" {
190                 continue;
191             }
192         } else {
193             continue;
194         };
195         try_hard_link(
196             entry.path(),
197             target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
198         );
199     }
200 }