Skip to content

Commit

Permalink
Handle -Xarch_<arch> <arg> for Clang
Browse files Browse the repository at this point in the history
A new ArgDisposition is introduced that combines the Concatenated
and Separated behaviors, but produces results similar to a plain
Separated argument with the full original -Xarch_<arch> argument.

Fixes #2090
  • Loading branch information
torarnv authored and alcroito committed Feb 21, 2025
1 parent c719e5a commit 4d7952d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/compiler/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub enum ArgDisposition {
CanBeSeparated(Delimiter),
/// As "-arg<delimiter>value"
Concatenated(Delimiter),
/// As "-arg<delimiter>suffix value"
ConcatenatedAndSeparated(Delimiter),
}

pub enum NormalizedDisposition {
Expand Down Expand Up @@ -456,6 +458,20 @@ impl<T: ArgumentValue> ArgInfo<T> {
a => a?,
}
}
ArgInfo::TakeArg(s, create, ArgDisposition::ConcatenatedAndSeparated(_)) => {
let len = s.len();
debug_assert_eq!(&arg[..len], s);

if let Some(a) = get_next_arg() {
Argument::WithValue(
arg.to_string().leak(),
create(a)?,
ArgDisposition::Separated,
)
} else {
return Err(ArgParseError::UnexpectedEndOfArgs);
}
}
})
}

Expand All @@ -471,6 +487,7 @@ impl<T: ArgumentValue> ArgInfo<T> {
}
&ArgInfo::TakeArg(s, _, ArgDisposition::CanBeSeparated(Some(d)))
| &ArgInfo::TakeArg(s, _, ArgDisposition::Concatenated(Some(d)))
| &ArgInfo::TakeArg(s, _, ArgDisposition::ConcatenatedAndSeparated(Some(d)))
if arg.len() > s.len() && arg.starts_with(s) =>
{
arg.as_bytes()[s.len()].cmp(&d)
Expand Down Expand Up @@ -855,6 +872,16 @@ mod tests {
info.process("-foo", || Some("bar".into())).unwrap(),
arg!(WithValue("-foo", Foo("bar"), CanBeConcatenated('=')))
);

let info = take_arg!("-foo", OsString, ConcatenatedAndSeparated('_'), Foo);
assert_eq!(
info.clone().process("-foo_bar", || None).unwrap_err(),
ArgParseError::UnexpectedEndOfArgs
);
assert_eq!(
info.process("-foo_bar", || Some("baz".into())).unwrap(),
arg!(WithValue("-foo_bar", Foo("baz"), Separated))
);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/compiler/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ counted_array!(pub static ARGS: [ArgInfo<gcc::ArgData>; _] = [
take_arg!("-MT", OsString, CanBeSeparated, DepTarget),
flag!("-Wno-unknown-cuda-version", PassThroughFlag),
flag!("-Wno-unused-parameter", PassThroughFlag),
take_arg!("-Xarch", OsString, ConcatenatedAndSeparated('_'), PassThrough),
take_arg!("-Xclang", OsString, Separated, XClang),
take_arg!("-add-plugin", OsString, Separated, PassThrough),
take_arg!("-debug-info-kind", OsString, Concatenated('='), PassThrough),
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/diab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ where
match arg {
Argument::WithValue(_, ref v, ArgDisposition::Separated)
| Argument::WithValue(_, ref v, ArgDisposition::CanBeConcatenated(_))
| Argument::WithValue(_, ref v, ArgDisposition::CanBeSeparated(_)) => {
| Argument::WithValue(_, ref v, ArgDisposition::CanBeSeparated(_))
| Argument::WithValue(_, ref v, ArgDisposition::ConcatenatedAndSeparated(_)) => {
if v.clone().into_arg_os_string().starts_with("@") {
cannot_cache!("@");
}
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ where
match arg {
Argument::WithValue(_, ref v, ArgDisposition::Separated)
| Argument::WithValue(_, ref v, ArgDisposition::CanBeConcatenated(_))
| Argument::WithValue(_, ref v, ArgDisposition::CanBeSeparated(_)) => {
| Argument::WithValue(_, ref v, ArgDisposition::CanBeSeparated(_))
| Argument::WithValue(_, ref v, ArgDisposition::ConcatenatedAndSeparated(_)) => {
if v.clone().into_arg_os_string().starts_with("@") {
cannot_cache!("@");
}
Expand Down
40 changes: 39 additions & 1 deletion tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ fn compile_hip_cmdline<T: AsRef<OsStr>>(
}

const INPUT: &str = "test.c";
const INPUT_HEADER: &str = "test.h";
const INPUT_CLANG_MULTICALL: &str = "test_clang_multicall.c";
const INPUT_WITH_WHITESPACE: &str = "test_whitespace.c";
const INPUT_WITH_WHITESPACE_ALT: &str = "test_whitespace_alt.c";
Expand Down Expand Up @@ -672,11 +673,15 @@ fn run_sccache_command_tests(compiler: Compiler, tempdir: &Path, preprocessor_ca
),
};
test_clang_cache_whitespace_normalization(
compiler,
compiler.clone(),
tempdir,
!is_appleclang && major >= 14,
preprocessor_cache_mode,
);

if is_appleclang {
test_clang_xarch_flags(compiler, tempdir)
}
} else {
test_clang_cache_whitespace_normalization(
compiler,
Expand Down Expand Up @@ -1720,6 +1725,39 @@ fn test_clang_cache_whitespace_normalization(
}
}

// Checks that the -include$PWD/test.h flag does not get re-ordered and
// placed somewhere other than directly after the -Xarch_<arch> flag.
// If it gets re-ordered, the compilation will fail.
fn test_clang_xarch_flags(compiler: Compiler, tempdir: &Path) {
let Compiler {
name,
exe,
env_vars,
} = compiler;
println!("test_clang_xarch_flags: {}", name);
// Compile a source file.
copy_to_tempdir(&[INPUT, INPUT_HEADER], tempdir);

let test_header_path = tempdir.join(INPUT_HEADER);
let include_arg = format!("-include{}", test_header_path.display());

let mut current_host_arch = std::env::consts::ARCH;
if current_host_arch == "aarch64" {
current_host_arch = "arm64";
}
let xarch_flag = format!("-Xarch_{}", current_host_arch);

let extra_args = vec![xarch_flag.into(), include_arg.into()];

debug!("compile xarch_flags");
sccache_command()
.args(compile_cmdline(name, exe, INPUT, OUTPUT, extra_args))
.current_dir(tempdir)
.envs(env_vars)
.assert()
.success();
}

#[cfg(unix)]
fn find_compilers() -> Vec<Compiler> {
let cwd = env::current_dir().unwrap();
Expand Down
1 change: 1 addition & 0 deletions tests/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void foo();

0 comments on commit 4d7952d

Please sign in to comment.