Skip to content

Commit f0392c2

Browse files
Discover Rust toolchain without Python
1 parent ccdb48a commit f0392c2

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/bootstrap/bootstrap.py

-4
Original file line numberDiff line numberDiff line change
@@ -1035,12 +1035,8 @@ def bootstrap(help_triggered):
10351035
env["BOOTSTRAP_PYTHON"] = sys.executable
10361036
env["BUILD_DIR"] = build.build_dir
10371037
env["RUSTC_BOOTSTRAP"] = '1'
1038-
env["CARGO"] = build.cargo()
1039-
env["RUSTC"] = build.rustc()
10401038
if toml_path:
10411039
env["BOOTSTRAP_CONFIG"] = toml_path
1042-
if build.rustfmt():
1043-
env["RUSTFMT"] = build.rustfmt()
10441040
run(args, env=env, verbose=build.verbose)
10451041

10461042

src/bootstrap/build.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
14
fn main() {
2-
println!("cargo:rustc-env=BUILD_TRIPLE={}", std::env::var("HOST").unwrap());
5+
println!("cargo:rerun-if-changed=build.rs");
6+
println!("cargo:rustc-env=BUILD_TRIPLE={}", env::var("HOST").unwrap());
7+
8+
// This may not be a canonicalized path.
9+
let mut rustc = PathBuf::from(env::var_os("RUSTC").unwrap());
10+
11+
if rustc.is_relative() {
12+
for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
13+
let absolute = dir.join(&rustc);
14+
if absolute.exists() {
15+
rustc = absolute;
16+
break;
17+
}
18+
}
19+
}
20+
assert!(rustc.is_absolute());
21+
22+
// FIXME: if the path is not utf-8, this is going to break. Unfortunately
23+
// Cargo doesn't have a way for us to specify non-utf-8 paths easily, so
24+
// we'll need to invent some encoding scheme if this becomes a problem.
25+
println!("cargo:rustc-env=RUSTC={}", rustc.to_str().unwrap());
326
}

src/bootstrap/config.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ struct Build {
291291
build_dir: Option<String>,
292292
cargo: Option<String>,
293293
rustc: Option<String>,
294-
rustfmt: Option<String>, /* allow bootstrap.py to use rustfmt key */
294+
rustfmt: Option<PathBuf>,
295295
docs: Option<bool>,
296296
compiler_docs: Option<bool>,
297297
submodules: Option<bool>,
@@ -493,9 +493,8 @@ impl Config {
493493
config.src = manifest_dir.parent().unwrap().parent().unwrap().to_owned();
494494
config.out = Config::path_from_python("BUILD_DIR");
495495

496-
config.initial_rustc = Config::path_from_python("RUSTC");
497-
config.initial_cargo = Config::path_from_python("CARGO");
498-
config.initial_rustfmt = env::var_os("RUSTFMT").map(Config::normalize_python_path);
496+
config.initial_cargo = PathBuf::from(env!("CARGO"));
497+
config.initial_rustc = PathBuf::from(env!("RUSTC"));
499498

500499
config
501500
}
@@ -584,6 +583,9 @@ impl Config {
584583
set(&mut config.full_bootstrap, build.full_bootstrap);
585584
set(&mut config.extended, build.extended);
586585
config.tools = build.tools;
586+
if build.rustfmt.is_some() {
587+
config.initial_rustfmt = build.rustfmt;
588+
}
587589
set(&mut config.verbose, build.verbose);
588590
set(&mut config.sanitizers, build.sanitizers);
589591
set(&mut config.profiler, build.profiler);
@@ -832,12 +834,15 @@ impl Config {
832834
set(&mut config.missing_tools, t.missing_tools);
833835
}
834836

837+
// Cargo does not provide a RUSTFMT environment variable, so we
838+
// synthesize it manually. Note that we also later check the config.toml
839+
// and set this to that path if necessary.
840+
let rustfmt = config.initial_rustc.with_file_name(exe("rustfmt", config.build));
841+
config.initial_rustfmt = if rustfmt.exists() { Some(rustfmt) } else { None };
842+
835843
// Now that we've reached the end of our configuration, infer the
836844
// default values for all options that we haven't otherwise stored yet.
837845

838-
set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
839-
set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
840-
841846
config.llvm_skip_rebuild = llvm_skip_rebuild.unwrap_or(false);
842847

843848
let default = false;

0 commit comments

Comments
 (0)