New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add independent flags to C or C++ files #1019
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Varphone Wong <varphone@qq.com>
…lset Signed-off-by: Varphone Wong <varphone@qq.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
There are a few things I'd like you to change.
Also, can you add some tests for this functionality in tests/test.rs
please?
extra_args = &self.cpp_args; | ||
} | ||
} | ||
_ => {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the c_flags
and cpp_flags
are ignored for msvc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MSVC has only one cl.exe
and has no special parameters for C
or C++
, so it can be ignored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't have -std=c11
which is used in example?
/// .cpp_flag("-std=c++17") | ||
/// .compile("foo"); | ||
/// ``` | ||
pub fn cpp_flag(&mut self, flag: &str) -> &mut Build { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think cxx_flags
makes more sense.
cpp
could be confused with pre-processors of C/C++ compiler.
@@ -1633,7 +1667,7 @@ impl Build { | |||
/// This will return a result instead of panicking; see expand() for the complete description. | |||
pub fn try_expand(&self) -> Result<Vec<u8>, Error> { | |||
let compiler = self.try_get_compiler()?; | |||
let mut cmd = compiler.to_command(); | |||
let mut cmd = compiler.to_command(None); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm I wonder if there's any C/C++ that affects the expand of C/C++ program?
I can't think of any specific to C or specific to C++, AFAIK the only way to affect that is via -D
.
Maybe changing std version would affect that, I recalled that newer C standard introduces more macros.
for flag in self.c_flags.iter() { | ||
cmd.c_args.push((**flag).into()); | ||
} | ||
|
||
for flag in self.cpp_flags.iter() { | ||
cmd.cpp_args.push((**flag).into()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need to do this in get_base_compiler
, there are some functions which call it directly.
This PR introduces a new feature for C/C++ flags, primarily to support the compilation of mixed C/C++ code.
Sometimes we use both C and C++ languages in a single project. However, the compilers for C and C++ are different, so we need to specify different compilers during compilation. This PR adds independent compiler flags for C and C++ files, allowing us to specify different compilers during compilation.
The main changes in this PR are as follows:
Build::c_flag()
andBuild::cpp_flag()
methods to add independent flags to C or C++ files.src
argument toTool::to_command()
method to auto-detect c/cpp compiler by source file extension for Clang/Gnu toolset.