diff --git a/ctest/src/generator.rs b/ctest/src/generator.rs index b79d319fe950a..3a976ba7d3667 100644 --- a/ctest/src/generator.rs +++ b/ctest/src/generator.rs @@ -62,6 +62,7 @@ pub struct TestGenerator { pub(crate) includes: Vec, out_dir: Option, pub(crate) flags: Vec, + pub(crate) flags_if_supported: Vec, pub(crate) global_defines: Vec<(String, Option)>, cfg: Vec<(String, Option)>, mapped_names: Vec, @@ -670,6 +671,15 @@ impl TestGenerator { self } + /// Add a flag to the C compiler invocation if the compiler supports it. + /// + /// This is useful for warning suppressions that are only available on some + /// compiler families or versions. + pub fn flag_if_supported(&mut self, flag: &str) -> &mut Self { + self.flags_if_supported.push(flag.to_string()); + self + } + /// Set a `-D` flag for the C compiler being called. /// /// This can be used to define various global variables to configure how header diff --git a/ctest/src/runner.rs b/ctest/src/runner.rs index d55bd50bc7275..26c9d82cb2d38 100644 --- a/ctest/src/runner.rs +++ b/ctest/src/runner.rs @@ -38,6 +38,7 @@ pub fn generate_test( let mut cfg = cc::Build::new(); cfg.file(output_file_path.with_extension(generator.language.extension())); cfg.host(&host); + cfg.target(&target); if target.contains("msvc") { cfg.flag("/W3") @@ -63,10 +64,12 @@ pub fn generate_test( .flag("-Werror") .flag("-Wno-unused-parameter") .flag("-Wno-type-limits") - // allow taking address of packed struct members: - .flag("-Wno-address-of-packed-member") - .flag("-Wno-unknown-warning-option") - .flag("-Wno-deprecated-declarations"); // allow deprecated items + .flag("-Wno-deprecated-declarations") // allow deprecated items + // Probe support instead of assuming a compiler family. This keeps + // the suppression on newer GCC/Clang while avoiding regressions on + // older toolchains that reject these flags. + .flag_if_supported("-Wno-address-of-packed-member") + .flag_if_supported("-Wno-unknown-warning-option"); } for p in &generator.includes { @@ -77,6 +80,10 @@ pub fn generate_test( cfg.flag(flag); } + for flag in &generator.flags_if_supported { + cfg.flag_if_supported(flag); + } + for (k, v) in &generator.global_defines { cfg.define(k, v.as_ref().map(|s| &s[..])); } @@ -84,8 +91,7 @@ pub fn generate_test( cfg.cpp(matches!(generator.language, Language::CXX)); let stem: &str = output_file_path.file_stem().unwrap().to_str().unwrap(); - cfg.target(&target) - .out_dir(output_file_path.parent().unwrap()) + cfg.out_dir(output_file_path.parent().unwrap()) .compile(stem); Ok(output_file_path)