Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stdenv & CC support for Zig #318034

Merged
merged 8 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkgs/build-support/cc-wrapper/add-flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ for var in "${var_templates_bool[@]}"; do
mangleVarBool "$var" ${role_suffixes[@]+"${role_suffixes[@]}"}
done

# `-B@out@/bin' forces cc to use ld-wrapper.sh when calling ld.
NIX_CFLAGS_COMPILE_@suffixSalt@="-B@out@/bin/ $NIX_CFLAGS_COMPILE_@suffixSalt@"
# Arocc does not support "-B"
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
# the script.
Expand Down
5 changes: 5 additions & 0 deletions pkgs/build-support/cc-wrapper/cc-wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ if [ "$nonFlagArgs" = 0 ]; then
dontLink=1
fi

# Arocc does not link
if [ "@isArocc@" = 1 ]; then
dontLink=1
fi

# Optionally filter out paths not refering to the store.
if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then
kept=()
Expand Down
32 changes: 21 additions & 11 deletions pkgs/build-support/cc-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
, propagateDoc ? cc != null && cc ? man
, extraTools ? [], extraPackages ? [], extraBuildCommands ? ""
, 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
, libcxx ? null

Expand Down Expand Up @@ -304,14 +306,17 @@ stdenvNoCC.mkDerivation {

outputs = [ "out" ] ++ optionals propagateDoc [ "man" "info" ];

# Cannot be in "passthru" due to "substituteAll"
inherit isArocc;

passthru = {
inherit targetPrefix suffixSalt;
# "cc" is the generic name for a C compiler, but there is no one for package
# providing the linker and related tools. The two we use now are GNU
# Binutils, and Apple's "cctools"; "bintools" as an attempt to find an
# unused middle-ground name that evokes both.
inherit bintools;
inherit cc libc libcxx nativeTools nativeLibc nativePrefix isGNU isClang;
inherit cc libc libcxx nativeTools nativeLibc nativePrefix isGNU isClang isZig;

emacsBufferSetup = pkgs: ''
; We should handle propagation here too
Expand Down Expand Up @@ -391,6 +396,10 @@ stdenvNoCC.mkDerivation {
ln -s ${targetPrefix}clang $out/bin/${targetPrefix}cc
export named_cc=${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

if [ -e $ccPath/${targetPrefix}g++ ]; then
Expand Down Expand Up @@ -475,7 +484,7 @@ stdenvNoCC.mkDerivation {
#
# TODO(@Ericson2314): Remove this after stable release and force
# everyone to refer to bintools-wrapper directly.
+ ''
+ optionalString (!isArocc) ''
if [[ -f "$bintools/nix-support/dynamic-linker" ]]; then
ln -s "$bintools/nix-support/dynamic-linker" "$out/nix-support"
fi
Expand All @@ -491,7 +500,7 @@ stdenvNoCC.mkDerivation {

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_solib}/lib" >> $out/nix-support/cc-ldflags
''
Expand All @@ -517,9 +526,9 @@ stdenvNoCC.mkDerivation {
''
# 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
+ concatMapStrings (libgcc: ''
+ optionals (!isArocc) (concatMapStrings (libgcc: ''
echo "-L${libgcc}/lib" >> $out/nix-support/cc-ldflags
'') (toList (gccForLibs.libgcc or [])))
'') (toList (gccForLibs.libgcc or []))))

##
## General libc support
Expand All @@ -539,9 +548,10 @@ stdenvNoCC.mkDerivation {
+ optionalString (libc != null) (''
touch "$out/nix-support/libc-cflags"
touch "$out/nix-support/libc-ldflags"
'' + optionalString (!isArocc) ''
echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags
'' + 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))) ''
for dir in "${cc}"/lib/gcc/*/*/include-fixed; do
echo '-idirafter' ''${dir} >> $out/nix-support/libc-cflags
Expand Down Expand Up @@ -597,15 +607,15 @@ stdenvNoCC.mkDerivation {
# ${cc_solib}/lib64 (even though it does actually search there...)..
# This confuses libtool. So add it to the compiler tool search
# path explicitly.
+ optionalString (!nativeTools) ''
+ optionalString (!nativeTools && !isArocc) ''
if [ -e "${cc_solib}/lib64" -a ! -L "${cc_solib}/lib64" ]; then
ccLDFlags+=" -L${cc_solib}/lib64"
ccCFlags+=" -B${cc_solib}/lib64"
fi
ccLDFlags+=" -L${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-ldflags"
basePath=$(echo $cc/lib/*/*/*)
Expand All @@ -626,7 +636,7 @@ stdenvNoCC.mkDerivation {
+ optionalString propagateDoc ''
ln -s ${cc.man} $man
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
''

Expand Down Expand Up @@ -667,7 +677,7 @@ stdenvNoCC.mkDerivation {
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
echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-crt1-cflags
done
Expand Down
8 changes: 8 additions & 0 deletions pkgs/by-name/me/meson/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ python3.pkgs.buildPythonApplication rec {
# Fix extraframework lookup on case-sensitive APFS.
# https://github.com/mesonbuild/meson/pull/13038
./007-case-sensitive-fs.patch

# Fix meson's detection for zig's linker
JohnRTitor marked this conversation as resolved.
Show resolved Hide resolved
# 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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random commits in unmerged pull requests are not stable URLs. If you must use a non-upstream patch like this, it has to be vendored to ensure it can still be built in future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also doesn't look like it's going to be upstream any time soon. I think this might have been a bit premature…

Copy link
Member Author

@RossComputerGuy RossComputerGuy Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, will pull this patch into it's own file in #331011

hash = "sha256-dDOmSRBKl/gs7I3kmLXIyQk3zsOdlaYov72pPSel4+I=";
})
];

buildInputs = lib.optionals (python3.pythonOlder "3.9") [
Expand Down
44 changes: 44 additions & 0 deletions pkgs/development/compilers/arocc/default.nix
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions pkgs/development/compilers/arocc/package.nix
Original file line number Diff line number Diff line change
@@ -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";
};
})
2 changes: 2 additions & 0 deletions pkgs/development/compilers/zig/0.10/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ stdenv.mkDerivation (finalAttrs: {

passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = {
version = testers.testVersion {
package = finalAttrs.finalPackage;
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/compilers/zig/0.11/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ stdenv.mkDerivation (finalAttrs: {

passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = {
version = testers.testVersion {
package = finalAttrs.finalPackage;
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/compilers/zig/0.12/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ stdenv.mkDerivation (finalAttrs: {

passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = {
version = testers.testVersion {
package = finalAttrs.finalPackage;
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/compilers/zig/0.13/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ stdenv.mkDerivation (finalAttrs: {

passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = {
version = testers.testVersion {
package = finalAttrs.finalPackage;
Expand Down
2 changes: 2 additions & 0 deletions pkgs/development/compilers/zig/0.9/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ stdenv.mkDerivation (finalAttrs: {

passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };
cc = callPackage ../cc.nix { zig = finalAttrs.finalPackage; };
stdenv = callPackage ../stdenv.nix { zig = finalAttrs.finalPackage; };
tests = {
version = testers.testVersion {
package = finalAttrs.finalPackage;
Expand Down
42 changes: 42 additions & 0 deletions pkgs/development/compilers/zig/cc.nix
Original file line number Diff line number Diff line change
@@ -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}";
}
6 changes: 6 additions & 0 deletions pkgs/development/compilers/zig/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ stdenv.mkDerivation (finalAttrs: {
hook = callPackage ./hook.nix {
zig = finalAttrs.finalPackage;
};
cc = callPackage ./cc.nix {
zig = finalAttrs.finalPackage;
};
stdenv = callPackage ./stdenv.nix {
zig = finalAttrs.finalPackage;
};
};

meta = {
Expand Down
6 changes: 6 additions & 0 deletions pkgs/development/compilers/zig/stdenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
stdenv,
overrideCC,
zig,
}:
overrideCC stdenv zig.cc
2 changes: 1 addition & 1 deletion pkgs/development/libraries/sqlite/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ stdenv.mkDerivation rec {
fi

# Necessary for FTS5 on Linux
export NIX_LDFLAGS="$NIX_LDFLAGS -lm"
export NIX_CFLAGS_LINK="$NIX_CFLAGS_LINK -lm"

echo ""
echo "NIX_CFLAGS_COMPILE = $NIX_CFLAGS_COMPILE"
Expand Down
4 changes: 4 additions & 0 deletions pkgs/stdenv/cross/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ in lib.init bootStages ++ [
then buildPackages.llvmPackages.libcxxClang
else if crossSystem.useLLVM or false
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;

extraNativeBuildInputs = old.extraNativeBuildInputs
Expand Down
9 changes: 9 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16662,6 +16662,8 @@ with pkgs;

isGNU = cc.isGNU 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;
} // extraArgs; in self);
Expand Down Expand Up @@ -24695,6 +24697,13 @@ with pkgs;
};
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 { };

zita-convolver = callPackage ../development/libraries/audio/zita-convolver { };
Expand Down
2 changes: 2 additions & 0 deletions pkgs/top-level/release.nix
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ let
agdaPackages = packagePlatforms pkgs.agdaPackages;

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" ];
pkgsStatic.stdenv = [ "x86_64-linux" "aarch64-linux" ];

Expand Down
Loading