]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_cranelift/build_system/mod.rs
New upstream version 1.69.0+dfsg1
[rustc.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, is_ci_opt, Compiler};
6
7 mod abi_cafe;
8 mod bench;
9 mod build_backend;
10 mod build_sysroot;
11 mod config;
12 mod path;
13 mod prepare;
14 mod rustc_info;
15 mod tests;
16 mod utils;
17
18 fn usage() {
19 eprintln!("{}", include_str!("usage.txt"));
20 }
21
22 macro_rules! arg_error {
23 ($($err:tt)*) => {{
24 eprintln!($($err)*);
25 usage();
26 std::process::exit(1);
27 }};
28 }
29
30 #[derive(PartialEq, Debug)]
31 enum Command {
32 Prepare,
33 Build,
34 Test,
35 AbiCafe,
36 Bench,
37 }
38
39 #[derive(Copy, Clone, Debug)]
40 pub(crate) enum SysrootKind {
41 None,
42 Clif,
43 Llvm,
44 }
45
46 pub fn main() {
47 if env::var("RUST_BACKTRACE").is_err() {
48 env::set_var("RUST_BACKTRACE", "1");
49 }
50 env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
51
52 if is_ci() {
53 // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway
54 env::set_var("CARGO_BUILD_INCREMENTAL", "false");
55
56 if !is_ci_opt() {
57 // Enable the Cranelift verifier
58 env::set_var("CG_CLIF_ENABLE_VERIFIER", "1");
59 }
60 }
61
62 let mut args = env::args().skip(1);
63 let command = match args.next().as_deref() {
64 Some("prepare") => Command::Prepare,
65 Some("build") => Command::Build,
66 Some("test") => Command::Test,
67 Some("abi-cafe") => Command::AbiCafe,
68 Some("bench") => Command::Bench,
69 Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
70 Some(command) => arg_error!("Unknown command {}", command),
71 None => {
72 usage();
73 process::exit(0);
74 }
75 };
76
77 let mut out_dir = PathBuf::from(".");
78 let mut channel = "release";
79 let mut sysroot_kind = SysrootKind::Clif;
80 let mut use_unstable_features = true;
81 while let Some(arg) = args.next().as_deref() {
82 match arg {
83 "--out-dir" => {
84 out_dir = PathBuf::from(args.next().unwrap_or_else(|| {
85 arg_error!("--out-dir requires argument");
86 }))
87 }
88 "--debug" => channel = "debug",
89 "--sysroot" => {
90 sysroot_kind = match args.next().as_deref() {
91 Some("none") => SysrootKind::None,
92 Some("clif") => SysrootKind::Clif,
93 Some("llvm") => SysrootKind::Llvm,
94 Some(arg) => arg_error!("Unknown sysroot kind {}", arg),
95 None => arg_error!("--sysroot requires argument"),
96 }
97 }
98 "--no-unstable-features" => use_unstable_features = false,
99 flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
100 arg => arg_error!("Unexpected argument {}", arg),
101 }
102 }
103
104 let bootstrap_host_compiler = Compiler::bootstrap_with_triple(
105 std::env::var("HOST_TRIPLE")
106 .ok()
107 .or_else(|| config::get_value("host"))
108 .unwrap_or_else(|| rustc_info::get_host_triple()),
109 );
110 let target_triple = std::env::var("TARGET_TRIPLE")
111 .ok()
112 .or_else(|| config::get_value("target"))
113 .unwrap_or_else(|| bootstrap_host_compiler.triple.clone());
114
115 // FIXME allow changing the location of these dirs using cli arguments
116 let current_dir = std::env::current_dir().unwrap();
117 out_dir = current_dir.join(out_dir);
118 let dirs = path::Dirs {
119 source_dir: current_dir.clone(),
120 download_dir: out_dir.join("download"),
121 build_dir: out_dir.join("build"),
122 dist_dir: out_dir.join("dist"),
123 };
124
125 path::RelPath::BUILD.ensure_exists(&dirs);
126
127 {
128 // Make sure we always explicitly specify the target dir
129 let target =
130 path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs);
131 env::set_var("CARGO_TARGET_DIR", &target);
132 let _ = std::fs::remove_file(&target);
133 std::fs::File::create(target).unwrap();
134 }
135
136 if command == Command::Prepare {
137 prepare::prepare(&dirs);
138 process::exit(0);
139 }
140
141 env::set_var("RUSTC", "rustc_should_be_set_explicitly");
142 env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly");
143
144 let cg_clif_dylib = build_backend::build_backend(
145 &dirs,
146 channel,
147 &bootstrap_host_compiler,
148 use_unstable_features,
149 );
150 match command {
151 Command::Prepare => {
152 // Handled above
153 }
154 Command::Test => {
155 tests::run_tests(
156 &dirs,
157 channel,
158 sysroot_kind,
159 &cg_clif_dylib,
160 &bootstrap_host_compiler,
161 target_triple.clone(),
162 );
163 }
164 Command::AbiCafe => {
165 if bootstrap_host_compiler.triple != target_triple {
166 eprintln!("Abi-cafe doesn't support cross-compilation");
167 process::exit(1);
168 }
169 abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &bootstrap_host_compiler);
170 }
171 Command::Build => {
172 build_sysroot::build_sysroot(
173 &dirs,
174 channel,
175 sysroot_kind,
176 &cg_clif_dylib,
177 &bootstrap_host_compiler,
178 target_triple,
179 );
180 }
181 Command::Bench => {
182 build_sysroot::build_sysroot(
183 &dirs,
184 channel,
185 sysroot_kind,
186 &cg_clif_dylib,
187 &bootstrap_host_compiler,
188 target_triple,
189 );
190 bench::benchmark(&dirs, &bootstrap_host_compiler);
191 }
192 }
193 }