Skip to content

Commit 8b85e7c

Browse files
committed
Remove build_helper
The majority of the code is only used by either rustbuild or rustc_llvm's build script. Rust_build is compiled once for rustbuild and once for every stage. This means that the majority of the code in this crate is needlessly compiled multiple times. By moving only the code actually used by the respective crates to rustbuild and rustc_llvm's build script, this needless duplicate compilation is avoided.
1 parent 65f6d33 commit 8b85e7c

27 files changed

+92
-101
lines changed

Cargo.lock

-6
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ dependencies = [
214214
name = "bootstrap"
215215
version = "0.0.0"
216216
dependencies = [
217-
"build_helper",
218217
"cc",
219218
"cmake",
220219
"filetime",
@@ -258,10 +257,6 @@ dependencies = [
258257
"toml",
259258
]
260259

261-
[[package]]
262-
name = "build_helper"
263-
version = "0.1.0"
264-
265260
[[package]]
266261
name = "bump-stage0"
267262
version = "0.1.0"
@@ -3893,7 +3888,6 @@ dependencies = [
38933888
name = "rustc_llvm"
38943889
version = "0.0.0"
38953890
dependencies = [
3896-
"build_helper",
38973891
"cc",
38983892
"libc",
38993893
]

compiler/rustc_llvm/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ emscripten = []
1111
libc = "0.2.73"
1212

1313
[build-dependencies]
14-
build_helper = { path = "../../src/build_helper" }
1514
cc = "1.0.69"

compiler/rustc_llvm/build.rs

+66-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::env;
2+
use std::ffi::{OsStr, OsString};
3+
use std::fmt::Display;
24
use std::path::{Path, PathBuf};
3-
use std::process::Command;
4-
5-
use build_helper::{output, tracked_env_var_os};
5+
use std::process::{Command, Stdio};
66

77
fn detect_llvm_link() -> (&'static str, &'static str) {
88
// Force the link mode we want, preferring static by default, but
@@ -14,13 +14,74 @@ fn detect_llvm_link() -> (&'static str, &'static str) {
1414
}
1515
}
1616

17+
// Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
18+
// break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM
19+
// shared library, which means that when our freshly built llvm-config goes to load it's
20+
// associated LLVM, it actually loads the compiler's LLVM. In particular when building the first
21+
// compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from
22+
// the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't
23+
// perfect -- we might actually want to see something from Cargo's added library paths -- but
24+
// for now it works.
25+
fn restore_library_path() {
26+
let key = tracked_env_var_os("REAL_LIBRARY_PATH_VAR").expect("REAL_LIBRARY_PATH_VAR");
27+
if let Some(env) = tracked_env_var_os("REAL_LIBRARY_PATH") {
28+
env::set_var(&key, &env);
29+
} else {
30+
env::remove_var(&key);
31+
}
32+
}
33+
34+
/// Reads an environment variable and adds it to dependencies.
35+
/// Supposed to be used for all variables except those set for build scripts by cargo
36+
/// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts>
37+
fn tracked_env_var_os<K: AsRef<OsStr> + Display>(key: K) -> Option<OsString> {
38+
println!("cargo:rerun-if-env-changed={}", key);
39+
env::var_os(key)
40+
}
41+
42+
fn rerun_if_changed_anything_in_dir(dir: &Path) {
43+
let mut stack = dir
44+
.read_dir()
45+
.unwrap()
46+
.map(|e| e.unwrap())
47+
.filter(|e| &*e.file_name() != ".git")
48+
.collect::<Vec<_>>();
49+
while let Some(entry) = stack.pop() {
50+
let path = entry.path();
51+
if entry.file_type().unwrap().is_dir() {
52+
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
53+
} else {
54+
println!("cargo:rerun-if-changed={}", path.display());
55+
}
56+
}
57+
}
58+
59+
#[track_caller]
60+
fn output(cmd: &mut Command) -> String {
61+
let output = match cmd.stderr(Stdio::inherit()).output() {
62+
Ok(status) => status,
63+
Err(e) => {
64+
println!("\n\nfailed to execute command: {:?}\nerror: {}\n\n", cmd, e);
65+
std::process::exit(1);
66+
}
67+
};
68+
if !output.status.success() {
69+
panic!(
70+
"command did not execute successfully: {:?}\n\
71+
expected success, got: {}",
72+
cmd, output.status
73+
);
74+
}
75+
String::from_utf8(output.stdout).unwrap()
76+
}
77+
1778
fn main() {
1879
if tracked_env_var_os("RUST_CHECK").is_some() {
1980
// If we're just running `check`, there's no need for LLVM to be built.
2081
return;
2182
}
2283

23-
build_helper::restore_library_path();
84+
restore_library_path();
2485

2586
let target = env::var("TARGET").expect("TARGET was not set");
2687
let llvm_config =
@@ -160,7 +221,7 @@ fn main() {
160221
cfg.debug(false);
161222
}
162223

163-
build_helper::rerun_if_changed_anything_in_dir(Path::new("llvm-wrapper"));
224+
rerun_if_changed_anything_in_dir(Path::new("llvm-wrapper"));
164225
cfg.file("llvm-wrapper/PassWrapper.cpp")
165226
.file("llvm-wrapper/RustWrapper.cpp")
166227
.file("llvm-wrapper/ArchiveWrapper.cpp")

src/bootstrap/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ path = "bin/llvm-config-wrapper.rs"
3434
test = false
3535

3636
[dependencies]
37-
build_helper = { path = "../build_helper" }
3837
cmake = "0.1.38"
3938
filetime = "0.2"
4039
num_cpus = "1.0"

src/build_helper/lib.rs renamed to src/bootstrap/build_helper.rs

+2-46
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use std::ffi::{OsStr, OsString};
2-
use std::fmt::Display;
1+
use std::fs;
32
use std::path::{Path, PathBuf};
43
use std::process::{Command, Stdio};
54
use std::time::{SystemTime, UNIX_EPOCH};
6-
use std::{env, fs};
75

86
/// A helper macro to `unwrap` a result except also print out details like:
97
///
@@ -13,7 +11,6 @@ use std::{env, fs};
1311
///
1412
/// This is currently used judiciously throughout the build system rather than
1513
/// using a `Result` with `try!`, but this may change one day...
16-
#[macro_export]
1714
macro_rules! t {
1815
($e:expr) => {
1916
match $e {
@@ -29,31 +26,7 @@ macro_rules! t {
2926
}
3027
};
3128
}
32-
33-
/// Reads an environment variable and adds it to dependencies.
34-
/// Supposed to be used for all variables except those set for build scripts by cargo
35-
/// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts>
36-
pub fn tracked_env_var_os<K: AsRef<OsStr> + Display>(key: K) -> Option<OsString> {
37-
println!("cargo:rerun-if-env-changed={}", key);
38-
env::var_os(key)
39-
}
40-
41-
// Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
42-
// break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM
43-
// shared library, which means that when our freshly built llvm-config goes to load it's
44-
// associated LLVM, it actually loads the compiler's LLVM. In particular when building the first
45-
// compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from
46-
// the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't
47-
// perfect -- we might actually want to see something from Cargo's added library paths -- but
48-
// for now it works.
49-
pub fn restore_library_path() {
50-
let key = tracked_env_var_os("REAL_LIBRARY_PATH_VAR").expect("REAL_LIBRARY_PATH_VAR");
51-
if let Some(env) = tracked_env_var_os("REAL_LIBRARY_PATH") {
52-
env::set_var(&key, &env);
53-
} else {
54-
env::remove_var(&key);
55-
}
56-
}
29+
pub(crate) use t;
5730

5831
pub fn run(cmd: &mut Command, print_cmd_on_fail: bool) {
5932
if !try_run(cmd, print_cmd_on_fail) {
@@ -130,23 +103,6 @@ pub fn output(cmd: &mut Command) -> String {
130103
String::from_utf8(output.stdout).unwrap()
131104
}
132105

133-
pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
134-
let mut stack = dir
135-
.read_dir()
136-
.unwrap()
137-
.map(|e| e.unwrap())
138-
.filter(|e| &*e.file_name() != ".git")
139-
.collect::<Vec<_>>();
140-
while let Some(entry) = stack.pop() {
141-
let path = entry.path();
142-
if entry.file_type().unwrap().is_dir() {
143-
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
144-
} else {
145-
println!("cargo:rerun-if-changed={}", path.display());
146-
}
147-
}
148-
}
149-
150106
/// Returns the last-modified time for `path`, or zero if it doesn't exist.
151107
pub fn mtime(path: &Path) -> SystemTime {
152108
fs::metadata(path).and_then(|f| f.modified()).unwrap_or(UNIX_EPOCH)

src/bootstrap/builder.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use std::path::{Component, Path, PathBuf};
1111
use std::process::Command;
1212
use std::time::{Duration, Instant};
1313

14-
use build_helper::{output, t};
15-
14+
use crate::build_helper::{output, t};
1615
use crate::cache::{Cache, Interned, INTERNER};
1716
use crate::check;
1817
use crate::compile;

src/bootstrap/cc_detect.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ use std::path::{Path, PathBuf};
2626
use std::process::Command;
2727
use std::{env, iter};
2828

29-
use build_helper::output;
30-
29+
use crate::build_helper::output;
3130
use crate::config::{Target, TargetSelection};
3231
use crate::{Build, CLang, GitRepo};
3332

src/bootstrap/channel.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use std::path::Path;
99
use std::process::Command;
1010

11-
use build_helper::output;
12-
11+
use crate::build_helper::output;
1312
use crate::Build;
1413

1514
pub enum GitInfo {

src/bootstrap/clean.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use std::fs;
99
use std::io::{self, ErrorKind};
1010
use std::path::Path;
1111

12-
use build_helper::t;
13-
12+
use crate::build_helper::t;
1413
use crate::Build;
1514

1615
pub fn clean(build: &Build, all: bool) {

src/bootstrap/compile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use std::path::{Path, PathBuf};
1616
use std::process::{exit, Command, Stdio};
1717
use std::str;
1818

19-
use build_helper::{output, t, up_to_date};
2019
use serde::Deserialize;
2120

21+
use crate::build_helper::{output, t, up_to_date};
2222
use crate::builder::Cargo;
2323
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
2424
use crate::cache::{Interned, INTERNER};

src/bootstrap/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ use std::fs;
1212
use std::path::{Path, PathBuf};
1313
use std::str::FromStr;
1414

15+
use crate::build_helper::t;
1516
use crate::builder::TaskPath;
1617
use crate::cache::{Interned, INTERNER};
1718
use crate::channel::GitInfo;
1819
pub use crate::flags::Subcommand;
1920
use crate::flags::{Color, Flags};
2021
use crate::util::exe;
21-
use build_helper::t;
2222
use serde::Deserialize;
2323

2424
macro_rules! check_ci_llvm {

src/bootstrap/dist.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use std::fs;
1414
use std::path::{Path, PathBuf};
1515
use std::process::Command;
1616

17-
use build_helper::{output, t};
18-
17+
use crate::build_helper::{output, t};
1918
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
2019
use crate::cache::{Interned, INTERNER};
2120
use crate::compile;

src/bootstrap/doc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ use std::fs;
1212
use std::io;
1313
use std::path::{Path, PathBuf};
1414

15-
use crate::Mode;
16-
use build_helper::{t, up_to_date};
17-
15+
use crate::build_helper::{t, up_to_date};
1816
use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
1917
use crate::cache::{Interned, INTERNER};
2018
use crate::compile;
2119
use crate::config::{Config, TargetSelection};
2220
use crate::tool::{self, prepare_tool_cargo, SourceType, Tool};
2321
use crate::util::symlink_dir;
22+
use crate::Mode;
2423

2524
macro_rules! submodule_helper {
2625
($path:expr, submodule) => {

src/bootstrap/flags.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::env;
77
use std::path::PathBuf;
88
use std::process;
99

10-
use build_helper::t;
1110
use getopts::Options;
1211

12+
use crate::build_helper::t;
1313
use crate::builder::Builder;
1414
use crate::config::{Config, TargetSelection};
1515
use crate::setup::Profile;

src/bootstrap/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Runs rustfmt on the repository.
22
3+
use crate::build_helper::{output, t};
34
use crate::Build;
4-
use build_helper::{output, t};
55
use ignore::WalkBuilder;
66
use std::collections::VecDeque;
77
use std::path::{Path, PathBuf};

src/bootstrap/install.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::fs;
88
use std::path::{Component, PathBuf};
99
use std::process::Command;
1010

11-
use build_helper::t;
11+
use crate::build_helper::t;
1212

1313
use crate::dist::{self, sanitize_sh};
1414
use crate::tarball::GeneratedTarball;

src/bootstrap/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ use std::os::unix::fs::symlink as symlink_file;
116116
#[cfg(windows)]
117117
use std::os::windows::fs::symlink_file;
118118

119-
use build_helper::{mtime, output, run, run_suppressed, t, try_run, try_run_suppressed};
120119
use filetime::FileTime;
121120

121+
use crate::build_helper::{mtime, output, run, run_suppressed, t, try_run, try_run_suppressed};
122122
use crate::builder::Kind;
123123
use crate::config::{LlvmLibunwind, TargetSelection};
124124
use crate::util::{exe, libdir, CiEnv};
125125

126+
mod build_helper;
126127
mod builder;
127128
mod cache;
128129
mod cc_detect;

src/bootstrap/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::path::PathBuf;
22
use std::process::Command;
33

4-
use build_helper::output;
54
use serde::Deserialize;
65

6+
use crate::build_helper::output;
77
use crate::cache::INTERNER;
88
use crate::{Build, Crate};
99

src/bootstrap/native.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ use std::io;
1616
use std::path::{Path, PathBuf};
1717
use std::process::Command;
1818

19-
use build_helper::{output, t};
20-
19+
use crate::build_helper::up_to_date;
20+
use crate::build_helper::{output, t};
2121
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
2222
use crate::config::TargetSelection;
2323
use crate::util::{self, exe};
2424
use crate::{CLang, GitRepo};
25-
use build_helper::up_to_date;
2625

2726
pub struct Meta {
2827
stamp: HashStamp,

src/bootstrap/run.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::build_helper::output;
12
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
23
use crate::dist::distdir;
34
use crate::tool::Tool;
4-
use build_helper::output;
55
use std::process::Command;
66

77
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

src/bootstrap/sanity.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use std::fs;
1515
use std::path::PathBuf;
1616
use std::process::Command;
1717

18-
use build_helper::output;
19-
18+
use crate::build_helper::output;
2019
use crate::cache::INTERNER;
2120
use crate::config::Target;
2221
use crate::Build;

0 commit comments

Comments
 (0)