Skip to content

Commit a684753

Browse files
authored
Merge pull request rust-lang#1374 from bjorn3/non_rustup_build3
Allow building and testing without rustup
2 parents 24f1569 + a555b8a commit a684753

14 files changed

+146
-92
lines changed

build_system/abi_cafe.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ 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);
22-
spawn_and_wait(ABI_CAFE.fetch("cargo", &bootstrap_host_compiler.rustc, dirs));
2323

2424
eprintln!("Building sysroot for abi-cafe");
2525
build_sysroot::build_sysroot(
@@ -28,6 +28,7 @@ pub(crate) fn run(
2828
sysroot_kind,
2929
cg_clif_dylib,
3030
bootstrap_host_compiler,
31+
rustup_toolchain_name,
3132
bootstrap_host_compiler.triple.clone(),
3233
);
3334

build_system/bench.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::Path;
44
use super::path::{Dirs, RelPath};
55
use super::prepare::GitRepo;
66
use super::rustc_info::get_file_name;
7-
use super::utils::{hyperfine_command, spawn_and_wait};
7+
use super::utils::{hyperfine_command, spawn_and_wait, Compiler};
88

99
static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github(
1010
"ebobby",
@@ -13,11 +13,11 @@ static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github(
1313
"<none>",
1414
);
1515

16-
pub(crate) fn benchmark(dirs: &Dirs) {
17-
benchmark_simple_raytracer(dirs);
16+
pub(crate) fn benchmark(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
17+
benchmark_simple_raytracer(dirs, bootstrap_host_compiler);
1818
}
1919

20-
fn benchmark_simple_raytracer(dirs: &Dirs) {
20+
fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
2121
if std::process::Command::new("hyperfine").output().is_err() {
2222
eprintln!("Hyperfine not installed");
2323
eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine");
@@ -31,8 +31,9 @@ fn benchmark_simple_raytracer(dirs: &Dirs) {
3131
let bench_runs = env::var("BENCH_RUNS").unwrap_or_else(|_| "10".to_string()).parse().unwrap();
3232

3333
eprintln!("[BENCH COMPILE] ebobby/simple-raytracer");
34-
let cargo_clif =
35-
RelPath::DIST.to_path(dirs).join(get_file_name("cargo_clif", "bin").replace('_', "-"));
34+
let cargo_clif = RelPath::DIST
35+
.to_path(dirs)
36+
.join(get_file_name(&bootstrap_host_compiler.rustc, "cargo_clif", "bin").replace('_', "-"));
3637
let manifest_path = SIMPLE_RAYTRACER_REPO.source_dir().to_path(dirs).join("Cargo.toml");
3738
let target_dir = RelPath::BUILD.join("simple_raytracer").to_path(dirs);
3839

@@ -75,9 +76,18 @@ fn benchmark_simple_raytracer(dirs: &Dirs) {
7576
bench_runs,
7677
None,
7778
&[
78-
Path::new(".").join(get_file_name("raytracer_cg_llvm", "bin")).to_str().unwrap(),
79-
Path::new(".").join(get_file_name("raytracer_cg_clif", "bin")).to_str().unwrap(),
80-
Path::new(".").join(get_file_name("raytracer_cg_clif_opt", "bin")).to_str().unwrap(),
79+
Path::new(".")
80+
.join(get_file_name(&bootstrap_host_compiler.rustc, "raytracer_cg_llvm", "bin"))
81+
.to_str()
82+
.unwrap(),
83+
Path::new(".")
84+
.join(get_file_name(&bootstrap_host_compiler.rustc, "raytracer_cg_clif", "bin"))
85+
.to_str()
86+
.unwrap(),
87+
Path::new(".")
88+
.join(get_file_name(&bootstrap_host_compiler.rustc, "raytracer_cg_clif_opt", "bin"))
89+
.to_str()
90+
.unwrap(),
8191
],
8292
);
8393
bench_run.current_dir(RelPath::BUILD.to_path(dirs));

build_system/build_backend.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ pub(crate) fn build_backend(
5353
.target_dir(dirs)
5454
.join(&bootstrap_host_compiler.triple)
5555
.join(channel)
56-
.join(get_file_name("rustc_codegen_cranelift", "dylib"))
56+
.join(get_file_name(&bootstrap_host_compiler.rustc, "rustc_codegen_cranelift", "dylib"))
5757
}

build_system/build_sysroot.rs

+16-4
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);
@@ -40,19 +41,30 @@ pub(crate) fn build_sysroot(
4041
try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
4142

4243
// Build and copy rustc and cargo wrappers
43-
let wrapper_base_name = get_file_name("____", "bin");
44-
let toolchain_name = get_toolchain_name();
44+
let wrapper_base_name = get_file_name(&bootstrap_host_compiler.rustc, "____", "bin");
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

+40-7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub(crate) fn main() {
7878
let mut channel = "release";
7979
let mut sysroot_kind = SysrootKind::Clif;
8080
let mut use_unstable_features = true;
81+
let mut frozen = false;
8182
while let Some(arg) = args.next().as_deref() {
8283
match arg {
8384
"--out-dir" => {
@@ -96,17 +97,38 @@ pub(crate) fn main() {
9697
}
9798
}
9899
"--no-unstable-features" => use_unstable_features = false,
100+
"--frozen" => frozen = true,
99101
flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag),
100102
arg => arg_error!("Unexpected argument {}", arg),
101103
}
102104
}
103105

104-
let bootstrap_host_compiler = Compiler::bootstrap_with_triple(
105-
std::env::var("HOST_TRIPLE")
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+
};
114+
let bootstrap_host_compiler = {
115+
let cargo = rustc_info::get_cargo_path();
116+
let rustc = rustc_info::get_rustc_path();
117+
let rustdoc = rustc_info::get_rustdoc_path();
118+
let triple = std::env::var("HOST_TRIPLE")
106119
.ok()
107120
.or_else(|| config::get_value("host"))
108-
.unwrap_or_else(|| rustc_info::get_host_triple()),
109-
);
121+
.unwrap_or_else(|| rustc_info::get_host_triple(&rustc));
122+
Compiler {
123+
cargo,
124+
rustc,
125+
rustdoc,
126+
rustflags: String::new(),
127+
rustdocflags: String::new(),
128+
triple,
129+
runner: vec![],
130+
}
131+
};
110132
let target_triple = std::env::var("TARGET_TRIPLE")
111133
.ok()
112134
.or_else(|| config::get_value("target"))
@@ -120,6 +142,7 @@ pub(crate) fn main() {
120142
download_dir: out_dir.join("download"),
121143
build_dir: out_dir.join("build"),
122144
dist_dir: out_dir.join("dist"),
145+
frozen,
123146
};
124147

125148
path::RelPath::BUILD.ensure_exists(&dirs);
@@ -134,7 +157,7 @@ pub(crate) fn main() {
134157
}
135158

136159
if command == Command::Prepare {
137-
prepare::prepare(&dirs);
160+
prepare::prepare(&dirs, &bootstrap_host_compiler.rustc);
138161
process::exit(0);
139162
}
140163

@@ -158,6 +181,7 @@ pub(crate) fn main() {
158181
sysroot_kind,
159182
&cg_clif_dylib,
160183
&bootstrap_host_compiler,
184+
rustup_toolchain_name.as_deref(),
161185
target_triple.clone(),
162186
);
163187
}
@@ -166,7 +190,14 @@ pub(crate) fn main() {
166190
eprintln!("Abi-cafe doesn't support cross-compilation");
167191
process::exit(1);
168192
}
169-
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+
);
170201
}
171202
Command::Build => {
172203
build_sysroot::build_sysroot(
@@ -175,6 +206,7 @@ pub(crate) fn main() {
175206
sysroot_kind,
176207
&cg_clif_dylib,
177208
&bootstrap_host_compiler,
209+
rustup_toolchain_name.as_deref(),
178210
target_triple,
179211
);
180212
}
@@ -185,9 +217,10 @@ pub(crate) fn main() {
185217
sysroot_kind,
186218
&cg_clif_dylib,
187219
&bootstrap_host_compiler,
220+
rustup_toolchain_name.as_deref(),
188221
target_triple,
189222
);
190-
bench::benchmark(&dirs);
223+
bench::benchmark(&dirs, &bootstrap_host_compiler);
191224
}
192225
}
193226
}

build_system/path.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub(crate) struct Dirs {
99
pub(crate) download_dir: PathBuf,
1010
pub(crate) build_dir: PathBuf,
1111
pub(crate) dist_dir: PathBuf,
12+
pub(crate) frozen: bool,
1213
}
1314

1415
#[doc(hidden)]

build_system/prepare.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,19 @@ use super::rustc_info::{get_default_sysroot, get_rustc_version};
99
use super::tests::LIBCORE_TESTS_SRC;
1010
use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait};
1111

12-
pub(crate) fn prepare(dirs: &Dirs) {
12+
pub(crate) fn prepare(dirs: &Dirs, rustc: &Path) {
1313
RelPath::DOWNLOAD.ensure_fresh(dirs);
1414

15-
spawn_and_wait(super::build_backend::CG_CLIF.fetch("cargo", "rustc", dirs));
16-
17-
prepare_stdlib(dirs);
18-
spawn_and_wait(super::build_sysroot::STANDARD_LIBRARY.fetch("cargo", "rustc", dirs));
19-
20-
prepare_coretests(dirs);
21-
spawn_and_wait(super::tests::LIBCORE_TESTS.fetch("cargo", "rustc", dirs));
15+
prepare_stdlib(dirs, rustc);
16+
prepare_coretests(dirs, rustc);
2217

2318
super::tests::RAND_REPO.fetch(dirs);
24-
spawn_and_wait(super::tests::RAND.fetch("cargo", "rustc", dirs));
2519
super::tests::REGEX_REPO.fetch(dirs);
26-
spawn_and_wait(super::tests::REGEX.fetch("cargo", "rustc", dirs));
2720
super::tests::PORTABLE_SIMD_REPO.fetch(dirs);
28-
spawn_and_wait(super::tests::PORTABLE_SIMD.fetch("cargo", "rustc", dirs));
2921
}
3022

31-
fn prepare_stdlib(dirs: &Dirs) {
32-
let sysroot_src_orig = get_default_sysroot(Path::new("rustc")).join("lib/rustlib/src/rust");
23+
fn prepare_stdlib(dirs: &Dirs, rustc: &Path) {
24+
let sysroot_src_orig = get_default_sysroot(rustc).join("lib/rustlib/src/rust");
3325
assert!(sysroot_src_orig.exists());
3426

3527
eprintln!("[COPY] stdlib src");
@@ -44,7 +36,7 @@ fn prepare_stdlib(dirs: &Dirs) {
4436
&SYSROOT_SRC.to_path(dirs).join("library"),
4537
);
4638

47-
let rustc_version = get_rustc_version(Path::new("rustc"));
39+
let rustc_version = get_rustc_version(rustc);
4840
fs::write(SYSROOT_RUSTC_VERSION.to_path(dirs), &rustc_version).unwrap();
4941

5042
eprintln!("[GIT] init");
@@ -53,8 +45,8 @@ fn prepare_stdlib(dirs: &Dirs) {
5345
apply_patches(dirs, "stdlib", &SYSROOT_SRC.to_path(dirs));
5446
}
5547

56-
fn prepare_coretests(dirs: &Dirs) {
57-
let sysroot_src_orig = get_default_sysroot(Path::new("rustc")).join("lib/rustlib/src/rust");
48+
fn prepare_coretests(dirs: &Dirs, rustc: &Path) {
49+
let sysroot_src_orig = get_default_sysroot(rustc).join("lib/rustlib/src/rust");
5850
assert!(sysroot_src_orig.exists());
5951

6052
eprintln!("[COPY] coretests src");

build_system/rustc_info.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ pub(crate) fn get_rustc_version(rustc: &Path) -> String {
77
String::from_utf8(version_info).unwrap()
88
}
99

10-
pub(crate) fn get_host_triple() -> String {
10+
pub(crate) fn get_host_triple(rustc: &Path) -> String {
1111
let version_info =
12-
Command::new("rustc").stderr(Stdio::inherit()).args(&["-vV"]).output().unwrap().stdout;
12+
Command::new(rustc).stderr(Stdio::inherit()).args(&["-vV"]).output().unwrap().stdout;
1313
String::from_utf8(version_info)
1414
.unwrap()
1515
.lines()
@@ -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"])
@@ -73,8 +82,9 @@ pub(crate) fn get_default_sysroot(rustc: &Path) -> PathBuf {
7382
Path::new(String::from_utf8(default_sysroot).unwrap().trim()).to_owned()
7483
}
7584

76-
pub(crate) fn get_file_name(crate_name: &str, crate_type: &str) -> String {
77-
let file_name = Command::new("rustc")
85+
// FIXME call once for each target and pass result around in struct
86+
pub(crate) fn get_file_name(rustc: &Path, crate_name: &str, crate_type: &str) -> String {
87+
let file_name = Command::new(rustc)
7888
.stderr(Stdio::inherit())
7989
.args(&[
8090
"--crate-name",

build_system/tests.rs

+3
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

0 commit comments

Comments
 (0)