Skip to content

Commit 4dbd319

Browse files
committed
Move most code from y.rs to build_system/mod.rs
y.rs can't be rustfmt'ed without making it no longer be a valid bash script.
1 parent f328359 commit 4dbd319

File tree

5 files changed

+138
-138
lines changed

5 files changed

+138
-138
lines changed

build_system/build_backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(crate) fn build_backend(
4949
cmd.env("RUSTFLAGS", rustflags);
5050

5151
eprintln!("[BUILD] rustc_codegen_cranelift");
52-
crate::utils::spawn_and_wait(cmd);
52+
super::utils::spawn_and_wait(cmd);
5353

5454
Path::new("target").join(host_triple).join(channel)
5555
}

build_system/build_sysroot.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::fs;
33
use std::path::{Path, PathBuf};
44
use std::process::{self, Command};
55

6-
use crate::rustc_info::{get_file_name, get_rustc_version};
7-
use crate::utils::{spawn_and_wait, try_hard_link};
8-
use crate::SysrootKind;
6+
use super::rustc_info::{get_file_name, get_rustc_version};
7+
use super::utils::{spawn_and_wait, try_hard_link};
8+
use super::SysrootKind;
99

1010
pub(crate) fn build_sysroot(
1111
channel: &str,
@@ -52,7 +52,7 @@ pub(crate) fn build_sysroot(
5252
.arg("-g");
5353
spawn_and_wait(build_cargo_wrapper_cmd);
5454

55-
let default_sysroot = crate::rustc_info::get_default_sysroot();
55+
let default_sysroot = super::rustc_info::get_default_sysroot();
5656

5757
let rustlib = target_dir.join("lib").join("rustlib");
5858
let host_rustlib_lib = rustlib.join(host_triple).join("lib");
@@ -167,7 +167,7 @@ fn build_clif_sysroot_for_triple(
167167

168168
let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
169169

170-
if !crate::config::get_bool("keep_sysroot") {
170+
if !super::config::get_bool("keep_sysroot") {
171171
// Cleanup the target dir with the exception of build scripts and the incremental cache
172172
for dir in ["build", "deps", "examples", "native"] {
173173
if build_dir.join(dir).exists() {

build_system/mod.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
use std::process;
4+
5+
mod build_backend;
6+
mod build_sysroot;
7+
mod config;
8+
mod prepare;
9+
mod rustc_info;
10+
mod utils;
11+
12+
fn usage() {
13+
eprintln!("Usage:");
14+
eprintln!(" ./y.rs prepare");
15+
eprintln!(
16+
" ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]"
17+
);
18+
}
19+
20+
macro_rules! arg_error {
21+
($($err:tt)*) => {{
22+
eprintln!($($err)*);
23+
usage();
24+
std::process::exit(1);
25+
}};
26+
}
27+
28+
enum Command {
29+
Build,
30+
}
31+
32+
#[derive(Copy, Clone)]
33+
pub(crate) enum SysrootKind {
34+
None,
35+
Clif,
36+
Llvm,
37+
}
38+
39+
pub fn main() {
40+
env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
41+
env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
42+
// The target dir is expected in the default location. Guard against the user changing it.
43+
env::set_var("CARGO_TARGET_DIR", "target");
44+
45+
let mut args = env::args().skip(1);
46+
let command = match args.next().as_deref() {
47+
Some("prepare") => {
48+
if args.next().is_some() {
49+
arg_error!("./x.rs prepare doesn't expect arguments");
50+
}
51+
prepare::prepare();
52+
process::exit(0);
53+
}
54+
Some("build") => Command::Build,
55+
Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
56+
Some(command) => arg_error!("Unknown command {}", command),
57+
None => {
58+
usage();
59+
process::exit(0);
60+
}
61+
};
62+
63+
let mut target_dir = PathBuf::from("build");
64+
let mut channel = "release";
65+
let mut sysroot_kind = SysrootKind::Clif;
66+
let mut use_unstable_features = true;
67+
while let Some(arg) = args.next().as_deref() {
68+
match arg {
69+
"--target-dir" => {
70+
target_dir = PathBuf::from(args.next().unwrap_or_else(|| {
71+
arg_error!("--target-dir requires argument");
72+
}))
73+
}
74+
"--debug" => channel = "debug",
75+
"--sysroot" => {
76+
sysroot_kind = match args.next().as_deref() {
77+
Some("none") => SysrootKind::None,
78+
Some("clif") => SysrootKind::Clif,
79+
Some("llvm") => SysrootKind::Llvm,
80+
Some(arg) => arg_error!("Unknown sysroot kind {}", arg),
81+
None => arg_error!("--sysroot requires argument"),
82+
}
83+
}
84+
"--no-unstable-features" => use_unstable_features = false,
85+
flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
86+
arg => arg_error!("Unexpected argument {}", arg),
87+
}
88+
}
89+
90+
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
91+
host_triple
92+
} else if let Some(host_triple) = config::get_value("host") {
93+
host_triple
94+
} else {
95+
rustc_info::get_host_triple()
96+
};
97+
let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") {
98+
if target_triple != "" {
99+
target_triple
100+
} else {
101+
host_triple.clone() // Empty target triple can happen on GHA
102+
}
103+
} else if let Some(target_triple) = config::get_value("target") {
104+
target_triple
105+
} else {
106+
host_triple.clone()
107+
};
108+
109+
if target_triple.ends_with("-msvc") {
110+
eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift.");
111+
eprintln!("Switch to the MinGW toolchain for Windows support.");
112+
eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to");
113+
eprintln!("set the global default target to MinGW");
114+
process::exit(1);
115+
}
116+
117+
let cg_clif_build_dir =
118+
build_backend::build_backend(channel, &host_triple, use_unstable_features);
119+
build_sysroot::build_sysroot(
120+
channel,
121+
sysroot_kind,
122+
&target_dir,
123+
cg_clif_build_dir,
124+
&host_triple,
125+
&target_triple,
126+
);
127+
}

build_system/prepare.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::fs;
55
use std::path::Path;
66
use std::process::Command;
77

8-
use crate::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
9-
use crate::utils::{copy_dir_recursively, spawn_and_wait};
8+
use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
9+
use super::utils::{copy_dir_recursively, spawn_and_wait};
1010

1111
pub(crate) fn prepare() {
1212
prepare_sysroot();

y.rs

Lines changed: 3 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -23,136 +23,9 @@ exec ${0/.rs/.bin} $@
2323
//!
2424
//! The name `y.rs` was chosen to not conflict with rustc's `x.py`.
2525

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-
}
26+
#[path = "build_system/mod.rs"]
27+
mod build_system;
6928

7029
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-
);
30+
build_system::main();
15831
}

0 commit comments

Comments
 (0)