Skip to content

Commit

Permalink
Make is_flag_supported_inner take an &Tool (#1337)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisDenton authored Jan 1, 2025
1 parent 05a529c commit cfa61d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
17 changes: 5 additions & 12 deletions src/flags.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::target::TargetInfo;
use crate::{Build, Error, ErrorKind, ToolFamily};
use crate::{Build, Error, ErrorKind, Tool, ToolFamily};
use std::borrow::Cow;
use std::ffi::OsString;
use std::path::Path;

#[derive(Debug, PartialEq, Default)]
pub(crate) struct RustcCodegenFlags<'a> {
Expand Down Expand Up @@ -158,21 +157,15 @@ impl<'this> RustcCodegenFlags<'this> {
}

// Rust and clang/cc don't agree on what equivalent flags should look like.
pub(crate) fn cc_flags(
&self,
build: &Build,
path: &Path,
family: ToolFamily,
target: &TargetInfo,
flags: &mut Vec<OsString>,
) {
pub(crate) fn cc_flags(&self, build: &Build, tool: &mut Tool, target: &TargetInfo) {
let family = tool.family;
// Push `flag` to `flags` if it is supported by the currently used CC
let mut push_if_supported = |flag: OsString| {
if build
.is_flag_supported_inner(&flag, path, target)
.is_flag_supported_inner(&flag, tool, target)
.unwrap_or(false)
{
flags.push(flag);
tool.args.push(flag);
} else {
build.cargo_output.print_warning(&format!(
"Inherited flag {:?} is not supported by the currently used CC",
Expand Down
16 changes: 6 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,19 +645,19 @@ impl Build {
pub fn is_flag_supported(&self, flag: impl AsRef<OsStr>) -> Result<bool, Error> {
self.is_flag_supported_inner(
flag.as_ref(),
self.get_base_compiler()?.path(),
&self.get_base_compiler()?,
&self.get_target()?,
)
}

fn is_flag_supported_inner(
&self,
flag: &OsStr,
compiler_path: &Path,
tool: &Tool,
target: &TargetInfo<'_>,
) -> Result<bool, Error> {
let compiler_flag = CompilerFlag {
compiler: compiler_path.into(),
compiler: tool.path().into(),
flag: flag.into(),
};

Expand All @@ -679,7 +679,7 @@ impl Build {
let mut compiler = {
let mut cfg = Build::new();
cfg.flag(flag)
.compiler(compiler_path)
.compiler(tool.path())
.cargo_metadata(self.cargo_output.metadata)
.opt_level(0)
.debug(false)
Expand Down Expand Up @@ -1957,7 +1957,7 @@ impl Build {

for flag in self.flags_supported.iter() {
if self
.is_flag_supported_inner(flag, &cmd.path, &target)
.is_flag_supported_inner(flag, &cmd, &target)
.unwrap_or(false)
{
cmd.push_cc_arg((**flag).into());
Expand Down Expand Up @@ -2438,13 +2438,9 @@ impl Build {
None => return Ok(()),
};

let Tool {
family, path, args, ..
} = cmd;

let env = env_os.to_string_lossy();
let codegen_flags = RustcCodegenFlags::parse(&env)?;
codegen_flags.cc_flags(self, path, *family, target, args);
codegen_flags.cc_flags(self, cmd, target);
Ok(())
}

Expand Down

0 comments on commit cfa61d1

Please sign in to comment.