From 588db24344dc2b626bb050067e9e6cda2de3bc59 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 12 Feb 2024 14:41:18 +0100 Subject: [PATCH] Correctly handle `--use-system-gcc` --- build_system/src/build.rs | 4 ++-- build_system/src/cargo.rs | 2 +- build_system/src/config.rs | 35 ++++++++++------------------- build_system/src/test.rs | 45 +++++++++++++++++++------------------- build_system/src/utils.rs | 6 ++++- 5 files changed, 42 insertions(+), 50 deletions(-) diff --git a/build_system/src/build.rs b/build_system/src/build.rs index efae5a46b04..308ad346549 100644 --- a/build_system/src/build.rs +++ b/build_system/src/build.rs @@ -207,7 +207,7 @@ fn build_codegen(args: &mut BuildArg) -> Result<(), String> { } run_command_with_output_and_env(&command, None, Some(&env))?; - args.config_info.setup(&mut env, None)?; + args.config_info.setup(&mut env, false)?; // We voluntarily ignore the error. let _ = fs::remove_dir_all("target/out"); @@ -229,7 +229,7 @@ pub fn run() -> Result<(), String> { Some(args) => args, None => return Ok(()), }; - args.config_info.setup_gcc_path(None)?; + args.config_info.setup_gcc_path()?; build_codegen(&mut args)?; Ok(()) } diff --git a/build_system/src/cargo.rs b/build_system/src/cargo.rs index 67b301d9aa6..1cfcdba6b1c 100644 --- a/build_system/src/cargo.rs +++ b/build_system/src/cargo.rs @@ -77,7 +77,7 @@ pub fn run() -> Result<(), String> { })?; let mut env: HashMap = std::env::vars().collect(); - ConfigInfo::default().setup(&mut env, None)?; + ConfigInfo::default().setup(&mut env, false)?; let toolchain = get_toolchain()?; let toolchain_version = rustc_toolchain_version_info(&toolchain)?; diff --git a/build_system/src/config.rs b/build_system/src/config.rs index 5ba6233617e..49782fc64ef 100644 --- a/build_system/src/config.rs +++ b/build_system/src/config.rs @@ -159,30 +159,17 @@ impl ConfigInfo { command } - pub fn setup_gcc_path(&mut self, override_gcc_path: Option<&str>) -> Result<(), String> { + pub fn setup_gcc_path(&mut self) -> Result<(), String> { let ConfigFile { gcc_path, .. } = ConfigFile::new(self.config_file.as_deref())?; - self.gcc_path = match override_gcc_path { - Some(path) => { - if gcc_path.is_some() { - println!( - "overriding setting from `{}`", - self.config_file.as_deref().unwrap_or("config.toml") - ); - } - path.to_string() - } + self.gcc_path = match gcc_path { + Some(path) => path, + // FIXME: Once we support "download", rewrite this. None => { - match gcc_path { - Some(path) => path, - // FIXME: Once we support "download", rewrite this. - None => { - return Err(format!( - "missing `gcc-path` value from `{}`", - self.config_file.as_deref().unwrap_or("config.toml"), - )) - } - } + return Err(format!( + "missing `gcc-path` value from `{}`", + self.config_file.as_deref().unwrap_or("config.toml"), + )) } }; Ok(()) @@ -191,12 +178,12 @@ impl ConfigInfo { pub fn setup( &mut self, env: &mut HashMap, - override_gcc_path: Option<&str>, + use_system_gcc: bool, ) -> Result<(), String> { env.insert("CARGO_INCREMENTAL".to_string(), "0".to_string()); - if self.gcc_path.is_empty() || override_gcc_path.is_some() { - self.setup_gcc_path(override_gcc_path)?; + if self.gcc_path.is_empty() && !use_system_gcc { + self.setup_gcc_path()?; } env.insert("GCC_PATH".to_string(), self.gcc_path.clone()); diff --git a/build_system/src/test.rs b/build_system/src/test.rs index 1cacd6efc7f..806e18431c4 100644 --- a/build_system/src/test.rs +++ b/build_system/src/test.rs @@ -31,7 +31,10 @@ fn get_runners() -> Runners { "--test-failing-rustc", ("Run failing rustc tests", test_failing_rustc), ); - runners.insert("--projects", ("Run the tests of popular crates", test_projects)); + runners.insert( + "--projects", + ("Run the tests of popular crates", test_projects), + ); runners.insert("--test-libcore", ("Run libcore tests", test_libcore)); runners.insert("--clean", ("Empty cargo target directory", clean)); runners.insert("--build-sysroot", ("Build sysroot", build_sysroot)); @@ -109,7 +112,7 @@ fn show_usage() { struct TestArg { no_default_features: bool, build_only: bool, - gcc_path: Option, + use_system_gcc: bool, runners: BTreeSet, flags: Vec, backend: Option, @@ -121,7 +124,6 @@ struct TestArg { impl TestArg { fn new() -> Result, String> { - let mut use_system_gcc = false; let mut test_arg = Self::default(); // We skip binary name and the `test` command. @@ -147,7 +149,10 @@ impl TestArg { return Err("Expected an argument after `--features`, found nothing".into()) } }, - "--use-system-gcc" => use_system_gcc = true, + "--use-system-gcc" => { + println!("Using system GCC"); + test_arg.use_system_gcc = true; + } "--build-only" => test_arg.build_only = true, "--use-backend" => match args.next() { Some(backend) if !backend.is_empty() => test_arg.backend = Some(backend), @@ -180,11 +185,6 @@ impl TestArg { } } } - - if use_system_gcc { - println!("Using system GCC"); - test_arg.gcc_path = Some("gcc".to_string()); - } } match (test_arg.current_part, test_arg.nb_parts) { (Some(_), Some(_)) | (None, None) => {} @@ -703,7 +703,7 @@ fn test_projects(env: &Env, args: &TestArg) -> Result<(), String> { //"https://github.com/rust-lang/cargo", // TODO: very slow, only run on master? ]; - let run_tests = |projects_path, iter: &mut dyn Iterator| -> Result<(), String> { + let run_tests = |projects_path, iter: &mut dyn Iterator| -> Result<(), String> { for project in iter { let clone_result = git_clone(project, Some(projects_path), true)?; let repo_path = Path::new(&clone_result.repo_dir); @@ -727,8 +727,7 @@ fn test_projects(env: &Env, args: &TestArg) -> Result<(), String> { let start = current_part * count; // We remove the projects we don't want to test. run_tests(projects_path, &mut projects.iter().skip(start).take(count))?; - } - else { + } else { run_tests(projects_path, &mut projects.iter())?; } @@ -1166,15 +1165,17 @@ pub fn run() -> Result<(), String> { }; let mut env: HashMap = std::env::vars().collect(); - args.config_info.setup_gcc_path(None)?; - env.insert( - "LIBRARY_PATH".to_string(), - args.config_info.gcc_path.clone(), - ); - env.insert( - "LD_LIBRARY_PATH".to_string(), - args.config_info.gcc_path.clone(), - ); + if !args.use_system_gcc { + args.config_info.setup_gcc_path()?; + env.insert( + "LIBRARY_PATH".to_string(), + args.config_info.gcc_path.clone(), + ); + env.insert( + "LD_LIBRARY_PATH".to_string(), + args.config_info.gcc_path.clone(), + ); + } build_if_no_backend(&env, &args)?; if args.build_only { @@ -1182,7 +1183,7 @@ pub fn run() -> Result<(), String> { return Ok(()); } - args.config_info.setup(&mut env, args.gcc_path.as_deref())?; + args.config_info.setup(&mut env, args.use_system_gcc)?; if args.runners.is_empty() { run_all(&env, &args)?; diff --git a/build_system/src/utils.rs b/build_system/src/utils.rs index b288eff94a5..046008ae1a2 100644 --- a/build_system/src/utils.rs +++ b/build_system/src/utils.rs @@ -254,7 +254,11 @@ pub struct CloneResult { pub repo_dir: String, } -pub fn git_clone(to_clone: &str, dest: Option<&Path>, shallow_clone: bool) -> Result { +pub fn git_clone( + to_clone: &str, + dest: Option<&Path>, + shallow_clone: bool, +) -> Result { let repo_name = to_clone.split('/').last().unwrap(); let repo_name = match repo_name.strip_suffix(".git") { Some(n) => n.to_string(),