|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Options |
| 4 | +DEPLOYMENT_TARGET=14.0 |
| 5 | +BUILD_SHARED_LIBS=ON |
| 6 | +LLAMA_STATIC=OFF |
| 7 | +LLAMA_BUILD_EXAMPLES=OFF |
| 8 | +LLAMA_BUILD_TESTS=OFF |
| 9 | +LLAMA_BUILD_SERVER=OFF |
| 10 | +GGML_METAL_EMBED_LIBRARY=ON |
| 11 | +GGML_METAL_USE_BF16=ON |
| 12 | + |
| 13 | +set -xe |
| 14 | + |
| 15 | +rm -rf build-ios |
| 16 | +rm -rf build-ios-sim |
| 17 | +rm -rf build-ios-device |
| 18 | + |
| 19 | +# Function to setup framework structure for a given architecture |
| 20 | +setup_framework_structure() { |
| 21 | + local build_dir=$1 |
| 22 | + local framework_name="llama" |
| 23 | + |
| 24 | + # Create framework directory structure |
| 25 | + mkdir -p ${build_dir}/framework/${framework_name}.framework |
| 26 | + mkdir -p ${build_dir}/framework/${framework_name}.framework/Headers |
| 27 | + mkdir -p ${build_dir}/framework/${framework_name}.framework/Modules |
| 28 | + |
| 29 | + # Copy all required headers. |
| 30 | + cp include/llama.h ${build_dir}/framework/${framework_name}.framework/Headers/ |
| 31 | + cp ggml/include/ggml.h ${build_dir}/framework/${framework_name}.framework/Headers/ |
| 32 | + cp ggml/include/ggml-alloc.h ${build_dir}/framework/${framework_name}.framework/Headers/ |
| 33 | + cp ggml/include/ggml-backend.h ${build_dir}/framework/${framework_name}.framework/Headers/ |
| 34 | + cp ggml/include/ggml-metal.h ${build_dir}/framework/${framework_name}.framework/Headers/ |
| 35 | + cp ggml/include/ggml-cpu.h ${build_dir}/framework/${framework_name}.framework/Headers/ |
| 36 | + cp ggml/include/ggml-blas.h ${build_dir}/framework/${framework_name}.framework/Headers/ |
| 37 | + |
| 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. |
| 42 | + cat > ${build_dir}/framework/${framework_name}.framework/Modules/module.modulemap << EOF |
| 43 | +framework module llama { |
| 44 | + header "llama.h" |
| 45 | + header "ggml.h" |
| 46 | + header "ggml-alloc.h" |
| 47 | + header "ggml-backend.h" |
| 48 | + header "ggml-metal.h" |
| 49 | + header "ggml-cpu.h" |
| 50 | + header "ggml-blas.h" |
| 51 | + |
| 52 | + export * |
| 53 | +} |
| 54 | +EOF |
| 55 | + |
| 56 | + # Create Info.plist (Information Property List) file that describes the app. |
| 57 | + # This will be a Framework bundle (FMWK). |
| 58 | + cat > ${build_dir}/framework/${framework_name}.framework/Info.plist << EOF |
| 59 | +<?xml version="1.0" encoding="UTF-8"?> |
| 60 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 61 | +<plist version="1.0"> |
| 62 | +<dict> |
| 63 | + <key>CFBundleDevelopmentRegion</key> |
| 64 | + <string>en</string> |
| 65 | + <key>CFBundleExecutable</key> |
| 66 | + <string>llama</string> |
| 67 | + <key>CFBundleIdentifier</key> |
| 68 | + <string>ggml-org.llama.framework</string> |
| 69 | + <key>CFBundleInfoDictionaryVersion</key> |
| 70 | + <string>6.0</string> |
| 71 | + <key>CFBundleName</key> |
| 72 | + <string>llama</string> |
| 73 | + <key>CFBundlePackageType</key> |
| 74 | + <string>FMWK</string> |
| 75 | + <key>CFBundleShortVersionString</key> |
| 76 | + <string>1.0</string> |
| 77 | + <key>CFBundleVersion</key> |
| 78 | + <string>1</string> |
| 79 | + <key>MinimumOSVersion</key> |
| 80 | + <string>14.0</string> |
| 81 | +</dict> |
| 82 | +</plist> |
| 83 | +EOF |
| 84 | +} |
| 85 | + |
| 86 | +# Common options for all builds. |
| 87 | +COMMON_CMAKE_ARGS=( |
| 88 | + -DIOS=ON |
| 89 | + -DCMAKE_SYSTEM_NAME=iOS |
| 90 | + -DCMAKE_OSX_DEPLOYMENT_TARGET=${DEPLOYMENT_TARGET} |
| 91 | + -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=NO |
| 92 | + -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY="" |
| 93 | + -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO |
| 94 | + -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} |
| 95 | + -DLLAMA_STATIC=${LLAMA_STATIC} |
| 96 | + -DLLAMA_BUILD_EXAMPLES=${LLAMA_BUILD_EXAMPLES} |
| 97 | + -DLLAMA_BUILD_TESTS=${LLAMA_BUILD_TESTS} |
| 98 | + -DLLAMA_BUILD_SERVER=${LLAMA_BUILD_SERVER} |
| 99 | + -DGGML_METAL_EMBED_LIBRARY=${GGML_METAL_EMBED_LIBRARY} |
| 100 | + -DGGML_METAL_USE_BF16=${GGML_METAL_USE_BF16} |
| 101 | + -DCMAKE_INSTALL_NAME_DIR="@rpath/llama.framework/llama" |
| 102 | +) |
| 103 | + |
| 104 | +# Build for iOS simulator. |
| 105 | +cmake -B build-ios-sim -G Xcode \ |
| 106 | + "${COMMON_CMAKE_ARGS[@]}" \ |
| 107 | + -DCMAKE_OSX_SYSROOT=iphonesimulator \ |
| 108 | + -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \ |
| 109 | + -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphonesimulator \ |
| 110 | + -S . |
| 111 | +cmake --build build-ios-sim --config Release |
| 112 | + |
| 113 | +# Build for iOS devices. |
| 114 | +cmake -B build-ios-device -G Xcode \ |
| 115 | + "${COMMON_CMAKE_ARGS[@]}" \ |
| 116 | + -DCMAKE_OSX_SYSROOT=iphoneos \ |
| 117 | + -DCMAKE_OSX_ARCHITECTURES="arm64" \ |
| 118 | + -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphoneos \ |
| 119 | + -S . |
| 120 | +cmake --build build-ios-device --config Release |
| 121 | + |
| 122 | +# Setup frameworks and copy binaries and headers. |
| 123 | +setup_framework_structure "build-ios-sim" |
| 124 | +setup_framework_structure "build-ios-device" |
| 125 | + |
| 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 |
| 129 | + |
| 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 |
| 133 | + |
| 134 | +# Create XCFramework with the two builds which will contain both simulator |
| 135 | +# and device versions in the same framework. |
| 136 | +xcodebuild -create-xcframework \ |
| 137 | + -framework $(pwd)/build-ios-sim/framework/llama.framework \ |
| 138 | + -framework $(pwd)/build-ios-device/framework/llama.framework \ |
| 139 | + -output $(pwd)/build-ios/llama.xcframework |
| 140 | + |
| 141 | +# The generated framework can be found in build-ios/llama.xcframework and |
| 142 | +# can be added to a projects "Frameworks, Libraries, and Embedded Content". |
0 commit comments