Skip to content

Commit f31c01c

Browse files
committed
Abstract LLVM building from bootstrap
This deduplicates the LLVM building functionality from compile.rs and check.rs.
1 parent 49b6773 commit f31c01c

File tree

2 files changed

+65
-94
lines changed

2 files changed

+65
-94
lines changed

src/bootstrap/check.rs

+4-46
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
//! Implementation of compiling the compiler and standard library, in "check" mode.
1212
1313
use compile::{run_cargo, std_cargo, test_cargo, rustc_cargo, rustc_cargo_env, add_to_sysroot};
14-
use compile::compiler_file;
14+
use compile::build_codegen_backend;
1515
use builder::{RunConfig, Builder, ShouldRun, Step};
1616
use {Compiler, Mode};
17-
use native;
1817
use cache::{INTERNER, Interned};
1918
use std::path::PathBuf;
2019

@@ -136,60 +135,19 @@ impl Step for CodegenBackend {
136135
let build = builder.build;
137136
let compiler = builder.compiler(0, build.build);
138137
let target = self.target;
138+
let backend = self.backend;
139139

140140
let mut cargo = builder.cargo(compiler, Mode::Librustc, target, "check");
141141
let mut features = build.rustc_features().to_string();
142142
cargo.arg("--manifest-path").arg(build.src.join("src/librustc_trans/Cargo.toml"));
143143
rustc_cargo_env(build, &mut cargo);
144144

145-
match &*self.backend {
146-
"llvm" | "emscripten" => {
147-
// Build LLVM for our target. This will implicitly build the
148-
// host LLVM if necessary.
149-
let llvm_config = builder.ensure(native::Llvm {
150-
target,
151-
emscripten: self.backend == "emscripten",
152-
});
153-
154-
if self.backend == "emscripten" {
155-
features.push_str(" emscripten");
156-
}
157-
158-
// Pass down configuration from the LLVM build into the build of
159-
// librustc_llvm and librustc_trans.
160-
if build.is_rust_llvm(target) {
161-
cargo.env("LLVM_RUSTLLVM", "1");
162-
}
163-
cargo.env("LLVM_CONFIG", &llvm_config);
164-
if self.backend != "emscripten" {
165-
let target_config = build.config.target_config.get(&target);
166-
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
167-
cargo.env("CFG_LLVM_ROOT", s);
168-
}
169-
}
170-
// Building with a static libstdc++ is only supported on linux right now,
171-
// not for MSVC or macOS
172-
if build.config.llvm_static_stdcpp &&
173-
!target.contains("freebsd") &&
174-
!target.contains("windows") &&
175-
!target.contains("apple") {
176-
let file = compiler_file(build,
177-
build.cxx(target).unwrap(),
178-
target,
179-
"libstdc++.a");
180-
cargo.env("LLVM_STATIC_STDCPP", file);
181-
}
182-
if build.config.llvm_link_shared {
183-
cargo.env("LLVM_LINK_SHARED", "1");
184-
}
185-
}
186-
_ => panic!("unknown backend: {}", self.backend),
187-
}
145+
features += &build_codegen_backend(&builder, &mut cargo, &compiler, target, backend);
188146

189147
let _folder = build.fold_output(|| format!("stage{}-rustc_trans", compiler.stage));
190148
run_cargo(build,
191149
cargo.arg("--features").arg(features),
192-
&codegen_backend_stamp(build, compiler, target, self.backend),
150+
&codegen_backend_stamp(build, compiler, target, backend),
193151
true);
194152
}
195153
}

src/bootstrap/compile.rs

+61-48
Original file line numberDiff line numberDiff line change
@@ -621,14 +621,15 @@ impl Step for CodegenBackend {
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(build, 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
///

0 commit comments

Comments
 (0)