Skip to content

Commit

Permalink
zig: split cc and bintools wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
RossComputerGuy committed Dec 23, 2024
1 parent e3b6744 commit db214ed
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 75 deletions.
34 changes: 34 additions & 0 deletions pkgs/development/compilers/zig/bintools.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
lib,
stdenv,
zig,
runCommand,
makeWrapper,
}:
let
targetPrefix = lib.optionalString (
stdenv.hostPlatform != stdenv.targetPlatform
) "${stdenv.targetPlatform.config}-";
in
runCommand "zig-bintools-${zig.version}"
{
pname = "zig-bintools";
inherit (zig) version meta;

nativeBuildInputs = [ makeWrapper ];

passthru = {
isZig = true;
inherit targetPrefix;
};

inherit zig;
}
''
mkdir -p $out/bin
for tool in ar objcopy ranlib; do
makeWrapper "$zig/bin/zig" "$out/bin/${targetPrefix}$tool" \
--add-flags "$tool" \
--run "export ZIG_GLOBAL_CACHE_DIR=\$(mktemp -d)"
done
''
100 changes: 31 additions & 69 deletions pkgs/development/compilers/zig/cc.nix
Original file line number Diff line number Diff line change
@@ -1,76 +1,38 @@
{
lib,
wrapCCWith,
wrapBintoolsWith,
makeWrapper,
stdenv,
runCommand,
stdenv,
targetPackages,
zig,
stdenv,
makeWrapper,
}:
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 cc c++; 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
'';

bintools =
let
targetPrefix = lib.optionalString (
stdenv.hostPlatform != stdenv.targetPlatform
) "${stdenv.targetPlatform.config}-";
in
wrapBintoolsWith {
bintools =
runCommand "zig-bintools-${zig.version}"
{
pname = "zig-bintools";
inherit (zig) version meta;

nativeBuildInputs = [ makeWrapper ];

passthru = {
isZig = true;
inherit targetPrefix;
};

inherit zig;
}
''
mkdir -p $out/bin
for tool in ar objcopy; do
makeWrapper "$zig/bin/zig" "$out/bin/${targetPrefix}-$tool" \
--add-flags "$tool" \
--run "export ZIG_GLOBAL_CACHE_DIR=\$(mktemp -d)"
done
'';
let
targetPrefix = lib.optionalString (
stdenv.hostPlatform != stdenv.targetPlatform
) "${stdenv.targetPlatform.config}-";
in
runCommand "zig-cc-${zig.version}"
{
pname = "zig-cc";
inherit (zig) version meta;

nativeBuildInputs = [ makeWrapper ];

passthru = {
isZig = true;
inherit targetPrefix;
};

nixSupport.cc-cflags =
[
"-target"
"${stdenv.targetPlatform.parsed.cpu.name}-${stdenv.targetPlatform.parsed.kernel.name}-${stdenv.targetPlatform.parsed.abi.name}"
]
++ lib.optional (
stdenv.targetPlatform.isLinux && !(targetPackages.isStatic or false)
) "-Wl,-dynamic-linker=${targetPackages.stdenv.cc.bintools.dynamicLinker}";
}
inherit zig;
}
''
mkdir -p $out/bin
for tool in cc c++ ld.lld; do
makeWrapper "$zig/bin/zig" "$out/bin/${targetPrefix}$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
mv $out/bin/ld.lld $out/bin/ld
''
27 changes: 27 additions & 0 deletions pkgs/development/compilers/zig/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
fetchFromGitHub,
cmake,
llvmPackages,
targetPackages,
libxml2,
zlib,
coreutils,
callPackage,
version,
hash,
patches ? [ ],
overrideCC,
wrapCCWith,
wrapBintoolsWith,
}@args:

stdenv.mkDerivation (
Expand Down Expand Up @@ -132,6 +136,29 @@ stdenv.mkDerivation (
runHook postInstallCheck
'';

passthru = {
hook = callPackage ./hook.nix { zig = finalAttrs.finalPackage; };

bintools-unwrapped = callPackage ./bintools.nix { zig = finalAttrs.finalPackage; };
bintools = wrapBintoolsWith { bintools = finalAttrs.finalPackage.bintools-unwrapped; };

cc-unwrapped = callPackage ./cc.nix { zig = finalAttrs.finalPackage; };
cc = wrapCCWith {
cc = finalAttrs.finalPackage.cc-unwrapped;
bintools = finalAttrs.finalPackage.bintools;
nixSupport.cc-cflags =
[
"-target"
"${stdenv.targetPlatform.parsed.cpu.name}-${stdenv.targetPlatform.parsed.kernel.name}-${stdenv.targetPlatform.parsed.abi.name}"
]
++ lib.optional (
stdenv.targetPlatform.isLinux && !(stdenv.targetPlatform.isStatic or false)
) "-Wl,-dynamic-linker=${targetPackages.stdenv.cc.bintools.dynamicLinker}";
};

stdenv = overrideCC stdenv finalAttrs.finalPackage.cc;
};

meta = {
description = "General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software";
homepage = "https://ziglang.org/";
Expand Down
6 changes: 0 additions & 6 deletions pkgs/development/compilers/zig/stdenv.nix

This file was deleted.

0 comments on commit db214ed

Please sign in to comment.