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.

@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.

src/lib.rs Show resolved Hide resolved
Comment on lines +1827 to +1872
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.

///
/// The `src` argument is used to determine the preferred compiler for the
/// source file. If `None`, the default compiler is used.
pub fn to_command(&self, src: Option<&PathBuf>) -> Command {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking change, please move this to a new function.

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.

2 participants