Skip to content

Commit 6acdd67

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

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
@@ -265,6 +265,8 @@ pub struct Build {
265265
known_flag_support_status: Arc<Mutex<HashMap<String, bool>>>,
266266
ar_flags: Vec<Arc<str>>,
267267
asm_flags: Vec<Arc<str>>,
268+
c_flags: Vec<Arc<str>>,
269+
cpp_flags: Vec<Arc<str>>,
268270
no_default_flags: bool,
269271
files: Vec<Arc<Path>>,
270272
cpp: bool,
@@ -388,6 +390,8 @@ impl Build {
388390
known_flag_support_status: Arc::new(Mutex::new(HashMap::new())),
389391
ar_flags: Vec::new(),
390392
asm_flags: Vec::new(),
393+
c_flags: Vec::new(),
394+
cpp_flags: Vec::new(),
391395
no_default_flags: false,
392396
files: Vec::new(),
393397
shared_flag: None,
@@ -564,6 +568,36 @@ impl Build {
564568
self
565569
}
566570

571+
/// Add an arbitrary flag to the invocation of the compiler for c files
572+
///
573+
/// # Example
574+
///
575+
/// ```no_run
576+
/// cc::Build::new()
577+
/// .file("src/foo.c")
578+
/// .c_flag("-std=c99")
579+
/// .compile("foo");
580+
/// ```
581+
pub fn c_flag(&mut self, flag: &str) -> &mut Build {
582+
self.c_flags.push(flag.into());
583+
self
584+
}
585+
586+
/// Add an arbitrary flag to the invocation of the compiler for cpp files
587+
///
588+
/// # Example
589+
///
590+
/// ```no_run
591+
/// cc::Build::new()
592+
/// .file("src/foo.cpp")
593+
/// .cpp_flag("-std=c++17")
594+
/// .compile("foo");
595+
/// ```
596+
pub fn cpp_flag(&mut self, flag: &str) -> &mut Build {
597+
self.cpp_flags.push(flag.into());
598+
self
599+
}
600+
567601
fn ensure_check_file(&self) -> Result<PathBuf, Error> {
568602
let out_dir = self.get_out_dir()?;
569603
let src = if self.cuda {
@@ -1790,6 +1824,14 @@ impl Build {
17901824
cmd.push_cc_arg(warnings_to_errors_flag);
17911825
}
17921826

1827+
for flag in self.c_flags.iter() {
1828+
cmd.c_args.push((**flag).into());
1829+
}
1830+
1831+
for flag in self.cpp_flags.iter() {
1832+
cmd.cpp_args.push((**flag).into());
1833+
}
1834+
17931835
Ok(cmd)
17941836
}
17951837

src/tool.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct Tool {
2929
pub(crate) cc_wrapper_path: Option<PathBuf>,
3030
pub(crate) cc_wrapper_args: Vec<OsString>,
3131
pub(crate) args: Vec<OsString>,
32+
pub(crate) c_args: Vec<OsString>,
33+
pub(crate) cpp_args: Vec<OsString>,
3234
pub(crate) env: Vec<(OsString, OsString)>,
3335
pub(crate) family: ToolFamily,
3436
pub(crate) cuda: bool,
@@ -77,6 +79,8 @@ impl Tool {
7779
cc_wrapper_path: None,
7880
cc_wrapper_args: Vec::new(),
7981
args: Vec::new(),
82+
c_args: Vec::new(),
83+
cpp_args: Vec::new(),
8084
env: Vec::new(),
8185
family,
8286
cuda: false,
@@ -193,6 +197,8 @@ impl Tool {
193197
cc_wrapper_path: None,
194198
cc_wrapper_args: Vec::new(),
195199
args: Vec::new(),
200+
c_args: Vec::new(),
201+
cpp_args: Vec::new(),
196202
env: Vec::new(),
197203
family,
198204
cuda,

0 commit comments

Comments
 (0)