clang, cc-wrapper: make wrapper flang-aware

Build on the initial LLVM Flang wrapper work by exposing the needed clang passthru and teaching cc-wrapper about flang-specific flag handling.

Co-authored-by: stove <stove@rivosinc.com>
Co-authored-by: acture <acture@gmail.com>
Co-authored-by: Emily <hello@emily.moe>
Co-authored-by: Philip Taron <philip.taron@gmail.com>
This commit is contained in:
Acture 2026-03-30 17:03:04 +08:00
commit 6014579a95
5 changed files with 48 additions and 20 deletions

View file

@ -36,6 +36,6 @@ elif [[ $0 != *cpp ]]; then
fi
fi
if [[ "@darwinMinVersion@" ]]; then
if [[ "@darwinMinVersion@" ]] && [ "@isFlang@" != 1 ]; then
extraBefore+=(-Werror=unguarded-availability)
fi

View file

@ -11,6 +11,8 @@ var_templates_list=(
NIX_CXXSTDLIB_COMPILE
NIX_CXXSTDLIB_LINK
NIX_GNATFLAGS_COMPILE
NIX_FFLAGS_COMPILE
NIX_FFLAGS_COMPILE_BEFORE
)
var_templates_bool=(
NIX_ENFORCE_NO_NATIVE

View file

@ -44,7 +44,7 @@ while (( "$n" < "$nParams" )); do
case "$p" in
-[cSEM] | -MM) dontLink=1 ;;
-cc1) cc1=1 ;;
-cc1 | -fc1 ) cc1=1 ;;
-nostdinc) cInclude=0 cxxInclude=0 ;;
-nostdinc++) cxxInclude=0 ;;
-nostdlib) cxxLibrary=0 ;;
@ -185,9 +185,19 @@ fi
source @out@/nix-support/add-hardening.sh
# Add the flags for the C compiler proper.
extraAfter=(${hardeningCFlagsAfter[@]+"${hardeningCFlagsAfter[@]}"} $NIX_CFLAGS_COMPILE_@suffixSalt@)
extraBefore=(${hardeningCFlagsBefore[@]+"${hardeningCFlagsBefore[@]}"} $NIX_CFLAGS_COMPILE_BEFORE_@suffixSalt@)
# Add the flags for the compiler proper. Flang reads its user-supplied
# flags from the Fortran-specific NIX_FFLAGS_COMPILE channel so that
# C-only flags injected by setup hooks (e.g. -frandom-seed= from
# reproducible-builds.sh, which Flang does not accept) never reach the
# Fortran driver. This mirrors the NIX_GNATFLAGS_COMPILE channel that
# the Ada/GNAT wrapper uses for the same reason.
if [ "@isFlang@" = 1 ]; then
extraAfter=(${hardeningCFlagsAfter[@]+"${hardeningCFlagsAfter[@]}"} $NIX_FFLAGS_COMPILE_@suffixSalt@)
extraBefore=(${hardeningCFlagsBefore[@]+"${hardeningCFlagsBefore[@]}"} $NIX_FFLAGS_COMPILE_BEFORE_@suffixSalt@)
else
extraAfter=(${hardeningCFlagsAfter[@]+"${hardeningCFlagsAfter[@]}"} $NIX_CFLAGS_COMPILE_@suffixSalt@)
extraBefore=(${hardeningCFlagsBefore[@]+"${hardeningCFlagsBefore[@]}"} $NIX_CFLAGS_COMPILE_BEFORE_@suffixSalt@)
fi
if [ "$dontLink" != 1 ]; then
linkType=$(checkLinkType $NIX_LDFLAGS_BEFORE_@suffixSalt@ "${params[@]}" ${NIX_CFLAGS_LINK_@suffixSalt@:-} $NIX_LDFLAGS_@suffixSalt@)

View file

@ -26,6 +26,7 @@
nixSupport ? { },
isGNU ? false,
isClang ? cc.isClang or false,
isFlang ? cc.isFlang or false,
isZig ? cc.isZig or false,
isArocc ? cc.isArocc or false,
isCcache ? cc.isCcache or false,
@ -385,7 +386,9 @@ let
#
# TODO: Drop `mangle-NIX_STORE-in-__FILE__.patch` from GCC and make
# this unconditional once the upstream bug is fixed.
useMacroPrefixMap = !isGNU;
useMacroPrefixMap = !isGNU && !isFlang;
systemIncludeFlag = if isFlang || isArocc then "-I" else "-idirafter";
fortifyIncludeFlag = if isFlang then "-I" else "-isystem";
in
assert includeFortifyHeaders' -> fortify-headers != null;
@ -573,10 +576,18 @@ stdenvNoCC.mkDerivation {
''
+ optionalString cc.langFortran or false ''
wrap ${targetPrefix}gfortran $wrapper $ccPath/${targetPrefix}gfortran
ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}g77
ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}f77
export named_fc=${targetPrefix}gfortran
if [ -e $ccPath/${targetPrefix}gfortran ]; then
wrap ${targetPrefix}gfortran $wrapper $ccPath/${targetPrefix}gfortran
ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}g77
ln -sv ${targetPrefix}gfortran $out/bin/${targetPrefix}f77
export named_fc=${targetPrefix}gfortran
elif [ -e $ccPath/${targetPrefix}flang ]; then
wrap ${targetPrefix}flang $wrapper $ccPath/${targetPrefix}flang
export named_fc=${targetPrefix}flang
elif [ -e $ccPath/flang ]; then
wrap ${targetPrefix}flang $wrapper $ccPath/flang
export named_fc=${targetPrefix}flang
fi
''
+ optionalString cc.langGo or false ''
@ -712,9 +723,7 @@ stdenvNoCC.mkDerivation {
echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags
''
+ ''
include "-${
if isArocc then "I" else "idirafter"
}" "${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags
include "${systemIncludeFlag}" "${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags
''
+ optionalString isGNU ''
for dir in "${cc}"/lib/gcc/*/*/include-fixed; do
@ -722,9 +731,9 @@ stdenvNoCC.mkDerivation {
done
''
+ optionalString (libc.w32api or null != null) ''
echo '-idirafter ${lib.getDev libc.w32api}${
include "${systemIncludeFlag}" "${lib.getDev libc.w32api}${
libc.incdir or "/include/w32api"
}' >> $out/nix-support/libc-cflags
}" >> $out/nix-support/libc-cflags
''
+ ''
@ -739,7 +748,7 @@ stdenvNoCC.mkDerivation {
# like option that forces the libc headers before all -idirafter,
# hence -isystem here.
+ optionalString includeFortifyHeaders' ''
include -isystem "${fortify-headers}/include" >> $out/nix-support/libc-cflags
include "${fortifyIncludeFlag}" "${fortify-headers}/include" >> $out/nix-support/libc-cflags
''
)
@ -760,7 +769,7 @@ stdenvNoCC.mkDerivation {
# already knows how to find its own libstdc++, and adding
# additional -isystem flags will confuse gfortran (see
# https://github.com/NixOS/nixpkgs/pull/209870#issuecomment-1500550903)
+ optionalString (libcxx == null && isClang && (useGccForLibs && gccForLibs.langCC or false)) ''
+ optionalString (libcxx == null && isClang && useGccForLibs && (cc.langCC or false)) ''
for dir in ${gccForLibs}/include/c++/*; do
include -cxx-isystem "$dir" >> $out/nix-support/libcxx-cxxflags
done
@ -827,6 +836,7 @@ stdenvNoCC.mkDerivation {
optionalString
(
(cc.isClang or false)
&& !isFlang
&& !(cc.isROCm or false)
&& !targetPlatform.isDarwin
&& !targetPlatform.isAndroid
@ -861,7 +871,8 @@ stdenvNoCC.mkDerivation {
let
enable_fp = !targetPlatform.isx86_32 && !targetPlatform.isS390;
enable_leaf_fp =
enable_fp
!isFlang
&& enable_fp
&& (
targetPlatform.isx86_64
|| targetPlatform.isAarch64
@ -927,7 +938,7 @@ stdenvNoCC.mkDerivation {
# well with multi line flags, so make the flags single line again
+ ''
for flags in "$out/nix-support"/*flags*; do
substituteInPlace "$flags" --replace $'\n' ' '
substituteInPlace "$flags" --replace-quiet $'\n' ' '
done
substituteAll ${./add-flags.sh} $out/nix-support/add-flags.sh
@ -973,6 +984,7 @@ stdenvNoCC.mkDerivation {
env = {
inherit isClang;
inherit isFlang;
# for substitution in utils.bash
# TODO(@sternenseemann): invent something cleaner than passing in "" in case of absence

View file

@ -16,6 +16,7 @@
fixDarwinDylibNames,
enableManpages ? false,
enableClangToolsExtra ? true,
extraPatches ? [ ],
devExtraCmakeFlags ? [ ],
replaceVars,
getVersionFile,
@ -73,7 +74,8 @@ stdenv.mkDerivation (
];
stripLen = 1;
hash = "sha256-1NKej08R9SPlbDY/5b0OKUsHjX07i9brR84yXiPwi7E=";
});
})
++ extraPatches;
nativeBuildInputs = [
cmake
@ -189,6 +191,8 @@ stdenv.mkDerivation (
passthru = {
inherit libllvm;
isClang = true;
langC = true;
langCC = true;
hardeningUnsupportedFlagsByTargetPlatform =
targetPlatform:
[ "fortify3" ]