Skip to content

Commit 5445bbe

Browse files
committed
Create a typed wrapper for codegen backends
To avoid representing them just with strings.
1 parent 32e7a4b commit 5445bbe

File tree

13 files changed

+142
-81
lines changed

13 files changed

+142
-81
lines changed

src/bootstrap/src/core/build_steps/check.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::core::builder::{
1313
};
1414
use crate::core::config::TargetSelection;
1515
use crate::utils::build_stamp::{self, BuildStamp};
16-
use crate::{Compiler, Mode, Subcommand};
16+
use crate::{CodegenBackendKind, Compiler, Mode, Subcommand};
1717

1818
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1919
pub struct Std {
@@ -312,7 +312,7 @@ fn prepare_compiler_for_check(
312312
pub struct CodegenBackend {
313313
pub build_compiler: Compiler,
314314
pub target: TargetSelection,
315-
pub backend: &'static str,
315+
pub backend: CodegenBackendKind,
316316
}
317317

318318
impl Step for CodegenBackend {
@@ -327,14 +327,14 @@ impl Step for CodegenBackend {
327327
fn make_run(run: RunConfig<'_>) {
328328
// FIXME: only check the backend(s) that were actually selected in run.paths
329329
let build_compiler = prepare_compiler_for_check(run.builder, run.target, Mode::Codegen);
330-
for &backend in &["cranelift", "gcc"] {
330+
for backend in [CodegenBackendKind::Cranelift, CodegenBackendKind::Gcc] {
331331
run.builder.ensure(CodegenBackend { build_compiler, target: run.target, backend });
332332
}
333333
}
334334

335335
fn run(self, builder: &Builder<'_>) {
336336
// FIXME: remove once https://github.com/rust-lang/rust/issues/112393 is resolved
337-
if builder.build.config.vendor && self.backend == "gcc" {
337+
if builder.build.config.vendor && self.backend.is_gcc() {
338338
println!("Skipping checking of `rustc_codegen_gcc` with vendoring enabled.");
339339
return;
340340
}
@@ -354,19 +354,22 @@ impl Step for CodegenBackend {
354354

355355
cargo
356356
.arg("--manifest-path")
357-
.arg(builder.src.join(format!("compiler/rustc_codegen_{backend}/Cargo.toml")));
357+
.arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend.name())));
358358
rustc_cargo_env(builder, &mut cargo, target);
359359

360-
let _guard = builder.msg_check(format!("rustc_codegen_{backend}"), target, None);
360+
let _guard = builder.msg_check(format!("rustc_codegen_{}", backend.name()), target, None);
361361

362-
let stamp = build_stamp::codegen_backend_stamp(builder, build_compiler, target, backend)
362+
let stamp = build_stamp::codegen_backend_stamp(builder, build_compiler, target, &backend)
363363
.with_prefix("check");
364364

365365
run_cargo(builder, cargo, builder.config.free_args.clone(), &stamp, vec![], true, false);
366366
}
367367

368368
fn metadata(&self) -> Option<StepMetadata> {
369-
Some(StepMetadata::check(self.backend, self.target).built_by(self.build_compiler))
369+
Some(
370+
StepMetadata::check(&format!("cg_{}", self.backend.name()), self.target)
371+
.built_by(self.build_compiler),
372+
)
370373
}
371374
}
372375

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ use crate::utils::exec::command;
3333
use crate::utils::helpers::{
3434
exe, get_clang_cl_resource_dir, is_debug_info, is_dylib, symlink_dir, t, up_to_date,
3535
};
36-
use crate::{CLang, Compiler, DependencyType, FileType, GitRepo, LLVM_TOOLS, Mode, debug, trace};
36+
use crate::{
37+
CLang, CodegenBackendKind, Compiler, DependencyType, FileType, GitRepo, LLVM_TOOLS, Mode,
38+
debug, trace,
39+
};
3740

3841
/// Build a standard library for the given `target` using the given `compiler`.
3942
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -1330,7 +1333,7 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
13301333
}
13311334

13321335
if let Some(backend) = builder.config.default_codegen_backend(target) {
1333-
cargo.env("CFG_DEFAULT_CODEGEN_BACKEND", backend);
1336+
cargo.env("CFG_DEFAULT_CODEGEN_BACKEND", backend.name());
13341337
}
13351338

13361339
let libdir_relative = builder.config.libdir_relative().unwrap_or_else(|| Path::new("lib"));
@@ -1543,7 +1546,7 @@ impl Step for RustcLink {
15431546
pub struct CodegenBackend {
15441547
pub target: TargetSelection,
15451548
pub compiler: Compiler,
1546-
pub backend: String,
1549+
pub backend: CodegenBackendKind,
15471550
}
15481551

15491552
fn needs_codegen_config(run: &RunConfig<'_>) -> bool {
@@ -1568,7 +1571,7 @@ fn is_codegen_cfg_needed(path: &TaskPath, run: &RunConfig<'_>) -> bool {
15681571
if path.contains(CODEGEN_BACKEND_PREFIX) {
15691572
let mut needs_codegen_backend_config = true;
15701573
for backend in run.builder.config.codegen_backends(run.target) {
1571-
if path.ends_with(&(CODEGEN_BACKEND_PREFIX.to_owned() + backend)) {
1574+
if path.ends_with(&(CODEGEN_BACKEND_PREFIX.to_owned() + backend.name())) {
15721575
needs_codegen_backend_config = false;
15731576
}
15741577
}
@@ -1602,7 +1605,7 @@ impl Step for CodegenBackend {
16021605
}
16031606

16041607
for backend in run.builder.config.codegen_backends(run.target) {
1605-
if backend == "llvm" {
1608+
if backend.is_llvm() {
16061609
continue; // Already built as part of rustc
16071610
}
16081611

@@ -1663,20 +1666,21 @@ impl Step for CodegenBackend {
16631666
);
16641667
cargo
16651668
.arg("--manifest-path")
1666-
.arg(builder.src.join(format!("compiler/rustc_codegen_{backend}/Cargo.toml")));
1669+
.arg(builder.src.join(format!("compiler/rustc_codegen_{}/Cargo.toml", backend.name())));
16671670
rustc_cargo_env(builder, &mut cargo, target);
16681671

16691672
// Ideally, we'd have a separate step for the individual codegen backends,
16701673
// like we have in tests (test::CodegenGCC) but that would require a lot of restructuring.
16711674
// If the logic gets more complicated, it should probably be done.
1672-
if backend == "gcc" {
1675+
if backend.is_gcc() {
16731676
let gcc = builder.ensure(Gcc { target });
16741677
add_cg_gcc_cargo_flags(&mut cargo, &gcc);
16751678
}
16761679

16771680
let tmp_stamp = BuildStamp::new(&out_dir).with_prefix("tmp");
16781681

1679-
let _guard = builder.msg_build(compiler, format_args!("codegen backend {backend}"), target);
1682+
let _guard =
1683+
builder.msg_build(compiler, format_args!("codegen backend {}", backend.name()), target);
16801684
let files = run_cargo(builder, cargo, vec![], &tmp_stamp, vec![], false, false);
16811685
if builder.config.dry_run() {
16821686
return;
@@ -1731,7 +1735,7 @@ fn copy_codegen_backends_to_sysroot(
17311735
}
17321736

17331737
for backend in builder.config.codegen_backends(target) {
1734-
if backend == "llvm" {
1738+
if backend.is_llvm() {
17351739
continue; // Already built as part of rustc
17361740
}
17371741

@@ -2161,7 +2165,7 @@ impl Step for Assemble {
21612165
let _codegen_backend_span =
21622166
span!(tracing::Level::DEBUG, "building requested codegen backends").entered();
21632167
for backend in builder.config.codegen_backends(target_compiler.host) {
2164-
if backend == "llvm" {
2168+
if backend.is_llvm() {
21652169
debug!("llvm codegen backend is already built as part of rustc");
21662170
continue; // Already built as part of rustc
21672171
}

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::utils::helpers::{
3232
exe, is_dylib, move_file, t, target_supports_cranelift_backend, timeit,
3333
};
3434
use crate::utils::tarball::{GeneratedTarball, OverlayKind, Tarball};
35-
use crate::{Compiler, DependencyType, FileType, LLVM_TOOLS, Mode, trace};
35+
use crate::{CodegenBackendKind, Compiler, DependencyType, FileType, LLVM_TOOLS, Mode, trace};
3636

3737
pub fn pkgname(builder: &Builder<'_>, component: &str) -> String {
3838
format!("{}-{}", component, builder.rust_package_vers())
@@ -1372,10 +1372,10 @@ impl Step for Miri {
13721372
}
13731373
}
13741374

1375-
#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
1375+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
13761376
pub struct CodegenBackend {
13771377
pub compiler: Compiler,
1378-
pub backend: String,
1378+
pub backend: CodegenBackendKind,
13791379
}
13801380

13811381
impl Step for CodegenBackend {
@@ -1389,7 +1389,7 @@ impl Step for CodegenBackend {
13891389

13901390
fn make_run(run: RunConfig<'_>) {
13911391
for backend in run.builder.config.codegen_backends(run.target) {
1392-
if backend == "llvm" {
1392+
if backend.is_llvm() {
13931393
continue; // Already built as part of rustc
13941394
}
13951395

@@ -1412,28 +1412,30 @@ impl Step for CodegenBackend {
14121412
return None;
14131413
}
14141414

1415-
if !builder.config.codegen_backends(self.compiler.host).contains(&self.backend.to_string())
1416-
{
1415+
if !builder.config.codegen_backends(self.compiler.host).contains(&self.backend) {
14171416
return None;
14181417
}
14191418

1420-
if self.backend == "cranelift" && !target_supports_cranelift_backend(self.compiler.host) {
1419+
if self.backend.is_cranelift() && !target_supports_cranelift_backend(self.compiler.host) {
14211420
builder.info("target not supported by rustc_codegen_cranelift. skipping");
14221421
return None;
14231422
}
14241423

14251424
let compiler = self.compiler;
14261425
let backend = self.backend;
14271426

1428-
let mut tarball =
1429-
Tarball::new(builder, &format!("rustc-codegen-{backend}"), &compiler.host.triple);
1430-
if backend == "cranelift" {
1427+
let mut tarball = Tarball::new(
1428+
builder,
1429+
&format!("rustc-codegen-{}", backend.name()),
1430+
&compiler.host.triple,
1431+
);
1432+
if backend.is_cranelift() {
14311433
tarball.set_overlay(OverlayKind::RustcCodegenCranelift);
14321434
} else {
1433-
panic!("Unknown backend rustc_codegen_{backend}");
1435+
panic!("Unknown backend rustc_codegen_{}", backend.name());
14341436
}
14351437
tarball.is_preview(true);
1436-
tarball.add_legal_and_readme_to(format!("share/doc/rustc_codegen_{backend}"));
1438+
tarball.add_legal_and_readme_to(format!("share/doc/rustc_codegen_{}", backend.name()));
14371439

14381440
let src = builder.sysroot(compiler);
14391441
let backends_src = builder.sysroot_codegen_backends(compiler);
@@ -1445,7 +1447,7 @@ impl Step for CodegenBackend {
14451447
// Don't use custom libdir here because ^lib/ will be resolved again with installer
14461448
let backends_dst = PathBuf::from("lib").join(backends_rel);
14471449

1448-
let backend_name = format!("rustc_codegen_{backend}");
1450+
let backend_name = format!("rustc_codegen_{}", backend.name());
14491451
let mut found_backend = false;
14501452
for backend in fs::read_dir(&backends_src).unwrap() {
14511453
let file_name = backend.unwrap().file_name();
@@ -1575,7 +1577,7 @@ impl Step for Extended {
15751577
add_component!("analysis" => Analysis { compiler, target });
15761578
add_component!("rustc-codegen-cranelift" => CodegenBackend {
15771579
compiler: builder.compiler(stage, target),
1578-
backend: "cranelift".to_string(),
1580+
backend: CodegenBackendKind::Cranelift,
15791581
});
15801582
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {
15811583
build_compiler: compiler,

src/bootstrap/src/core/build_steps/install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::core::config::{Config, TargetSelection};
1212
use crate::utils::exec::command;
1313
use crate::utils::helpers::t;
1414
use crate::utils::tarball::GeneratedTarball;
15-
use crate::{Compiler, Kind};
15+
use crate::{CodegenBackendKind, Compiler, Kind};
1616

1717
#[cfg(target_os = "illumos")]
1818
const SHELL: &str = "bash";
@@ -276,7 +276,7 @@ install!((self, builder, _config),
276276
RustcCodegenCranelift, alias = "rustc-codegen-cranelift", Self::should_build(_config), only_hosts: true, {
277277
if let Some(tarball) = builder.ensure(dist::CodegenBackend {
278278
compiler: self.compiler,
279-
backend: "cranelift".to_string(),
279+
backend: CodegenBackendKind::Cranelift,
280280
}) {
281281
install_sh(builder, "rustc-codegen-cranelift", self.compiler.stage, Some(self.target), &tarball);
282282
} else {

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::utils::helpers::{
3333
linker_flags, t, target_supports_cranelift_backend, up_to_date,
3434
};
3535
use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
36-
use crate::{CLang, DocTests, GitRepo, Mode, PathSet, debug, envify};
36+
use crate::{CLang, CodegenBackendKind, DocTests, GitRepo, Mode, PathSet, debug, envify};
3737

3838
const ADB_TEST_DIR: &str = "/data/local/tmp/work";
3939

@@ -1786,7 +1786,9 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
17861786
cmd.arg("--llvm-filecheck").arg(builder.llvm_filecheck(builder.config.host_target));
17871787

17881788
if let Some(codegen_backend) = builder.config.default_codegen_backend(compiler.host) {
1789-
cmd.arg("--codegen-backend").arg(&codegen_backend);
1789+
// Tells compiletest which codegen backend is used by default by the compiler.
1790+
// It is used to e.g. ignore tests that don't support that codegen backend.
1791+
cmd.arg("--codegen-backend").arg(codegen_backend.name());
17901792
}
17911793

17921794
if builder.build.config.llvm_enzyme {
@@ -3406,7 +3408,7 @@ impl Step for CodegenCranelift {
34063408
return;
34073409
}
34083410

3409-
if !builder.config.codegen_backends(run.target).contains(&"cranelift".to_owned()) {
3411+
if !builder.config.codegen_backends(run.target).contains(&CodegenBackendKind::Cranelift) {
34103412
builder.info("cranelift not in rust.codegen-backends. skipping");
34113413
return;
34123414
}
@@ -3533,7 +3535,7 @@ impl Step for CodegenGCC {
35333535
return;
35343536
}
35353537

3536-
if !builder.config.codegen_backends(run.target).contains(&"gcc".to_owned()) {
3538+
if !builder.config.codegen_backends(run.target).contains(&CodegenBackendKind::Gcc) {
35373539
builder.info("gcc not in rust.codegen-backends. skipping");
35383540
return;
35393541
}

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use crate::core::config::flags::Color;
1010
use crate::utils::build_stamp;
1111
use crate::utils::helpers::{self, LldThreads, check_cfg_arg, linker_args, linker_flags};
1212
use crate::{
13-
BootstrapCommand, CLang, Compiler, Config, DocTests, DryRun, EXTRA_CHECK_CFGS, GitRepo, Mode,
14-
RemapScheme, TargetSelection, command, prepare_behaviour_dump_dir, t,
13+
BootstrapCommand, CLang, CodegenBackendKind, Compiler, Config, DocTests, DryRun,
14+
EXTRA_CHECK_CFGS, GitRepo, Mode, RemapScheme, TargetSelection, command,
15+
prepare_behaviour_dump_dir, t,
1516
};
1617

1718
/// Represents flag values in `String` form with whitespace delimiter to pass it to the compiler
@@ -1286,7 +1287,8 @@ impl Builder<'_> {
12861287

12871288
if let Some(limit) = limit
12881289
&& (build_compiler_stage == 0
1289-
|| self.config.default_codegen_backend(target).unwrap_or_default() == "llvm")
1290+
|| self.config.default_codegen_backend(target).unwrap_or_default()
1291+
== CodegenBackendKind::Llvm)
12901292
{
12911293
rustflags.arg(&format!("-Cllvm-args=-import-instr-limit={limit}"));
12921294
}

src/bootstrap/src/core/builder/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub trait Step: 'static + Clone + Debug + PartialEq + Eq + Hash {
141141
#[allow(unused)]
142142
#[derive(Debug, PartialEq, Eq)]
143143
pub struct StepMetadata {
144-
name: &'static str,
144+
name: String,
145145
kind: Kind,
146146
target: TargetSelection,
147147
built_by: Option<Compiler>,
@@ -151,28 +151,28 @@ pub struct StepMetadata {
151151
}
152152

153153
impl StepMetadata {
154-
pub fn build(name: &'static str, target: TargetSelection) -> Self {
154+
pub fn build(name: &str, target: TargetSelection) -> Self {
155155
Self::new(name, target, Kind::Build)
156156
}
157157

158-
pub fn check(name: &'static str, target: TargetSelection) -> Self {
158+
pub fn check(name: &str, target: TargetSelection) -> Self {
159159
Self::new(name, target, Kind::Check)
160160
}
161161

162-
pub fn doc(name: &'static str, target: TargetSelection) -> Self {
162+
pub fn doc(name: &str, target: TargetSelection) -> Self {
163163
Self::new(name, target, Kind::Doc)
164164
}
165165

166-
pub fn dist(name: &'static str, target: TargetSelection) -> Self {
166+
pub fn dist(name: &str, target: TargetSelection) -> Self {
167167
Self::new(name, target, Kind::Dist)
168168
}
169169

170-
pub fn test(name: &'static str, target: TargetSelection) -> Self {
170+
pub fn test(name: &str, target: TargetSelection) -> Self {
171171
Self::new(name, target, Kind::Test)
172172
}
173173

174-
fn new(name: &'static str, target: TargetSelection, kind: Kind) -> Self {
175-
Self { name, kind, target, built_by: None, stage: None, metadata: None }
174+
fn new(name: &str, target: TargetSelection, kind: Kind) -> Self {
175+
Self { name: name.to_string(), kind, target, built_by: None, stage: None, metadata: None }
176176
}
177177

178178
pub fn built_by(mut self, compiler: Compiler) -> Self {

0 commit comments

Comments
 (0)