Skip to content

Commit 78fc510

Browse files
committed
Auto merge of #49890 - varkor:xpy-check-rustc_trans, r=alexcrichton
Add rustc_trans to x.py check r? @Mark-Simulacrum I looked at `bootstrap/compile.rs` and `bootstrap/check.rs` to try to work out which steps were appropriate, but I'm sure I've overlooked some details here, so it's worth checking carefully I've got all the steps right (e.g. I wasn't sure whether we want to build LLVM if necessary with `x.py check`, though I thought it was probably better to than to not). From a quick test, it seems to be working, though.
2 parents 883bf4b + 86acb09 commit 78fc510

File tree

4 files changed

+138
-55
lines changed

4 files changed

+138
-55
lines changed

src/bootstrap/builder.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<'a> Builder<'a> {
310310
tool::Compiletest, tool::RemoteTestServer, tool::RemoteTestClient,
311311
tool::RustInstaller, tool::Cargo, tool::Rls, tool::Rustdoc, tool::Clippy,
312312
native::Llvm, tool::Rustfmt, tool::Miri, native::Lld),
313-
Kind::Check => describe!(check::Std, check::Test, check::Rustc),
313+
Kind::Check => describe!(check::Std, check::Test, check::Rustc, check::CodegenBackend),
314314
Kind::Test => describe!(test::Tidy, test::Bootstrap, test::Ui, test::RunPass,
315315
test::CompileFail, test::ParseFail, test::RunFail, test::RunPassValgrind,
316316
test::MirOpt, test::Codegen, test::CodegenUnits, test::Incremental, test::Debuginfo,
@@ -552,6 +552,12 @@ impl<'a> Builder<'a> {
552552
.arg("--target")
553553
.arg(target);
554554

555+
// Set a flag for `check` so that certain build scripts can do less work
556+
// (e.g. not building/requiring LLVM).
557+
if cmd == "check" {
558+
cargo.env("RUST_CHECK", "1");
559+
}
560+
555561
// If we were invoked from `make` then that's already got a jobserver
556562
// set up for us so no need to tell Cargo about jobs all over again.
557563
if env::var_os("MAKEFLAGS").is_none() && env::var_os("MFLAGS").is_none() {
@@ -836,7 +842,7 @@ impl<'a> Builder<'a> {
836842
cargo
837843
}
838844

839-
/// Ensure that a given step is built, returning it's output. This will
845+
/// Ensure that a given step is built, returning its output. This will
840846
/// cache the step, so it is safe (and good!) to call this as often as
841847
/// needed to ensure that all dependencies are built.
842848
pub fn ensure<S: Step>(&'a self, step: S) -> S::Output {

src/bootstrap/check.rs

+58-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
//! Implementation of compiling the compiler and standard library, in "check" mode.
1212
13-
use compile::{run_cargo, std_cargo, test_cargo, rustc_cargo, add_to_sysroot};
13+
use compile::{run_cargo, std_cargo, test_cargo, rustc_cargo, rustc_cargo_env, add_to_sysroot};
1414
use builder::{RunConfig, Builder, ShouldRun, Step};
1515
use {Compiler, Mode};
16-
use cache::Interned;
16+
use cache::{INTERNER, Interned};
1717
use std::path::PathBuf;
1818

1919
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
@@ -104,6 +104,52 @@ impl Step for Rustc {
104104
}
105105
}
106106

107+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
108+
pub struct CodegenBackend {
109+
pub target: Interned<String>,
110+
pub backend: Interned<String>,
111+
}
112+
113+
impl Step for CodegenBackend {
114+
type Output = ();
115+
const ONLY_HOSTS: bool = true;
116+
const DEFAULT: bool = true;
117+
118+
fn should_run(run: ShouldRun) -> ShouldRun {
119+
run.all_krates("rustc_trans")
120+
}
121+
122+
fn make_run(run: RunConfig) {
123+
let backend = run.builder.config.rust_codegen_backends.get(0);
124+
let backend = backend.cloned().unwrap_or_else(|| {
125+
INTERNER.intern_str("llvm")
126+
});
127+
run.builder.ensure(CodegenBackend {
128+
target: run.target,
129+
backend,
130+
});
131+
}
132+
133+
fn run(self, builder: &Builder) {
134+
let compiler = builder.compiler(0, builder.config.build);
135+
let target = self.target;
136+
let backend = self.backend;
137+
138+
let mut cargo = builder.cargo(compiler, Mode::Librustc, target, "check");
139+
let features = builder.rustc_features().to_string();
140+
cargo.arg("--manifest-path").arg(builder.src.join("src/librustc_trans/Cargo.toml"));
141+
rustc_cargo_env(builder, &mut cargo);
142+
143+
// We won't build LLVM if it's not available, as it shouldn't affect `check`.
144+
145+
let _folder = builder.fold_output(|| format!("stage{}-rustc_trans", compiler.stage));
146+
run_cargo(builder,
147+
cargo.arg("--features").arg(features),
148+
&codegen_backend_stamp(builder, compiler, target, backend),
149+
true);
150+
}
151+
}
152+
107153
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
108154
pub struct Test {
109155
pub target: Interned<String>,
@@ -161,3 +207,13 @@ pub fn libtest_stamp(builder: &Builder, compiler: Compiler, target: Interned<Str
161207
pub fn librustc_stamp(builder: &Builder, compiler: Compiler, target: Interned<String>) -> PathBuf {
162208
builder.cargo_out(compiler, Mode::Librustc, target).join(".librustc-check.stamp")
163209
}
210+
211+
/// Cargo's output path for librustc_trans in a given stage, compiled by a particular
212+
/// compiler for the specified target and backend.
213+
fn codegen_backend_stamp(builder: &Builder,
214+
compiler: Compiler,
215+
target: Interned<String>,
216+
backend: Interned<String>) -> PathBuf {
217+
builder.cargo_out(compiler, Mode::Librustc, target)
218+
.join(format!(".librustc_trans-{}-check.stamp", backend))
219+
}

src/bootstrap/compile.rs

+66-51
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ pub fn rustc_cargo(builder: &Builder, cargo: &mut Command) {
519519
rustc_cargo_env(builder, cargo);
520520
}
521521

522-
fn rustc_cargo_env(builder: &Builder, cargo: &mut Command) {
522+
pub fn rustc_cargo_env(builder: &Builder, cargo: &mut Command) {
523523
// Set some configuration variables picked up by build scripts and
524524
// the compiler alike
525525
cargo.env("CFG_RELEASE", builder.rust_release())
@@ -614,21 +614,22 @@ impl Step for CodegenBackend {
614614
run.builder.ensure(CodegenBackend {
615615
compiler: run.builder.compiler(run.builder.top_stage, run.host),
616616
target: run.target,
617-
backend
617+
backend,
618618
});
619619
}
620620

621621
fn run(self, builder: &Builder) {
622622
let compiler = self.compiler;
623623
let target = self.target;
624+
let backend = self.backend;
624625

625626
builder.ensure(Rustc { compiler, target });
626627

627628
if builder.force_use_stage1(compiler, target) {
628629
builder.ensure(CodegenBackend {
629630
compiler: builder.compiler(1, builder.config.build),
630631
target,
631-
backend: self.backend,
632+
backend,
632633
});
633634
return;
634635
}
@@ -639,52 +640,7 @@ impl Step for CodegenBackend {
639640
.arg(builder.src.join("src/librustc_trans/Cargo.toml"));
640641
rustc_cargo_env(builder, &mut cargo);
641642

642-
match &*self.backend {
643-
"llvm" | "emscripten" => {
644-
// Build LLVM for our target. This will implicitly build the
645-
// host LLVM if necessary.
646-
let llvm_config = builder.ensure(native::Llvm {
647-
target,
648-
emscripten: self.backend == "emscripten",
649-
});
650-
651-
if self.backend == "emscripten" {
652-
features.push_str(" emscripten");
653-
}
654-
655-
builder.info(&format!("Building stage{} codegen artifacts ({} -> {}, {})",
656-
compiler.stage, &compiler.host, target, self.backend));
657-
658-
// Pass down configuration from the LLVM build into the build of
659-
// librustc_llvm and librustc_trans.
660-
if builder.is_rust_llvm(target) {
661-
cargo.env("LLVM_RUSTLLVM", "1");
662-
}
663-
cargo.env("LLVM_CONFIG", &llvm_config);
664-
if self.backend != "emscripten" {
665-
let target_config = builder.config.target_config.get(&target);
666-
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
667-
cargo.env("CFG_LLVM_ROOT", s);
668-
}
669-
}
670-
// Building with a static libstdc++ is only supported on linux right now,
671-
// not for MSVC or macOS
672-
if builder.config.llvm_static_stdcpp &&
673-
!target.contains("freebsd") &&
674-
!target.contains("windows") &&
675-
!target.contains("apple") {
676-
let file = compiler_file(builder,
677-
builder.cxx(target).unwrap(),
678-
target,
679-
"libstdc++.a");
680-
cargo.env("LLVM_STATIC_STDCPP", file);
681-
}
682-
if builder.config.llvm_link_shared {
683-
cargo.env("LLVM_LINK_SHARED", "1");
684-
}
685-
}
686-
_ => panic!("unknown backend: {}", self.backend),
687-
}
643+
features += &build_codegen_backend(&builder, &mut cargo, &compiler, target, backend);
688644

689645
let tmp_stamp = builder.cargo_out(compiler, Mode::Librustc, target)
690646
.join(".tmp.stamp");
@@ -711,12 +667,69 @@ impl Step for CodegenBackend {
711667
codegen_backend.display(),
712668
f.display());
713669
}
714-
let stamp = codegen_backend_stamp(builder, compiler, target, self.backend);
670+
let stamp = codegen_backend_stamp(builder, compiler, target, backend);
715671
let codegen_backend = codegen_backend.to_str().unwrap();
716672
t!(t!(File::create(&stamp)).write_all(codegen_backend.as_bytes()));
717673
}
718674
}
719675

676+
pub fn build_codegen_backend(builder: &Builder,
677+
cargo: &mut Command,
678+
compiler: &Compiler,
679+
target: Interned<String>,
680+
backend: Interned<String>) -> String {
681+
let mut features = String::new();
682+
683+
match &*backend {
684+
"llvm" | "emscripten" => {
685+
// Build LLVM for our target. This will implicitly build the
686+
// host LLVM if necessary.
687+
let llvm_config = builder.ensure(native::Llvm {
688+
target,
689+
emscripten: backend == "emscripten",
690+
});
691+
692+
if backend == "emscripten" {
693+
features.push_str(" emscripten");
694+
}
695+
696+
builder.info(&format!("Building stage{} codegen artifacts ({} -> {}, {})",
697+
compiler.stage, &compiler.host, target, backend));
698+
699+
// Pass down configuration from the LLVM build into the build of
700+
// librustc_llvm and librustc_trans.
701+
if builder.is_rust_llvm(target) {
702+
cargo.env("LLVM_RUSTLLVM", "1");
703+
}
704+
cargo.env("LLVM_CONFIG", &llvm_config);
705+
if backend != "emscripten" {
706+
let target_config = builder.config.target_config.get(&target);
707+
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
708+
cargo.env("CFG_LLVM_ROOT", s);
709+
}
710+
}
711+
// Building with a static libstdc++ is only supported on linux right now,
712+
// not for MSVC or macOS
713+
if builder.config.llvm_static_stdcpp &&
714+
!target.contains("freebsd") &&
715+
!target.contains("windows") &&
716+
!target.contains("apple") {
717+
let file = compiler_file(builder,
718+
builder.cxx(target).unwrap(),
719+
target,
720+
"libstdc++.a");
721+
cargo.env("LLVM_STATIC_STDCPP", file);
722+
}
723+
if builder.config.llvm_link_shared {
724+
cargo.env("LLVM_LINK_SHARED", "1");
725+
}
726+
}
727+
_ => panic!("unknown backend: {}", backend),
728+
}
729+
730+
features
731+
}
732+
720733
/// Creates the `codegen-backends` folder for a compiler that's about to be
721734
/// assembled as a complete compiler.
722735
///
@@ -795,6 +808,8 @@ pub fn librustc_stamp(builder: &Builder, compiler: Compiler, target: Interned<St
795808
builder.cargo_out(compiler, Mode::Librustc, target).join(".librustc.stamp")
796809
}
797810

811+
/// Cargo's output path for librustc_trans in a given stage, compiled by a particular
812+
/// compiler for the specified target and backend.
798813
fn codegen_backend_stamp(builder: &Builder,
799814
compiler: Compiler,
800815
target: Interned<String>,
@@ -803,7 +818,7 @@ fn codegen_backend_stamp(builder: &Builder,
803818
.join(format!(".librustc_trans-{}.stamp", backend))
804819
}
805820

806-
fn compiler_file(builder: &Builder,
821+
pub fn compiler_file(builder: &Builder,
807822
compiler: &Path,
808823
target: Interned<String>,
809824
file: &str) -> PathBuf {

src/librustc_llvm/build.rs

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ fn detect_llvm_link() -> (&'static str, &'static str) {
2828
}
2929

3030
fn main() {
31+
if env::var_os("RUST_CHECK").is_some() {
32+
// If we're just running `check`, there's no need for LLVM to be built.
33+
println!("cargo:rerun-if-env-changed=RUST_CHECK");
34+
return;
35+
}
36+
3137
let target = env::var("TARGET").expect("TARGET was not set");
3238
let llvm_config = env::var_os("LLVM_CONFIG")
3339
.map(PathBuf::from)

0 commit comments

Comments
 (0)