]> git.lizzy.rs Git - rust.git/blob - y.rs
Rename cargo executable to cargo-clif
[rust.git] / y.rs
1 #!/usr/bin/env bash
2 #![allow()] /*This line is ignored by bash
3 # This block is ignored by rustc
4 set -e
5 echo "[BUILD] y.rs" 1>&2
6 rustc $0 -o ${0/.rs/.bin} -g
7 exec ${0/.rs/.bin} $@
8 */
9
10 //! The build system for cg_clif
11 //!
12 //! # Manual compilation
13 //!
14 //! If your system doesn't support shell scripts you can manually compile and run this file using
15 //! for example:
16 //!
17 //! ```shell
18 //! $ rustc y.rs -o y.bin
19 //! $ ./y.bin
20 //! ```
21 //!
22 //! # Naming
23 //!
24 //! The name `y.rs` was chosen to not conflict with rustc's `x.py`.
25
26 use std::env;
27 use std::path::PathBuf;
28 use std::process;
29
30 #[path = "build_system/build_backend.rs"]
31 mod build_backend;
32 #[path = "build_system/build_sysroot.rs"]
33 mod build_sysroot;
34 #[path = "build_system/config.rs"]
35 mod config;
36 #[path = "build_system/prepare.rs"]
37 mod prepare;
38 #[path = "build_system/rustc_info.rs"]
39 mod rustc_info;
40 #[path = "build_system/utils.rs"]
41 mod utils;
42
43 fn usage() {
44     eprintln!("Usage:");
45     eprintln!("  ./y.rs prepare");
46     eprintln!(
47         "  ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]"
48     );
49 }
50
51 macro_rules! arg_error {
52     ($($err:tt)*) => {{
53         eprintln!($($err)*);
54         usage();
55         std::process::exit(1);
56     }};
57 }
58
59 enum Command {
60     Build,
61 }
62
63 #[derive(Copy, Clone)]
64 enum SysrootKind {
65     None,
66     Clif,
67     Llvm,
68 }
69
70 fn main() {
71     env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
72     env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
73     // The target dir is expected in the default location. Guard against the user changing it.
74     env::set_var("CARGO_TARGET_DIR", "target");
75
76     let mut args = env::args().skip(1);
77     let command = match args.next().as_deref() {
78         Some("prepare") => {
79             if args.next().is_some() {
80                 arg_error!("./x.rs prepare doesn't expect arguments");
81             }
82             prepare::prepare();
83             process::exit(0);
84         }
85         Some("build") => Command::Build,
86         Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
87         Some(command) => arg_error!("Unknown command {}", command),
88         None => {
89             usage();
90             process::exit(0);
91         }
92     };
93
94     let mut target_dir = PathBuf::from("build");
95     let mut channel = "release";
96     let mut sysroot_kind = SysrootKind::Clif;
97     let mut use_unstable_features = true;
98     while let Some(arg) = args.next().as_deref() {
99         match arg {
100             "--target-dir" => {
101                 target_dir = PathBuf::from(args.next().unwrap_or_else(|| {
102                     arg_error!("--target-dir requires argument");
103                 }))
104             }
105             "--debug" => channel = "debug",
106             "--sysroot" => {
107                 sysroot_kind = match args.next().as_deref() {
108                     Some("none") => SysrootKind::None,
109                     Some("clif") => SysrootKind::Clif,
110                     Some("llvm") => SysrootKind::Llvm,
111                     Some(arg) => arg_error!("Unknown sysroot kind {}", arg),
112                     None => arg_error!("--sysroot requires argument"),
113                 }
114             }
115             "--no-unstable-features" => use_unstable_features = false,
116             flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
117             arg => arg_error!("Unexpected argument {}", arg),
118         }
119     }
120
121     let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
122         host_triple
123     } else if let Some(host_triple) = crate::config::get_value("host") {
124         host_triple
125     } else {
126         rustc_info::get_host_triple()
127     };
128     let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
129         if target_triple != "" {
130             target_triple
131         } else {
132             host_triple.clone() // Empty target triple can happen on GHA
133         }
134     } else if let Some(target_triple) = crate::config::get_value("target") {
135         target_triple
136     } else {
137         host_triple.clone()
138     };
139
140     if target_triple.ends_with("-msvc") {
141         eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift.");
142         eprintln!("Switch to the MinGW toolchain for Windows support.");
143         eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to");
144         eprintln!("set the global default target to MinGW");
145         process::exit(1);
146     }
147
148     let cg_clif_build_dir =
149         build_backend::build_backend(channel, &host_triple, use_unstable_features);
150     build_sysroot::build_sysroot(
151         channel,
152         sysroot_kind,
153         &target_dir,
154         cg_clif_build_dir,
155         &host_triple,
156         &target_triple,
157     );
158 }