Skip to content

Commit 12b0e8a

Browse files
committed
clang: don't set -march for overridden target
If -target is explicitly passed to clang, we shouldn't pass our -march value for the default target, because it probably won't exist for the target being used. Up until now, clang has been lenient with this, but it's a hard error with clang 17, so since gcc.arch is always set on aarch64, fixing this is a hard requirement for upgrading our default clang to 17. Before (with clang 17 on aarch64-linux): $ clang -target bpf -c -o /dev/null test.bpf.c clang: warning: ignoring '-fstack-protector-strong' option as it is not currently supported for target 'bpf' [-Woption-ignored] clang: error: unsupported option '-march=' for target 'bpf' clang: warning: argument unused during compilation: '--gcc-toolchain=/nix/store/cngak08nb1yk44gnjipv5rg1ahh1xkax-gcc-13.2.0' [-Wunused-command-line-argument] After: $ clang -target bpf -c -o /dev/null test.bpf.c clang: warning: ignoring '-fstack-protector-strong' option as it is not currently supported for target 'bpf' [-Woption-ignored] clang: warning: argument unused during compilation: '--gcc-toolchain=/nix/store/cngak08nb1yk44gnjipv5rg1ahh1xkax-gcc-13.2.0' [-Wunused-command-line-argument]
1 parent 269ce72 commit 12b0e8a

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

pkgs/build-support/cc-wrapper/add-clang-cc-cflags-before.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ for p in "${params[@]}"; do
77
done
88

99
if $needsTarget; then
10-
extraBefore+=(-target @defaultTarget@)
10+
extraBefore+=(-target @defaultTarget@ @march@)
1111
fi

pkgs/build-support/cc-wrapper/default.nix

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,11 @@ stdenv.mkDerivation {
595595
# Always add -march based on cpu in triple. Sometimes there is a
596596
# discrepency (x86_64 vs. x86-64), so we provide an "arch" arg in
597597
# that case.
598+
#
599+
# For clang, this is handled in add-clang-cc-cflags-before.sh
600+
598601
# TODO: aarch64-darwin has mcpu incompatible with gcc
599-
+ optionalString ((targetPlatform ? gcc.arch) && (isClang || !(stdenv.isDarwin && stdenv.isAarch64)) &&
602+
+ optionalString ((targetPlatform ? gcc.arch) && !isClang && !(stdenv.isDarwin && stdenv.isAarch64) &&
600603
isGccArchSupported targetPlatform.gcc.arch) ''
601604
echo "-march=${targetPlatform.gcc.arch}" >> $out/nix-support/cc-cflags-before
602605
''
@@ -685,6 +688,10 @@ stdenv.mkDerivation {
685688
## Needs to go after ^ because the for loop eats \n and makes this file an invalid script
686689
##
687690
+ optionalString isClang ''
691+
# Escape twice: once for this script, once for the one it gets substituted into.
692+
export march=${lib.escapeShellArg
693+
(lib.optionalString (targetPlatform ? gcc.arch)
694+
(lib.escapeShellArg "-march=${targetPlatform.gcc.arch}"))}
688695
export defaultTarget=${targetPlatform.config}
689696
substituteAll ${./add-clang-cc-cflags-before.sh} $out/nix-support/add-local-cc-cflags-before.sh
690697
''

0 commit comments

Comments
 (0)