Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

varphone
Copy link

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:

  1. Add Build::c_flag() and Build::cpp_flag() methods to add independent flags to C or C++ files.
  2. Add src argument to Tool::to_command() method to auto-detect c/cpp compiler by source file extension for Clang/Gnu toolset.

Signed-off-by: Varphone Wong <varphone@qq.com>
…lset

Signed-off-by: Varphone Wong <varphone@qq.com>
@varphone varphone marked this pull request as ready for review March 28, 2024 05:31
Copy link
Collaborator

@NobodyXu NobodyXu left a 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;
}
}
_ => {}
Copy link
Collaborator

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?

Copy link
Author

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.

Copy link
Collaborator

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 {
Copy link
Collaborator

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);
Copy link
Collaborator

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.

Comment on lines +1827 to +1834
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());
}

Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants