Skip to content

Commit

Permalink
Rollup merge of rust-lang#94480 - bjorn3:no_build_helper, r=Mark-Simu…
Browse files Browse the repository at this point in the history
…lacrum

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.
  • Loading branch information
Dylan-DPC authored Mar 4, 2022
2 parents 58e9878 + 3b26b91 commit 03a9aaa
Show file tree
Hide file tree
Showing 27 changed files with 233 additions and 258 deletions.
6 changes: 0 additions & 6 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ dependencies = [
name = "bootstrap"
version = "0.0.0"
dependencies = [
"build_helper",
"cc",
"cmake",
"filetime",
Expand Down Expand Up @@ -258,10 +257,6 @@ dependencies = [
"toml",
]

[[package]]
name = "build_helper"
version = "0.1.0"

[[package]]
name = "bump-stage0"
version = "0.1.0"
Expand Down Expand Up @@ -3893,7 +3888,6 @@ dependencies = [
name = "rustc_llvm"
version = "0.0.0"
dependencies = [
"build_helper",
"cc",
"libc",
]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ emscripten = []
libc = "0.2.73"

[build-dependencies]
build_helper = { path = "../../src/build_helper" }
cc = "1.0.69"
71 changes: 66 additions & 5 deletions compiler/rustc_llvm/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::env;
use std::ffi::{OsStr, OsString};
use std::fmt::Display;
use std::path::{Path, PathBuf};
use std::process::Command;

use build_helper::{output, tracked_env_var_os};
use std::process::{Command, Stdio};

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

// Because Cargo adds the compiler's dylib path to our library search path, llvm-config may
// break: the dylib path for the compiler, as of this writing, contains a copy of the LLVM
// shared library, which means that when our freshly built llvm-config goes to load it's
// associated LLVM, it actually loads the compiler's LLVM. In particular when building the first
// compiler (i.e., in stage 0) that's a problem, as the compiler's LLVM is likely different from
// the one we want to use. As such, we restore the environment to what bootstrap saw. This isn't
// perfect -- we might actually want to see something from Cargo's added library paths -- but
// for now it works.
fn restore_library_path() {
let key = tracked_env_var_os("REAL_LIBRARY_PATH_VAR").expect("REAL_LIBRARY_PATH_VAR");
if let Some(env) = tracked_env_var_os("REAL_LIBRARY_PATH") {
env::set_var(&key, &env);
} else {
env::remove_var(&key);
}
}

/// Reads an environment variable and adds it to dependencies.
/// Supposed to be used for all variables except those set for build scripts by cargo
/// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts>
fn tracked_env_var_os<K: AsRef<OsStr> + Display>(key: K) -> Option<OsString> {
println!("cargo:rerun-if-env-changed={}", key);
env::var_os(key)
}

fn rerun_if_changed_anything_in_dir(dir: &Path) {
let mut stack = dir
.read_dir()
.unwrap()
.map(|e| e.unwrap())
.filter(|e| &*e.file_name() != ".git")
.collect::<Vec<_>>();
while let Some(entry) = stack.pop() {
let path = entry.path();
if entry.file_type().unwrap().is_dir() {
stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
} else {
println!("cargo:rerun-if-changed={}", path.display());
}
}
}

#[track_caller]
fn output(cmd: &mut Command) -> String {
let output = match cmd.stderr(Stdio::inherit()).output() {
Ok(status) => status,
Err(e) => {
println!("\n\nfailed to execute command: {:?}\nerror: {}\n\n", cmd, e);
std::process::exit(1);
}
};
if !output.status.success() {
panic!(
"command did not execute successfully: {:?}\n\
expected success, got: {}",
cmd, output.status
);
}
String::from_utf8(output.stdout).unwrap()
}

fn main() {
if tracked_env_var_os("RUST_CHECK").is_some() {
// If we're just running `check`, there's no need for LLVM to be built.
return;
}

build_helper::restore_library_path();
restore_library_path();

let target = env::var("TARGET").expect("TARGET was not set");
let llvm_config =
Expand Down Expand Up @@ -160,7 +221,7 @@ fn main() {
cfg.debug(false);
}

build_helper::rerun_if_changed_anything_in_dir(Path::new("llvm-wrapper"));
rerun_if_changed_anything_in_dir(Path::new("llvm-wrapper"));
cfg.file("llvm-wrapper/PassWrapper.cpp")
.file("llvm-wrapper/RustWrapper.cpp")
.file("llvm-wrapper/ArchiveWrapper.cpp")
Expand Down
1 change: 0 additions & 1 deletion src/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ path = "bin/llvm-config-wrapper.rs"
test = false

[dependencies]
build_helper = { path = "../build_helper" }
cmake = "0.1.38"
filetime = "0.2"
num_cpus = "1.0"
Expand Down
4 changes: 1 addition & 3 deletions src/bootstrap/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use std::path::{Component, Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};

use build_helper::{output, t};

use crate::cache::{Cache, Interned, INTERNER};
use crate::check;
use crate::compile;
Expand All @@ -25,7 +23,7 @@ use crate::native;
use crate::run;
use crate::test;
use crate::tool::{self, SourceType};
use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir};
use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir, output, t};
use crate::{Build, CLang, DocTests, GitRepo, Mode};

pub use crate::Compiler;
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/cc_detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, iter};

use build_helper::output;

use crate::config::{Target, TargetSelection};
use crate::util::output;
use crate::{Build, CLang, GitRepo};

// The `cc` crate doesn't provide a way to obtain a path to the detected archiver,
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use std::path::Path;
use std::process::Command;

use build_helper::output;

use crate::util::output;
use crate::Build;

pub enum GitInfo {
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use std::fs;
use std::io::{self, ErrorKind};
use std::path::Path;

use build_helper::t;

use crate::util::t;
use crate::Build;

pub fn clean(build: &Build, all: bool) {
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::path::{Path, PathBuf};
use std::process::{exit, Command, Stdio};
use std::str;

use build_helper::{output, t, up_to_date};
use serde::Deserialize;

use crate::builder::Cargo;
Expand All @@ -26,7 +25,7 @@ use crate::config::{LlvmLibunwind, TargetSelection};
use crate::dist;
use crate::native;
use crate::tool::SourceType;
use crate::util::{exe, is_debug_info, is_dylib, symlink_dir};
use crate::util::{exe, is_debug_info, is_dylib, output, symlink_dir, t, up_to_date};
use crate::LLVM_TOOLS;
use crate::{CLang, Compiler, DependencyType, GitRepo, Mode};

Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use crate::cache::{Interned, INTERNER};
use crate::channel::GitInfo;
pub use crate::flags::Subcommand;
use crate::flags::{Color, Flags};
use crate::util::exe;
use build_helper::t;
use crate::util::{exe, t};
use serde::Deserialize;

macro_rules! check_ci_llvm {
Expand Down
4 changes: 1 addition & 3 deletions src/bootstrap/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

use build_helper::{output, t};

use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
use crate::cache::{Interned, INTERNER};
use crate::compile;
use crate::config::TargetSelection;
use crate::tarball::{GeneratedTarball, OverlayKind, Tarball};
use crate::tool::{self, Tool};
use crate::util::{exe, is_dylib, timeit};
use crate::util::{exe, is_dylib, output, t, timeit};
use crate::{Compiler, DependencyType, Mode, LLVM_TOOLS};

pub fn pkgname(builder: &Builder<'_>, component: &str) -> String {
Expand Down
6 changes: 2 additions & 4 deletions src/bootstrap/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ use std::fs;
use std::io;
use std::path::{Path, PathBuf};

use crate::Mode;
use build_helper::{t, up_to_date};

use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
use crate::cache::{Interned, INTERNER};
use crate::compile;
use crate::config::{Config, TargetSelection};
use crate::tool::{self, prepare_tool_cargo, SourceType, Tool};
use crate::util::symlink_dir;
use crate::util::{symlink_dir, t, up_to_date};
use crate::Mode;

macro_rules! submodule_helper {
($path:expr, submodule) => {
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use std::env;
use std::path::PathBuf;
use std::process;

use build_helper::t;
use getopts::Options;

use crate::builder::Builder;
use crate::config::{Config, TargetSelection};
use crate::setup::Profile;
use crate::util::t;
use crate::{Build, DocTests};

pub enum Color {
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Runs rustfmt on the repository.

use crate::util::{output, t};
use crate::Build;
use build_helper::{output, t};
use ignore::WalkBuilder;
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::fs;
use std::path::{Component, PathBuf};
use std::process::Command;

use build_helper::t;
use crate::util::t;

use crate::dist::{self, sanitize_sh};
use crate::tarball::GeneratedTarball;
Expand Down
5 changes: 3 additions & 2 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ use std::os::unix::fs::symlink as symlink_file;
#[cfg(windows)]
use std::os::windows::fs::symlink_file;

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

use crate::builder::Kind;
use crate::config::{LlvmLibunwind, TargetSelection};
use crate::util::{exe, libdir, CiEnv};
use crate::util::{
exe, libdir, mtime, output, run, run_suppressed, t, try_run, try_run_suppressed, CiEnv,
};

mod builder;
mod cache;
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::path::PathBuf;
use std::process::Command;

use build_helper::output;
use serde::Deserialize;

use crate::cache::INTERNER;
use crate::util::output;
use crate::{Build, Crate};

#[derive(Deserialize)]
Expand Down
5 changes: 1 addition & 4 deletions src/bootstrap/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;

use build_helper::{output, t};

use crate::builder::{Builder, RunConfig, ShouldRun, Step};
use crate::config::TargetSelection;
use crate::util::{self, exe};
use crate::util::{self, exe, output, t, up_to_date};
use crate::{CLang, GitRepo};
use build_helper::up_to_date;

pub struct Meta {
stamp: HashStamp,
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
use crate::dist::distdir;
use crate::tool::Tool;
use build_helper::output;
use crate::util::output;
use std::process::Command;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ use std::fs;
use std::path::PathBuf;
use std::process::Command;

use build_helper::output;

use crate::cache::INTERNER;
use crate::config::Target;
use crate::util::output;
use crate::Build;

pub struct Finder {
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::{
process::Command,
};

use build_helper::t;

use crate::builder::Builder;
use crate::util::t;

#[derive(Copy, Clone)]
pub(crate) enum OverlayKind {
Expand Down
8 changes: 2 additions & 6 deletions src/bootstrap/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use std::iter;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use build_helper::{self, output, t};

use crate::builder::{Builder, Compiler, Kind, RunConfig, ShouldRun, Step};
use crate::cache::Interned;
use crate::compile;
Expand All @@ -22,7 +20,7 @@ use crate::flags::Subcommand;
use crate::native;
use crate::tool::{self, SourceType, Tool};
use crate::toolstate::ToolState;
use crate::util::{self, add_link_lib_path, dylib_path, dylib_path_var};
use crate::util::{self, add_link_lib_path, dylib_path, dylib_path_var, output, t};
use crate::Crate as CargoCrate;
use crate::{envify, CLang, DocTests, GitRepo, Mode};

Expand Down Expand Up @@ -2306,9 +2304,7 @@ impl Step for Distcheck {
.current_dir(&dir),
);
builder.run(
Command::new(build_helper::make(&builder.config.build.triple))
.arg("check")
.current_dir(&dir),
Command::new(util::make(&builder.config.build.triple)).arg("check").current_dir(&dir),
);

// Now make sure that rust-src has all of libstd's dependencies
Expand Down
Loading

0 comments on commit 03a9aaa

Please sign in to comment.