Skip to content

Commit a2f720d

Browse files
committed
Allow building and testing without rustup
This can be done by installing the nightly specified in rust-toolchain.toml and then pointing the CARGO, RUSTC and RUSTDOC env vars to the right executables.
1 parent 22befab commit a2f720d

File tree

9 files changed

+78
-18
lines changed

9 files changed

+78
-18
lines changed

build_system/abi_cafe.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub(crate) fn run(
1616
sysroot_kind: SysrootKind,
1717
dirs: &Dirs,
1818
cg_clif_dylib: &Path,
19+
rustup_toolchain_name: Option<&str>,
1920
bootstrap_host_compiler: &Compiler,
2021
) {
2122
ABI_CAFE_REPO.fetch(dirs);
@@ -27,6 +28,7 @@ pub(crate) fn run(
2728
sysroot_kind,
2829
cg_clif_dylib,
2930
bootstrap_host_compiler,
31+
rustup_toolchain_name,
3032
bootstrap_host_compiler.triple.clone(),
3133
);
3234

build_system/build_sysroot.rs

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

55
use super::path::{Dirs, RelPath};
6-
use super::rustc_info::{get_file_name, get_rustc_version, get_toolchain_name};
6+
use super::rustc_info::{get_file_name, get_rustc_version};
77
use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler};
88
use super::SysrootKind;
99

@@ -17,6 +17,7 @@ pub(crate) fn build_sysroot(
1717
sysroot_kind: SysrootKind,
1818
cg_clif_dylib_src: &Path,
1919
bootstrap_host_compiler: &Compiler,
20+
rustup_toolchain_name: Option<&str>,
2021
target_triple: String,
2122
) -> Compiler {
2223
eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
@@ -41,18 +42,29 @@ pub(crate) fn build_sysroot(
4142

4243
// Build and copy rustc and cargo wrappers
4344
let wrapper_base_name = get_file_name(&bootstrap_host_compiler.rustc, "____", "bin");
44-
let toolchain_name = get_toolchain_name();
4545
for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] {
4646
let wrapper_name = wrapper_base_name.replace("____", wrapper);
4747

4848
let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
4949
let wrapper_path = DIST_DIR.to_path(dirs).join(&wrapper_name);
5050
build_cargo_wrapper_cmd
51-
.env("TOOLCHAIN_NAME", toolchain_name.clone())
5251
.arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
5352
.arg("-o")
5453
.arg(&wrapper_path)
5554
.arg("-Cstrip=debuginfo");
55+
if let Some(rustup_toolchain_name) = &rustup_toolchain_name {
56+
build_cargo_wrapper_cmd
57+
.env("TOOLCHAIN_NAME", rustup_toolchain_name)
58+
.env_remove("CARGO")
59+
.env_remove("RUSTC")
60+
.env_remove("RUSTDOC");
61+
} else {
62+
build_cargo_wrapper_cmd
63+
.env_remove("TOOLCHAIN_NAME")
64+
.env("CARGO", &bootstrap_host_compiler.cargo)
65+
.env("RUSTC", &bootstrap_host_compiler.rustc)
66+
.env("RUSTDOC", &bootstrap_host_compiler.rustdoc);
67+
}
5668
spawn_and_wait(build_cargo_wrapper_cmd);
5769
try_hard_link(wrapper_path, BIN_DIR.to_path(dirs).join(wrapper_name));
5870
}

build_system/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ pub(crate) fn main() {
103103
}
104104
}
105105

106+
let rustup_toolchain_name = match (env::var("CARGO"), env::var("RUSTC"), env::var("RUSTDOC")) {
107+
(Ok(_), Ok(_), Ok(_)) => None,
108+
(Err(_), Err(_), Err(_)) => Some(rustc_info::get_toolchain_name()),
109+
_ => {
110+
eprintln!("All of CARGO, RUSTC and RUSTDOC need to be set or none must be set");
111+
process::exit(1);
112+
}
113+
};
106114
let bootstrap_host_compiler = {
107115
let cargo = rustc_info::get_cargo_path();
108116
let rustc = rustc_info::get_rustc_path();
@@ -173,6 +181,7 @@ pub(crate) fn main() {
173181
sysroot_kind,
174182
&cg_clif_dylib,
175183
&bootstrap_host_compiler,
184+
rustup_toolchain_name.as_deref(),
176185
target_triple.clone(),
177186
);
178187
}
@@ -181,7 +190,14 @@ pub(crate) fn main() {
181190
eprintln!("Abi-cafe doesn't support cross-compilation");
182191
process::exit(1);
183192
}
184-
abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &bootstrap_host_compiler);
193+
abi_cafe::run(
194+
channel,
195+
sysroot_kind,
196+
&dirs,
197+
&cg_clif_dylib,
198+
rustup_toolchain_name.as_deref(),
199+
&bootstrap_host_compiler,
200+
);
185201
}
186202
Command::Build => {
187203
build_sysroot::build_sysroot(
@@ -190,6 +206,7 @@ pub(crate) fn main() {
190206
sysroot_kind,
191207
&cg_clif_dylib,
192208
&bootstrap_host_compiler,
209+
rustup_toolchain_name.as_deref(),
193210
target_triple,
194211
);
195212
}
@@ -200,6 +217,7 @@ pub(crate) fn main() {
200217
sysroot_kind,
201218
&cg_clif_dylib,
202219
&bootstrap_host_compiler,
220+
rustup_toolchain_name.as_deref(),
203221
target_triple,
204222
);
205223
bench::benchmark(&dirs, &bootstrap_host_compiler);

build_system/rustc_info.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub(crate) fn get_toolchain_name() -> String {
3434
}
3535

3636
pub(crate) fn get_cargo_path() -> PathBuf {
37+
if let Ok(cargo) = std::env::var("CARGO") {
38+
return PathBuf::from(cargo);
39+
}
3740
let cargo_path = Command::new("rustup")
3841
.stderr(Stdio::inherit())
3942
.args(&["which", "cargo"])
@@ -44,6 +47,9 @@ pub(crate) fn get_cargo_path() -> PathBuf {
4447
}
4548

4649
pub(crate) fn get_rustc_path() -> PathBuf {
50+
if let Ok(rustc) = std::env::var("RUSTC") {
51+
return PathBuf::from(rustc);
52+
}
4753
let rustc_path = Command::new("rustup")
4854
.stderr(Stdio::inherit())
4955
.args(&["which", "rustc"])
@@ -54,6 +60,9 @@ pub(crate) fn get_rustc_path() -> PathBuf {
5460
}
5561

5662
pub(crate) fn get_rustdoc_path() -> PathBuf {
63+
if let Ok(rustdoc) = std::env::var("RUSTDOC") {
64+
return PathBuf::from(rustdoc);
65+
}
5766
let rustc_path = Command::new("rustup")
5867
.stderr(Stdio::inherit())
5968
.args(&["which", "rustdoc"])

build_system/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ pub(crate) fn run_tests(
217217
sysroot_kind: SysrootKind,
218218
cg_clif_dylib: &Path,
219219
bootstrap_host_compiler: &Compiler,
220+
rustup_toolchain_name: Option<&str>,
220221
target_triple: String,
221222
) {
222223
if config::get_bool("testsuite.no_sysroot") {
@@ -226,6 +227,7 @@ pub(crate) fn run_tests(
226227
SysrootKind::None,
227228
cg_clif_dylib,
228229
bootstrap_host_compiler,
230+
rustup_toolchain_name,
229231
target_triple.clone(),
230232
);
231233

@@ -251,6 +253,7 @@ pub(crate) fn run_tests(
251253
sysroot_kind,
252254
cg_clif_dylib,
253255
bootstrap_host_compiler,
256+
rustup_toolchain_name,
254257
target_triple.clone(),
255258
);
256259
// Rust's build system denies a couple of lints that trigger on several of the test

build_system/usage.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ OPTIONS:
3030
Require Cargo.lock and cache are up to date
3131

3232
REQUIREMENTS:
33-
* Rustup: The build system has a hard coded dependency on rustup to install the right nightly
34-
version and make sure it is used where necessary.
33+
* Rustup: By default rustup is used to install the right nightly version. If you don't want to
34+
use rustup, you can manually install the nightly version indicated by rust-toolchain.toml and
35+
point the CARGO, RUSTC and RUSTDOC env vars to the right executables.
3536
* Git: `./y.rs prepare` uses git for applying patches and on Windows for downloading test repos.
3637
* Curl and tar (non-Windows only): Used by `./y.rs prepare` to download a single commit for
3738
repos. Git will be used to clone the whole repo when using Windows.

scripts/cargo-clif.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ fn main() {
2828
env::set_var("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or(String::new()) + &rustflags);
2929
env::set_var("RUSTDOCFLAGS", env::var("RUSTDOCFLAGS").unwrap_or(String::new()) + &rustflags);
3030

31-
// Ensure that the right toolchain is used
32-
env::set_var("RUSTUP_TOOLCHAIN", env!("TOOLCHAIN_NAME"));
31+
let cargo = if let Some(cargo) = option_env!("CARGO") {
32+
cargo
33+
} else {
34+
// Ensure that the right toolchain is used
35+
env::set_var("RUSTUP_TOOLCHAIN", option_env!("TOOLCHAIN_NAME").expect("TOOLCHAIN_NAME"));
36+
"cargo"
37+
};
3338

3439
let args: Vec<_> = match env::args().nth(1).as_deref() {
3540
Some("jit") => {
@@ -64,10 +69,10 @@ fn main() {
6469
};
6570

6671
#[cfg(unix)]
67-
panic!("Failed to spawn cargo: {}", Command::new("cargo").args(args).exec());
72+
panic!("Failed to spawn cargo: {}", Command::new(cargo).args(args).exec());
6873

6974
#[cfg(not(unix))]
7075
std::process::exit(
71-
Command::new("cargo").args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
76+
Command::new(cargo).args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
7277
);
7378
}

scripts/rustc-clif.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ fn main() {
3030
}
3131
args.extend(passed_args);
3232

33-
// Ensure that the right toolchain is used
34-
env::set_var("RUSTUP_TOOLCHAIN", env!("TOOLCHAIN_NAME"));
33+
let rustc = if let Some(rustc) = option_env!("RUSTC") {
34+
rustc
35+
} else {
36+
// Ensure that the right toolchain is used
37+
env::set_var("RUSTUP_TOOLCHAIN", option_env!("TOOLCHAIN_NAME").expect("TOOLCHAIN_NAME"));
38+
"rustc"
39+
};
3540

3641
#[cfg(unix)]
37-
panic!("Failed to spawn rustc: {}", Command::new("rustc").args(args).exec());
42+
panic!("Failed to spawn rustc: {}", Command::new(rustc).args(args).exec());
3843

3944
#[cfg(not(unix))]
4045
std::process::exit(
41-
Command::new("rustc").args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
46+
Command::new(rustc).args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
4247
);
4348
}

scripts/rustdoc-clif.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ fn main() {
3030
}
3131
args.extend(passed_args);
3232

33-
// Ensure that the right toolchain is used
34-
env::set_var("RUSTUP_TOOLCHAIN", env!("TOOLCHAIN_NAME"));
33+
let rustdoc = if let Some(rustdoc) = option_env!("RUSTDOC") {
34+
rustdoc
35+
} else {
36+
// Ensure that the right toolchain is used
37+
env::set_var("RUSTUP_TOOLCHAIN", option_env!("TOOLCHAIN_NAME").expect("TOOLCHAIN_NAME"));
38+
"rustdoc"
39+
};
3540

3641
#[cfg(unix)]
37-
panic!("Failed to spawn rustdoc: {}", Command::new("rustdoc").args(args).exec());
42+
panic!("Failed to spawn rustdoc: {}", Command::new(rustdoc).args(args).exec());
3843

3944
#[cfg(not(unix))]
4045
std::process::exit(
41-
Command::new("rustdoc").args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
46+
Command::new(rustdoc).args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
4247
);
4348
}

0 commit comments

Comments
 (0)