Skip to content

Commit eaf7b2a

Browse files
Add new --with-llvm-sysroot option for test commands
1 parent 2818318 commit eaf7b2a

5 files changed

Lines changed: 67 additions & 23 deletions

File tree

build_system/src/build.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,9 @@ fn cleanup_sysroot_previous_build(library_dir: &Path) {
106106
);
107107
}
108108

109-
pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Result<(), String> {
110-
let start_dir = get_sysroot_dir();
111-
112-
// Symlink libgccjit.so to sysroot.
113-
let lib_path = start_dir.join("sysroot").join("lib");
114-
let rustlib_target_path = lib_path
109+
pub fn link_libgccjit_in_sysroot(config: &ConfigInfo, sysroot_path: &Path) -> Result<(), String> {
110+
let rustlib_target_path = sysroot_path
111+
.join("lib")
115112
.join("rustlib")
116113
.join(&config.host_triple)
117114
.join("codegen-backends")
@@ -124,8 +121,18 @@ pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Resu
124121
// First remove the file to be able to create the symlink even when the file already exists.
125122
let _ = fs::remove_file(&libgccjit_in_sysroot_path);
126123
create_dir(&rustlib_target_path)?;
127-
symlink(libgccjit_path, &libgccjit_in_sysroot_path)
124+
symlink(&libgccjit_path, &libgccjit_in_sysroot_path)
128125
.map_err(|error| format!("Cannot create symlink for libgccjit.so: {}", error))?;
126+
println!("Created symlink of `libgccjit.so` at {libgccjit_in_sysroot_path:?}");
127+
Ok(())
128+
}
129+
130+
pub fn build_sysroot(env: &HashMap<String, String>, config: &ConfigInfo) -> Result<(), String> {
131+
let start_dir = get_sysroot_dir();
132+
133+
// Symlink libgccjit.so to sysroot.
134+
let sysroot_path = start_dir.join("sysroot");
135+
link_libgccjit_in_sysroot(config, &sysroot_path)?;
129136

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

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

230-
args.config_info.setup(&mut env, false)?;
237+
// We always build the sysroot with `cg_gcc` so we don't need to build the sysroot with
238+
// `cg_llvm` since it's already distributed by rustup.
239+
args.config_info.setup(&mut env, false, false)?;
231240

232241
// We voluntarily ignore the error.
233242
let _ = fs::remove_dir_all("target/out");

build_system/src/config.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use std::{env as std_env, fs};
66
use boml::Toml;
77
use boml::types::TomlValue;
88

9+
use crate::build::link_libgccjit_in_sysroot;
910
use crate::utils::{
10-
create_dir, create_symlink, get_os_name, get_sysroot_dir, run_command_with_output,
11-
rustc_version_info, split_args,
11+
create_dir, create_symlink, get_os_name, get_rustup_sysroot_dir, get_sysroot_dir,
12+
run_command_with_output, rustc_version_info, split_args,
1213
};
1314

1415
#[derive(Default, PartialEq, Eq, Clone, Copy, Debug)]
@@ -314,6 +315,7 @@ impl ConfigInfo {
314315
&mut self,
315316
env: &mut HashMap<String, String>,
316317
use_system_gcc: bool,
318+
with_llvm_sysroot: bool,
317319
) -> Result<(), String> {
318320
env.insert("CARGO_INCREMENTAL".to_string(), "0".to_string());
319321

@@ -393,11 +395,10 @@ impl ConfigInfo {
393395
// by its build system directly so no need to set it ourselves.
394396
rustflags.push(format!("-Zcodegen-backend={backend}"));
395397
} else {
396-
rustflags.extend_from_slice(&[
397-
"--sysroot".to_string(),
398-
self.sysroot_path.clone(),
399-
format!("-Zcodegen-backend={}", self.cg_backend_path),
400-
]);
398+
if !with_llvm_sysroot {
399+
rustflags.extend_from_slice(&["--sysroot".to_string(), self.sysroot_path.clone()]);
400+
}
401+
rustflags.push(format!("-Zcodegen-backend={}", self.cg_backend_path));
401402
}
402403

403404
// This environment variable is useful in case we want to change options of rustc commands.
@@ -454,6 +455,13 @@ impl ConfigInfo {
454455
if !env.contains_key("RUSTC_LOG") {
455456
env.insert("RUSTC_LOG".to_string(), "warn".to_string());
456457
}
458+
459+
// In this case, it means we need to symlink `libgccjit.so` into the rustup sysroot.
460+
if with_llvm_sysroot {
461+
let sysroot_path = get_rustup_sysroot_dir()?;
462+
link_libgccjit_in_sysroot(self, &sysroot_path)?;
463+
}
464+
457465
Ok(())
458466
}
459467

build_system/src/rust_tools.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ impl RustcTools {
7272

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

7880
let toolchain_version = rustc_toolchain_version_info(&toolchain)?;

build_system/src/test.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ fn show_usage() {
6868
--use-system-gcc : Use system installed libgccjit
6969
--build-only : Only build rustc_codegen_gcc then exits
7070
--nb-parts : Used to split rustc_tests (for CI needs)
71-
--current-part : Used with `--nb-parts`, allows you to specify which parts to test"#
71+
--current-part : Used with `--nb-parts`, allows you to specify which parts to test
72+
--with-llvm-sysroot : Use the LLVM sysroot instead of the GCC one"#
7273
);
7374
ConfigInfo::show_usage();
7475
for (option, (doc, _)) in get_runners() {
@@ -93,6 +94,7 @@ struct TestArg {
9394
config_info: ConfigInfo,
9495
sysroot_features: Vec<String>,
9596
keep_lto_tests: bool,
97+
with_llvm_sysroot: bool,
9698
}
9799

98100
impl TestArg {
@@ -136,6 +138,9 @@ impl TestArg {
136138
return Err(format!("Expected an argument after `{arg}`, found nothing"));
137139
}
138140
},
141+
"--with-llvm-sysroot" => {
142+
test_arg.with_llvm_sysroot = true;
143+
}
139144
"--help" => {
140145
show_usage();
141146
return Ok(None);
@@ -559,9 +564,14 @@ fn asm_tests(env: &Env, args: &TestArg) -> Result<(), String> {
559564
let extra =
560565
if args.is_using_gcc_master_branch() { "" } else { " -Csymbol-mangling-version=v0" };
561566

567+
let sysroot_arg = if args.with_llvm_sysroot {
568+
String::new()
569+
} else {
570+
format!(" --sysroot {}", args.config_info.sysroot_path)
571+
};
572+
562573
let rustc_args = format!(
563-
"-Zpanic-abort-tests -Zcodegen-backend={codegen_backend_path} --sysroot {} -Cpanic=abort{extra}",
564-
args.config_info.sysroot_path
574+
"-Zpanic-abort-tests -Zcodegen-backend={codegen_backend_path}{sysroot_arg} -Cpanic=abort{extra}",
565575
);
566576

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

1041+
let sysroot_arg = if args.with_llvm_sysroot {
1042+
String::new()
1043+
} else {
1044+
format!(" --sysroot {}", args.config_info.sysroot_path)
1045+
};
1046+
10311047
let rustc_args = format!(
1032-
"{test_flags} -Zcodegen-backend={backend} --sysroot {sysroot}{extra}",
1048+
"{test_flags} -Zcodegen-backend={backend}{sysroot_arg}{extra}",
10331049
test_flags = env.get("TEST_FLAGS").unwrap_or(&String::new()),
10341050
backend = args.config_info.cg_backend_path,
1035-
sysroot = args.config_info.sysroot_path,
10361051
extra = extra,
10371052
);
10381053

@@ -1270,7 +1285,7 @@ pub fn run() -> Result<(), String> {
12701285
return Ok(());
12711286
}
12721287

1273-
args.config_info.setup(&mut env, args.use_system_gcc)?;
1288+
args.config_info.setup(&mut env, args.use_system_gcc, args.with_llvm_sysroot)?;
12741289

12751290
if args.runners.is_empty() {
12761291
run_all(&env, &args)?;

build_system/src/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,16 @@ pub fn get_sysroot_dir() -> PathBuf {
419419
Path::new(crate::BUILD_DIR).join("build_sysroot")
420420
}
421421

422+
pub fn get_rustup_sysroot_dir() -> Result<PathBuf, String> {
423+
let output = run_command(&[&"rustc", &"--print=sysroot"], None)?;
424+
425+
Ok(PathBuf::from(
426+
String::from_utf8(output.stdout)
427+
.map_err(|error| format!("`rustc --print=sysroot` failed: {error}"))?
428+
.trim(),
429+
))
430+
}
431+
422432
#[cfg(test)]
423433
mod tests {
424434
use super::*;

0 commit comments

Comments
 (0)