]> git.lizzy.rs Git - rust.git/blob - build_system/build_sysroot.rs
Rewrite build.sh in rust
[rust.git] / build_system / build_sysroot.rs
1 use crate::{try_hard_link, SysrootKind};
2 use std::env;
3 use std::fs;
4 use std::path::Path;
5 use std::process::{self, Command};
6
7 pub(crate) fn build_sysroot(
8     channel: &str,
9     sysroot_kind: SysrootKind,
10     target_dir: &Path,
11     cg_clif_dylib: String,
12     host_triple: &str,
13     target_triple: &str,
14 ) {
15     if target_dir.exists() {
16         fs::remove_dir_all(target_dir).unwrap();
17     }
18     fs::create_dir_all(target_dir.join("bin")).unwrap();
19     fs::create_dir_all(target_dir.join("lib")).unwrap();
20
21     // Copy the backend
22     for file in ["cg_clif", "cg_clif_build_sysroot"] {
23         try_hard_link(
24             Path::new("target").join(channel).join(file),
25             target_dir.join("bin").join(file),
26         );
27     }
28
29     try_hard_link(
30         Path::new("target").join(channel).join(&cg_clif_dylib),
31         target_dir.join("lib").join(cg_clif_dylib),
32     );
33
34     // Copy supporting files
35     try_hard_link("rust-toolchain", target_dir.join("rust-toolchain"));
36     try_hard_link("scripts/config.sh", target_dir.join("config.sh"));
37     try_hard_link("scripts/cargo.sh", target_dir.join("cargo.sh"));
38
39     let default_sysroot = crate::rustc_info::get_default_sysroot();
40
41     let rustlib = target_dir.join("lib").join("rustlib");
42     let host_rustlib_lib = rustlib.join(host_triple).join("lib");
43     let target_rustlib_lib = rustlib.join(target_triple).join("lib");
44     fs::create_dir_all(&host_rustlib_lib).unwrap();
45     fs::create_dir_all(&target_rustlib_lib).unwrap();
46
47     if target_triple == "x86_64-pc-windows-gnu" {
48         if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
49             eprintln!(
50                 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
51                 to compile a sysroot for it.",
52             );
53             process::exit(1);
54         }
55         for file in fs::read_dir(
56             default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
57         )
58         .unwrap()
59         {
60             let file = file.unwrap().path();
61             if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
62                 continue; // only copy object files
63             }
64             try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
65         }
66     }
67
68     match sysroot_kind {
69         SysrootKind::None => {} // Nothing to do
70         SysrootKind::Llvm => {
71             for file in fs::read_dir(
72                 default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
73             )
74             .unwrap()
75             {
76                 let file = file.unwrap().path();
77                 let file_name_str = file.file_name().unwrap().to_str().unwrap();
78                 if file_name_str.contains("rustc_")
79                     || file_name_str.contains("chalk")
80                     || file_name_str.contains("tracing")
81                     || file_name_str.contains("regex")
82                 {
83                     // These are large crates that are part of the rustc-dev component and are not
84                     // necessary to run regular programs.
85                     continue;
86                 }
87                 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
88             }
89
90             if target_triple != host_triple {
91                 for file in fs::read_dir(
92                     default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
93                 )
94                 .unwrap()
95                 {
96                     let file = file.unwrap().path();
97                     try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
98                 }
99             }
100         }
101         SysrootKind::Clif => {
102             let cwd = env::current_dir().unwrap();
103
104             let mut cmd = Command::new(cwd.join("build_sysroot").join("build_sysroot.sh"));
105             cmd.current_dir(target_dir).env("TARGET_TRIPLE", target_triple);
106             eprintln!("[BUILD] sysroot");
107             if !cmd.spawn().unwrap().wait().unwrap().success() {
108                 process::exit(1);
109             }
110
111             if host_triple != target_triple {
112                 let mut cmd = Command::new(cwd.join("build_sysroot").join("build_sysroot.sh"));
113                 cmd.current_dir(target_dir).env("TARGET_TRIPLE", host_triple);
114                 eprintln!("[BUILD] sysroot");
115                 if !cmd.spawn().unwrap().wait().unwrap().success() {
116                     process::exit(1);
117                 }
118             }
119
120             for file in fs::read_dir(host_rustlib_lib).unwrap() {
121                 let file = file.unwrap().path();
122                 if file.file_name().unwrap().to_str().unwrap().contains("std-") {
123                     try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
124                 }
125             }
126         }
127     }
128 }