]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/mod.rs
Rollup merge of #106100 - scottmcm:derived-less-than-test, r=compiler-errors
[rust.git] / compiler / rustc_codegen_cranelift / build_system / mod.rs
1 use std::env;
2 use std::path::PathBuf;
3 use std::process;
4
5 use self::utils::is_ci;
6
7 mod abi_cafe;
8 mod build_backend;
9 mod build_sysroot;
10 mod config;
11 mod path;
12 mod prepare;
13 mod rustc_info;
14 mod tests;
15 mod utils;
16
17 const USAGE: &str = r#"The build system of cg_clif.
18
19 USAGE:
20     ./y.rs prepare [--out-dir DIR]
21     ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features]
22     ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features]
23
24 OPTIONS:
25     --sysroot none|clif|llvm
26             Which sysroot libraries to use:
27             `none` will not include any standard library in the sysroot.
28             `clif` will build the standard library using Cranelift.
29             `llvm` will use the pre-compiled standard library of rustc which is compiled with LLVM.
30
31     --out-dir DIR
32             Specify the directory in which the download, build and dist directories are stored.
33             By default this is the working directory.
34
35     --no-unstable-features
36             fSome features are not yet ready for production usage. This option will disable these
37             features. This includes the JIT mode and inline assembly support.
38 "#;
39
40 fn usage() {
41     eprintln!("{USAGE}");
42 }
43
44 macro_rules! arg_error {
45     ($($err:tt)*) => {{
46         eprintln!($($err)*);
47         usage();
48         std::process::exit(1);
49     }};
50 }
51
52 #[derive(PartialEq, Debug)]
53 enum Command {
54     Prepare,
55     Build,
56     Test,
57 }
58
59 #[derive(Copy, Clone, Debug)]
60 pub(crate) enum SysrootKind {
61     None,
62     Clif,
63     Llvm,
64 }
65
66 pub fn main() {
67     env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
68     env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
69
70     if is_ci() {
71         // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway
72         env::set_var("CARGO_BUILD_INCREMENTAL", "false");
73     }
74
75     let mut args = env::args().skip(1);
76     let command = match args.next().as_deref() {
77         Some("prepare") => Command::Prepare,
78         Some("build") => Command::Build,
79         Some("test") => Command::Test,
80         Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
81         Some(command) => arg_error!("Unknown command {}", command),
82         None => {
83             usage();
84             process::exit(0);
85         }
86     };
87
88     let mut out_dir = PathBuf::from(".");
89     let mut channel = "release";
90     let mut sysroot_kind = SysrootKind::Clif;
91     let mut use_unstable_features = true;
92     while let Some(arg) = args.next().as_deref() {
93         match arg {
94             "--out-dir" => {
95                 out_dir = PathBuf::from(args.next().unwrap_or_else(|| {
96                     arg_error!("--out-dir requires argument");
97                 }))
98             }
99             "--debug" => channel = "debug",
100             "--sysroot" => {
101                 sysroot_kind = match args.next().as_deref() {
102                     Some("none") => SysrootKind::None,
103                     Some("clif") => SysrootKind::Clif,
104                     Some("llvm") => SysrootKind::Llvm,
105                     Some(arg) => arg_error!("Unknown sysroot kind {}", arg),
106                     None => arg_error!("--sysroot requires argument"),
107                 }
108             }
109             "--no-unstable-features" => use_unstable_features = false,
110             flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
111             arg => arg_error!("Unexpected argument {}", arg),
112         }
113     }
114
115     let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
116         host_triple
117     } else if let Some(host_triple) = config::get_value("host") {
118         host_triple
119     } else {
120         rustc_info::get_host_triple()
121     };
122     let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
123         if target_triple != "" {
124             target_triple
125         } else {
126             host_triple.clone() // Empty target triple can happen on GHA
127         }
128     } else if let Some(target_triple) = config::get_value("target") {
129         target_triple
130     } else {
131         host_triple.clone()
132     };
133
134     // FIXME allow changing the location of these dirs using cli arguments
135     let current_dir = std::env::current_dir().unwrap();
136     out_dir = current_dir.join(out_dir);
137     let dirs = path::Dirs {
138         source_dir: current_dir.clone(),
139         download_dir: out_dir.join("download"),
140         build_dir: out_dir.join("build"),
141         dist_dir: out_dir.join("dist"),
142     };
143
144     path::RelPath::BUILD.ensure_exists(&dirs);
145
146     {
147         // Make sure we always explicitly specify the target dir
148         let target =
149             path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs);
150         env::set_var("CARGO_TARGET_DIR", &target);
151         let _ = std::fs::remove_file(&target);
152         std::fs::File::create(target).unwrap();
153     }
154
155     if command == Command::Prepare {
156         prepare::prepare(&dirs);
157         process::exit(0);
158     }
159
160     let cg_clif_dylib =
161         build_backend::build_backend(&dirs, channel, &host_triple, use_unstable_features);
162     match command {
163         Command::Prepare => {
164             // Handled above
165         }
166         Command::Test => {
167             tests::run_tests(
168                 &dirs,
169                 channel,
170                 sysroot_kind,
171                 &cg_clif_dylib,
172                 &host_triple,
173                 &target_triple,
174             );
175
176             abi_cafe::run(
177                 channel,
178                 sysroot_kind,
179                 &dirs,
180                 &cg_clif_dylib,
181                 &host_triple,
182                 &target_triple,
183             );
184         }
185         Command::Build => {
186             build_sysroot::build_sysroot(
187                 &dirs,
188                 channel,
189                 sysroot_kind,
190                 &cg_clif_dylib,
191                 &host_triple,
192                 &target_triple,
193             );
194         }
195     }
196 }