]> git.lizzy.rs Git - rust.git/blob - y.rs
62debc117c354e84ec785ebb52672f6c6c7d40b6
[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 build/y.bin
19 //! $ build/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/prepare.rs"]
35 mod prepare;
36 #[path = "build_system/rustc_info.rs"]
37 mod rustc_info;
38 #[path = "build_system/utils.rs"]
39 mod utils;
40
41 fn usage() {
42     eprintln!("Usage:");
43     eprintln!("  ./y.rs prepare");
44     eprintln!("  ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR]");
45 }
46
47 macro_rules! arg_error {
48     ($($err:tt)*) => {{
49         eprintln!($($err)*);
50         usage();
51         std::process::exit(1);
52     }};
53 }
54
55 enum Command {
56     Build,
57 }
58
59 #[derive(Copy, Clone)]
60 enum SysrootKind {
61     None,
62     Clif,
63     Llvm,
64 }
65
66 fn main() {
67     env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
68     env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
69
70     let mut args = env::args().skip(1);
71     let command = match args.next().as_deref() {
72         Some("prepare") => {
73             if args.next().is_some() {
74                 arg_error!("./x.rs prepare doesn't expect arguments");
75             }
76             prepare::prepare();
77             process::exit(0);
78         }
79         Some("build") => Command::Build,
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 target_dir = PathBuf::from("build");
89     let mut channel = "release";
90     let mut sysroot_kind = SysrootKind::Clif;
91     while let Some(arg) = args.next().as_deref() {
92         match arg {
93             "--target-dir" => {
94                 target_dir = PathBuf::from(args.next().unwrap_or_else(|| {
95                     arg_error!("--target-dir requires argument");
96                 }))
97             }
98             "--debug" => channel = "debug",
99             "--sysroot" => {
100                 sysroot_kind = match args.next().as_deref() {
101                     Some("none") => SysrootKind::None,
102                     Some("clif") => SysrootKind::Clif,
103                     Some("llvm") => SysrootKind::Llvm,
104                     Some(arg) => arg_error!("Unknown sysroot kind {}", arg),
105                     None => arg_error!("--sysroot requires argument"),
106                 }
107             }
108             flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
109             arg => arg_error!("Unexpected argument {}", arg),
110         }
111     }
112
113     let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
114         host_triple
115     } else {
116         rustc_info::get_host_triple()
117     };
118     let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
119         if target_triple != "" {
120             target_triple
121         } else {
122             host_triple.clone() // Empty target triple can happen on GHA
123         }
124     } else {
125         host_triple.clone()
126     };
127
128     let cg_clif_dylib = build_backend::build_backend(channel);
129     build_sysroot::build_sysroot(
130         channel,
131         sysroot_kind,
132         &target_dir,
133         cg_clif_dylib,
134         &host_triple,
135         &target_triple,
136     );
137 }