char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b96806d96
char/aliceai-t5-moe
1#!/usr/bin/env bash 2# 3# usage: ./build-xcframework.sh [BUILD ...] (default: all builds) 4# builds: ios-sim ios-device macos visionos visionos-sim tvos-sim tvos-device 5# 6# Options 7IOS_MIN_OS_VERSION =16.4 8MACOS_MIN_OS_VERSION =13.3 9VISIONOS_MIN_OS_VERSION =1.0 10TVOS_MIN_OS_VERSION =16.4 11 12BUILD_SHARED_LIBS =OFF 13LLAMA_BUILD_APP =OFF 14LLAMA_BUILD_COMMON =OFF 15LLAMA_BUILD_EXAMPLES =OFF 16LLAMA_BUILD_TOOLS =OFF 17LLAMA_BUILD_TESTS =OFF 18LLAMA_BUILD_SERVER =OFF 19LLAMA_BUILD_MTMD =ON 20GGML_METAL =ON 21GGML_METAL_EMBED_LIBRARY =${ GGML_METAL_EMBED_LIBRARY :-ON} 22GGML_BLAS_DEFAULT =ON 23GGML_OPENMP =OFF 24 25# Max number of concurrent platform builds 26MAX_PARALLEL_BUILDS =1 27 28# Split the available cores between the concurrent builds (min 1) 29JOBS_PER_BUILD =$(($( sysctl -n hw.logicalcpu) /MAX_PARALLEL_BUILDS )) 30if [[" $ JOBS_PER_BUILD " -lt 1 ]];then 31JOBS_PER_BUILD =1 32fi 33 34# echo "build_fn build_dir release_dir platform is_simulator min_os" for a build name 35build_spec () { 36case " $ 1 " in 37 ios-sim)echo "build_ios_sim build-ios-sim Release-iphonesimulator ios true ${ IOS_MIN_OS_VERSION } " ;; 38 ios-device)echo "build_ios_device build-ios-device Release-iphoneos ios false ${ IOS_MIN_OS_VERSION } " ;; 39 macos)echo "build_macos build-macos Release macos false ${ MACOS_MIN_OS_VERSION } " ;; 40 visionos)echo "build_visionos build-visionos Release-xros visionos false ${ VISIONOS_MIN_OS_VERSION } " ;; 41 visionos-sim)echo "build_visionos_sim build-visionos-sim Release-xrsimulator visionos true ${ VISIONOS_MIN_OS_VERSION } " ;; 42 tvos-sim)echo "build_tvos_sim build-tvos-sim Release-appletvsimulator tvos true ${ TVOS_MIN_OS_VERSION } " ;; 43 tvos-device)echo "build_tvos_device build-tvos-device Release-appletvos tvos false ${ TVOS_MIN_OS_VERSION } " ;; 44 *)return 1 ;; 45esac 46} 47 48# Default: build everything 49if [[$ # -eq 0 ]];then 50BUILDS =(ios-sim ios-device macos visionos visionos-sim tvos-sim tvos-device) 51else 52BUILDS =(" $ @" ) 53fi 54for b in " ${ BUILDS [@]} " ;do 55if !build_spec " $ b " > /dev/null;then 56echo "Error: unknown build ' $ b '" >&2 57echo "Valid builds: ios-sim ios-device macos visionos visionos-sim tvos-sim tvos-device" >&2 58exit 1 59fi 60done 61 62COMMON_C_FLAGS ="-Wno-macro-redefined -Wno-shorten-64-to-32 -Wno-unused-command-line-argument -g" 63COMMON_CXX_FLAGS ="-Wno-macro-redefined -Wno-shorten-64-to-32 -Wno-unused-command-line-argument -g" 64 65# Common options for all builds 66COMMON_CMAKE_ARGS =( 67 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=NO 68 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY="" 69 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO 70 -DCMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT="dwarf-with-dsym" 71 -DCMAKE_XCODE_ATTRIBUTE_GCC_GENERATE_DEBUGGING_SYMBOLS=YES 72 -DCMAKE_XCODE_ATTRIBUTE_COPY_PHASE_STRIP=NO 73 -DCMAKE_XCODE_ATTRIBUTE_STRIP_INSTALLED_PRODUCT=NO 74 -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml 75 -DBUILD_SHARED_LIBS=${ BUILD_SHARED_LIBS } 76 -DLLAMA_BUILD_APP=${ LLAMA_BUILD_APP } 77 -DLLAMA_BUILD_COMMON=${ LLAMA_BUILD_COMMON } 78 -DLLAMA_BUILD_EXAMPLES=${ LLAMA_BUILD_EXAMPLES } 79 -DLLAMA_BUILD_TOOLS=${ LLAMA_BUILD_TOOLS } 80 -DLLAMA_BUILD_TESTS=${ LLAMA_BUILD_TESTS } 81 -DLLAMA_BUILD_SERVER=${ LLAMA_BUILD_SERVER } 82 -DLLAMA_BUILD_MTMD=${ LLAMA_BUILD_MTMD } 83 -DGGML_METAL_EMBED_LIBRARY=${ GGML_METAL_EMBED_LIBRARY } 84 -DGGML_BLAS_DEFAULT=${ GGML_BLAS_DEFAULT } 85 -DGGML_METAL=${ GGML_METAL } 86 -DGGML_NATIVE=OFF 87 -DGGML_OPENMP=${ GGML_OPENMP } 88) 89 90check_required_tool () { 91 localtool =$ 1 92 localinstall_message =$ 2 93 94if !command -v $ tool &> /dev/null;then 95echo "Error: $ tool is required but not found." 96echo " $ install_message " 97exit 1 98fi 99} 100echo "Checking for required tools..." 101check_required_tool "cmake" "Please install CMake 3.28.0 or later (brew install cmake)" 102check_required_tool "xcrun" "Please install Xcode and Xcode Command Line Tools (xcode-select --install)" 103 104XCODE_VERSION =$( xcrun xcodebuild -version 2 > /dev/null | head -n1 | awk '{ print $2 }' ) 105MAJOR_VERSION =$( echo $ XCODE_VERSION | cut -d. -f1 ) 106MINOR_VERSION =$( echo $ XCODE_VERSION | cut -d. -f2 ) 107echo "Detected Xcode version: $ XCODE_VERSION " 108 109set -e 110 111## Clean up previous builds 112rm -rf build-apple 113rm -rf build-ios-sim 114rm -rf build-ios-device 115rm -rf build-macos 116rm -rf build-visionos 117rm -rf build-visionos-sim 118rm -rf build-tvos-sim 119rm -rf build-tvos-device 120 121# Setup the xcframework build directory structure 122setup_framework_structure () { 123 localbuild_dir =$ 1 124 localmin_os_version =$ 2 125 localplatform =$ 3 # "ios", "macos", "visionos", or "tvos" 126 localframework_name ="llama" 127 128echo "Creating ${ platform } -style framework structure for ${ build_dir } " 129 130if [[" $ platform " =="macos" ]];then 131# macOS versioned structure uses versioned directories 132mkdir -p ${ build_dir } /framework/${ framework_name } .framework/Versions/A/Headers 133mkdir -p ${ build_dir } /framework/${ framework_name } .framework/Versions/A/Modules 134mkdir -p ${ build_dir } /framework/${ framework_name } .framework/Versions/A/Resources 135 136# Create symbolic links 137ln -sf A${ build_dir } /framework/${ framework_name } .framework/Versions/Current 138ln -sf Versions/Current/Headers${ build_dir } /framework/${ framework_name } .framework/Headers 139ln -sf Versions/Current/Modules${ build_dir } /framework/${ framework_name } .framework/Modules 140ln -sf Versions/Current/Resources${ build_dir } /framework/${ framework_name } .framework/Resources 141ln -sf Versions/Current/${ framework_name } ${ build_dir } /framework/${ framework_name } .framework/${ framework_name } 142 143# Set header and module paths 144 localheader_path =${ build_dir } /framework/${ framework_name } .framework/Versions/A/Headers/ 145 localmodule_path =${ build_dir } /framework/${ framework_name } .framework/Versions/A/Modules/ 146else 147# iOS/VisionOS/tvOS use a flat structure 148mkdir -p ${ build_dir } /framework/${ framework_name } .framework/Headers 149mkdir -p ${ build_dir } /framework/${ framework_name } .framework/Modules 150 151# Remove any existing structure to ensure clean build 152rm -rf ${ build_dir } /framework/${ framework_name } .framework/Versions 153 154# Set header and module paths 155 localheader_path =${ build_dir } /framework/${ framework_name } .framework/Headers/ 156 localmodule_path =${ build_dir } /framework/${ framework_name } .framework/Modules/ 157fi 158 159# Copy all required headers (common for all platforms) 160cp include/llama.h${ header_path } 161cp ggml/include/ggml.h${ header_path } 162cp ggml/include/ggml-opt.h${ header_path } 163cp ggml/include/ggml-alloc.h${ header_path } 164cp ggml/include/ggml-backend.h${ header_path } 165cp ggml/include/ggml-metal.h${ header_path } 166cp ggml/include/ggml-cpu.h${ header_path } 167cp ggml/include/ggml-blas.h${ header_path } 168cp ggml/include/gguf.h${ header_path } 169cp tools/mtmd/mtmd.h${ header_path } 170cp tools/mtmd/mtmd-helper.h${ header_path } 171 172if [[" $ GGML_METAL_EMBED_LIBRARY " =="OFF" ]];then 173if [[" $ platform " =="macos" ]];then 174cp ${ build_dir } /bin/*.metallib${ build_dir } /framework/${ framework_name } .framework/Versions/A/Resources/ 175else 176cp ${ build_dir } /bin/*.metallib${ build_dir } /framework/${ framework_name } .framework/ 177fi 178fi 179 180# Create module map (common for all platforms) 181cat > ${ module_path } module.modulemap <<EOF 182framework module llama { 183umbrella "Headers" 184 185link "c++" 186link framework "Accelerate" 187link framework "Metal" 188link framework "Foundation" 189 190export * 191} 192EOF 193 194# Platform-specific settings for Info.plist 195 localplatform_name ="" 196 localsdk_name ="" 197 localsupported_platform ="" 198 199case " $ platform " in 200"ios" ) 201platform_name ="iphoneos" 202sdk_name ="iphoneos ${ min_os_version } " 203supported_platform ="iPhoneOS" 204 localplist_path =" ${ build_dir } /framework/ ${ framework_name } .framework/Info.plist" 205 localdevice_family =' <key>UIDeviceFamily</key> 206<array> 207<integer>1</integer> 208<integer>2</integer> 209</array>' 210 ;; 211"macos" ) 212platform_name ="macosx" 213sdk_name ="macosx ${ min_os_version } " 214supported_platform ="MacOSX" 215 localplist_path =" ${ build_dir } /framework/ ${ framework_name } .framework/Versions/A/Resources/Info.plist" 216 localdevice_family ="" 217 ;; 218"visionos" ) 219platform_name ="xros" 220sdk_name ="xros ${ min_os_version } " 221supported_platform ="XRPlatform" 222 localplist_path =" ${ build_dir } /framework/ ${ framework_name } .framework/Info.plist" 223 localdevice_family ="" 224 ;; 225"tvos" ) 226platform_name ="appletvos" 227sdk_name ="appletvos ${ min_os_version } " 228supported_platform ="AppleTVOS" 229 localplist_path =" ${ build_dir } /framework/ ${ framework_name } .framework/Info.plist" 230 localdevice_family =' <key>UIDeviceFamily</key> 231<array> 232<integer>3</integer> 233</array>' 234 ;; 235esac 236 237# Create Info.plist 238cat > ${ plist_path } <<EOF 239<?xml version="1.0" encoding="UTF-8"?> 240<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 241<plist version="1.0"> 242<dict> 243<key>CFBundleDevelopmentRegion</key> 244<string>en</string> 245<key>CFBundleExecutable</key> 246<string>llama</string> 247<key>CFBundleIdentifier</key> 248<string>org.ggml.llama</string> 249<key>CFBundleInfoDictionaryVersion</key> 250<string>6.0</string> 251<key>CFBundleName</key> 252<string>llama</string> 253<key>CFBundlePackageType</key> 254<string>FMWK</string> 255<key>CFBundleShortVersionString</key> 256<string>1.0</string> 257<key>CFBundleVersion</key> 258<string>1</string> 259<key>MinimumOSVersion</key> 260<string> ${ min_os_version } </string> 261<key>CFBundleSupportedPlatforms</key> 262<array> 263<string> ${ supported_platform } </string> 264</array> ${ device_family } 265<key>DTPlatformName</key> 266<string> ${ platform_name } </string> 267<key>DTSDKName</key> 268<string> ${ sdk_name } </string> 269</dict> 270</plist> 271EOF 272} 273 274# Create dynamic libraries from static libraries. 275combine_static_libraries () { 276 localbuild_dir =" $ 1 " 277 localrelease_dir =" $ 2 " 278 localplatform =" $ 3 " # "ios", "macos", "visionos", or "tvos" 279 localis_simulator =" $ 4 " 280 localbase_dir =" $( pwd ) " 281 localframework_name ="llama" 282 283# Determine output path based on platform 284 localoutput_lib ="" 285if [[" $ platform " =="macos" ]];then 286# macOS uses versioned structure 287output_lib =" ${ build_dir } /framework/ ${ framework_name } .framework/Versions/A/ ${ framework_name } " 288else 289# iOS, visionOS, and tvOS use a directory flat structure 290output_lib =" ${ build_dir } /framework/ ${ framework_name } .framework/ ${ framework_name } " 291fi 292 293 locallibs =( 294" ${ base_dir } / ${ build_dir } /src/ ${ release_dir } /libllama.a" 295" ${ base_dir } / ${ build_dir } /ggml/src/ ${ release_dir } /libggml.a" 296" ${ base_dir } / ${ build_dir } /ggml/src/ ${ release_dir } /libggml-base.a" 297" ${ base_dir } / ${ build_dir } /ggml/src/ ${ release_dir } /libggml-cpu.a" 298" ${ base_dir } / ${ build_dir } /ggml/src/ggml-metal/ ${ release_dir } /libggml-metal.a" 299" ${ base_dir } / ${ build_dir } /ggml/src/ggml-blas/ ${ release_dir } /libggml-blas.a" 300" ${ base_dir } / ${ build_dir } /tools/mtmd/ ${ release_dir } /libmtmd.a" 301" ${ base_dir } / ${ build_dir } /vendor/hash/ ${ release_dir } /libvendor-hash.a" 302 ) 303 304# Create temporary directory for processing 305 localtemp_dir =" ${ base_dir } / ${ build_dir } /temp" 306mkdir -p " ${ temp_dir } " 307 308# Since we have multiple architectures libtool will find object files that do not 309# match the target architecture. We suppress these warnings. 310xcrun libtool-static -o " ${ temp_dir } /combined.a" " ${ libs [@]} " 2 > /dev/null 311 312# Determine SDK, architectures, and install_name based on platform and simulator flag. 313 localsdk ="" 314 localarchs ="" 315 localmin_version_flag ="" 316 localinstall_name ="" 317 318case " $ platform " in 319"ios" ) 320if [[" $ is_simulator " =="true" ]];then 321sdk ="iphonesimulator" 322archs ="arm64 x86_64" 323min_version_flag ="-mios-simulator-version-min= ${ IOS_MIN_OS_VERSION } " 324else 325sdk ="iphoneos" 326archs ="arm64" 327min_version_flag ="-mios-version-min= ${ IOS_MIN_OS_VERSION } " 328fi 329install_name ="@rpath/llama.framework/llama" 330 ;; 331"macos" ) 332sdk ="macosx" 333archs ="arm64 x86_64" 334min_version_flag ="-mmacosx-version-min= ${ MACOS_MIN_OS_VERSION } " 335install_name ="@rpath/llama.framework/Versions/Current/llama" 336 ;; 337"visionos" ) 338if [[" $ is_simulator " =="true" ]];then 339sdk ="xrsimulator" 340archs ="arm64 x86_64" 341min_version_flag ="-mtargetos=xros ${ VISIONOS_MIN_OS_VERSION } -simulator" 342else 343sdk ="xros" 344archs ="arm64" 345min_version_flag ="-mtargetos=xros ${ VISIONOS_MIN_OS_VERSION } " 346fi 347# Use flat structure for visionOS, same as iOS 348install_name ="@rpath/llama.framework/llama" 349 ;; 350"tvos" ) 351if [[" $ is_simulator " =="true" ]];then 352sdk ="appletvsimulator" 353archs ="arm64 x86_64" 354min_version_flag ="-mtvos-simulator-version-min= ${ TVOS_MIN_OS_VERSION } " 355else 356sdk ="appletvos" 357archs ="arm64" 358min_version_flag ="-mtvos-version-min= ${ TVOS_MIN_OS_VERSION } " 359fi 360install_name ="@rpath/llama.framework/llama" 361 ;; 362esac 363 364# Build architecture flags 365 localarch_flags ="" 366for arch in $ archs ;do 367arch_flags +=" -arch $ arch " 368done 369 370# Create dynamic library 371echo "Creating dynamic library for ${ platform } ." 372xcrun -sdk $ sdk clang++-dynamiclib \ 373-isysroot $( xcrun --sdk $ sdk --show-sdk-path ) \ 374$ arch_flags \ 375$ min_version_flag \ 376-Wl,-force_load, " ${ temp_dir } /combined.a" \ 377-framework Foundation-framework Metal-framework Accelerate \ 378-install_name " $ install_name " \ 379-o " ${ base_dir } / ${ output_lib } " 380 381# Platform-specific post-processing for device builds 382if [[" $ is_simulator " =="false" ]];then 383if xcrun -f vtool &>/dev/null;then 384case " $ platform " in 385"ios" ) 386echo "Marking binary as a framework binary for iOS..." 387xcrun vtool-set-build-version ios${ IOS_MIN_OS_VERSION } ${ IOS_MIN_OS_VERSION } -replace \ 388-output " ${ base_dir } / ${ output_lib } " " ${ base_dir } / ${ output_lib } " 389 ;; 390"visionos" ) 391echo "Marking binary as a framework binary for visionOS..." 392if [[" $ MAJOR_VERSION " -gt 16 ]] || [[" $ MAJOR_VERSION " -eq 16&& " $ MINOR_VERSION " -gt 2 ]];then 393echo "Xcode version greater than 16.2, using visionOS." 394VISION_OS_BUILD_VERSION ="visionos" 395else 396echo "Xcode version less than or equal to 16.2, using xros." 397VISION_OS_BUILD_VERSION ="xros" 398fi 399xcrun vtool-set-build-version ${ VISION_OS_BUILD_VERSION } ${ VISIONOS_MIN_OS_VERSION } ${ VISIONOS_MIN_OS_VERSION } -replace \ 400-output " ${ base_dir } / ${ output_lib } " " ${ base_dir } / ${ output_lib } " 401 ;; 402"tvos" ) 403echo "Marking binary as a framework binary for tvOS..." 404xcrun vtool-set-build-version tvos${ TVOS_MIN_OS_VERSION } ${ TVOS_MIN_OS_VERSION } -replace \ 405-output " ${ base_dir } / ${ output_lib } " " ${ base_dir } / ${ output_lib } " 406 ;; 407esac 408else 409echo "Warning: vtool not found. Binary may not pass App Store validation." 410fi 411fi 412 413echo "Creating properly formatted dSYM..." 414# Create a separate directory for dSYMs for all platforms 415mkdir -p " ${ base_dir } / ${ build_dir } /dSYMs" 416 417# iOS and visionOS style dSYM (flat structure) 418if [[" $ platform " =="ios" ||" $ platform " =="visionos" ||" $ platform " =="tvos" ]];then 419# Generate dSYM in the dSYMs directory 420xcrun dsymutil" ${ base_dir } / ${ output_lib } " -o " ${ base_dir } / ${ build_dir } /dSYMs/llama.dSYM" 421 422# Create a copy of the binary that will be stripped 423cp " ${ base_dir } / ${ output_lib } " " ${ temp_dir } /binary_to_strip" 424 425# Strip debug symbols from the copy 426xcrun strip-S " ${ temp_dir } /binary_to_strip" -o " ${ temp_dir } /stripped_lib" 427 428# Replace the original with the stripped version 429mv " ${ temp_dir } /stripped_lib" " ${ base_dir } / ${ output_lib } " 430else 431# macOS style dSYM 432# First strip debug info to a separate file 433xcrun strip-S " ${ base_dir } / ${ output_lib } " -o " ${ temp_dir } /stripped_lib" 434 435# Generate dSYM in the dSYMs directory 436xcrun dsymutil" ${ base_dir } / ${ output_lib } " -o " ${ base_dir } / ${ build_dir } /dSYMs/llama.dSYM" 437 438# Replace original binary with stripped version 439mv " ${ temp_dir } /stripped_lib" " ${ base_dir } / ${ output_lib } " 440fi 441 442# Remove any automatically generated dSYM files in the framework structure as they will 443# otherwise case Invalid Bundle Structure validation errors. 444if [ -d" ${ base_dir } / ${ output_lib } .dSYM" ];then 445echo "Removing generated dSYM file in framework structure: ${ base_dir } / ${ output_lib } .dSYM" 446rm -rf " ${ base_dir } / ${ output_lib } .dSYM" 447fi 448 449# Clean up 450rm -rf " ${ temp_dir } " 451} 452 453build_ios_sim () { 454echo "Building for iOS simulator..." 455cmake -B build-ios-sim-G Xcode \ 456" ${ COMMON_CMAKE_ARGS [@]} " \ 457-DCMAKE_OSX_DEPLOYMENT_TARGET= ${ IOS_MIN_OS_VERSION } \ 458-DIOS=ON \ 459-DCMAKE_SYSTEM_NAME=iOS \ 460-DCMAKE_OSX_SYSROOT=iphonesimulator \ 461-DGGML_METAL_TARGET_OS=ios \ 462-DCMAKE_OSX_ARCHITECTURES= "arm64;x86_64" \ 463-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphonesimulator \ 464-DCMAKE_C_FLAGS= " ${ COMMON_C_FLAGS } " \ 465-DCMAKE_CXX_FLAGS= " ${ COMMON_CXX_FLAGS } " \ 466-DLLAMA_OPENSSL=OFF \ 467-DMTMD_VIDEO=OFF \ 468-S . 469cmake --build build-ios-sim--config Release-j " ${ JOBS_PER_BUILD } " -- -quiet 470} 471 472build_ios_device () { 473echo "Building for iOS devices..." 474cmake -B build-ios-device-G Xcode \ 475" ${ COMMON_CMAKE_ARGS [@]} " \ 476-DCMAKE_OSX_DEPLOYMENT_TARGET= ${ IOS_MIN_OS_VERSION } \ 477-DCMAKE_SYSTEM_NAME=iOS \ 478-DCMAKE_OSX_SYSROOT=iphoneos \ 479-DGGML_METAL_TARGET_OS=ios \ 480-DCMAKE_OSX_ARCHITECTURES= "arm64" \ 481-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphoneos \ 482-DCMAKE_C_FLAGS= " ${ COMMON_C_FLAGS } " \ 483-DCMAKE_CXX_FLAGS= " ${ COMMON_CXX_FLAGS } " \ 484-DLLAMA_OPENSSL=OFF \ 485-DMTMD_VIDEO=OFF \ 486-S . 487cmake --build build-ios-device--config Release-j " ${ JOBS_PER_BUILD } " -- -quiet 488} 489 490build_macos () { 491echo "Building for macOS..." 492cmake -B build-macos-G Xcode \ 493" ${ COMMON_CMAKE_ARGS [@]} " \ 494-DCMAKE_OSX_DEPLOYMENT_TARGET= ${ MACOS_MIN_OS_VERSION } \ 495-DCMAKE_OSX_ARCHITECTURES= "arm64;x86_64" \ 496-DCMAKE_C_FLAGS= " ${ COMMON_C_FLAGS } " \ 497-DCMAKE_CXX_FLAGS= " ${ COMMON_CXX_FLAGS } " \ 498-DLLAMA_OPENSSL=OFF \ 499-S . 500cmake --build build-macos--config Release-j " ${ JOBS_PER_BUILD } " -- -quiet 501} 502 503build_visionos () { 504echo "Building for visionOS..." 505cmake -B build-visionos-G Xcode \ 506" ${ COMMON_CMAKE_ARGS [@]} " \ 507-DCMAKE_OSX_DEPLOYMENT_TARGET= ${ VISIONOS_MIN_OS_VERSION } \ 508-DCMAKE_OSX_ARCHITECTURES= "arm64" \ 509-DCMAKE_SYSTEM_NAME=visionOS \ 510-DCMAKE_OSX_SYSROOT=xros \ 511-DGGML_METAL_TARGET_OS=xros \ 512-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=xros \ 513-DCMAKE_C_FLAGS= " ${ COMMON_C_FLAGS } " \ 514-DCMAKE_CXX_FLAGS= " ${ COMMON_CXX_FLAGS } " \ 515-DLLAMA_OPENSSL=OFF \ 516-DLLAMA_BUILD_SERVER=OFF \ 517-DMTMD_VIDEO=OFF \ 518-S . 519cmake --build build-visionos--config Release-j " ${ JOBS_PER_BUILD } " -- -quiet 520} 521 522build_visionos_sim () { 523echo "Building for visionOS simulator..." 524cmake -B build-visionos-sim-G Xcode \ 525" ${ COMMON_CMAKE_ARGS [@]} " \ 526-DCMAKE_OSX_DEPLOYMENT_TARGET= ${ VISIONOS_MIN_OS_VERSION } \ 527-DCMAKE_OSX_ARCHITECTURES= "arm64;x86_64" \ 528-DCMAKE_SYSTEM_NAME=visionOS \ 529-DCMAKE_OSX_SYSROOT=xrsimulator \ 530-DGGML_METAL_TARGET_OS=xros \ 531-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=xrsimulator \ 532-DCMAKE_C_FLAGS= " ${ COMMON_C_FLAGS } " \ 533-DCMAKE_CXX_FLAGS= " ${ COMMON_CXX_FLAGS } " \ 534-DLLAMA_OPENSSL=OFF \ 535-DLLAMA_BUILD_SERVER=OFF \ 536-DMTMD_VIDEO=OFF \ 537-S . 538cmake --build build-visionos-sim--config Release-j " ${ JOBS_PER_BUILD } " -- -quiet 539} 540 541# Add tvOS builds (might need the same u_int definitions as watchOS and visionOS) 542build_tvos_sim () { 543echo "Building for tvOS simulator..." 544cmake -B build-tvos-sim-G Xcode \ 545" ${ COMMON_CMAKE_ARGS [@]} " \ 546-DCMAKE_OSX_DEPLOYMENT_TARGET= ${ TVOS_MIN_OS_VERSION } \ 547-DCMAKE_SYSTEM_NAME=tvOS \ 548-DCMAKE_OSX_SYSROOT=appletvsimulator \ 549-DGGML_METAL_TARGET_OS=tvos \ 550-DCMAKE_OSX_ARCHITECTURES= "arm64;x86_64" \ 551-DGGML_METAL=ON \ 552-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=appletvsimulator \ 553-DCMAKE_C_FLAGS= " ${ COMMON_C_FLAGS } " \ 554-DCMAKE_CXX_FLAGS= " ${ COMMON_CXX_FLAGS } " \ 555-DLLAMA_OPENSSL=OFF \ 556-DMTMD_VIDEO=OFF \ 557-S . 558cmake --build build-tvos-sim--config Release-j " ${ JOBS_PER_BUILD } " -- -quiet 559} 560 561build_tvos_device () { 562echo "Building for tvOS devices..." 563cmake -B build-tvos-device-G Xcode \ 564" ${ COMMON_CMAKE_ARGS [@]} " \ 565-DCMAKE_OSX_DEPLOYMENT_TARGET= ${ TVOS_MIN_OS_VERSION } \ 566-DCMAKE_SYSTEM_NAME=tvOS \ 567-DCMAKE_OSX_SYSROOT=appletvos \ 568-DGGML_METAL_TARGET_OS=tvos \ 569-DCMAKE_OSX_ARCHITECTURES= "arm64" \ 570-DGGML_METAL=ON \ 571-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=appletvos \ 572-DCMAKE_C_FLAGS= " ${ COMMON_C_FLAGS } " \ 573-DCMAKE_CXX_FLAGS= " ${ COMMON_CXX_FLAGS } " \ 574-DLLAMA_OPENSSL=OFF \ 575-DMTMD_VIDEO=OFF \ 576-S . 577cmake --build build-tvos-device--config Release-j " ${ JOBS_PER_BUILD } " -- -quiet 578} 579 580run_builds_parallel () { 581 local -apids =() 582 local -anames =() 583 localname i 584for name in " $ @" ;do 585# Wait for the oldest running build to free a slot 586if [[" ${# pids [@]} " -ge" $ MAX_PARALLEL_BUILDS " ]];then 587if !wait " ${ pids [0]} " ;then 588echo "ERROR: build ' ${ names [0]} ' failed, log follows ( ${ names [0]} .log):" >&2 589kill " ${ pids [@]} " 2 > /dev/null ||true 590cat " ${ names [0]} .log" >&2 591exit 1 592fi 593pids =(" ${ pids [@]:1} " ) 594names =(" ${ names [@]:1} " ) 595fi 596echo "Starting build: $ name (log: ${ name } .log, -j ${ JOBS_PER_BUILD } )" 597" $ name " > " ${ name } .log" 2 >&1 & 598pids +=(" $ !" ) 599names +=(" $ name " ) 600done 601# Wait for the remaining builds 602for i in " ${! pids [@]} " ;do 603if !wait " ${ pids [ $ i ]} " ;then 604echo "ERROR: build ' ${ names [ $ i ]} ' failed, log follows ( ${ names [ $ i ]} .log):" >&2 605kill " ${ pids [@]} " 2 > /dev/null ||true 606cat " ${ names [ $ i ]} .log" >&2 607exit 1 608fi 609done 610} 611 612BUILD_FNS =() 613for b in " ${ BUILDS [@]} " ;do 614read -r fn _< <( build_spec " $ b " ) 615BUILD_FNS +=(" $ fn " ) 616done 617echo "Building: ${ BUILDS [*]} (max ${ MAX_PARALLEL_BUILDS } at a time, -j ${ JOBS_PER_BUILD } each)..." 618run_builds_parallel " ${ BUILD_FNS [@]} " 619 620# Setup frameworks and copy binaries and headers 621echo "Setting up framework structures..." 622for b in " ${ BUILDS [@]} " ;do 623read -r _ bdir _ platform _ min_os< <( build_spec " $ b " ) 624setup_framework_structure " $ bdir " " $ min_os " " $ platform " 625done 626 627# Create dynamic libraries from static libraries 628echo "Creating dynamic libraries from static libraries..." 629for b in " ${ BUILDS [@]} " ;do 630read -r _ bdir rdir platform is_sim _< <( build_spec " $ b " ) 631combine_static_libraries " $ bdir " " $ rdir " " $ platform " " $ is_sim " 632done 633 634# Create XCFramework with correct debug symbols paths 635echo "Creating XCFramework..." 636XCFW_ARGS =() 637for b in " ${ BUILDS [@]} " ;do 638read -r _ bdir _ _ _ _< <( build_spec " $ b " ) 639XCFW_ARGS +=(-framework" $( pwd ) / ${ bdir } /framework/llama.framework" ) 640XCFW_ARGS +=(-debug-symbols" $( pwd ) / ${ bdir } /dSYMs/llama.dSYM" ) 641done 642xcrun xcodebuild-create-xcframework \ 643" ${ XCFW_ARGS [@]} " \ 644-output " $( pwd ) /build-apple/llama.xcframework"