Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ jobs:
- { gcc: "gcc-15.deb" }
- { gcc: "gcc-15-without-int128.deb" }
commands: [
"--std-tests",
"--build-sysroot --std-tests",
# FIXME: re-enable asm tests when GCC can emit in the right syntax.
# "--asm-tests",
"--test-libcore",
"--extended-rand-tests",
"--extended-regex-example-tests",
"--extended-regex-tests",
"--test-successful-rustc --nb-parts 2 --current-part 0",
"--test-successful-rustc --nb-parts 2 --current-part 1",
"--projects",
# "--build-sysroot --asm-tests",
"--build-sysroot --test-libcore",
"--build-sysroot --extended-rand-tests",
"--build-sysroot --extended-regex-example-tests",
"--build-sysroot --extended-regex-tests",
"--build-sysroot --test-successful-rustc --nb-parts 2 --current-part 0",
"--build-sysroot --test-successful-rustc --nb-parts 2 --current-part 1",
"--build-sysroot --projects",
"--test-successful-rustc --nb-parts 2 --current-part 0 --with-llvm-sysroot",
"--test-successful-rustc --nb-parts 2 --current-part 1 --with-llvm-sysroot",
"--projects --with-llvm-sysroot",
]

steps:
Expand Down Expand Up @@ -119,7 +122,7 @@ jobs:

- name: Run tests
run: |
./y.sh test --release --clean --build-sysroot ${{ matrix.commands }}
./y.sh test --release --clean ${{ matrix.commands }}

duplicates:
runs-on: ubuntu-24.04
Expand Down
27 changes: 18 additions & 9 deletions build_system/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,9 @@ fn cleanup_sysroot_previous_build(library_dir: &Path) {
);
}

pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Result<(), String> {
let start_dir = get_sysroot_dir();

// Symlink libgccjit.so to sysroot.
let lib_path = start_dir.join("sysroot").join("lib");
let rustlib_target_path = lib_path
pub fn link_libgccjit_in_sysroot(config: &ConfigInfo, sysroot_path: &Path) -> Result<(), String> {
let rustlib_target_path = sysroot_path
.join("lib")
.join("rustlib")
.join(&config.host_triple)
.join("codegen-backends")
Expand All @@ -124,8 +121,18 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
// First remove the file to be able to create the symlink even when the file already exists.
let _ = fs::remove_file(&libgccjit_in_sysroot_path);
create_dir(&rustlib_target_path)?;
symlink(libgccjit_path, &libgccjit_in_sysroot_path)
symlink(&libgccjit_path, &libgccjit_in_sysroot_path)
.map_err(|error| format!("Cannot create symlink for libgccjit.so: {}", error))?;
println!("Created symlink of `libgccjit.so` at {libgccjit_in_sysroot_path:?}");
Ok(())
}

pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Result<(), String> {
let start_dir = get_sysroot_dir();

// Symlink libgccjit.so to sysroot.
let sysroot_path = start_dir.join("sysroot");
link_libgccjit_in_sysroot(config, &sysroot_path)?;

let library_dir = start_dir.join("sysroot_src").join("library");
cleanup_sysroot_previous_build(&library_dir);
Expand Down Expand Up @@ -179,7 +186,7 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
run_command_with_output_and_env(&args, Some(&sysroot_dir), Some(&env))?;

// Copy files to sysroot
let sysroot_path = lib_path.join(format!("rustlib/{}/lib/", config.target_triple));
let sysroot_path = sysroot_path.join(format!("lib/rustlib/{}/lib/", config.target_triple));
// To avoid errors like "multiple candidates for `rmeta` dependency `core` found", we clean the
// sysroot directory before copying the sysroot build artifacts.
let _ = fs::remove_dir_all(&sysroot_path);
Expand Down Expand Up @@ -227,7 +234,9 @@ fn build_codegen(args: &mut BuildArg) -> Result<(), String> {
}
run_command_with_output_and_env(&command, None, Some(&env))?;

args.config_info.setup(&mut env, false)?;
// We always build the sysroot with `cg_gcc` so we don't need to build the sysroot with
// `cg_llvm` since it's already distributed by rustup.
args.config_info.setup(&mut env, false, false)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment to mention that we always build the sysroot with cg_gcc and we don't need to build the sysroot with cg_llvm since it's already distributed by rustup and we can just not provide --sysroot to use it.


// We voluntarily ignore the error.
let _ = fs::remove_dir_all("target/out");
Expand Down
22 changes: 15 additions & 7 deletions build_system/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use std::{env as std_env, fs};
use boml::Toml;
use boml::types::TomlValue;

use crate::build::link_libgccjit_in_sysroot;
use crate::utils::{
create_dir, create_symlink, get_os_name, get_sysroot_dir, run_command_with_output,
rustc_version_info, split_args,
create_dir, create_symlink, get_os_name, get_rustup_sysroot_dir, get_sysroot_dir,
run_command_with_output, rustc_version_info, split_args,
};

#[derive(Default, PartialEq, Eq, Clone, Copy, Debug)]
Expand Down Expand Up @@ -314,6 +315,7 @@ impl ConfigInfo {
&mut self,
env: &mut HashMap<String, String>,
use_system_gcc: bool,
with_llvm_sysroot: bool,
Comment on lines 317 to +318
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use 1 or 2 enums here to make the caller code clearer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the added value is pretty minimal imo.

) -> Result<(), String> {
env.insert("CARGO_INCREMENTAL".to_string(), "0".to_string());

Expand Down Expand Up @@ -393,11 +395,10 @@ impl ConfigInfo {
// by its build system directly so no need to set it ourselves.
rustflags.push(format!("-Zcodegen-backend={backend}"));
} else {
rustflags.extend_from_slice(&[
"--sysroot".to_string(),
self.sysroot_path.clone(),
format!("-Zcodegen-backend={}", self.cg_backend_path),
]);
if !with_llvm_sysroot {
rustflags.extend_from_slice(&["--sysroot".to_string(), self.sysroot_path.clone()]);
}
rustflags.push(format!("-Zcodegen-backend={}", self.cg_backend_path));
}

// This environment variable is useful in case we want to change options of rustc commands.
Expand Down Expand Up @@ -454,6 +455,13 @@ impl ConfigInfo {
if !env.contains_key("RUSTC_LOG") {
env.insert("RUSTC_LOG".to_string(), "warn".to_string());
}

// In this case, it means we need to symlink `libgccjit.so` into the rustup sysroot.
if with_llvm_sysroot {
let sysroot_path = get_rustup_sysroot_dir()?;
link_libgccjit_in_sysroot(self, &sysroot_path)?;
}

Ok(())
}

Expand Down
4 changes: 3 additions & 1 deletion build_system/src/rust_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ impl RustcTools {

let mut env: HashMap<String, String> = std::env::vars().collect();
let mut config = ConfigInfo::default();
config.setup(&mut env, false)?;
// We always build the sysroot with `cg_gcc` so we don't need to build the sysroot with
// `cg_llvm` since it's already distributed by rustup.
config.setup(&mut env, false, false)?;
let toolchain = get_toolchain()?;

let toolchain_version = rustc_toolchain_version_info(&toolchain)?;
Expand Down
27 changes: 21 additions & 6 deletions build_system/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ fn show_usage() {
--use-system-gcc : Use system installed libgccjit
--build-only : Only build rustc_codegen_gcc then exits
--nb-parts : Used to split rustc_tests (for CI needs)
--current-part : Used with `--nb-parts`, allows you to specify which parts to test"#
--current-part : Used with `--nb-parts`, allows you to specify which parts to test
--with-llvm-sysroot : Use the LLVM sysroot instead of the GCC one"#
);
ConfigInfo::show_usage();
for (option, (doc, _)) in get_runners() {
Expand All @@ -93,6 +94,7 @@ struct TestArg {
config_info: ConfigInfo,
sysroot_features: Vec<String>,
keep_lto_tests: bool,
with_llvm_sysroot: bool,
}

impl TestArg {
Expand Down Expand Up @@ -136,6 +138,9 @@ impl TestArg {
return Err(format!("Expected an argument after `{arg}`, found nothing"));
}
},
"--with-llvm-sysroot" => {
test_arg.with_llvm_sysroot = true;
}
"--help" => {
show_usage();
return Ok(None);
Expand Down Expand Up @@ -559,9 +564,14 @@ fn asm_tests(env: &Env, args: &TestArg) -> Result<(), String> {
let extra =
if args.is_using_gcc_master_branch() { "" } else { " -Csymbol-mangling-version=v0" };

let sysroot_arg = if args.with_llvm_sysroot {
String::new()
} else {
format!(" --sysroot {}", args.config_info.sysroot_path)
};

let rustc_args = format!(
"-Zpanic-abort-tests -Zcodegen-backend={codegen_backend_path} --sysroot {} -Cpanic=abort{extra}",
args.config_info.sysroot_path
"-Zpanic-abort-tests -Zcodegen-backend={codegen_backend_path}{sysroot_arg} -Cpanic=abort{extra}",
);

run_command_with_env(
Expand Down Expand Up @@ -1028,11 +1038,16 @@ where
let extra =
if args.is_using_gcc_master_branch() { "" } else { " -Csymbol-mangling-version=v0" };

let sysroot_arg = if args.with_llvm_sysroot {
String::new()
} else {
format!(" --sysroot {}", args.config_info.sysroot_path)
};

let rustc_args = format!(
"{test_flags} -Zcodegen-backend={backend} --sysroot {sysroot}{extra}",
"{test_flags} -Zcodegen-backend={backend}{sysroot_arg}{extra}",
test_flags = env.get("TEST_FLAGS").unwrap_or(&String::new()),
backend = args.config_info.cg_backend_path,
sysroot = args.config_info.sysroot_path,
extra = extra,
);

Expand Down Expand Up @@ -1270,7 +1285,7 @@ pub fn run() -> Result<(), String> {
return Ok(());
}

args.config_info.setup(&mut env, args.use_system_gcc)?;
args.config_info.setup(&mut env, args.use_system_gcc, args.with_llvm_sysroot)?;

if args.runners.is_empty() {
run_all(&env, &args)?;
Expand Down
10 changes: 10 additions & 0 deletions build_system/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,16 @@ pub fn get_sysroot_dir() -> PathBuf {
Path::new(crate::BUILD_DIR).join("build_sysroot")
}

pub fn get_rustup_sysroot_dir() -> Result<PathBuf, String> {
let output = run_command(&[&"rustc", &"--print=sysroot"], None)?;

Ok(PathBuf::from(
String::from_utf8(output.stdout)
.map_err(|error| format!("`rustc --print=sysroot` failed: {error}"))?
.trim(),
))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading