Skip to content

Commit cdccb18

Browse files
author
Tom McLaughlin
committed
Fix up dylibs on macOS
1 parent 2e0e966 commit cdccb18

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed

assets/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
The libffi.a in this folder was compiled from Nixpkgs master on 12/6/2024 on a x86_64-darwin machine. It's include to support cross-compiling to that target on an aarch64-darwin.

assets/libffi.a

45.6 KB
Binary file not shown.

flake.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
inputs.flake-utils.url = "github:numtide/flake-utils";
3-
inputs.haskellNix.url = "github:input-output-hk/haskell.nix";
3+
inputs.haskellNix.url = "github:input-output-hk/haskell.nix/angerman/fix-install_name_tool";
44
inputs.gitignore = {
55
url = "github:hercules-ci/gitignore.nix";
66
inputs.nixpkgs.follows = "nixpkgs";
@@ -40,7 +40,9 @@
4040

4141
modules = [{
4242
packages.rust-notebook-language-server.components.exes.rust-notebook-language-server.dontStrip = false;
43-
}];
43+
} (
44+
pkgs.lib.optionalAttrs pkgs.stdenv.isDarwin (import ./nix/macos-modules.nix { inherit pkgs; })
45+
)];
4446
};
4547
})
4648
];

nix/fix-dylib.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# Example usage:
3+
# fix_dylib "./myapp" "libffi.8.dylib" "libffi.dylib"
4+
fix_dylib() {
5+
local executable="$1"
6+
local dylib_name="$2"
7+
local target_name="$3"
8+
9+
if otool -L "$executable" | grep -q "/nix/store/.*/$dylib_name"; then
10+
echo "Fixing $dylib_name reference in $executable"
11+
local old_path=$(otool -L "$executable" | grep "/nix/store/.*/$dylib_name" | awk '{print $1}')
12+
install_name_tool -change "$old_path" "/usr/lib/$target_name" "$executable"
13+
if otool -L "$executable" | grep -q "/usr/lib/$target_name"; then
14+
echo "Successfully fixed $dylib_name in $executable (now points to $target_name)"
15+
else
16+
echo "Failed to fix $dylib_name in $executable (attempted to point to $target_name)"
17+
return 1
18+
fi
19+
fi
20+
}
21+
22+
# Example usage:
23+
# check_no_nix_refs "./myapp"
24+
check_no_nix_refs() {
25+
local executable="$1"
26+
27+
if otool -L "$executable" | tail -n +2 | grep -q "/nix/store/"; then
28+
echo "ERROR: $executable still contains Nix store references:"
29+
otool -L "$executable" | tail -n +2 | grep "/nix/store/"
30+
return 1
31+
fi
32+
}

nix/macos-modules.nix

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{ pkgs }:
2+
3+
{
4+
packages.rust-notebook-language-server.components.exes.rust-notebook-language-server.postInstall = ''
5+
${builtins.readFile ./fix-dylib.sh}
6+
7+
fix_dylib "$out/bin/rust-notebook-language-server" libiconv.2.dylib libiconv.dylib
8+
fix_dylib "$out/bin/rust-notebook-language-server" libffi.8.dylib libffi.dylib
9+
fix_dylib "$out/bin/rust-notebook-language-server" libpcre.1.dylib libpcre.dylib
10+
fix_dylib "$out/bin/rust-notebook-language-server" libc++.1.0.dylib libc++.dylib
11+
fix_dylib "$out/bin/rust-notebook-language-server" libc++abi.1.0.dylib libc++abi.dylib
12+
check_no_nix_refs "$out/bin/rust-notebook-language-server"
13+
14+
strip "$out/bin/rust-notebook-language-server"
15+
'';
16+
17+
packages.rust-notebook-language-server.components.exes.rust-notebook-language-server.configureFlags = let
18+
# Nixpkgs can't currently give us a cross-compiled x86_64-darwin libffi.a when we're building on aarch64-darwin.
19+
# So, we bundle one in the repo.
20+
# Tried to also detect if we're on aarch64-darwin, so it can work normally if the build machine is x86_64-darwin,
21+
# but that is deliberately difficult here (builtins.currentSystem is considered an "impure builtin".)
22+
libffi = if pkgs.stdenv.targetPlatform.system == "x86_64-darwin"
23+
then "${../assets/libffi.a}"
24+
else "${pkgs.pkgsStatic.libffi}/lib/libffi.a";
25+
in
26+
[
27+
''--ghc-options="-optl-Wl,-dead_strip -optl-Wl,-dead_strip_dylibs -optl-Wl,-force_load,${libffi}"''
28+
];
29+
}

src/Transform/Util.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE ConstraintKinds #-}
2-
{-# LANGUAGE PolyKinds #-}
3-
{-# LANGUAGE GADTs #-}
42
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE GADTs #-}
4+
{-# LANGUAGE PolyKinds #-}
55
{-# LANGUAGE RankNTypes #-}
66

77
module Transform.Util where

0 commit comments

Comments
 (0)