Skip to content

Commit

Permalink
Build both kinds of roots on macOS, fix up dylibs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom McLaughlin committed Dec 6, 2024
1 parent bc7884b commit 381ee5b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .aliases/dev-root
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ set -eo pipefail
SCRIPTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$SCRIPTDIR/.."

nix build .#gcroots -o .gcroots
nix build .#packages.aarch64-darwin.gcroots -o .gcroots-aarch64-darwin
nix build .#packages.x86_64-darwin.gcroots -o .gcroots-x86_64-darwin
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ result
dist-newstyle

.direnv
.gcroots
.gcroots*
14 changes: 11 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@

baseModules = {
packages.haskell-notebook-language-server.components.exes.haskell-notebook-language-server.dontStrip = false;
# packages.haskell-notebook-language-server.components.exes.haskell-notebook-language-server.postFixup = ''
# echo AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
# '';
} // pkgs.lib.optionalAttrs pkgs.stdenv.isDarwin {
packages.haskell-notebook-language-server.components.exes.haskell-notebook-language-server.postInstall = ''
${builtins.readFile ./nix/fix-dylib.sh}
fix_dylib "$out/bin/haskell-notebook-language-server" libiconv.2.dylib libiconv.dylib
fix_dylib "$out/bin/haskell-notebook-language-server" libffi.8.dylib libffi.dylib
check_no_nix_refs "$out/bin/haskell-notebook-language-server"
'';
packages.haskell-notebook-language-server.components.exes.haskell-notebook-language-server.configureFlags = [
''--ghc-options="-optl-Wl,-dead_strip -optl-Wl,-dead_strip_dylibs -optl-Wl,-force_load,${pkgs.pkgsStatic.libffi}/lib/libffi.a"''
];
};

flake = compiler-nix-name: src: (pkgs.hixProject compiler-nix-name src [baseModules]).flake {};
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
}

0 comments on commit 381ee5b

Please sign in to comment.