Skip to content

Commit

Permalink
Rollup merge of rust-lang#76544 - Mark-Simulacrum:less-python, r=alex…
Browse files Browse the repository at this point in the history
…crichton

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.
  • Loading branch information
Dylan-DPC committed Sep 19, 2020
2 parents 28e052f + afed561 commit bda12c4
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ authors = ["The Rust Project Developers"]
name = "bootstrap"
version = "0.0.0"
edition = "2018"
build = "build.rs"

[lib]
path = "lib.rs"
Expand Down
9 changes: 1 addition & 8 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,6 @@ def bootstrap(help_triggered):
parser = argparse.ArgumentParser(description='Build rust')
parser.add_argument('--config')
parser.add_argument('--build')
parser.add_argument('--src')
parser.add_argument('--clean', action='store_true')
parser.add_argument('-v', '--verbose', action='count', default=0)

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

# Configure initial bootstrap
build = RustBuild()
build.rust_root = args.src or os.path.abspath(os.path.join(__file__, '../../..'))
build.rust_root = os.path.abspath(os.path.join(__file__, '../../..'))
build.verbose = args.verbose
build.clean = args.clean

Expand Down Expand Up @@ -1032,18 +1031,12 @@ def bootstrap(help_triggered):
args = [build.bootstrap_binary()]
args.extend(sys.argv[1:])
env = os.environ.copy()
env["BUILD"] = build.build
env["SRC"] = build.rust_root
env["BOOTSTRAP_PARENT_ID"] = str(os.getpid())
env["BOOTSTRAP_PYTHON"] = sys.executable
env["BUILD_DIR"] = build.build_dir
env["RUSTC_BOOTSTRAP"] = '1'
env["CARGO"] = build.cargo()
env["RUSTC"] = build.rustc()
if toml_path:
env["BOOTSTRAP_CONFIG"] = toml_path
if build.rustfmt():
env["RUSTFMT"] = build.rustfmt()
run(args, env=env, verbose=build.verbose)


Expand Down
26 changes: 26 additions & 0 deletions src/bootstrap/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::env;
use std::path::PathBuf;

fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rustc-env=BUILD_TRIPLE={}", env::var("HOST").unwrap());

// This may not be a canonicalized path.
let mut rustc = PathBuf::from(env::var_os("RUSTC").unwrap());

if rustc.is_relative() {
for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) {
let absolute = dir.join(&rustc);
if absolute.exists() {
rustc = absolute;
break;
}
}
}
assert!(rustc.is_absolute());

// FIXME: if the path is not utf-8, this is going to break. Unfortunately
// Cargo doesn't have a way for us to specify non-utf-8 paths easily, so
// we'll need to invent some encoding scheme if this becomes a problem.
println!("cargo:rustc-env=RUSTC={}", rustc.to_str().unwrap());
}
3 changes: 3 additions & 0 deletions src/bootstrap/builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ fn configure(cmd: &str, host: &[&str], target: &[&str]) -> Config {
config.dry_run = true;
config.ninja_in_file = false;
// try to avoid spurious failures in dist where we create/delete each others file
config.out = PathBuf::from(env::var_os("BOOTSTRAP_OUTPUT_DIRECTORY").unwrap());
config.initial_rustc = PathBuf::from(env::var_os("RUSTC").unwrap());
config.initial_cargo = PathBuf::from(env::var_os("BOOTSTRAP_INITIAL_CARGO").unwrap());
let dir = config
.out
.join("tmp-rustbuild-tests")
Expand Down
25 changes: 16 additions & 9 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ struct Build {
build_dir: Option<String>,
cargo: Option<String>,
rustc: Option<String>,
rustfmt: Option<String>, /* allow bootstrap.py to use rustfmt key */
rustfmt: Option<PathBuf>,
docs: Option<bool>,
compiler_docs: Option<bool>,
submodules: Option<bool>,
Expand Down Expand Up @@ -487,13 +487,14 @@ impl Config {
config.missing_tools = false;

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

config.initial_rustc = Config::path_from_python("RUSTC");
config.initial_cargo = Config::path_from_python("CARGO");
config.initial_rustfmt = env::var_os("RUSTFMT").map(Config::normalize_python_path);
config.initial_cargo = PathBuf::from(env!("CARGO"));
config.initial_rustc = PathBuf::from(env!("RUSTC"));

config
}
Expand Down Expand Up @@ -582,6 +583,9 @@ impl Config {
set(&mut config.full_bootstrap, build.full_bootstrap);
set(&mut config.extended, build.extended);
config.tools = build.tools;
if build.rustfmt.is_some() {
config.initial_rustfmt = build.rustfmt;
}
set(&mut config.verbose, build.verbose);
set(&mut config.sanitizers, build.sanitizers);
set(&mut config.profiler, build.profiler);
Expand Down Expand Up @@ -836,12 +840,15 @@ impl Config {
set(&mut config.missing_tools, t.missing_tools);
}

// Cargo does not provide a RUSTFMT environment variable, so we
// synthesize it manually. Note that we also later check the config.toml
// and set this to that path if necessary.
let rustfmt = config.initial_rustc.with_file_name(exe("rustfmt", config.build));
config.initial_rustfmt = if rustfmt.exists() { Some(rustfmt) } else { None };

// Now that we've reached the end of our configuration, infer the
// default values for all options that we haven't otherwise stored yet.

set(&mut config.initial_rustc, build.rustc.map(PathBuf::from));
set(&mut config.initial_cargo, build.cargo.map(PathBuf::from));

config.llvm_skip_rebuild = llvm_skip_rebuild.unwrap_or(false);

let default = false;
Expand Down
2 changes: 2 additions & 0 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,8 @@ impl Step for Bootstrap {
.current_dir(builder.src.join("src/bootstrap"))
.env("RUSTFLAGS", "-Cdebuginfo=2")
.env("CARGO_TARGET_DIR", builder.out.join("bootstrap"))
.env("BOOTSTRAP_OUTPUT_DIRECTORY", &builder.config.out)
.env("BOOTSTRAP_INITIAL_CARGO", &builder.config.initial_cargo)
.env("RUSTC_BOOTSTRAP", "1")
.env("RUSTC", &builder.initial_rustc);
if let Some(flags) = option_env!("RUSTFLAGS") {
Expand Down
4 changes: 4 additions & 0 deletions src/bootstrap/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,10 @@ impl<'a> Builder<'a> {
}

add_dylib_path(lib_paths, &mut cmd);

// Provide a RUSTC for this command to use.
cmd.env("RUSTC", &self.initial_rustc);

cmd
}
}

0 comments on commit bda12c4

Please sign in to comment.