Skip to content

Commit 17472a9

Browse files
committed
Improve GCC build in bootstrap
1 parent a0cf0f2 commit 17472a9

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

Diff for: src/bootstrap/src/core/build_steps/compile.rs

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::{env, fs, str};
1717

1818
use serde_derive::Deserialize;
1919

20+
use crate::core::build_steps::gcc::{Gcc, add_cg_gcc_cargo_flags};
2021
use crate::core::build_steps::tool::SourceType;
2122
use crate::core::build_steps::{dist, llvm};
2223
use crate::core::builder;
@@ -1500,6 +1501,14 @@ impl Step for CodegenBackend {
15001501
.arg(builder.src.join(format!("compiler/rustc_codegen_{backend}/Cargo.toml")));
15011502
rustc_cargo_env(builder, &mut cargo, target, compiler.stage);
15021503

1504+
// Ideally, we'd have a separate step for the individual codegen backends,
1505+
// like we have in tests (test::CodegenGCC) but that would require a lot of restructuring.
1506+
// If the logic gets more complicated, it should probably be done.
1507+
if backend == "gcc" {
1508+
let gcc = builder.ensure(Gcc { target });
1509+
add_cg_gcc_cargo_flags(&mut cargo, &gcc);
1510+
}
1511+
15031512
let tmp_stamp = BuildStamp::new(&out_dir).with_prefix("tmp");
15041513

15051514
let _guard = builder.msg_build(compiler, format_args!("codegen backend {backend}"), target);

Diff for: src/bootstrap/src/core/build_steps/gcc.rs

+37-14
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
//! ensure that they're always in place if needed.
1010
1111
use std::fs;
12-
use std::path::PathBuf;
12+
use std::path::{Path, PathBuf};
1313
use std::sync::OnceLock;
1414

1515
use build_helper::ci::CiEnv;
1616

1717
use crate::Kind;
18-
use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
18+
use crate::core::builder::{Builder, Cargo, RunConfig, ShouldRun, Step};
1919
use crate::core::config::TargetSelection;
2020
use crate::utils::build_stamp::{BuildStamp, generate_smart_stamp_hash};
2121
use crate::utils::exec::command;
@@ -29,7 +29,8 @@ pub struct Meta {
2929
}
3030

3131
pub enum GccBuildStatus {
32-
AlreadyBuilt,
32+
/// libgccjit is already built at this path
33+
AlreadyBuilt(PathBuf),
3334
ShouldBuild(Meta),
3435
}
3536

@@ -41,9 +42,6 @@ pub fn prebuilt_gcc_config(builder: &Builder<'_>, target: TargetSelection) -> Gc
4142
// Initialize the gcc submodule if not initialized already.
4243
builder.config.update_submodule("src/gcc");
4344

44-
// FIXME (GuillaumeGomez): To be done once gccjit has been built in the CI.
45-
// builder.config.maybe_download_ci_gcc();
46-
4745
let root = builder.src.join("src/gcc");
4846
let out_dir = builder.gcc_out(target).join("build");
4947
let install_dir = builder.gcc_out(target).join("install");
@@ -70,19 +68,37 @@ pub fn prebuilt_gcc_config(builder: &Builder<'_>, target: TargetSelection) -> Gc
7068
stamp.path().display()
7169
));
7270
}
73-
return GccBuildStatus::AlreadyBuilt;
71+
let path = libgccjit_built_path(&install_dir);
72+
if path.is_file() {
73+
return GccBuildStatus::AlreadyBuilt(path);
74+
} else {
75+
builder.info(&format!(
76+
"GCC stamp is up-to-date, but the libgccjit.so file was not found at `{}`",
77+
path.display(),
78+
));
79+
}
7480
}
7581

7682
GccBuildStatus::ShouldBuild(Meta { stamp, out_dir, install_dir, root })
7783
}
7884

85+
/// Returns the path to a libgccjit.so file in the install directory of GCC.
86+
fn libgccjit_built_path(install_dir: &Path) -> PathBuf {
87+
install_dir.join("lib/libgccjit.so")
88+
}
89+
7990
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
8091
pub struct Gcc {
8192
pub target: TargetSelection,
8293
}
8394

95+
#[derive(Clone)]
96+
pub struct GccOutput {
97+
pub libgccjit: PathBuf,
98+
}
99+
84100
impl Step for Gcc {
85-
type Output = bool;
101+
type Output = GccOutput;
86102

87103
const ONLY_HOSTS: bool = true;
88104

@@ -94,14 +110,14 @@ impl Step for Gcc {
94110
run.builder.ensure(Gcc { target: run.target });
95111
}
96112

97-
/// Compile GCC for `target`.
98-
fn run(self, builder: &Builder<'_>) -> bool {
113+
/// Compile GCC (specifically `libgccjit`) for `target`.
114+
fn run(self, builder: &Builder<'_>) -> Self::Output {
99115
let target = self.target;
100116

101117
// If GCC has already been built, we avoid building it again.
102118
let Meta { stamp, out_dir, install_dir, root } = match prebuilt_gcc_config(builder, target)
103119
{
104-
GccBuildStatus::AlreadyBuilt => return true,
120+
GccBuildStatus::AlreadyBuilt(path) => return GccOutput { libgccjit: path },
105121
GccBuildStatus::ShouldBuild(m) => m,
106122
};
107123

@@ -110,8 +126,9 @@ impl Step for Gcc {
110126
let _time = helpers::timeit(builder);
111127
t!(fs::create_dir_all(&out_dir));
112128

129+
let libgccjit_path = libgccjit_built_path(&install_dir);
113130
if builder.config.dry_run() {
114-
return true;
131+
return GccOutput { libgccjit: libgccjit_path };
115132
}
116133

117134
// GCC creates files (e.g. symlinks to the downloaded dependencies)
@@ -173,11 +190,17 @@ impl Step for Gcc {
173190

174191
let lib_alias = install_dir.join("lib/libgccjit.so.0");
175192
if !lib_alias.exists() {
176-
t!(builder.symlink_file(install_dir.join("lib/libgccjit.so"), lib_alias,));
193+
t!(builder.symlink_file(&libgccjit_path, lib_alias));
177194
}
178195

179196
t!(stamp.write());
180197

181-
true
198+
GccOutput { libgccjit: libgccjit_path }
182199
}
183200
}
201+
202+
/// Configures a Cargo invocation so that it can build the GCC codegen backend.
203+
pub fn add_cg_gcc_cargo_flags(cargo: &mut Cargo, gcc: &GccOutput) {
204+
// Add the path to libgccjit.so to the linker search paths.
205+
cargo.rustflag(&format!("-L{}", gcc.libgccjit.parent().unwrap().display()));
206+
}

Diff for: src/bootstrap/src/core/build_steps/test.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use clap_complete::shells;
1212

1313
use crate::core::build_steps::compile::run_cargo;
1414
use crate::core::build_steps::doc::DocumentationFormat;
15+
use crate::core::build_steps::gcc::{Gcc, add_cg_gcc_cargo_flags};
1516
use crate::core::build_steps::llvm::get_llvm_version;
1617
use crate::core::build_steps::synthetic_targets::MirOptPanicAbortSyntheticTarget;
1718
use crate::core::build_steps::tool::{self, SourceType, Tool};
@@ -3520,6 +3521,8 @@ impl Step for CodegenGCC {
35203521
let compiler = self.compiler;
35213522
let target = self.target;
35223523

3524+
let gcc = builder.ensure(Gcc { target });
3525+
35233526
builder.ensure(
35243527
compile::Std::new(compiler, target)
35253528
.extra_rust_args(&["-Csymbol-mangling-version=v0", "-Cpanic=abort"]),
@@ -3546,6 +3549,7 @@ impl Step for CodegenGCC {
35463549
.arg("--manifest-path")
35473550
.arg(builder.src.join("compiler/rustc_codegen_gcc/build_system/Cargo.toml"));
35483551
compile::rustc_cargo_env(builder, &mut cargo, target, compiler.stage);
3552+
add_cg_gcc_cargo_flags(&mut cargo, &gcc);
35493553

35503554
// Avoid incremental cache issues when changing rustc
35513555
cargo.env("CARGO_BUILD_INCREMENTAL", "false");
@@ -3578,9 +3582,10 @@ impl Step for CodegenGCC {
35783582
.env("CG_RUSTFLAGS", "-Alinker-messages")
35793583
.arg("--")
35803584
.arg("test")
3581-
.arg("--use-system-gcc")
35823585
.arg("--use-backend")
35833586
.arg("gcc")
3587+
.arg("--gcc-path")
3588+
.arg(gcc.libgccjit.parent().unwrap())
35843589
.arg("--out-dir")
35853590
.arg(builder.stage_out(compiler, Mode::ToolRustc).join("cg_gcc"))
35863591
.arg("--release")

0 commit comments

Comments
 (0)