Skip to content

Commit 7467d17

Browse files
committed
Auto merge of #76544 - Mark-Simulacrum:less-python, r=alexcrichton
De-couple Python and bootstrap slightly This revises rustbuild's entry points from Python to rely less on magic environment variables, preferring to use Cargo-provided environment variables where feasible. Notably, BUILD_DIR and BOOTSTRAP_CONFIG are *not* moved, because both more-or-less have some non-trivial discovery logic and replicating it in rustbuild seems unfortunate; if it moved to Cargo that would be a different story. Best reviewed by-commit.
2 parents 1fd5b9d + cf33aad commit 7467d17

File tree

9 files changed

+58
-20
lines changed

9 files changed

+58
-20
lines changed

src/bootstrap/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ authors = ["The Rust Project Developers"]
33
name = "bootstrap"
44
version = "0.0.0"
55
edition = "2018"
6+
build = "build.rs"
67

78
[lib]
89
path = "lib.rs"

src/bootstrap/bootstrap.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,6 @@ def bootstrap(help_triggered):
966966
parser = argparse.ArgumentParser(description='Build rust')
967967
parser.add_argument('--config')
968968
parser.add_argument('--build')
969-
parser.add_argument('--src')
970969
parser.add_argument('--clean', action='store_true')
971970
parser.add_argument('-v', '--verbose', action='count', default=0)
972971

@@ -975,7 +974,7 @@ def bootstrap(help_triggered):
975974

976975
# Configure initial bootstrap
977976
build = RustBuild()
978-
build.rust_root = args.src or os.path.abspath(os.path.join(__file__, '../../..'))
977+
build.rust_root = os.path.abspath(os.path.join(__file__, '../../..'))
979978
build.verbose = args.verbose
980979
build.clean = args.clean
981980

@@ -1032,18 +1031,12 @@ def bootstrap(help_triggered):
10321031
args = [build.bootstrap_binary()]
10331032
args.extend(sys.argv[1:])
10341033
env = os.environ.copy()
1035-
env["BUILD"] = build.build
1036-
env["SRC"] = build.rust_root
10371034
env["BOOTSTRAP_PARENT_ID"] = str(os.getpid())
10381035
env["BOOTSTRAP_PYTHON"] = sys.executable
10391036
env["BUILD_DIR"] = build.build_dir
10401037
env["RUSTC_BOOTSTRAP"] = '1'
1041-
env["CARGO"] = build.cargo()
1042-
env["RUSTC"] = build.rustc()
10431038
if toml_path:
10441039
env["BOOTSTRAP_CONFIG"] = toml_path
1045-
if build.rustfmt():
1046-
env["RUSTFMT"] = build.rustfmt()
10471040
run(args, env=env, verbose=build.verbose)
10481041

10491042

src/bootstrap/build.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
4+
fn main() {
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());
26+
}

src/bootstrap/builder/tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ fn configure(cmd: &str, host: &[&str], target: &[&str]) -> Config {
1010
config.dry_run = true;
1111
config.ninja_in_file = false;
1212
// try to avoid spurious failures in dist where we create/delete each others file
13+
config.out = PathBuf::from(env::var_os("BOOTSTRAP_OUTPUT_DIRECTORY").unwrap());
14+
config.initial_rustc = PathBuf::from(env::var_os("RUSTC").unwrap());
15+
config.initial_cargo = PathBuf::from(env::var_os("BOOTSTRAP_INITIAL_CARGO").unwrap());
1316
let dir = config
1417
.out
1518
.join("tmp-rustbuild-tests")

src/bootstrap/config.rs

+16-9
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>,
@@ -487,13 +487,14 @@ impl Config {
487487
config.missing_tools = false;
488488

489489
// set by bootstrap.py
490-
config.build = TargetSelection::from_user(&env::var("BUILD").expect("'BUILD' to be set"));
491-
config.src = Config::path_from_python("SRC");
490+
config.build = TargetSelection::from_user(&env!("BUILD_TRIPLE"));
491+
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
492+
// Undo `src/bootstrap`
493+
config.src = manifest_dir.parent().unwrap().parent().unwrap().to_owned();
492494
config.out = Config::path_from_python("BUILD_DIR");
493495

494-
config.initial_rustc = Config::path_from_python("RUSTC");
495-
config.initial_cargo = Config::path_from_python("CARGO");
496-
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"));
497498

498499
config
499500
}
@@ -582,6 +583,9 @@ impl Config {
582583
set(&mut config.full_bootstrap, build.full_bootstrap);
583584
set(&mut config.extended, build.extended);
584585
config.tools = build.tools;
586+
if build.rustfmt.is_some() {
587+
config.initial_rustfmt = build.rustfmt;
588+
}
585589
set(&mut config.verbose, build.verbose);
586590
set(&mut config.sanitizers, build.sanitizers);
587591
set(&mut config.profiler, build.profiler);
@@ -836,12 +840,15 @@ impl Config {
836840
set(&mut config.missing_tools, t.missing_tools);
837841
}
838842

843+
// Cargo does not provide a RUSTFMT environment variable, so we
844+
// synthesize it manually. Note that we also later check the config.toml
845+
// and set this to that path if necessary.
846+
let rustfmt = config.initial_rustc.with_file_name(exe("rustfmt", config.build));
847+
config.initial_rustfmt = if rustfmt.exists() { Some(rustfmt) } else { None };
848+
839849
// Now that we've reached the end of our configuration, infer the
840850
// default values for all options that we haven't otherwise stored yet.
841851

842-
set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
843-
set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));
844-
845852
config.llvm_skip_rebuild = llvm_skip_rebuild.unwrap_or(false);
846853

847854
let default = false;

src/bootstrap/test.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,8 @@ impl Step for Compiletest {
12811281
cmd.arg("--rustfix-coverage");
12821282
}
12831283

1284+
cmd.env("BOOTSTRAP_CARGO", &builder.initial_cargo);
1285+
12841286
builder.ci_env.force_coloring_in_ci(&mut cmd);
12851287

12861288
builder.info(&format!(
@@ -2022,6 +2024,8 @@ impl Step for Bootstrap {
20222024
.current_dir(builder.src.join("src/bootstrap"))
20232025
.env("RUSTFLAGS", "-Cdebuginfo=2")
20242026
.env("CARGO_TARGET_DIR", builder.out.join("bootstrap"))
2027+
.env("BOOTSTRAP_OUTPUT_DIRECTORY", &builder.config.out)
2028+
.env("BOOTSTRAP_INITIAL_CARGO", &builder.config.initial_cargo)
20252029
.env("RUSTC_BOOTSTRAP", "1")
20262030
.env("RUSTC", &builder.initial_rustc);
20272031
if let Some(flags) = option_env!("RUSTFLAGS") {

src/bootstrap/tool.rs

+4
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,10 @@ impl<'a> Builder<'a> {
700700
}
701701

702702
add_dylib_path(lib_paths, &mut cmd);
703+
704+
// Provide a RUSTC for this command to use.
705+
cmd.env("RUSTC", &self.initial_rustc);
706+
703707
cmd
704708
}
705709
}

src/test/run-make/thumb-none-cortex-m/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ all:
3535
# HACK(eddyb) sets `RUSTC_BOOTSTRAP=1` so Cargo can accept nightly features.
3636
# These come from the top-level Rust workspace, that this crate is not a
3737
# member of, but Cargo tries to load the workspace `Cargo.toml` anyway.
38-
cd $(WORK_DIR) && cd $(CRATE) && env RUSTC_BOOTSTRAP=1 $(CARGO) build --target $(TARGET) -v
38+
cd $(WORK_DIR) && cd $(CRATE) && env RUSTC_BOOTSTRAP=1 $(BOOTSTRAP_CARGO) build --target $(TARGET) -v

src/test/run-make/thumb-none-qemu/script.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pushd $WORK_DIR
1212
# These come from the top-level Rust workspace, that this crate is not a
1313
# member of, but Cargo tries to load the workspace `Cargo.toml` anyway.
1414
env RUSTC_BOOTSTRAP=1 RUSTFLAGS="-C linker=arm-none-eabi-ld -C link-arg=-Tlink.x" \
15-
$CARGO run --target $TARGET | grep "x = 42"
15+
$BOOTSTRAP_CARGO run --target $TARGET | grep "x = 42"
1616
env RUSTC_BOOTSTRAP=1 RUSTFLAGS="-C linker=arm-none-eabi-ld -C link-arg=-Tlink.x" \
17-
$CARGO run --target $TARGET --release | grep "x = 42"
17+
$BOOTSTRAP_CARGO run --target $TARGET --release | grep "x = 42"
1818
popd
1919
popd

0 commit comments

Comments
 (0)