22#
33# Options
44DEPLOYMENT_TARGET=14.0
5- BUILD_SHARED_LIBS=ON
6- LLAMA_STATIC=OFF
5+ BUILD_SHARED_LIBS=OFF
76LLAMA_BUILD_EXAMPLES=OFF
87LLAMA_BUILD_TESTS=OFF
98LLAMA_BUILD_SERVER=OFF
9+ GGML_METAL=ON
1010GGML_METAL_EMBED_LIBRARY=ON
11+ GGML_BLAS_DEFAULT=ON
1112GGML_METAL_USE_BF16=ON
1213
1314set -xe
@@ -26,7 +27,7 @@ setup_framework_structure() {
2627 mkdir -p ${build_dir} /framework/${framework_name} .framework/Headers
2728 mkdir -p ${build_dir} /framework/${framework_name} .framework/Modules
2829
29- # Copy all required headers.
30+ # Copy all required headers
3031 cp include/llama.h ${build_dir} /framework/${framework_name} .framework/Headers/
3132 cp ggml/include/ggml.h ${build_dir} /framework/${framework_name} .framework/Headers/
3233 cp ggml/include/ggml-alloc.h ${build_dir} /framework/${framework_name} .framework/Headers/
@@ -35,10 +36,7 @@ setup_framework_structure() {
3536 cp ggml/include/ggml-cpu.h ${build_dir} /framework/${framework_name} .framework/Headers/
3637 cp ggml/include/ggml-blas.h ${build_dir} /framework/${framework_name} .framework/Headers/
3738
38- # Create module map that describes how which headers are part of the llama module.
39- # This enables swift code to import 'llama' and get access to all public interfaces
40- # the headers with the usage of export *. For example, LibLlama.swift imports the
41- # 'llama' module.
39+ # Create module map
4240 cat > ${build_dir} /framework/${framework_name} .framework/Modules/module.modulemap << EOF
4341framework module llama {
4442 header "llama.h"
@@ -49,12 +47,14 @@ framework module llama {
4947 header "ggml-cpu.h"
5048 header "ggml-blas.h"
5149
50+ link "c++"
51+ link framework "Accelerate"
52+
5253 export *
5354}
5455EOF
5556
56- # Create Info.plist (Information Property List) file that describes the app.
57- # This will be a Framework bundle (FMWK).
57+ # Create Info.plist
5858 cat > ${build_dir} /framework/${framework_name} .framework/Info.plist << EOF
5959<?xml version="1.0" encoding="UTF-8"?>
6060<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
8383EOF
8484}
8585
86- # Common options for all builds.
86+ # Common options for all builds
8787COMMON_CMAKE_ARGS=(
8888 -DIOS=ON
8989 -DCMAKE_SYSTEM_NAME=iOS
@@ -92,16 +92,16 @@ COMMON_CMAKE_ARGS=(
9292 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY=" "
9393 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO
9494 -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
95- -DLLAMA_STATIC=${LLAMA_STATIC}
9695 -DLLAMA_BUILD_EXAMPLES=${LLAMA_BUILD_EXAMPLES}
9796 -DLLAMA_BUILD_TESTS=${LLAMA_BUILD_TESTS}
9897 -DLLAMA_BUILD_SERVER=${LLAMA_BUILD_SERVER}
9998 -DGGML_METAL_EMBED_LIBRARY=${GGML_METAL_EMBED_LIBRARY}
99+ -DGGML_BLAS_DEFAULT=${GGML_BLAS_DEFAULT}
100+ -DGGML_METAL=${GGML_METAL}
100101 -DGGML_METAL_USE_BF16=${GGML_METAL_USE_BF16}
101- -DCMAKE_INSTALL_NAME_DIR=" @rpath/llama.framework/llama"
102102)
103103
104- # Build for iOS simulator.
104+ # Build for iOS simulator
105105cmake -B build-ios-sim -G Xcode \
106106 " ${COMMON_CMAKE_ARGS[@]} " \
107107 -DCMAKE_OSX_SYSROOT=iphonesimulator \
@@ -110,7 +110,7 @@ cmake -B build-ios-sim -G Xcode \
110110 -S .
111111cmake --build build-ios-sim --config Release
112112
113- # Build for iOS devices.
113+ # Build for iOS devices
114114cmake -B build-ios-device -G Xcode \
115115 " ${COMMON_CMAKE_ARGS[@]} " \
116116 -DCMAKE_OSX_SYSROOT=iphoneos \
@@ -119,24 +119,36 @@ cmake -B build-ios-device -G Xcode \
119119 -S .
120120cmake --build build-ios-device --config Release
121121
122- # Setup frameworks and copy binaries and headers.
122+ # Setup frameworks and copy binaries and headers
123123setup_framework_structure " build-ios-sim"
124124setup_framework_structure " build-ios-device"
125125
126- # Copy and rename the binaries.
127- cp build-ios-sim/bin/Release/libllama.dylib build-ios-sim/framework/llama.framework/llama
128- cp build-ios-device/bin/Release/libllama.dylib build-ios-device/framework/llama.framework/llama
126+ # Function to combine static libraries
127+ combine_static_libraries () {
128+ local build_dir=" $1 "
129+ local platform=" $2 "
130+ local output_lib=" ${build_dir} /framework/llama.framework/llama"
131+ local base_dir=" $( pwd) "
132+
133+ # Combine all static libraries using libtool
134+ libtool -static -o " ${base_dir} /${output_lib} " \
135+ " ${base_dir} /${build_dir} /src/Release-${platform} /libllama.a" \
136+ " ${base_dir} /${build_dir} /ggml/src/Release-${platform} /libggml.a" \
137+ " ${base_dir} /${build_dir} /ggml/src/Release-${platform} /libggml-base.a" \
138+ " ${base_dir} /${build_dir} /ggml/src/Release-${platform} /libggml-cpu.a" \
139+ " ${base_dir} /${build_dir} /ggml/src/ggml-metal/Release-${platform} /libggml-metal.a" \
140+ " ${base_dir} /${build_dir} /ggml/src/ggml-blas/Release-${platform} /libggml-blas.a"
141+ }
129142
130- # Fix the install name in the binaries
131- install_name_tool -id " @rpath/llama.framework/llama " build-ios-sim/framework/llama.framework/llama
132- install_name_tool -id " @rpath/llama.framework/llama " build-ios-device/framework/llama.framework/llama
143+ # Combine libraries for simulator and device builds
144+ combine_static_libraries " build-ios-sim" " iphonesimulator "
145+ combine_static_libraries " build-ios-device" " iphoneos "
133146
134- # Create XCFramework with the two builds which will contain both simulator
135- # and device versions in the same framework.
147+ # Create XCFramework
136148xcodebuild -create-xcframework \
137149 -framework $( pwd) /build-ios-sim/framework/llama.framework \
138150 -framework $( pwd) /build-ios-device/framework/llama.framework \
139151 -output $( pwd) /build-ios/llama.xcframework
140152
141153# The generated framework can be found in build-ios/llama.xcframework and
142- # can be added to a projects "Frameworks, Libraries, and Embedded Content".
154+ # can be added to a projects "Frameworks, Libraries, and Embedded Content"
0 commit comments