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
10 changes: 10 additions & 0 deletions ctest/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct TestGenerator {
pub(crate) includes: Vec<PathBuf>,
out_dir: Option<PathBuf>,
pub(crate) flags: Vec<String>,
pub(crate) flags_if_supported: Vec<String>,
pub(crate) global_defines: Vec<(String, Option<String>)>,
cfg: Vec<(String, Option<String>)>,
mapped_names: Vec<MappedName>,
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions ctest/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand All @@ -77,15 +80,18 @@ 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[..]));
}

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)
Expand Down
Loading