2 use std::path::PathBuf;
5 use self::utils::is_ci;
17 const USAGE: &str = r#"The build system of cg_clif.
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]
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.
32 Specify the directory in which the download, build and dist directories are stored.
33 By default this is the working directory.
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.
44 macro_rules! arg_error {
48 std::process::exit(1);
52 #[derive(PartialEq, Debug)]
59 #[derive(Copy, Clone, Debug)]
60 pub(crate) enum SysrootKind {
67 env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
68 env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
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");
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),
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() {
95 out_dir = PathBuf::from(args.next().unwrap_or_else(|| {
96 arg_error!("--out-dir requires argument");
99 "--debug" => channel = "debug",
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"),
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),
115 let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
117 } else if let Some(host_triple) = config::get_value("host") {
120 rustc_info::get_host_triple()
122 let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
123 if target_triple != "" {
126 host_triple.clone() // Empty target triple can happen on GHA
128 } else if let Some(target_triple) = config::get_value("target") {
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"),
144 path::RelPath::BUILD.ensure_exists(&dirs);
147 // Make sure we always explicitly specify the target dir
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();
155 if command == Command::Prepare {
156 prepare::prepare(&dirs);
161 build_backend::build_backend(&dirs, channel, &host_triple, use_unstable_features);
163 Command::Prepare => {
186 build_sysroot::build_sysroot(