Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b616d7b

Browse files
committedMay 10, 2022
unify bootstrap and shim binaries
they are now dispatched based on the binary name it is called with. `target/debug/rust{c, doc}` are now hard linked to `bootstrap`.
1 parent fee75fb commit b616d7b

11 files changed

+95
-78
lines changed
 

‎src/bootstrap/Cargo.toml

+3-23
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,15 @@ name = "bootstrap"
33
version = "0.0.0"
44
edition = "2021"
55
build = "build.rs"
6-
default-run = "bootstrap"
6+
default-run = "rustbuild-binary-dispatch-shim"
77

88
[lib]
99
path = "lib.rs"
1010
doctest = false
1111

1212
[[bin]]
13-
name = "bootstrap"
14-
path = "bin/main.rs"
15-
test = false
16-
17-
[[bin]]
18-
name = "rustc"
19-
path = "bin/rustc.rs"
20-
test = false
21-
22-
[[bin]]
23-
name = "rustdoc"
24-
path = "bin/rustdoc.rs"
25-
test = false
26-
27-
[[bin]]
28-
name = "sccache-plus-cl"
29-
path = "bin/sccache-plus-cl.rs"
30-
test = false
31-
32-
[[bin]]
33-
name = "llvm-config-wrapper"
34-
path = "bin/llvm-config-wrapper.rs"
13+
name = "rustbuild-binary-dispatch-shim"
14+
path = "entrypoints/dispatcher.rs"
3515
test = false
3616

3717
[dependencies]

‎src/bootstrap/bootstrap.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,15 @@ def bootstrap_binary(self):
837837
>>> rb = RustBuild()
838838
>>> rb.build_dir = "build"
839839
>>> rb.bootstrap_binary() == os.path.join("build", "bootstrap",
840-
... "debug", "bootstrap")
840+
... "debug", "{}{}".format("rustbuild-binary-dispatch-shim", rb.exe_suffix()))
841841
True
842842
"""
843-
return os.path.join(self.build_dir, "bootstrap", "debug", "bootstrap")
843+
return os.path.join(
844+
self.build_dir,
845+
"bootstrap",
846+
"debug",
847+
"{}{}".format("rustbuild-binary-dispatch-shim", self.exe_suffix())
848+
)
844849

845850
def build_bootstrap(self):
846851
"""Build bootstrap"""

‎src/bootstrap/dylib_util.rs

-28
This file was deleted.

‎src/bootstrap/bin/main.rs ‎src/bootstrap/entrypoints/bootstrap.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
//! rustbuild, the Rust build system
2-
//!
3-
//! This is the entry point for the build system used to compile the `rustc`
4-
//! compiler. Lots of documentation can be found in the `README.md` file in the
5-
//! parent directory, and otherwise documentation can be found throughout the `build`
6-
//! directory in each respective module.
7-
81
use std::env;
92

103
use bootstrap::{Build, Config, Subcommand, VERSION};
114

12-
fn main() {
5+
pub fn main() {
136
let args = env::args().skip(1).collect::<Vec<_>>();
147
let config = Config::parse(&args);
158

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! rustbuild, the Rust build system
2+
//!
3+
//! This is the entry point for the build system used to compile the `rustc`
4+
//! compiler. Lots of documentation can be found in the `README.md` file in the
5+
//! parent directory, and otherwise documentation can be found throughout the `build`
6+
//! directory in each respective module.
7+
8+
use std::env;
9+
use std::env::consts::EXE_SUFFIX;
10+
use std::ffi::OsStr;
11+
use std::path::Path;
12+
13+
mod bootstrap;
14+
mod llvm_config_wrapper;
15+
mod rustc;
16+
mod rustdoc;
17+
mod sccache_plus_cl;
18+
19+
fn main() {
20+
match env::args_os()
21+
.next()
22+
.as_deref()
23+
.map(Path::new)
24+
.and_then(Path::file_name)
25+
.and_then(OsStr::to_str)
26+
.map(|s| s.strip_suffix(EXE_SUFFIX).unwrap_or(s))
27+
{
28+
// the shim name is here to make default-run work.
29+
Some("bootstrap" | "rustbuild-binary-dispatch-shim") => bootstrap::main(),
30+
Some("rustc") => rustc::main(),
31+
Some("rustdoc") => rustdoc::main(),
32+
Some("sccache-plus-cl") => sccache_plus_cl::main(),
33+
Some("llvm-config-wrapper") => llvm_config_wrapper::main(),
34+
Some(arg) => panic!("invalid executable name: {}", arg),
35+
None => panic!("argv[0] does not exist"),
36+
}
37+
}

‎src/bootstrap/bin/llvm-config-wrapper.rs ‎src/bootstrap/entrypoints/llvm_config_wrapper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::env;
55
use std::io::{self, Write};
66
use std::process::{self, Command, Stdio};
77

8-
fn main() {
8+
pub fn main() {
99
let real_llvm_config = env::var_os("LLVM_CONFIG_REAL").unwrap();
1010
let mut cmd = Command::new(real_llvm_config);
1111
cmd.args(env::args().skip(1)).stderr(Stdio::piped());

‎src/bootstrap/bin/rustc.rs ‎src/bootstrap/entrypoints/rustc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
//! switching compilers for the bootstrap and for build scripts will probably
1616
//! never get replaced.
1717
18-
include!("../dylib_util.rs");
19-
18+
use bootstrap::util::{dylib_path, dylib_path_var};
2019
use std::env;
2120
use std::path::PathBuf;
2221
use std::process::{Child, Command};
2322
use std::str::FromStr;
2423
use std::time::Instant;
2524

26-
fn main() {
25+
pub fn main() {
2726
let args = env::args_os().skip(1).collect::<Vec<_>>();
2827

2928
// Detect whether or not we're a build script depending on whether --target

‎src/bootstrap/bin/rustdoc.rs ‎src/bootstrap/entrypoints/rustdoc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
//!
33
//! See comments in `src/bootstrap/rustc.rs` for more information.
44
5+
use bootstrap::util::{dylib_path, dylib_path_var};
56
use std::env;
67
use std::ffi::OsString;
78
use std::path::PathBuf;
89
use std::process::Command;
910

10-
include!("../dylib_util.rs");
11-
12-
fn main() {
11+
pub fn main() {
1312
let args = env::args_os().skip(1).collect::<Vec<_>>();
1413
let rustdoc = env::var_os("RUSTDOC_REAL").expect("RUSTDOC_REAL was not set");
1514
let libdir = env::var_os("RUSTDOC_LIBDIR").expect("RUSTDOC_LIBDIR was not set");

‎src/bootstrap/bin/sccache-plus-cl.rs ‎src/bootstrap/entrypoints/sccache_plus_cl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env;
22
use std::process::{self, Command};
33

4-
fn main() {
4+
pub fn main() {
55
let target = env::var("SCCACHE_TARGET").unwrap();
66
// Locate the actual compiler that we're invoking
77
env::set_var("CC", env::var_os("SCCACHE_CC").unwrap());

‎src/bootstrap/lib.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
use std::cell::{Cell, RefCell};
107107
use std::collections::{HashMap, HashSet};
108108
use std::env;
109+
use std::env::consts::EXE_SUFFIX;
109110
use std::fs::{self, File};
110111
use std::path::{Path, PathBuf};
111112
use std::process::{self, Command};
@@ -450,15 +451,10 @@ impl Build {
450451
let bootstrap_out = if std::env::var("BOOTSTRAP_PYTHON").is_ok() {
451452
out.join("bootstrap").join("debug")
452453
} else {
453-
let workspace_target_dir = std::env::var("CARGO_TARGET_DIR")
454+
std::env::var("CARGO_TARGET_DIR")
454455
.map(PathBuf::from)
455-
.unwrap_or_else(|_| src.join("target"));
456-
let bootstrap_out = workspace_target_dir.join("debug");
457-
if !bootstrap_out.join("rustc").exists() && !cfg!(test) {
458-
// this restriction can be lifted whenever https://github.com/rust-lang/rfcs/pull/3028 is implemented
459-
panic!("run `cargo build --bins` before `cargo run`")
460-
}
461-
bootstrap_out
456+
.unwrap_or_else(|_| src.join("target"))
457+
.join("debug")
462458
};
463459

464460
let mut build = Build {
@@ -502,6 +498,20 @@ impl Build {
502498
tool_artifacts: Default::default(),
503499
};
504500

501+
let bootstrap_out = &build.bootstrap_out;
502+
let dispatcher = bootstrap_out.join(format!("rustbuild-binary-dispatch-shim{EXE_SUFFIX}"));
503+
if dispatcher.exists() && !cfg!(test) {
504+
build.copy(&dispatcher, &bootstrap_out.join(format!("bootstrap{EXE_SUFFIX}")));
505+
build.copy(&dispatcher, &bootstrap_out.join(format!("rustc{EXE_SUFFIX}")));
506+
build.copy(&dispatcher, &bootstrap_out.join(format!("rustdoc{EXE_SUFFIX}")));
507+
build.copy(&dispatcher, &bootstrap_out.join(format!("sccache-plus-cl{EXE_SUFFIX}")));
508+
build
509+
.copy(&dispatcher, &bootstrap_out.join(format!("llvm-config-wrapper{EXE_SUFFIX}")));
510+
} else if !cfg!(test) {
511+
// tests do not need these files to be copied.
512+
panic!("bootstrap binary shim ({}) does not exist", dispatcher.display());
513+
}
514+
505515
build.verbose("finding compilers");
506516
cc_detect::find(&mut build);
507517
// When running `setup`, the profile is about to change, so any requirements we have now may

‎src/bootstrap/util.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,29 @@ pub fn add_dylib_path(path: Vec<PathBuf>, cmd: &mut Command) {
7272
cmd.env(dylib_path_var(), t!(env::join_paths(list)));
7373
}
7474

75-
include!("dylib_util.rs");
75+
/// Returns the environment variable which the dynamic library lookup path
76+
/// resides in for this platform.
77+
pub fn dylib_path_var() -> &'static str {
78+
if cfg!(target_os = "windows") {
79+
"PATH"
80+
} else if cfg!(target_os = "macos") {
81+
"DYLD_LIBRARY_PATH"
82+
} else if cfg!(target_os = "haiku") {
83+
"LIBRARY_PATH"
84+
} else {
85+
"LD_LIBRARY_PATH"
86+
}
87+
}
88+
89+
/// Parses the `dylib_path_var()` environment variable, returning a list of
90+
/// paths that are members of this lookup path.
91+
pub fn dylib_path() -> Vec<std::path::PathBuf> {
92+
let var = match env::var_os(dylib_path_var()) {
93+
Some(v) => v,
94+
None => return vec![],
95+
};
96+
env::split_paths(&var).collect()
97+
}
7698

7799
/// Adds a list of lookup paths to `cmd`'s link library lookup path.
78100
pub fn add_link_lib_path(path: Vec<PathBuf>, cmd: &mut Command) {

0 commit comments

Comments
 (0)
Please sign in to comment.