Skip to content

Commit c2698bc

Browse files
committed
refactor(bin): fix libllama builds for ios simulator
1 parent cb56f40 commit c2698bc

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

bin/clean.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ declare arch=""
55
declare platform=""
66
declare do_full_clean=0
77
declare do_clean_env_only=0
8+
declare do_clean_llama=0
89

910
if (( TARGET_OS_IPHONE )); then
1011
arch="arm64"
@@ -29,6 +30,9 @@ while (( $# > 0 )); do
2930
do_full_clean=1
3031
do_clean_env_only=1
3132
continue
33+
elif [[ "$arg" = "--llama" ]]; then
34+
do_clean_llama=1
35+
continue
3236
elif [[ "$arg" = "--platform" ]]; then
3337
if [[ "$1" = "ios" ]] || [[ "$1" = "iPhoneOS" ]] || [[ "$1" = "iphoneos" ]]; then
3438
arch="arm64"
@@ -58,10 +62,15 @@ if (( do_full_clean )); then
5862
fi
5963
elif [ -n "$arch" ] || [ -n "$platform" ]; then
6064
if (( do_full_clean )); then
65+
## likely unreachable
6166
echo "error - cannot mix '--full' and '--arch/--platform'" >&2
6267
exit 1
6368
fi
6469

70+
if (( do_clean_llama )); then
71+
rm -rf "$root/build/$target-$platform/llama/build" || exit $?
72+
fi
73+
6574
if [[ "$platform" = "android" ]]; then
6675
if [[ "$arch" = "arm64" ]]; then
6776
arch="/arm64-v8a/g')"

bin/install.sh

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,8 @@ function _prepare {
535535
}
536536

537537
function _install {
538-
declare arch="$1"
539-
declare platform="$2"
538+
local arch="$1"
539+
local platform="$2"
540540

541541
if [ "$platform" == "desktop" ]; then
542542
echo "# copying sources to $SOCKET_HOME/src"
@@ -711,27 +711,27 @@ function _compile_libuv_android {
711711
local platform="android"
712712
local arch=$1
713713
local host_arch="$(host_arch)"
714-
clang="$(android_clang "$ANDROID_HOME" "$NDK_VERSION" "$host" "$host_arch")"
715-
clang_target="$(android_clang_target "$arch")"
716-
ar="$(android_ar "$ANDROID_HOME" "$NDK_VERSION" "$host" "$host_arch")"
717-
android_includes=$(android_arch_includes "$arch")
714+
local clang="$(android_clang "$ANDROID_HOME" "$NDK_VERSION" "$host" "$host_arch")"
715+
local clang_target="$(android_clang_target "$arch")"
716+
local ar="$(android_ar "$ANDROID_HOME" "$NDK_VERSION" "$host" "$host_arch")"
717+
local android_includes=$(android_arch_includes "$arch")
718718

719719
local cflags=("$clang_target" -std=gnu89 -g -pedantic -I"$root"/build/uv/include -I"$root"/build/uv/src -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -D_LARGEFILE_SOURCE -fPIC -Wall -Wextra -Wno-pedantic -Wno-sign-compare -Wno-unused-parameter -Wno-implicit-function-declaration)
720720
cflags+=("${android_includes[*]}")
721721
local objects=()
722722
local sources=("unix/async.c" "unix/core.c" "unix/dl.c" "unix/fs.c" "unix/getaddrinfo.c" "unix/getnameinfo.c" "unix/linux.c" "unix/loop.c" "unix/loop-watcher.c" "unix/pipe.c" "unix/poll.c" "unix/process.c" "unix/proctitle.c" "unix/random-devurandom.c" "unix/random-getentropy.c" "unix/random-getrandom.c" "unix/random-sysctl-linux.c" "unix/signal.c" "unix/stream.c" "unix/tcp.c" "unix/thread.c" "unix/tty.c" "unix/udp.c" fs-poll.c idna.c inet.c random.c strscpy.c strtok.c threadpool.c timer.c uv-common.c uv-data-getter-setters.c version.c)
723723

724-
declare output_directory="$root/build/$arch-$platform/uv$d"
724+
local output_directory="$root/build/$arch-$platform/uv$d"
725725
mkdir -p "$output_directory"
726726

727-
declare src_directory="$root/build/uv/src"
727+
local src_directory="$root/build/uv/src"
728728

729729
trap onsignal INT TERM
730730
local i=0
731731
local max_concurrency=$CPU_CORES
732-
build_static=0
733-
declare base_lib="libuv"
734-
declare static_library="$root/build/$arch-$platform/lib/$base_lib.a"
732+
local build_static=0
733+
local base_lib="libuv"
734+
local static_library="$root/build/$arch-$platform/lib/$base_lib.a"
735735

736736
for source in "${sources[@]}"; do
737737
if (( i++ > max_concurrency )); then
@@ -788,7 +788,7 @@ function _compile_libuv_android {
788788
# If an empty ${objects[@]} is provided to ar, it will still spit out a header without an error code.
789789
# therefore check the output size
790790
# This error condition should only occur after a code change
791-
lib_size="$(stat_size "$static_library")"
791+
local lib_size="$(stat_size "$static_library")"
792792
if (( lib_size < $(android_min_expected_static_lib_size "$base_lib") )); then
793793
echo >&2 "not ok - $static_library size looks wrong: $lib_size, renaming as .bad"
794794
mv "$static_library" "$static_library.bad"
@@ -797,17 +797,17 @@ function _compile_libuv_android {
797797
}
798798

799799
function _compile_llama_metal {
800-
target=$1
801-
hosttarget=$1
802-
platform=$2
800+
local target=$1
801+
local hosttarget=$1
802+
local platform=$2
803803

804804
if [ -z "$target" ]; then
805805
target="$(host_arch)"
806806
platform="desktop"
807807
fi
808808

809809
echo "# building METAL for $platform ($target) on $host..."
810-
STAGING_DIR="$BUILD_DIR/$target-$platform/llama"
810+
local STAGING_DIR="$BUILD_DIR/$target-$platform/llama"
811811

812812
if [ ! -d "$STAGING_DIR" ]; then
813813
mkdir -p "$STAGING_DIR"
@@ -857,7 +857,7 @@ function _compile_llama {
857857
mkdir -p "$STAGING_DIR/build/"
858858
mkdir -p ../bin
859859

860-
declare cmake_args=(
860+
local cmake_args=(
861861
-DLLAMA_BUILD_TESTS=OFF
862862
-DLLAMA_BUILD_SERVER=OFF
863863
-DLLAMA_BUILD_EXAMPLES=OFF
@@ -898,14 +898,20 @@ function _compile_llama {
898898

899899
rm -f "$root/build/$(host_arch)-desktop/lib$d"/*.{so,la,dylib}*
900900
return
901-
#elif [ "$platform" == "iPhoneOS" ] || [ "$platform" == "iPhoneSimulator" ]; then
902-
elif [ "$platform" == "iPhoneOS" ]; then
901+
elif [ "$platform" == "iPhoneOS" ] || [ "$platform" == "iPhoneSimulator" ]; then
903902
# https://github.com/ggerganov/llama.cpp/discussions/4508
904-
905903
local ar="$(xcrun -sdk $sdk -find ar)"
904+
906905
local cc="$(xcrun -sdk $sdk -find clang)"
907906
local cxx="$(xcrun -sdk $sdk -find clang++)"
908-
local cflags="--target=$target-apple-ios -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk -m$sdk-version-min=$SDKMINVERSION -DLLAMA_METAL_EMBED_LIBRARY=ON -DUSE_NEON_DOTPROD -march=armv8.2-a+dotprod"
907+
local cflags="--target=$target-apple-ios -isysroot $PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk -m$sdk-version-min=$SDKMINVERSION -DLLAMA_METAL_EMBED_LIBRARY=ON -DUSE_NEON_DOTPROD "
908+
if [ "$platform" == "iPhoneOS" ]; then
909+
cflags+="-march=armv8.2-a+dotprod"
910+
elif [ "$platform" == "iPhoneSimulator" ] && [ "$target" == "arm64" ]; then
911+
cflags+="-march=armv8.2-a+dotprod"
912+
elif [ "$platform" == "iPhoneSimulator" ] && [ "$target" == "x86_64" ]; then
913+
cflags+="-march=x86-64 -target=x86-apple-ios-simulator"
914+
fi
909915

910916
export AR="$ar"
911917
export CFLAGS="$cflags"
@@ -914,15 +920,16 @@ function _compile_llama {
914920
export CC="$cc"
915921
export SDKROOT="$PLATFORMPATH/$platform.platform/Developer/SDKs/$platform$SDKVERSION.sdk"
916922

917-
quiet cmake -S . -B build -DCMAKE_OSX_ARCHITECTURES="$target" -DCMAKE_OSX_SYSROOT="$SDKROOT" -DCMAKE_C_COMPILER="$cc" -DCMAKE_CXX_COMPILER="$cxx" -DCMAKE_INSTALL_PREFIX="$BUILD_DIR/$target-$platform" -DLLAMA_NATIVE=OFF -DGGML_ARM_DOTPROD=ON ${cmake_args[@]} &&
918-
quiet cmake --build build &&
919-
quiet cmake --build build -- -j"$CPU_CORES" &&
920-
quiet cmake --install build
923+
cmake -S . -B build -DCMAKE_SYSTEM_NAME="iOS" -DCMAKE_OSX_ARCHITECTURES="$target" -DCMAKE_OSX_SYSROOT="$SDKROOT" -DCMAKE_C_COMPILER="$cc" -DCMAKE_CXX_COMPILER="$cxx" -DCMAKE_INSTALL_PREFIX="$BUILD_DIR/$target-$platform" -DLLAMA_NATIVE=OFF -DGGML_ARM_DOTPROD=ON ${cmake_args[@]} &&
924+
cmake --build build &&
925+
cmake --build build -- -j"$CPU_CORES" &&
926+
cmake --install build
921927

922-
if [ ! $? = 0 ]; then
928+
if (( $? != 0 )); then
923929
die $? "not ok - Unable to compile libllama for '$platform'"
924930
return
925931
fi
932+
return
926933
elif [ "$platform" == "android" ]; then
927934
if [[ "$host" == "Win32" ]]; then
928935
echo "WARN - Building libllama for Android on Windows is not yet supported"
@@ -952,22 +959,22 @@ function _compile_llama {
952959

953960
cd "$BUILD_DIR" || exit 1
954961
rm -f "$root/build/$target-$platform/lib$d"/*.{so,la,dylib}*
955-
echo "ok - built llama for $target-$platform"
962+
echo "ok - built libllama for $target-$platform"
956963
return 0
957964
}
958965

959966
function _compile_libuv {
960-
target=$1
961-
hosttarget=$1
962-
platform=$2
967+
local target=$1
968+
local hosttarget=$1
969+
local platform=$2
963970

964971
if [ -z "$target" ]; then
965972
target="$(host_arch)"
966973
platform="desktop"
967974
fi
968975

969976
echo "# building libuv for $platform ($target) on $host..."
970-
STAGING_DIR="$BUILD_DIR/$target-$platform/uv"
977+
local STAGING_DIR="$BUILD_DIR/$target-$platform/uv"
971978

972979
if [ ! -d "$STAGING_DIR" ]; then
973980
mkdir -p "$STAGING_DIR"
@@ -1085,7 +1092,7 @@ function _check_compiler_features {
10851092
cflags+=("-I$root")
10861093

10871094
$CXX "${cflags[@]}" "${ldflags[@]}" - -o /dev/null >/dev/null << EOF_CC
1088-
#include "src/runtime/runtime.hh"
1095+
#include "src/runtime.hh"
10891096
int main () { return 0; }
10901097
EOF_CC
10911098

@@ -1112,19 +1119,19 @@ if [[ "$(uname -s)" == "Darwin" ]] && [[ -z "$NO_IOS" ]]; then
11121119
die $? "not ok - xcode needs to be installed from the mac app store: https://apps.apple.com/us/app/xcode/id497799835"
11131120

11141121
_compile_llama_metal arm64 iPhoneOS
1115-
_compile_llama_metal x86_64 iPhoneSimulator
11161122
_compile_llama_metal arm64 iPhoneSimulator
1123+
_compile_llama_metal x86_64 iPhoneSimulator
11171124
fi
11181125

11191126
{
11201127
_compile_llama
1121-
echo "ok - built llama for $platform ($target)"
1128+
echo "ok - built libllama for desktop ($(host_arch))"
11221129
} & _compile_llama_pid=$!
11231130

11241131
# Although we're passing -j$CPU_CORES on non Win32, we still don't get max utiliztion on macos. Start this before fat libs.
11251132
{
11261133
_compile_libuv
1127-
echo "ok - built libuv for $platform ($target)"
1134+
echo "ok - built libuv for desktop ($(host_arch))"
11281135
} & _compile_libuv_pid=$!
11291136

11301137
if [[ "$(uname -s)" == "Darwin" ]] && [[ -z "$NO_IOS" ]]; then
@@ -1143,11 +1150,11 @@ if [[ "$(uname -s)" == "Darwin" ]] && [[ -z "$NO_IOS" ]]; then
11431150
_compile_llama arm64 iPhoneOS & pids+=($!)
11441151

11451152
_compile_libuv x86_64 iPhoneSimulator & pids+=($!)
1146-
#_compile_llama x86_64 iPhoneSimulator & pids+=($!)
1153+
_compile_llama x86_64 iPhoneSimulator & pids+=($!)
11471154

11481155
if [[ "$arch" = "arm64" ]]; then
11491156
_compile_libuv arm64 iPhoneSimulator & pids+=($!)
1150-
#_compile_llama arm64 iPhoneSimulator & pids+=($!)
1157+
_compile_llama arm64 iPhoneSimulator & pids+=($!)
11511158
fi
11521159

11531160
for pid in "${pids[@]}"; do wait "$pid"; done

0 commit comments

Comments
 (0)