Skip to content

Commit

Permalink
[zig/nix] fixes for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
rrbutani committed Feb 8, 2023
1 parent b74e957 commit 1366315
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions zig/derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pkgs,
stdenv,
lib,
darwin,
libiconv,
zig,
pkg-config,
SDL2,
Expand Down Expand Up @@ -38,6 +40,13 @@ let
done;
'';
};

# `apple_sdk` defaults to `10_12` instead of `11_0` on `x86_64-darwin` but we
# need `CoreHaptics` to successfully link against `SDL2` and `CoreHaptics` is
# not available in `10_12`.
#
# So, we use `11_0`, even on x86_64.
inherit (darwin.apple_sdk_11_0) frameworks;
in

stdenv.mkDerivation rec {
Expand All @@ -48,9 +57,12 @@ stdenv.mkDerivation rec {
devTools = [ zig ];
};

buildInputs = []
++ lib.optional (!stdenv.isDarwin) SDL2
++ lib.optional stdenv.isDarwin SDL2-lowercase
buildInputs = [(if stdenv.isDarwin then SDL2-lowercase else SDL2)]
++ lib.optionals stdenv.isDarwin (with frameworks; [
IOKit GameController CoreAudio AudioToolbox QuartzCore Carbon Metal
Cocoa ForceFeedback CoreHaptics
])
++ lib.optional stdenv.isDarwin libiconv
;

nativeBuildInputs = [ zig pkg-config ]
Expand All @@ -60,6 +72,33 @@ stdenv.mkDerivation rec {
dontConfigure = true;
dontBuild = true;

# Unforunately `zig`'s parsing of `NIX_LDFLAGS` bails when it encounters any
# flags it does not expect.
# https://github.com/ziglang/zig/blob/fe6dcdba1407f00584725318404814571cdbd828/lib/std/zig/system/NativePaths.zig#L79
#
# When `zig` sees the `-liconv` flag that's in `NIX_LDFLAGS` on macOS, it
# bails, causing it to miss the `-L` path for SDL.
#
# Really, this should be fixed in upstream (zig) but for now we just strip out
# the `-l` flags:
preInstall = ''
readonly ORIGINAL_NIX_LDFLAGS=($NIX_LDFLAGS)
NIX_LDFLAGS=""
for c in "''${ORIGINAL_NIX_LDFLAGS[@]}"; do
# brittle, bad, etc; this presumes `-l...` style args (no space)
if [[ $c =~ ^-l.* ]]; then
echo "dropping link flag: $c"
continue
else
echo "keeping link flag: $c"
NIX_LDFLAGS="$NIX_LDFLAGS $c"
fi
done
export NIX_LDFLAGS
'';

ZIG_FLAGS = []
++ lib.optional fastSupport "-Doptimize=ReleaseFast"
++ lib.optional safeSupport "-Doptimize=ReleaseSafe"
Expand Down

0 comments on commit 1366315

Please sign in to comment.