Merge pull request #318034 from ExpidusOS/feat/zig-cc

Add stdenv & CC support for Zig
This commit is contained in:
tomberek 2024-07-28 14:09:31 -04:00 committed by GitHub
commit cc7b011fe1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 226 additions and 14 deletions

View File

@ -27,8 +27,11 @@ for var in "${var_templates_bool[@]}"; do
mangleVarBool "$var" ${role_suffixes[@]+"${role_suffixes[@]}"} mangleVarBool "$var" ${role_suffixes[@]+"${role_suffixes[@]}"}
done done
# `-B@out@/bin' forces cc to use ld-wrapper.sh when calling ld. # Arocc does not support "-B"
NIX_CFLAGS_COMPILE_@suffixSalt@="-B@out@/bin/ $NIX_CFLAGS_COMPILE_@suffixSalt@" if [[ -z "@isArocc@" ]]; then
# `-B@out@/bin' forces cc to use ld-wrapper.sh when calling ld.
NIX_CFLAGS_COMPILE_@suffixSalt@="-B@out@/bin/ $NIX_CFLAGS_COMPILE_@suffixSalt@"
fi
# Export and assign separately in order that a failing $(..) will fail # Export and assign separately in order that a failing $(..) will fail
# the script. # the script.

View File

@ -79,6 +79,11 @@ if [ "$nonFlagArgs" = 0 ]; then
dontLink=1 dontLink=1
fi fi
# Arocc does not link
if [ "@isArocc@" = 1 ]; then
dontLink=1
fi
# Optionally filter out paths not refering to the store. # Optionally filter out paths not refering to the store.
if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then
kept=() kept=()

View File

@ -15,7 +15,9 @@
, propagateDoc ? cc != null && cc ? man , propagateDoc ? cc != null && cc ? man
, extraTools ? [], extraPackages ? [], extraBuildCommands ? "" , extraTools ? [], extraPackages ? [], extraBuildCommands ? ""
, nixSupport ? {} , nixSupport ? {}
, isGNU ? false, isClang ? cc.isClang or false, isCcache ? cc.isCcache or false, gnugrep ? null , isGNU ? false, isClang ? cc.isClang or false, isZig ? cc.isZig or false
, isArocc ? cc.isArocc or false, isCcache ? cc.isCcache or false
, gnugrep ? null
, expand-response-params , expand-response-params
, libcxx ? null , libcxx ? null
@ -304,6 +306,9 @@ stdenvNoCC.mkDerivation {
outputs = [ "out" ] ++ optionals propagateDoc [ "man" "info" ]; outputs = [ "out" ] ++ optionals propagateDoc [ "man" "info" ];
# Cannot be in "passthru" due to "substituteAll"
inherit isArocc;
passthru = { passthru = {
inherit targetPrefix suffixSalt; inherit targetPrefix suffixSalt;
# "cc" is the generic name for a C compiler, but there is no one for package # "cc" is the generic name for a C compiler, but there is no one for package
@ -311,7 +316,7 @@ stdenvNoCC.mkDerivation {
# Binutils, and Apple's "cctools"; "bintools" as an attempt to find an # Binutils, and Apple's "cctools"; "bintools" as an attempt to find an
# unused middle-ground name that evokes both. # unused middle-ground name that evokes both.
inherit bintools; inherit bintools;
inherit cc libc libcxx nativeTools nativeLibc nativePrefix isGNU isClang; inherit cc libc libcxx nativeTools nativeLibc nativePrefix isGNU isClang isZig;
emacsBufferSetup = pkgs: '' emacsBufferSetup = pkgs: ''
; We should handle propagation here too ; We should handle propagation here too
@ -391,6 +396,10 @@ stdenvNoCC.mkDerivation {
ln -s ${targetPrefix}clang $out/bin/${targetPrefix}cc ln -s ${targetPrefix}clang $out/bin/${targetPrefix}cc
export named_cc=${targetPrefix}clang export named_cc=${targetPrefix}clang
export named_cxx=${targetPrefix}clang++ export named_cxx=${targetPrefix}clang++
elif [ -e $ccPath/arocc ]; then
wrap ${targetPrefix}arocc $wrapper $ccPath/arocc
ln -s ${targetPrefix}arocc $out/bin/${targetPrefix}cc
export named_cc=${targetPrefix}arocc
fi fi
if [ -e $ccPath/${targetPrefix}g++ ]; then if [ -e $ccPath/${targetPrefix}g++ ]; then
@ -475,7 +484,7 @@ stdenvNoCC.mkDerivation {
# #
# TODO(@Ericson2314): Remove this after stable release and force # TODO(@Ericson2314): Remove this after stable release and force
# everyone to refer to bintools-wrapper directly. # everyone to refer to bintools-wrapper directly.
+ '' + optionalString (!isArocc) ''
if [[ -f "$bintools/nix-support/dynamic-linker" ]]; then if [[ -f "$bintools/nix-support/dynamic-linker" ]]; then
ln -s "$bintools/nix-support/dynamic-linker" "$out/nix-support" ln -s "$bintools/nix-support/dynamic-linker" "$out/nix-support"
fi fi
@ -491,7 +500,7 @@ stdenvNoCC.mkDerivation {
echo "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-cflags echo "-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-cflags
'' ''
+ optionalString useGccForLibs '' + optionalString (useGccForLibs && !isArocc) ''
echo "-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-ldflags echo "-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version}" >> $out/nix-support/cc-ldflags
echo "-L${gccForLibs_solib}/lib" >> $out/nix-support/cc-ldflags echo "-L${gccForLibs_solib}/lib" >> $out/nix-support/cc-ldflags
'' ''
@ -517,9 +526,9 @@ stdenvNoCC.mkDerivation {
'' ''
# this ensures that when clang passes -lgcc_s to lld (as it does # this ensures that when clang passes -lgcc_s to lld (as it does
# when building e.g. firefox), lld is able to find libgcc_s.so # when building e.g. firefox), lld is able to find libgcc_s.so
+ concatMapStrings (libgcc: '' + optionals (!isArocc) (concatMapStrings (libgcc: ''
echo "-L${libgcc}/lib" >> $out/nix-support/cc-ldflags echo "-L${libgcc}/lib" >> $out/nix-support/cc-ldflags
'') (toList (gccForLibs.libgcc or []))) '') (toList (gccForLibs.libgcc or []))))
## ##
## General libc support ## General libc support
@ -539,9 +548,10 @@ stdenvNoCC.mkDerivation {
+ optionalString (libc != null) ('' + optionalString (libc != null) (''
touch "$out/nix-support/libc-cflags" touch "$out/nix-support/libc-cflags"
touch "$out/nix-support/libc-ldflags" touch "$out/nix-support/libc-ldflags"
'' + optionalString (!isArocc) ''
echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags
'' + optionalString (!(cc.langD or false)) '' '' + optionalString (!(cc.langD or false)) ''
echo "-idirafter ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags echo "-${if isArocc then "I" else "idirafter"} ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags
'' + optionalString (isGNU && (!(cc.langD or false))) '' '' + optionalString (isGNU && (!(cc.langD or false))) ''
for dir in "${cc}"/lib/gcc/*/*/include-fixed; do for dir in "${cc}"/lib/gcc/*/*/include-fixed; do
echo '-idirafter' ''${dir} >> $out/nix-support/libc-cflags echo '-idirafter' ''${dir} >> $out/nix-support/libc-cflags
@ -597,7 +607,7 @@ stdenvNoCC.mkDerivation {
# ${cc_solib}/lib64 (even though it does actually search there...).. # ${cc_solib}/lib64 (even though it does actually search there...)..
# This confuses libtool. So add it to the compiler tool search # This confuses libtool. So add it to the compiler tool search
# path explicitly. # path explicitly.
+ optionalString (!nativeTools) '' + optionalString (!nativeTools && !isArocc) ''
if [ -e "${cc_solib}/lib64" -a ! -L "${cc_solib}/lib64" ]; then if [ -e "${cc_solib}/lib64" -a ! -L "${cc_solib}/lib64" ]; then
ccLDFlags+=" -L${cc_solib}/lib64" ccLDFlags+=" -L${cc_solib}/lib64"
ccCFlags+=" -B${cc_solib}/lib64" ccCFlags+=" -B${cc_solib}/lib64"
@ -605,7 +615,7 @@ stdenvNoCC.mkDerivation {
ccLDFlags+=" -L${cc_solib}/lib" ccLDFlags+=" -L${cc_solib}/lib"
ccCFlags+=" -B${cc_solib}/lib" ccCFlags+=" -B${cc_solib}/lib"
'' + optionalString cc.langAda or false '' '' + optionalString (cc.langAda or false && !isArocc) ''
touch "$out/nix-support/gnat-cflags" touch "$out/nix-support/gnat-cflags"
touch "$out/nix-support/gnat-ldflags" touch "$out/nix-support/gnat-ldflags"
basePath=$(echo $cc/lib/*/*/*) basePath=$(echo $cc/lib/*/*/*)
@ -626,7 +636,7 @@ stdenvNoCC.mkDerivation {
+ optionalString propagateDoc '' + optionalString propagateDoc ''
ln -s ${cc.man} $man ln -s ${cc.man} $man
ln -s ${cc.info} $info ln -s ${cc.info} $info
'' + optionalString (cc.langD or cc.langJava or false) '' '' + optionalString (cc.langD or cc.langJava or false && !isArocc) ''
echo "-B${zlib}${zlib.libdir or "/lib/"}" >> $out/nix-support/libc-cflags echo "-B${zlib}${zlib.libdir or "/lib/"}" >> $out/nix-support/libc-cflags
'' ''
@ -667,7 +677,7 @@ stdenvNoCC.mkDerivation {
hardening_unsupported_flags+=" stackprotector" hardening_unsupported_flags+=" stackprotector"
'' ''
+ optionalString (libc != null && targetPlatform.isAvr) '' + optionalString (libc != null && targetPlatform.isAvr && !isArocc) ''
for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do
echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-crt1-cflags echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-crt1-cflags
done done

View File

@ -90,6 +90,14 @@ python3.pkgs.buildPythonApplication rec {
# Fix extraframework lookup on case-sensitive APFS. # Fix extraframework lookup on case-sensitive APFS.
# https://github.com/mesonbuild/meson/pull/13038 # https://github.com/mesonbuild/meson/pull/13038
./007-case-sensitive-fs.patch ./007-case-sensitive-fs.patch
# Fix meson's detection for zig's linker
# https://github.com/mesonbuild/meson/pull/12293
(fetchpatch {
name = "linker-support-zig-cc.patch";
url = "https://github.com/mesonbuild/meson/pull/12293/commits/2baae244c995794d9addfe6ed924dfa72f01be82.patch";
hash = "sha256-dDOmSRBKl/gs7I3kmLXIyQk3zsOdlaYov72pPSel4+I=";
})
]; ];
buildInputs = lib.optionals (python3.pythonOlder "3.9") [ buildInputs = lib.optionals (python3.pythonOlder "3.9") [

View File

@ -0,0 +1,44 @@
{
lib,
fetchFromGitHub,
callPackage,
zig_0_13,
}:
let
versions = [
{
zig = zig_0_13;
version = "0-unstable-06-01";
src = fetchFromGitHub {
owner = "Vexu";
repo = "arocc";
rev = "55cb6d1b682b83f75ad4f60e34c6fcd2336e8531";
hash = "sha256-xs3zNQIC5drrQYT4nxL7Q69xSEdbdMv5+3hQpsX3q5A=";
};
}
];
mkPackage =
{
zig,
version,
src,
}:
callPackage ./package.nix { inherit zig version src; };
pkgsList = lib.map mkPackage versions;
pkgsAttrsUnwrapped = lib.listToAttrs (
lib.map (pkg: lib.nameValuePair "${pkg.version}-unwrapped" pkg) pkgsList
);
pkgsAttrsWrapped = lib.listToAttrs (
lib.map (pkg: lib.nameValuePair pkg.version pkg.wrapped) pkgsList
);
pkgsAttrs = pkgsAttrsWrapped // pkgsAttrsUnwrapped;
in
{
latest-unwrapped = lib.last pkgsList;
latest = (lib.last pkgsList).wrapped;
}
// pkgsAttrs

View File

@ -0,0 +1,33 @@
{
lib,
stdenv,
wrapCCWith,
overrideCC,
zig,
version,
src,
}:
stdenv.mkDerivation (finalAttrs: {
pname = "arocc";
inherit version src;
nativeBuildInputs = [ zig.hook ];
passthru = {
inherit zig;
isArocc = true;
wrapped = wrapCCWith { cc = finalAttrs.finalPackage; };
stdenv = overrideCC stdenv finalAttrs.passthru.wrapped;
};
meta = {
description = "C compiler written in Zig.";
homepage = "http://aro.vexu.eu/";
license = with lib.licenses; [
mit
unicode-30
];
maintainers = with lib.maintainers; [ RossComputerGuy ];
mainProgram = "arocc";
};
})

View File

@ -89,6 +89,8 @@ stdenv.mkDerivation (finalAttrs: {
passthru = { passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = { tests = {
version = testers.testVersion { version = testers.testVersion {
package = finalAttrs.finalPackage; package = finalAttrs.finalPackage;

View File

@ -83,6 +83,8 @@ stdenv.mkDerivation (finalAttrs: {
passthru = { passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = { tests = {
version = testers.testVersion { version = testers.testVersion {
package = finalAttrs.finalPackage; package = finalAttrs.finalPackage;

View File

@ -95,6 +95,8 @@ stdenv.mkDerivation (finalAttrs: {
passthru = { passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = { tests = {
version = testers.testVersion { version = testers.testVersion {
package = finalAttrs.finalPackage; package = finalAttrs.finalPackage;

View File

@ -95,6 +95,8 @@ stdenv.mkDerivation (finalAttrs: {
passthru = { passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = { tests = {
version = testers.testVersion { version = testers.testVersion {
package = finalAttrs.finalPackage; package = finalAttrs.finalPackage;

View File

@ -91,6 +91,8 @@ stdenv.mkDerivation (finalAttrs: {
passthru = { passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; }; hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = { tests = {
version = testers.testVersion { version = testers.testVersion {
package = finalAttrs.finalPackage; package = finalAttrs.finalPackage;

View File

@ -0,0 +1,42 @@
{
lib,
wrapCCWith,
makeWrapper,
runCommand,
targetPlatform,
targetPackages,
zig,
}:
wrapCCWith {
cc =
runCommand "zig-cc-${zig.version}"
{
pname = "zig-cc";
inherit (zig) version meta;
nativeBuildInputs = [ makeWrapper ];
passthru.isZig = true;
inherit zig;
}
''
mkdir -p $out/bin
for tool in ar cc c++ objcopy; do
makeWrapper "$zig/bin/zig" "$out/bin/$tool" \
--add-flags "$tool" \
--run "export ZIG_GLOBAL_CACHE_DIR=\$(mktemp -d)"
done
mv $out/bin/c++ $out/bin/clang++
mv $out/bin/cc $out/bin/clang
'';
nixSupport.cc-cflags =
[
"-target"
"${targetPlatform.parsed.cpu.name}-${targetPlatform.parsed.kernel.name}-${targetPlatform.parsed.abi.name}"
]
++ lib.optional (
targetPlatform.isLinux && !(targetPackages.isStatic or false)
) "-Wl,-dynamic-linker=${targetPackages.stdenv.cc.bintools.dynamicLinker}";
}

View File

@ -71,6 +71,12 @@ stdenv.mkDerivation (finalAttrs: {
hook = callPackage ./hook.nix { hook = callPackage ./hook.nix {
zig = finalAttrs.finalPackage; zig = finalAttrs.finalPackage;
}; };
cc = callPackage ./cc.nix {
zig = finalAttrs.finalPackage;
};
stdenv = callPackage ./stdenv.nix {
zig = finalAttrs.finalPackage;
};
}; };
meta = { meta = {

View File

@ -0,0 +1,6 @@
{
stdenv,
overrideCC,
zig,
}:
overrideCC stdenv zig.cc

View File

@ -72,7 +72,7 @@ stdenv.mkDerivation rec {
fi fi
# Necessary for FTS5 on Linux # Necessary for FTS5 on Linux
export NIX_LDFLAGS="$NIX_LDFLAGS -lm" export NIX_CFLAGS_LINK="$NIX_CFLAGS_LINK -lm"
echo "" echo ""
echo "NIX_CFLAGS_COMPILE = $NIX_CFLAGS_COMPILE" echo "NIX_CFLAGS_COMPILE = $NIX_CFLAGS_COMPILE"

View File

@ -75,6 +75,10 @@ in lib.init bootStages ++ [
then buildPackages.llvmPackages.libcxxClang then buildPackages.llvmPackages.libcxxClang
else if crossSystem.useLLVM or false else if crossSystem.useLLVM or false
then buildPackages.llvmPackages.clang then buildPackages.llvmPackages.clang
else if crossSystem.useZig or false
then buildPackages.zig.cc
else if crossSystem.useArocc or false
then buildPackages.arocc
else buildPackages.gcc; else buildPackages.gcc;
extraNativeBuildInputs = old.extraNativeBuildInputs extraNativeBuildInputs = old.extraNativeBuildInputs

View File

@ -16661,6 +16661,8 @@ with pkgs;
isGNU = cc.isGNU or false; isGNU = cc.isGNU or false;
isClang = cc.isClang or false; isClang = cc.isClang or false;
isArocc = cc.isArocc or false;
isZig = cc.isZig or false;
inherit cc bintools libc libcxx extraPackages nixSupport zlib; inherit cc bintools libc libcxx extraPackages nixSupport zlib;
} // extraArgs; in self); } // extraArgs; in self);
@ -24694,6 +24696,13 @@ with pkgs;
}; };
zig = zig_0_13; zig = zig_0_13;
zigStdenv = if stdenv.cc.isZig then stdenv else lowPrio zig.passthru.stdenv;
aroccPackages = recurseIntoAttrs (callPackage ../development/compilers/arocc {});
arocc = aroccPackages.latest;
aroccStdenv = if stdenv.cc.isArocc then stdenv else lowPrio arocc.cc.passthru.stdenv;
zimlib = callPackage ../development/libraries/zimlib { }; zimlib = callPackage ../development/libraries/zimlib { };
zita-convolver = callPackage ../development/libraries/audio/zita-convolver { }; zita-convolver = callPackage ../development/libraries/audio/zita-convolver { };

View File

@ -291,6 +291,8 @@ let
agdaPackages = packagePlatforms pkgs.agdaPackages; agdaPackages = packagePlatforms pkgs.agdaPackages;
pkgsLLVM.stdenv = [ "x86_64-linux" "aarch64-linux" ]; pkgsLLVM.stdenv = [ "x86_64-linux" "aarch64-linux" ];
pkgsArocc.stdenv = [ "x86_64-linux" "aarch64-linux" ];
pkgsZig.stdenv = [ "x86_64-linux" "aarch64-linux" ];
pkgsMusl.stdenv = [ "x86_64-linux" "aarch64-linux" ]; pkgsMusl.stdenv = [ "x86_64-linux" "aarch64-linux" ];
pkgsStatic.stdenv = [ "x86_64-linux" "aarch64-linux" ]; pkgsStatic.stdenv = [ "x86_64-linux" "aarch64-linux" ];

View File

@ -203,6 +203,36 @@ let
}; };
}; };
pkgsArocc = nixpkgsFun {
overlays = [
(self': super': {
pkgsArocc = super';
})
] ++ overlays;
# Bootstrap a cross stdenv using the Aro C compiler.
# This is currently not possible when compiling natively,
# so we don't need to check hostPlatform != buildPlatform.
crossSystem = stdenv.hostPlatform // {
useArocc = true;
linker = "lld";
};
};
pkgsZig = nixpkgsFun {
overlays = [
(self': super': {
pkgsZig = super';
})
] ++ overlays;
# Bootstrap a cross stdenv using the Zig toolchain.
# This is currently not possible when compiling natively,
# so we don't need to check hostPlatform != buildPlatform.
crossSystem = stdenv.hostPlatform // {
useZig = true;
linker = "lld";
};
};
# All packages built with the Musl libc. This will override the # All packages built with the Musl libc. This will override the
# default GNU libc on Linux systems. Non-Linux systems are not # default GNU libc on Linux systems. Non-Linux systems are not
# supported. 32-bit is also not supported. # supported. 32-bit is also not supported.