Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b609ca5

Browse files
committed
[WIP] Distribute cg_clif as a rustup component
1 parent 724ba7f commit b609ca5

File tree

10 files changed

+161
-21
lines changed

10 files changed

+161
-21
lines changed

config.example.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ change-id = 115898
626626
# and currently the only standard options supported are `"llvm"`, `"cranelift"`
627627
# and `"gcc"`. The first backend in this list will be used as default by rustc
628628
# when no explicit backend is specified.
629-
#codegen-backends = ["llvm"]
629+
#codegen-backends = ["llvm", "cranelift"]
630630

631631
# Indicates whether LLD will be compiled and made available in the sysroot for
632632
# rustc to execute.

src/bootstrap/configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def apply_args(known_args, option_checking, config):
375375
elif option.name == 'target':
376376
set('build.target', value.split(','), config)
377377
elif option.name == 'full-tools':
378-
set('rust.codegen-backends', ['llvm'], config)
378+
set('rust.codegen-backends', ['llvm', 'cranelift'], config)
379379
set('rust.lld', True, config)
380380
set('rust.llvm-tools', True, config)
381381
set('build.extended', True, config)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,13 +1190,12 @@ fn is_codegen_cfg_needed(path: &TaskPath, run: &RunConfig<'_>) -> bool {
11901190
impl Step for CodegenBackend {
11911191
type Output = ();
11921192
const ONLY_HOSTS: bool = true;
1193-
// Only the backends specified in the `codegen-backends` entry of `config.toml` are built.
1194-
const DEFAULT: bool = true;
11951193

11961194
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
11971195
run.paths(&["compiler/rustc_codegen_cranelift", "compiler/rustc_codegen_gcc"])
11981196
}
11991197

1198+
// Only the backends specified in the `codegen-backends` entry of `config.toml` are built.
12001199
fn make_run(run: RunConfig<'_>) {
12011200
if needs_codegen_config(&run) {
12021201
return;

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

Lines changed: 118 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,6 @@ impl Step for Rustc {
443443
}
444444
}
445445

446-
// Copy over the codegen backends
447-
let backends_src = builder.sysroot_codegen_backends(compiler);
448-
let backends_rel = backends_src
449-
.strip_prefix(&src)
450-
.unwrap()
451-
.strip_prefix(builder.sysroot_libdir_relative(compiler))
452-
.unwrap();
453-
// Don't use custom libdir here because ^lib/ will be resolved again with installer
454-
let backends_dst = image.join("lib").join(&backends_rel);
455-
456-
t!(fs::create_dir_all(&backends_dst));
457-
builder.cp_r(&backends_src, &backends_dst);
458-
459446
// Copy libLLVM.so to the lib dir as well, if needed. While not
460447
// technically needed by rustc itself it's needed by lots of other
461448
// components like the llvm tools and LLD. LLD is included below and
@@ -1282,6 +1269,76 @@ impl Step for Miri {
12821269
}
12831270
}
12841271

1272+
#[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)]
1273+
pub struct CodegenBackend {
1274+
pub compiler: Compiler,
1275+
pub backend: Interned<String>,
1276+
}
1277+
1278+
impl Step for CodegenBackend {
1279+
type Output = Option<GeneratedTarball>;
1280+
const DEFAULT: bool = true;
1281+
const ONLY_HOSTS: bool = true;
1282+
1283+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1284+
run.path("compiler/rustc_codegen_cranelift")
1285+
}
1286+
1287+
fn make_run(run: RunConfig<'_>) {
1288+
for &backend in &run.builder.config.rust_codegen_backends {
1289+
if backend == "llvm" {
1290+
continue; // Already built as part of rustc
1291+
}
1292+
1293+
run.builder.ensure(CodegenBackend {
1294+
compiler: run.builder.compiler(run.builder.top_stage, run.target),
1295+
backend,
1296+
});
1297+
}
1298+
}
1299+
1300+
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
1301+
// This prevents rustc_codegen_cranelift from being built for "dist"
1302+
// or "install" on the stable/beta channels. It is not yet stable and
1303+
// should not be included.
1304+
if !builder.build.unstable_features() {
1305+
return None;
1306+
}
1307+
let compiler = self.compiler;
1308+
let backend = self.backend;
1309+
1310+
let mut tarball =
1311+
Tarball::new(builder, &format!("rustc-codegen-{}", backend), &compiler.host.triple);
1312+
if backend == "cranelift" {
1313+
tarball.set_overlay(OverlayKind::RustcCodegenCranelift);
1314+
} else {
1315+
panic!("Unknown backend rustc_codegen_{}", backend);
1316+
}
1317+
tarball.is_preview(true);
1318+
tarball.add_legal_and_readme_to(format!("share/doc/rustc_codegen_{}", backend));
1319+
1320+
let src = builder.sysroot(compiler);
1321+
let backends_src = builder.sysroot_codegen_backends(compiler);
1322+
let backends_rel = backends_src
1323+
.strip_prefix(&src)
1324+
.unwrap()
1325+
.strip_prefix(builder.sysroot_libdir_relative(compiler))
1326+
.unwrap();
1327+
// Don't use custom libdir here because ^lib/ will be resolved again with installer
1328+
let backends_dst = PathBuf::from("lib").join(&backends_rel);
1329+
1330+
let backend_name = format!("rustc_codegen_{}", backend);
1331+
for backend in fs::read_dir(&backends_src).unwrap() {
1332+
let file_name = backend.unwrap().file_name();
1333+
if file_name.to_str().unwrap().contains(&backend_name) {
1334+
tarball.add_file(backends_src.join(file_name), &backends_dst, 0o644);
1335+
}
1336+
}
1337+
1338+
Some(tarball.generate())
1339+
}
1340+
}
1341+
12851342
#[derive(Debug, PartialOrd, Ord, Copy, Clone, Hash, PartialEq, Eq)]
12861343
pub struct Rustfmt {
12871344
pub compiler: Compiler,
@@ -1452,6 +1509,10 @@ impl Step for Extended {
14521509
add_component!("clippy" => Clippy { compiler, target });
14531510
add_component!("miri" => Miri { compiler, target });
14541511
add_component!("analysis" => Analysis { compiler, target });
1512+
add_component!("rustc-codegen-cranelift" => CodegenBackend {
1513+
compiler: builder.compiler(stage, target),
1514+
backend: INTERNER.intern_str("cranelift"),
1515+
});
14551516

14561517
let etc = builder.src.join("src/etc/installer");
14571518

@@ -1548,6 +1609,7 @@ impl Step for Extended {
15481609
prepare(tool);
15491610
}
15501611
}
1612+
prepare("rustc-codegen-cranelift");
15511613
// create an 'uninstall' package
15521614
builder.install(&etc.join("pkg/postinstall"), &pkg.join("uninstall"), 0o755);
15531615
pkgbuild("uninstall");
@@ -1577,6 +1639,7 @@ impl Step for Extended {
15771639

15781640
let prepare = |name: &str| {
15791641
builder.create_dir(&exe.join(name));
1642+
dbg!(name);
15801643
let dir = if name == "rust-std" || name == "rust-analysis" {
15811644
format!("{}-{}", name, target.triple)
15821645
} else if name == "rust-analyzer" {
@@ -1587,9 +1650,30 @@ impl Step for Extended {
15871650
"rust-demangler-preview".to_string()
15881651
} else if name == "miri" {
15891652
"miri-preview".to_string()
1653+
} else if name == "rustc-codegen-cranelift" {
1654+
"rustc-codegen-cranelift-preview".to_string()
15901655
} else {
15911656
name.to_string()
15921657
};
1658+
fn visit_dirs(dir: &Path) -> std::io::Result<()> {
1659+
if dir.is_dir() {
1660+
for entry in fs::read_dir(dir)? {
1661+
let entry = entry?;
1662+
let path = entry.path();
1663+
eprintln!("{:?}", path);
1664+
if path.is_dir() {
1665+
if let Err(err) = visit_dirs(&path) {
1666+
dbg!(err);
1667+
}
1668+
}
1669+
}
1670+
}
1671+
Ok(())
1672+
}
1673+
let _ = visit_dirs(
1674+
"\\\\?\\D:\\a\\rust\\rust\\build\\tmp\\tarball\\rust\\x86_64-pc-windows-msvc"
1675+
.as_ref(),
1676+
);
15931677
builder.cp_r(
15941678
&work.join(&format!("{}-{}", pkgname(builder, name), target.triple)).join(dir),
15951679
&exe.join(name),
@@ -1605,6 +1689,7 @@ impl Step for Extended {
16051689
prepare(tool);
16061690
}
16071691
}
1692+
prepare("rustc-codegen-cranelift");
16081693
if target.ends_with("windows-gnu") {
16091694
prepare("rust-mingw");
16101695
}
@@ -1779,6 +1864,23 @@ impl Step for Extended {
17791864
.arg("-t")
17801865
.arg(etc.join("msi/remove-duplicates.xsl")),
17811866
);
1867+
builder.run(
1868+
Command::new(&heat)
1869+
.current_dir(&exe)
1870+
.arg("dir")
1871+
.arg("rustc-codegen-cranelift")
1872+
.args(&heat_flags)
1873+
.arg("-cg")
1874+
.arg("RustcCodegenCraneliftGroup")
1875+
.arg("-dr")
1876+
.arg("RustcCodegenCranelift")
1877+
.arg("-var")
1878+
.arg("var.RustcCodegenCraneliftDir")
1879+
.arg("-out")
1880+
.arg(exe.join("RustcCodegenCraneliftGroup.wxs"))
1881+
.arg("-t")
1882+
.arg(etc.join("msi/remove-duplicates.xsl")),
1883+
);
17821884
if target.ends_with("windows-gnu") {
17831885
builder.run(
17841886
Command::new(&heat)
@@ -1807,6 +1909,7 @@ impl Step for Extended {
18071909
.arg("-dCargoDir=cargo")
18081910
.arg("-dStdDir=rust-std")
18091911
.arg("-dAnalysisDir=rust-analysis")
1912+
.arg("-dRustcCodegenCraneliftDir=rustc-codegen-cranelift")
18101913
.arg("-arch")
18111914
.arg(&arch)
18121915
.arg("-out")
@@ -1852,6 +1955,7 @@ impl Step for Extended {
18521955
if built_tools.contains("rust-demangler") {
18531956
candle("RustDemanglerGroup.wxs".as_ref());
18541957
}
1958+
candle("RustcCodegenCraneliftGroup.wxs".as_ref());
18551959
if built_tools.contains("rust-analyzer") {
18561960
candle("RustAnalyzerGroup.wxs".as_ref());
18571961
}
@@ -1882,6 +1986,7 @@ impl Step for Extended {
18821986
.arg("CargoGroup.wixobj")
18831987
.arg("StdGroup.wixobj")
18841988
.arg("AnalysisGroup.wixobj")
1989+
.arg("RustcCodegenCraneliftGroup.wixobj")
18851990
.current_dir(&exe);
18861991

18871992
if built_tools.contains("clippy") {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
1313
use crate::core::config::{Config, TargetSelection};
1414
use crate::utils::helpers::t;
1515
use crate::utils::tarball::GeneratedTarball;
16+
use crate::INTERNER;
1617
use crate::{Compiler, Kind};
1718

1819
#[cfg(target_os = "illumos")]
@@ -281,6 +282,19 @@ install!((self, builder, _config),
281282
});
282283
install_sh(builder, "rustc", self.compiler.stage, Some(self.target), &tarball);
283284
};
285+
RustcCodegenCranelift, alias = "rustc-codegen-cranelift", Self::should_build(_config), only_hosts: true, {
286+
if let Some(tarball) = builder.ensure(dist::CodegenBackend {
287+
compiler: self.compiler,
288+
backend: INTERNER.intern_str("cranelift"),
289+
}) {
290+
install_sh(builder, "rustc-codegen-cranelift", self.compiler.stage, Some(self.target), &tarball);
291+
} else {
292+
builder.info(
293+
&format!("skipping Install CodegenBackend(\"cranelift\") stage{} ({})",
294+
self.compiler.stage, self.target),
295+
);
296+
}
297+
};
284298
);
285299

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

src/bootstrap/src/core/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ impl<'a> Builder<'a> {
815815
dist::JsonDocs,
816816
dist::Mingw,
817817
dist::Rustc,
818+
dist::CodegenBackend,
818819
dist::Std,
819820
dist::RustcDev,
820821
dist::Analysis,

src/bootstrap/src/core/config/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,8 @@ impl Config {
10681068
config.channel = "dev".to_string();
10691069
config.codegen_tests = true;
10701070
config.rust_dist_src = true;
1071-
config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
1071+
config.rust_codegen_backends =
1072+
vec![INTERNER.intern_str("llvm"), INTERNER.intern_str("cranelift")];
10721073
config.deny_warnings = true;
10731074
config.bindir = "bin".into();
10741075
config.dist_include_mingw_linker = true;

src/bootstrap/src/utils/tarball.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) enum OverlayKind {
1919
RustDemangler,
2020
RLS,
2121
RustAnalyzer,
22+
RustcCodegenCranelift,
2223
}
2324

2425
impl OverlayKind {
@@ -58,6 +59,11 @@ impl OverlayKind {
5859
"src/tools/rust-analyzer/LICENSE-APACHE",
5960
"src/tools/rust-analyzer/LICENSE-MIT",
6061
],
62+
OverlayKind::RustcCodegenCranelift => &[
63+
"compiler/rustc_codegen_cranelift/Readme.md",
64+
"compiler/rustc_codegen_cranelift/LICENSE-APACHE",
65+
"compiler/rustc_codegen_cranelift/LICENSE-MIT",
66+
],
6167
}
6268
}
6369

@@ -80,6 +86,7 @@ impl OverlayKind {
8086
OverlayKind::RustAnalyzer => builder
8187
.rust_analyzer_info
8288
.version(builder, &builder.release_num("rust-analyzer/crates/rust-analyzer")),
89+
OverlayKind::RustcCodegenCranelift => builder.rust_version(),
8390
}
8491
}
8592
}
@@ -320,6 +327,7 @@ impl<'a> Tarball<'a> {
320327
cmd.arg("--compression-formats").arg(formats.join(","));
321328
}
322329
cmd.args(&["--compression-profile", &self.builder.config.dist_compression_profile]);
330+
dbg!(&cmd);
323331
self.builder.run(&mut cmd);
324332

325333
// Ensure there are no symbolic links in the tarball. In particular,

src/tools/build-manifest/src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ static PKG_INSTALLERS: &[&str] = &["x86_64-apple-darwin", "aarch64-apple-darwin"
191191

192192
static MINGW: &[&str] = &["i686-pc-windows-gnu", "x86_64-pc-windows-gnu"];
193193

194-
static NIGHTLY_ONLY_COMPONENTS: &[PkgType] = &[PkgType::Miri, PkgType::JsonDocs];
194+
static NIGHTLY_ONLY_COMPONENTS: &[PkgType] =
195+
&[PkgType::Miri, PkgType::JsonDocs, PkgType::RustcCodegenCranelift];
195196

196197
macro_rules! t {
197198
($e:expr) => {
@@ -335,7 +336,15 @@ impl Builder {
335336

336337
// NOTE: this profile is effectively deprecated; do not add new components to it.
337338
let mut complete = default;
338-
complete.extend([Rls, RustAnalyzer, RustSrc, LlvmTools, RustAnalysis, Miri]);
339+
complete.extend([
340+
Rls,
341+
RustAnalyzer,
342+
RustSrc,
343+
LlvmTools,
344+
RustAnalysis,
345+
Miri,
346+
RustcCodegenCranelift,
347+
]);
339348
profile("complete", &complete);
340349

341350
// The compiler libraries are not stable for end users, and they're also huge, so we only
@@ -422,7 +431,8 @@ impl Builder {
422431
| PkgType::Rustfmt
423432
| PkgType::LlvmTools
424433
| PkgType::RustAnalysis
425-
| PkgType::JsonDocs => {
434+
| PkgType::JsonDocs
435+
| PkgType::RustcCodegenCranelift => {
426436
extensions.push(host_component(pkg));
427437
}
428438
PkgType::RustcDev | PkgType::RustcDocs => {

src/tools/build-manifest/src/versions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pkg_type! {
5757
LlvmTools = "llvm-tools"; preview = true,
5858
Miri = "miri"; preview = true,
5959
JsonDocs = "rust-docs-json"; preview = true,
60+
RustcCodegenCranelift = "rustc-codegen-cranelift"; preview = true,
6061
}
6162

6263
impl PkgType {
@@ -80,6 +81,7 @@ impl PkgType {
8081
PkgType::Rustfmt => false,
8182
PkgType::LlvmTools => false,
8283
PkgType::Miri => false,
84+
PkgType::RustcCodegenCranelift => false,
8385

8486
PkgType::Rust => true,
8587
PkgType::RustStd => true,

0 commit comments

Comments
 (0)