Skip to content

Commit ecbdfab

Browse files
committed
Add independent flags to C or C++ files
Signed-off-by: Varphone Wong <varphone@qq.com>
1 parent 3ba2356 commit ecbdfab

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ pub struct Build {
270270
known_flag_support_status_cache: Arc<Mutex<HashMap<CompilerFlag, bool>>>,
271271
ar_flags: Vec<Arc<str>>,
272272
asm_flags: Vec<Arc<str>>,
273+
c_flags: Vec<Arc<str>>,
274+
cpp_flags: Vec<Arc<str>>,
273275
no_default_flags: bool,
274276
files: Vec<Arc<Path>>,
275277
cpp: bool,
@@ -393,6 +395,8 @@ impl Build {
393395
known_flag_support_status_cache: Arc::new(Mutex::new(HashMap::new())),
394396
ar_flags: Vec::new(),
395397
asm_flags: Vec::new(),
398+
c_flags: Vec::new(),
399+
cpp_flags: Vec::new(),
396400
no_default_flags: false,
397401
files: Vec::new(),
398402
shared_flag: None,
@@ -569,6 +573,36 @@ impl Build {
569573
self
570574
}
571575

576+
/// Add an arbitrary flag to the invocation of the compiler for c files
577+
///
578+
/// # Example
579+
///
580+
/// ```no_run
581+
/// cc::Build::new()
582+
/// .file("src/foo.c")
583+
/// .c_flag("-std=c99")
584+
/// .compile("foo");
585+
/// ```
586+
pub fn c_flag(&mut self, flag: &str) -> &mut Build {
587+
self.c_flags.push(flag.into());
588+
self
589+
}
590+
591+
/// Add an arbitrary flag to the invocation of the compiler for cpp files
592+
///
593+
/// # Example
594+
///
595+
/// ```no_run
596+
/// cc::Build::new()
597+
/// .file("src/foo.cpp")
598+
/// .cpp_flag("-std=c++17")
599+
/// .compile("foo");
600+
/// ```
601+
pub fn cpp_flag(&mut self, flag: &str) -> &mut Build {
602+
self.cpp_flags.push(flag.into());
603+
self
604+
}
605+
572606
fn ensure_check_file(&self) -> Result<PathBuf, Error> {
573607
let out_dir = self.get_out_dir()?;
574608
let src = if self.cuda {
@@ -1828,6 +1862,14 @@ impl Build {
18281862
cmd.push_cc_arg(warnings_to_errors_flag);
18291863
}
18301864

1865+
for flag in self.c_flags.iter() {
1866+
cmd.c_args.push((**flag).into());
1867+
}
1868+
1869+
for flag in self.cpp_flags.iter() {
1870+
cmd.cpp_args.push((**flag).into());
1871+
}
1872+
18311873
Ok(cmd)
18321874
}
18331875

src/tool.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct Tool {
3030
pub(crate) cc_wrapper_path: Option<PathBuf>,
3131
pub(crate) cc_wrapper_args: Vec<OsString>,
3232
pub(crate) args: Vec<OsString>,
33+
pub(crate) c_args: Vec<OsString>,
34+
pub(crate) cpp_args: Vec<OsString>,
3335
pub(crate) env: Vec<(OsString, OsString)>,
3436
pub(crate) family: ToolFamily,
3537
pub(crate) cuda: bool,
@@ -78,6 +80,8 @@ impl Tool {
7880
cc_wrapper_path: None,
7981
cc_wrapper_args: Vec::new(),
8082
args: Vec::new(),
83+
c_args: Vec::new(),
84+
cpp_args: Vec::new(),
8185
env: Vec::new(),
8286
family,
8387
cuda: false,
@@ -219,6 +223,8 @@ impl Tool {
219223
cc_wrapper_path: None,
220224
cc_wrapper_args: Vec::new(),
221225
args: Vec::new(),
226+
c_args: Vec::new(),
227+
cpp_args: Vec::new(),
222228
env: Vec::new(),
223229
family,
224230
cuda,

0 commit comments

Comments
 (0)