Skip to content

Commit

Permalink
saner nix <-> zig target conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloudef committed Jan 15, 2024
1 parent 8b3478a commit e03e508
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 61 deletions.
14 changes: 7 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
# Zig versions.
# <https://ziglang.org/download/index.json>
zigv = _pkgs.callPackage ./versions.nix {
zigSystem = zig2nix-lib.resolveSystem system;
zigSystem = zig2nix-lib.zigDoubleFromString system;
zigHook = zig-hook;
};

# Converts zon files to json
zon2json = let
target = zig2nix-lib.resolveTarget null {config = system;} true;
target = zig2nix-lib.resolveTarget { nix = system; musl = true; };
in _pkgs.callPackage tools/zon2json/default.nix {
zig = zigv.master;
zigBuildFlags = [ "-Dtarget=${target}" ];
Expand Down Expand Up @@ -86,8 +86,8 @@
#! access: (zig-env {}).thing

# Solving platform specific spaghetti below
runtimeForTarget = config: let
parsed = (zig2nix-lib.elaborate {inherit config;}).parsed;
runtimeForTarget = target: let
system = zig2nix-lib.mkZigSystemFromString target;
env = {
linux = {
LIBRARY_PATH = "LD_LIBRARY_PATH";
Expand All @@ -108,9 +108,9 @@
};
bins = {};
in {
env = env.${parsed.kernel.name} or {};
libs = libs.${parsed.kernel.name} or [];
bins = bins.${parsed.kernel.name} or [];
env = env.${system.kernel.name} or {};
libs = libs.${system.kernel.name} or [];
bins = bins.${system.kernel.name} or [];
};

_linux_extra = let
Expand Down
130 changes: 78 additions & 52 deletions lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,84 @@ with builtins;
with lib;

rec {
zigTargetToNixTarget = target: let
kernel = {
freestanding = l: "${head l}-unknown-none-${last l}";
linux = l: "${head l}-unknown-linux-${last l}";
macos = l: "${head l}-apple-darwin";
windows = l: "${head l}-w64-mingw32";
wasi = l: "${head l}-unknown-wasi";
};
cpu = {
powerpc64 = s: "${s}abi64";
sparcv9 = s: "sparc64-${removePrefix "sparcv9-" s}";
thumb = s: "armv5tel-${removePrefix "thumb-" s}";
x86 = s: "i386-${removePrefix "x86-" s}";
};
split = splitString "-" target;
in cpu."${head split}" or (_: _) (kernel."${elemAt split 1}" split);

nixTargetToZigTarget = target: let
kernel = {
none = t: "${t.cpu.name}-freestanding-${t.abi.name}";
linux = t: "${t.cpu.name}-linux-${t.abi.name}";
darwin = t: "${t.cpu.name}-macos-none";
windows = t: "${t.cpu.name}-windows-gnu";
wasi = t: "${t.cpu.name}-wasi-musl";
mkZigSystemFromString = s: let
res = tryEval (systems.parse.mkSystemFromString s);
in {
zig = let
parts = splitString "-" s;

cpu = elemAt parts 0;

kernel = {
"2" = elemAt parts 1;
"3" = elemAt parts 1;
"4" = elemAt parts 2;
}.${toString (length parts)} or (throw "zig target string has invalid number of hyphen-separated components");

abi = {
"2" = "none";
"3" = elemAt parts 2;
"4" = elemAt parts 3;
}.${toString (length parts)} or (throw "zig target string has invalid number of hyphen-separated components");

nixKernel = let
stripped = elemAt (splitString "." kernel) 0;
in {
freestanding = "none";
}.${stripped} or stripped;

nixAbi = {
none = "unknown";
}.${abi} or abi;

system = systems.parse.mkSystemFromSkeleton {
inherit cpu;
kernel = nixKernel;
abi = nixAbi;
};
in system // {
zig = {
inherit cpu kernel abi;
supportsStaticLinking = system.kernel.execFormat.name != "macho";
};
};
cpu = {
powerpc64 = s: removeSuffix "abi64" s;
sparc64 = s: "sparcv9-${removePrefix "sparc64-" s}";
armv5tel = s: "thumb-${removePrefix "armv5tel-" s}";
i386 = s: "x86-${removePrefix "i386-" s}";

nix = let
zigKernel = {
none = "freestanding";
darwin = "macos";
}.${res.value.kernel.name} or res.value.kernel.name;

zigAbi = {
unknown = "none";
}.${res.value.abi.name} or res.value.abi.name;
in res.value // {
zig = {
cpu = res.value.cpu.name;
kernel = zigKernel;
abi = zigAbi;
supportsStaticLinking = res.value.kernel.execFormat.name != "macho";
};
};
in cpu."${target.cpu.name}" or (_: _) (kernel."${target.kernel.name}" target);

elaborate = system: let
target = system.config;
in systems.elaborate (system
// optionalAttrs (hasSuffix "mingw32" target) { libc = "msvcrt"; }
// optionalAttrs (hasSuffix "darwin" target) { libc = "libSystem"; }
// optionalAttrs (hasSuffix "wasi" target) { libc = "wasilibc"; }
// optionalAttrs (hasInfix "musl" target) { libc = "musl"; }
// optionalAttrs (hasInfix "gnu" target) { libc = "glibc"; }
);

supportsStatic = target: let
inherit (systems.elaborate target) parsed;
in parsed.kernel.name != "darwin";

resolveTarget = target: platform: preferMusl: let
resolved = if target != null then target else nixTargetToZigTarget (elaborate platform).parsed;
in if preferMusl then replaceStrings [ "-gnu" ] [ "-musl" ] resolved else resolved;

resolveSystem = system: let
resolved = nixTargetToZigTarget (elaborate {config = system;}).parsed;
in concatStringsSep "-" (sublist 0 2 (splitString "-" resolved));
}.${if res.success then "nix" else "zig"};

zigDoubleFromSystem = system: "${system.zig.cpu}-${system.zig.kernel}";
zigTripleFromSystem = system: "${system.zig.cpu}-${system.zig.kernel}-${system.zig.abi}";
zigDoubleFromString = s: zigDoubleFromSystem (mkZigSystemFromString s);
zigTripleFromString = s: zigTripleFromSystem (mkZigSystemFromString s);

zigTripleFromPlatform = p: let
system = mkZigSystemFromString p.config;
in {
darwin = "${system.zig.cpu}-${system.zig.kernel}.${p.darwinSdkVersion}-${system.zig.abi}";
}.${system.kernel.name} or (zigTripleFromSystem system);

# helper for resolving final target for building a package from derivation attrs
resolveTarget = args: let
target =
if args ? zig && args.zig != null then zigTripleFromString args.zig
else if args ? nix && args.nix != null then zigTripleFromString args.nix
else if args ? platform && args.platform != null then zigTripleFromPlatform args.platform
else throw "either zig, nix or platform must be specified";
in if args.musl or false then replaceStrings [ "-gnu" ] [ "-musl" ] target else target;
}
8 changes: 6 additions & 2 deletions package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ with builtins;
with lib;

let
target = zig2nix-lib.resolveTarget zigTarget stdenvNoCC.targetPlatform zigPreferMusl;
target = zig2nix-lib.resolveTarget {
zig = zigTarget;
platform = stdenvNoCC.targetPlatform;
musl = zigPreferMusl;
};
zon = zig2nix-lib.fromZON zigBuildZon;
deps = runCommandLocal "deps" {} ''${zon2nix}/bin/zon2nix "${zigBuildZonLock}" > $out'';
runtime = runtimeForTarget (zig2nix-lib.zigTargetToNixTarget target);
runtime = runtimeForTarget target;
wrapper-args = zigWrapperArgs
++ optionals (length runtime.bins > 0) [ "--prefix" "PATH" ":" (makeBinPath runtime.bins) ]
++ optionals (length runtime.libs > 0) [ "--prefix" runtime.env.LIBRARY_PATH ":" (makeLibraryPath runtime.libs) ];
Expand Down

0 comments on commit e03e508

Please sign in to comment.