Skip to content

Commit

Permalink
Fix up dylibs on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom McLaughlin committed Dec 9, 2024
1 parent 2e0e966 commit cdccb18
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 7 deletions.
2 changes: 2 additions & 0 deletions assets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +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.
Binary file added assets/libffi.a
Binary file not shown.
7 changes: 4 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.haskellNix.url = "github:input-output-hk/haskell.nix";
inputs.haskellNix.url = "github:input-output-hk/haskell.nix/angerman/fix-install_name_tool";
inputs.gitignore = {
url = "github:hercules-ci/gitignore.nix";
inputs.nixpkgs.follows = "nixpkgs";
Expand Down Expand Up @@ -40,7 +40,9 @@

modules = [{
packages.rust-notebook-language-server.components.exes.rust-notebook-language-server.dontStrip = false;
}];
} (
pkgs.lib.optionalAttrs pkgs.stdenv.isDarwin (import ./nix/macos-modules.nix { inherit pkgs; })
)];
};
})
];
Expand Down
32 changes: 32 additions & 0 deletions nix/fix-dylib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# Example usage:
# fix_dylib "./myapp" "libffi.8.dylib" "libffi.dylib"
fix_dylib() {
local executable="$1"
local dylib_name="$2"
local target_name="$3"

if otool -L "$executable" | grep -q "/nix/store/.*/$dylib_name"; then
echo "Fixing $dylib_name reference in $executable"
local old_path=$(otool -L "$executable" | grep "/nix/store/.*/$dylib_name" | awk '{print $1}')
install_name_tool -change "$old_path" "/usr/lib/$target_name" "$executable"
if otool -L "$executable" | grep -q "/usr/lib/$target_name"; then
echo "Successfully fixed $dylib_name in $executable (now points to $target_name)"
else
echo "Failed to fix $dylib_name in $executable (attempted to point to $target_name)"
return 1
fi
fi
}

# Example usage:
# check_no_nix_refs "./myapp"
check_no_nix_refs() {
local executable="$1"

if otool -L "$executable" | tail -n +2 | grep -q "/nix/store/"; then
echo "ERROR: $executable still contains Nix store references:"
otool -L "$executable" | tail -n +2 | grep "/nix/store/"
return 1
fi
}
29 changes: 29 additions & 0 deletions nix/macos-modules.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{ pkgs }:

{
packages.rust-notebook-language-server.components.exes.rust-notebook-language-server.postInstall = ''
${builtins.readFile ./fix-dylib.sh}
fix_dylib "$out/bin/rust-notebook-language-server" libiconv.2.dylib libiconv.dylib
fix_dylib "$out/bin/rust-notebook-language-server" libffi.8.dylib libffi.dylib
fix_dylib "$out/bin/rust-notebook-language-server" libpcre.1.dylib libpcre.dylib
fix_dylib "$out/bin/rust-notebook-language-server" libc++.1.0.dylib libc++.dylib
fix_dylib "$out/bin/rust-notebook-language-server" libc++abi.1.0.dylib libc++abi.dylib
check_no_nix_refs "$out/bin/rust-notebook-language-server"
strip "$out/bin/rust-notebook-language-server"
'';

packages.rust-notebook-language-server.components.exes.rust-notebook-language-server.configureFlags = let
# Nixpkgs can't currently give us a cross-compiled x86_64-darwin libffi.a when we're building on aarch64-darwin.
# So, we bundle one in the repo.
# Tried to also detect if we're on aarch64-darwin, so it can work normally if the build machine is x86_64-darwin,
# but that is deliberately difficult here (builtins.currentSystem is considered an "impure builtin".)
libffi = if pkgs.stdenv.targetPlatform.system == "x86_64-darwin"
then "${../assets/libffi.a}"
else "${pkgs.pkgsStatic.libffi}/lib/libffi.a";
in
[
''--ghc-options="-optl-Wl,-dead_strip -optl-Wl,-dead_strip_dylibs -optl-Wl,-force_load,${libffi}"''
];
}
4 changes: 2 additions & 2 deletions src/Transform/Util.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}

module Transform.Util where
Expand Down

0 comments on commit cdccb18

Please sign in to comment.