Relink and package mobile libraries from Cabal v2 slices

This commit is contained in:
Moritz Angermann committed 2026-10-15 17:28:47 +09:00
1 parent a14b239878
commit d8c9f932e6
3 files changed
+216 -175

No files matched your search

+10
View File
@@ -39,6 +39,16 @@ simulator libraries retain their separate outputs. The existing iOS preparation
scripts retag ARM64 archives with `mac2ios -s` when an Apple Silicon simulator is
needed.
The Cabal v2 builder stores ordinary package archives in its staged store.
`link-mobile-library.sh` relinks the selected package and its dependencies before
that store is pruned. iOS ZIPs contain both the plain `libHSsimplex-chat-*.a` and
the rolled `libHSsimplex-chat-*-ghc*.a` used by `update-pbxproj.sh`. Android and
Windows clients link the threaded RTS into `libsimplex.so` or `libsimplex.dll`;
Android's separate `libsupport.so` resolves RTS symbols from the client library.
ARM64 Android libraries retain the 16 KiB linker alignment. These steps run in
the derivation's build/install phases because v2 component overrides do not run
the older `postBuild` and `postInstall` hooks.
`NIX_ARGS` passes additional options to `nix build`, and `NIX_OUT_DIR` changes the
output-link directory. Each derivation has a two-hour timeout by default; set
`NIX_BUILD_TIMEOUT` in seconds to change it. For a wall-clock limit on a local
+78
View File
@@ -0,0 +1,78 @@
#!/usr/bin/env bash
# Relink a Cabal v2 store unit before haskell.nix prunes its composed store.
set -euo pipefail
if (( $# < 6 )); then
echo "usage: $0 GHC GHC_PKG PACKAGE_DB PACKAGE KIND OUTPUT_DIR [LINK_OPTION ...]" >&2
exit 2
fi
ghc=$1
ghc_pkg=$2
package_db=$3
package=$4
kind=$5
output_dir=$6
shift 6
mkdir -p "$output_dir/dist"
output_dir=$(cd "$output_dir" && pwd)
read -r -a units <<< "$("$ghc_pkg" --package-db="$package_db" field "$package" id --simple-output)"
if (( ${#units[@]} != 1 )); then
echo "expected one $package unit in $package_db, got: ${units[*]}" >&2
exit 1
fi
unit=${units[0]}
read -r -a libraries <<< "$("$ghc_pkg" --package-db="$package_db" field --ipid "$unit" hs-libraries --simple-output)"
read -r -a library_dirs <<< "$("$ghc_pkg" --package-db="$package_db" field --ipid "$unit" library-dirs --simple-output)"
if (( ${#libraries[@]} != 1 )); then
echo "expected one Haskell archive for $unit, got: ${libraries[*]}" >&2
exit 1
fi
archive=""
for directory in "${library_dirs[@]}"; do
if [[ -f "$directory/lib${libraries[0]}.a" ]]; then
archive="$directory/lib${libraries[0]}.a"
break
fi
done
if [[ -z "$archive" ]]; then
echo "cannot find the vanilla Haskell archive for $unit" >&2
exit 1
fi
# A source input makes GHC perform the final link even with only -package-id.
# It must not implicitly import the build machine's Prelude on cross targets.
cat > "$output_dir/MobileBundle.hs" <<'EOF'
{-# LANGUAGE NoImplicitPrelude #-}
module MobileBundle () where
EOF
cd "$output_dir"
common=(-v0 -no-hs-main -hide-all-packages -package-db "$package_db")
case "$kind" in
ios)
# Xcode's update-pbxproj.sh consumes both the plain and rolled archives.
plain=$(basename "$archive")
cp "$archive" "dist/$plain"
version=$("$ghc" --numeric-version)
"$ghc" "${common[@]}" -staticlib -threaded -package-id "$unit" \
"$@" MobileBundle.hs -o "dist/${plain%.a}-ghc$version.a"
;;
android | windows)
if [[ "$kind" == android ]]; then
output=libsimplex.so
else
output=libsimplex.dll
fi
"$ghc" "${common[@]}" -shared -threaded -flink-rts -package-id "$unit" \
"$@" MobileBundle.hs -o "$output"
;;
android-support)
# libsimplex supplies the RTS/base symbols; do not bundle a second RTS.
"$ghc" "${common[@]}" -shared -fno-link-rts -no-auto-link-packages \
"$@" "$archive" MobileBundle.hs -o libsupport.so
;;
*)
echo "unknown mobile library kind: $kind" >&2
exit 2
;;
esac