build-xcframework.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. #!/bin/bash
  2. #
  3. # Options
  4. IOS_MIN_OS_VERSION=16.4
  5. MACOS_MIN_OS_VERSION=13.3
  6. VISIONOS_MIN_OS_VERSION=1.0
  7. TVOS_MIN_OS_VERSION=16.4
  8. BUILD_SHARED_LIBS=OFF
  9. LLAMA_BUILD_EXAMPLES=OFF
  10. LLAMA_BUILD_TESTS=OFF
  11. LLAMA_BUILD_SERVER=OFF
  12. GGML_METAL=ON
  13. GGML_METAL_EMBED_LIBRARY=ON
  14. GGML_BLAS_DEFAULT=ON
  15. GGML_METAL_USE_BF16=ON
  16. GGML_OPENMP=OFF
  17. COMMON_C_FLAGS="-Wno-macro-redefined -Wno-shorten-64-to-32 -Wno-unused-command-line-argument -g"
  18. COMMON_CXX_FLAGS="-Wno-macro-redefined -Wno-shorten-64-to-32 -Wno-unused-command-line-argument -g"
  19. # Common options for all builds
  20. COMMON_CMAKE_ARGS=(
  21. -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=NO
  22. -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY=""
  23. -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO
  24. -DCMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT="dwarf-with-dsym"
  25. -DCMAKE_XCODE_ATTRIBUTE_GCC_GENERATE_DEBUGGING_SYMBOLS=YES
  26. -DCMAKE_XCODE_ATTRIBUTE_COPY_PHASE_STRIP=NO
  27. -DCMAKE_XCODE_ATTRIBUTE_STRIP_INSTALLED_PRODUCT=NO
  28. -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=ggml
  29. -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
  30. -DLLAMA_BUILD_EXAMPLES=${LLAMA_BUILD_EXAMPLES}
  31. -DLLAMA_BUILD_TESTS=${LLAMA_BUILD_TESTS}
  32. -DLLAMA_BUILD_SERVER=${LLAMA_BUILD_SERVER}
  33. -DGGML_METAL_EMBED_LIBRARY=${GGML_METAL_EMBED_LIBRARY}
  34. -DGGML_BLAS_DEFAULT=${GGML_BLAS_DEFAULT}
  35. -DGGML_METAL=${GGML_METAL}
  36. -DGGML_METAL_USE_BF16=${GGML_METAL_USE_BF16}
  37. -DGGML_NATIVE=OFF
  38. -DGGML_OPENMP=${GGML_OPENMP}
  39. )
  40. XCODE_VERSION=$(xcodebuild -version 2>/dev/null | head -n1 | awk '{ print $2 }')
  41. MAJOR_VERSION=$(echo $XCODE_VERSION | cut -d. -f1)
  42. MINOR_VERSION=$(echo $XCODE_VERSION | cut -d. -f2)
  43. echo "Detected Xcode version: $XCODE_VERSION"
  44. check_required_tool() {
  45. local tool=$1
  46. local install_message=$2
  47. if ! command -v $tool &> /dev/null; then
  48. echo "Error: $tool is required but not found."
  49. echo "$install_message"
  50. exit 1
  51. fi
  52. }
  53. echo "Checking for required tools..."
  54. check_required_tool "cmake" "Please install CMake 3.28.0 or later (brew install cmake)"
  55. check_required_tool "xcodebuild" "Please install Xcode and Xcode Command Line Tools (xcode-select --install)"
  56. check_required_tool "libtool" "Please install libtool which should be available with Xcode Command Line Tools (CLT). Make sure Xcode CLT is installed (xcode-select --install)"
  57. check_required_tool "dsymutil" "Please install Xcode and Xcode Command Line Tools (xcode-select --install)"
  58. set -e
  59. ## Clean up previous builds
  60. rm -rf build-apple
  61. rm -rf build-ios-sim
  62. rm -rf build-ios-device
  63. rm -rf build-macos
  64. rm -rf build-visionos
  65. rm -rf build-visionos-sim
  66. rm -rf build-tvos-sim
  67. rm -rf build-tvos-device
  68. # Setup the xcframework build directory structure
  69. setup_framework_structure() {
  70. local build_dir=$1
  71. local min_os_version=$2
  72. local platform=$3 # "ios", "macos", "visionos", or "tvos"
  73. local framework_name="llama"
  74. echo "Creating ${platform}-style framework structure for ${build_dir}"
  75. if [[ "$platform" == "macos" ]]; then
  76. # macOS versioned structure uses versioned directories
  77. mkdir -p ${build_dir}/framework/${framework_name}.framework/Versions/A/Headers
  78. mkdir -p ${build_dir}/framework/${framework_name}.framework/Versions/A/Modules
  79. mkdir -p ${build_dir}/framework/${framework_name}.framework/Versions/A/Resources
  80. # Create symbolic links
  81. ln -sf A ${build_dir}/framework/${framework_name}.framework/Versions/Current
  82. ln -sf Versions/Current/Headers ${build_dir}/framework/${framework_name}.framework/Headers
  83. ln -sf Versions/Current/Modules ${build_dir}/framework/${framework_name}.framework/Modules
  84. ln -sf Versions/Current/Resources ${build_dir}/framework/${framework_name}.framework/Resources
  85. ln -sf Versions/Current/${framework_name} ${build_dir}/framework/${framework_name}.framework/${framework_name}
  86. # Set header and module paths
  87. local header_path=${build_dir}/framework/${framework_name}.framework/Versions/A/Headers/
  88. local module_path=${build_dir}/framework/${framework_name}.framework/Versions/A/Modules/
  89. else
  90. # iOS/VisionOS/tvOS use a flat structure
  91. mkdir -p ${build_dir}/framework/${framework_name}.framework/Headers
  92. mkdir -p ${build_dir}/framework/${framework_name}.framework/Modules
  93. # Remove any existing structure to ensure clean build
  94. rm -rf ${build_dir}/framework/${framework_name}.framework/Versions
  95. # Set header and module paths
  96. local header_path=${build_dir}/framework/${framework_name}.framework/Headers/
  97. local module_path=${build_dir}/framework/${framework_name}.framework/Modules/
  98. fi
  99. # Copy all required headers (common for all platforms)
  100. cp include/llama.h ${header_path}
  101. cp ggml/include/ggml.h ${header_path}
  102. cp ggml/include/ggml-alloc.h ${header_path}
  103. cp ggml/include/ggml-backend.h ${header_path}
  104. cp ggml/include/ggml-metal.h ${header_path}
  105. cp ggml/include/ggml-cpu.h ${header_path}
  106. cp ggml/include/ggml-blas.h ${header_path}
  107. cp ggml/include/gguf.h ${header_path}
  108. # Create module map (common for all platforms)
  109. cat > ${module_path}module.modulemap << EOF
  110. framework module llama {
  111. header "llama.h"
  112. header "ggml.h"
  113. header "ggml-alloc.h"
  114. header "ggml-backend.h"
  115. header "ggml-metal.h"
  116. header "ggml-cpu.h"
  117. header "ggml-blas.h"
  118. header "gguf.h"
  119. link "c++"
  120. link framework "Accelerate"
  121. link framework "Metal"
  122. link framework "Foundation"
  123. export *
  124. }
  125. EOF
  126. # Platform-specific settings for Info.plist
  127. local platform_name=""
  128. local sdk_name=""
  129. local supported_platform=""
  130. case "$platform" in
  131. "ios")
  132. platform_name="iphoneos"
  133. sdk_name="iphoneos${min_os_version}"
  134. supported_platform="iPhoneOS"
  135. local plist_path="${build_dir}/framework/${framework_name}.framework/Info.plist"
  136. local device_family=' <key>UIDeviceFamily</key>
  137. <array>
  138. <integer>1</integer>
  139. <integer>2</integer>
  140. </array>'
  141. ;;
  142. "macos")
  143. platform_name="macosx"
  144. sdk_name="macosx${min_os_version}"
  145. supported_platform="MacOSX"
  146. local plist_path="${build_dir}/framework/${framework_name}.framework/Versions/A/Resources/Info.plist"
  147. local device_family=""
  148. ;;
  149. "visionos")
  150. platform_name="xros"
  151. sdk_name="xros${min_os_version}"
  152. supported_platform="XRPlatform"
  153. local plist_path="${build_dir}/framework/${framework_name}.framework/Info.plist"
  154. local device_family=""
  155. ;;
  156. "tvos")
  157. platform_name="appletvos"
  158. sdk_name="appletvos${min_os_version}"
  159. supported_platform="AppleTVOS"
  160. local plist_path="${build_dir}/framework/${framework_name}.framework/Info.plist"
  161. local device_family=' <key>UIDeviceFamily</key>
  162. <array>
  163. <integer>3</integer>
  164. </array>'
  165. ;;
  166. esac
  167. # Create Info.plist
  168. cat > ${plist_path} << EOF
  169. <?xml version="1.0" encoding="UTF-8"?>
  170. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  171. <plist version="1.0">
  172. <dict>
  173. <key>CFBundleDevelopmentRegion</key>
  174. <string>en</string>
  175. <key>CFBundleExecutable</key>
  176. <string>llama</string>
  177. <key>CFBundleIdentifier</key>
  178. <string>org.ggml.llama</string>
  179. <key>CFBundleInfoDictionaryVersion</key>
  180. <string>6.0</string>
  181. <key>CFBundleName</key>
  182. <string>llama</string>
  183. <key>CFBundlePackageType</key>
  184. <string>FMWK</string>
  185. <key>CFBundleShortVersionString</key>
  186. <string>1.0</string>
  187. <key>CFBundleVersion</key>
  188. <string>1</string>
  189. <key>MinimumOSVersion</key>
  190. <string>${min_os_version}</string>
  191. <key>CFBundleSupportedPlatforms</key>
  192. <array>
  193. <string>${supported_platform}</string>
  194. </array>${device_family}
  195. <key>DTPlatformName</key>
  196. <string>${platform_name}</string>
  197. <key>DTSDKName</key>
  198. <string>${sdk_name}</string>
  199. </dict>
  200. </plist>
  201. EOF
  202. }
  203. # Create dynamic libraries from static libraries.
  204. combine_static_libraries() {
  205. local build_dir="$1"
  206. local release_dir="$2"
  207. local platform="$3" # "ios", "macos", "visionos", or "tvos"
  208. local is_simulator="$4"
  209. local base_dir="$(pwd)"
  210. local framework_name="llama"
  211. # Determine output path based on platform
  212. local output_lib=""
  213. if [[ "$platform" == "macos" ]]; then
  214. # macOS uses versioned structure
  215. output_lib="${build_dir}/framework/${framework_name}.framework/Versions/A/${framework_name}"
  216. else
  217. # iOS, visionOS, and tvOS use a directory flat structure
  218. output_lib="${build_dir}/framework/${framework_name}.framework/${framework_name}"
  219. fi
  220. local libs=(
  221. "${base_dir}/${build_dir}/src/${release_dir}/libllama.a"
  222. "${base_dir}/${build_dir}/ggml/src/${release_dir}/libggml.a"
  223. "${base_dir}/${build_dir}/ggml/src/${release_dir}/libggml-base.a"
  224. "${base_dir}/${build_dir}/ggml/src/${release_dir}/libggml-cpu.a"
  225. "${base_dir}/${build_dir}/ggml/src/ggml-metal/${release_dir}/libggml-metal.a"
  226. "${base_dir}/${build_dir}/ggml/src/ggml-blas/${release_dir}/libggml-blas.a"
  227. )
  228. # Create temporary directory for processing
  229. local temp_dir="${base_dir}/${build_dir}/temp"
  230. mkdir -p "${temp_dir}"
  231. # Since we have multiple architectures libtool will find object files that do not
  232. # match the target architecture. We suppress these warnings.
  233. libtool -static -o "${temp_dir}/combined.a" "${libs[@]}" 2> /dev/null
  234. # Determine SDK, architectures, and install_name based on platform and simulator flag.
  235. local sdk=""
  236. local archs=""
  237. local min_version_flag=""
  238. local install_name=""
  239. case "$platform" in
  240. "ios")
  241. if [[ "$is_simulator" == "true" ]]; then
  242. sdk="iphonesimulator"
  243. archs="arm64 x86_64"
  244. min_version_flag="-mios-simulator-version-min=${IOS_MIN_OS_VERSION}"
  245. else
  246. sdk="iphoneos"
  247. archs="arm64"
  248. min_version_flag="-mios-version-min=${IOS_MIN_OS_VERSION}"
  249. fi
  250. install_name="@rpath/llama.framework/llama"
  251. ;;
  252. "macos")
  253. sdk="macosx"
  254. archs="arm64 x86_64"
  255. min_version_flag="-mmacosx-version-min=${MACOS_MIN_OS_VERSION}"
  256. install_name="@rpath/llama.framework/Versions/Current/llama"
  257. ;;
  258. "visionos")
  259. if [[ "$is_simulator" == "true" ]]; then
  260. sdk="xrsimulator"
  261. archs="arm64 x86_64"
  262. min_version_flag="-mtargetos=xros${VISIONOS_MIN_OS_VERSION}-simulator"
  263. else
  264. sdk="xros"
  265. archs="arm64"
  266. min_version_flag="-mtargetos=xros${VISIONOS_MIN_OS_VERSION}"
  267. fi
  268. # Use flat structure for visionOS, same as iOS
  269. install_name="@rpath/llama.framework/llama"
  270. ;;
  271. "tvos")
  272. if [[ "$is_simulator" == "true" ]]; then
  273. sdk="appletvsimulator"
  274. archs="arm64 x86_64"
  275. min_version_flag="-mtvos-simulator-version-min=${TVOS_MIN_OS_VERSION}"
  276. else
  277. sdk="appletvos"
  278. archs="arm64"
  279. min_version_flag="-mtvos-version-min=${TVOS_MIN_OS_VERSION}"
  280. fi
  281. install_name="@rpath/llama.framework/llama"
  282. ;;
  283. esac
  284. # Build architecture flags
  285. local arch_flags=""
  286. for arch in $archs; do
  287. arch_flags+=" -arch $arch"
  288. done
  289. # Create dynamic library
  290. echo "Creating dynamic library for ${platform}."
  291. xcrun -sdk $sdk clang++ -dynamiclib \
  292. -isysroot $(xcrun --sdk $sdk --show-sdk-path) \
  293. $arch_flags \
  294. $min_version_flag \
  295. -Wl,-force_load,"${temp_dir}/combined.a" \
  296. -framework Foundation -framework Metal -framework Accelerate \
  297. -install_name "$install_name" \
  298. -o "${base_dir}/${output_lib}"
  299. # Platform-specific post-processing for device builds
  300. if [[ "$is_simulator" == "false" ]]; then
  301. if command -v xcrun vtool &>/dev/null; then
  302. case "$platform" in
  303. "ios")
  304. echo "Marking binary as a framework binary for iOS..."
  305. xcrun vtool -set-build-version ios ${IOS_MIN_OS_VERSION} ${IOS_MIN_OS_VERSION} -replace \
  306. -output "${base_dir}/${output_lib}" "${base_dir}/${output_lib}"
  307. ;;
  308. "visionos")
  309. echo "Marking binary as a framework binary for visionOS..."
  310. if [[ "$MAJOR_VERSION" -gt 16 ]] || [[ "$MAJOR_VERSION" -eq 16 && "$MINOR_VERSION" -gt 2 ]]; then
  311. echo "Xcode version greater than 16.2, using visionOS."
  312. VISION_OS_BUILD_VERSION="visionos"
  313. else
  314. echo "Xcode version less than or equal to 16.2, using xros."
  315. VISION_OS_BUILD_VERSION="xros"
  316. fi
  317. xcrun vtool -set-build-version ${VISION_OS_BUILD_VERSION} ${VISIONOS_MIN_OS_VERSION} ${VISIONOS_MIN_OS_VERSION} -replace \
  318. -output "${base_dir}/${output_lib}" "${base_dir}/${output_lib}"
  319. ;;
  320. "tvos")
  321. echo "Marking binary as a framework binary for tvOS..."
  322. xcrun vtool -set-build-version tvos ${TVOS_MIN_OS_VERSION} ${TVOS_MIN_OS_VERSION} -replace \
  323. -output "${base_dir}/${output_lib}" "${base_dir}/${output_lib}"
  324. ;;
  325. esac
  326. else
  327. echo "Warning: vtool not found. Binary may not pass App Store validation."
  328. fi
  329. fi
  330. echo "Creating properly formatted dSYM..."
  331. # Create a separate directory for dSYMs for all platforms
  332. mkdir -p "${base_dir}/${build_dir}/dSYMs"
  333. # iOS and visionOS style dSYM (flat structure)
  334. if [[ "$platform" == "ios" || "$platform" == "visionos" || "$platform" == "tvos" ]]; then
  335. # Generate dSYM in the dSYMs directory
  336. xcrun dsymutil "${base_dir}/${output_lib}" -o "${base_dir}/${build_dir}/dSYMs/llama.dSYM"
  337. # Create a copy of the binary that will be stripped
  338. cp "${base_dir}/${output_lib}" "${temp_dir}/binary_to_strip"
  339. # Strip debug symbols from the copy
  340. xcrun strip -S "${temp_dir}/binary_to_strip" -o "${temp_dir}/stripped_lib"
  341. # Replace the original with the stripped version
  342. mv "${temp_dir}/stripped_lib" "${base_dir}/${output_lib}"
  343. else
  344. # macOS style dSYM
  345. # First strip debug info to a separate file
  346. xcrun strip -S "${base_dir}/${output_lib}" -o "${temp_dir}/stripped_lib"
  347. # Generate dSYM in the dSYMs directory
  348. xcrun dsymutil "${base_dir}/${output_lib}" -o "${base_dir}/${build_dir}/dSYMs/llama.dSYM"
  349. # Replace original binary with stripped version
  350. mv "${temp_dir}/stripped_lib" "${base_dir}/${output_lib}"
  351. fi
  352. # Remove any automatically generated dSYM files in the framework structure as they will
  353. # otherwise case Invalid Bundle Structure validation errors.
  354. if [ -d "${base_dir}/${output_lib}.dSYM" ]; then
  355. echo "Removing generated dSYM file in framework structure: ${base_dir}/${output_lib}.dSYM"
  356. rm -rf "${base_dir}/${output_lib}.dSYM"
  357. fi
  358. # Clean up
  359. rm -rf "${temp_dir}"
  360. }
  361. echo "Building for iOS simulator..."
  362. cmake -B build-ios-sim -G Xcode \
  363. "${COMMON_CMAKE_ARGS[@]}" \
  364. -DCMAKE_OSX_DEPLOYMENT_TARGET=${IOS_MIN_OS_VERSION} \
  365. -DIOS=ON \
  366. -DCMAKE_SYSTEM_NAME=iOS \
  367. -DCMAKE_OSX_SYSROOT=iphonesimulator \
  368. -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
  369. -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphonesimulator \
  370. -DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
  371. -DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
  372. -DLLAMA_CURL=OFF \
  373. -S .
  374. cmake --build build-ios-sim --config Release -- -quiet
  375. echo "Building for iOS devices..."
  376. cmake -B build-ios-device -G Xcode \
  377. "${COMMON_CMAKE_ARGS[@]}" \
  378. -DCMAKE_OSX_DEPLOYMENT_TARGET=${IOS_MIN_OS_VERSION} \
  379. -DCMAKE_OSX_SYSROOT=iphoneos \
  380. -DCMAKE_OSX_ARCHITECTURES="arm64" \
  381. -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=iphoneos \
  382. -DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
  383. -DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
  384. -DLLAMA_CURL=OFF \
  385. -S .
  386. cmake --build build-ios-device --config Release -- -quiet
  387. echo "Building for macOS..."
  388. cmake -B build-macos -G Xcode \
  389. "${COMMON_CMAKE_ARGS[@]}" \
  390. -DCMAKE_OSX_DEPLOYMENT_TARGET=${MACOS_MIN_OS_VERSION} \
  391. -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
  392. -DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
  393. -DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
  394. -DLLAMA_CURL=OFF \
  395. -S .
  396. cmake --build build-macos --config Release -- -quiet
  397. echo "Building for visionOS..."
  398. cmake -B build-visionos -G Xcode \
  399. "${COMMON_CMAKE_ARGS[@]}" \
  400. -DCMAKE_OSX_DEPLOYMENT_TARGET=${VISIONOS_MIN_OS_VERSION} \
  401. -DCMAKE_OSX_ARCHITECTURES="arm64" \
  402. -DCMAKE_SYSTEM_NAME=visionOS \
  403. -DCMAKE_OSX_SYSROOT=xros \
  404. -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=xros \
  405. -DCMAKE_C_FLAGS="-D_XOPEN_SOURCE=700 ${COMMON_C_FLAGS}" \
  406. -DCMAKE_CXX_FLAGS="-D_XOPEN_SOURCE=700 ${COMMON_CXX_FLAGS}" \
  407. -DLLAMA_CURL=OFF \
  408. -S .
  409. cmake --build build-visionos --config Release -- -quiet
  410. echo "Building for visionOS simulator..."
  411. cmake -B build-visionos-sim -G Xcode \
  412. "${COMMON_CMAKE_ARGS[@]}" \
  413. -DCMAKE_OSX_DEPLOYMENT_TARGET=${VISIONOS_MIN_OS_VERSION} \
  414. -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
  415. -DCMAKE_SYSTEM_NAME=visionOS \
  416. -DCMAKE_OSX_SYSROOT=xrsimulator \
  417. -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=xrsimulator \
  418. -DCMAKE_C_FLAGS="-D_XOPEN_SOURCE=700 ${COMMON_C_FLAGS}" \
  419. -DCMAKE_CXX_FLAGS="-D_XOPEN_SOURCE=700 ${COMMON_CXX_FLAGS}" \
  420. -DLLAMA_CURL=OFF \
  421. -S .
  422. cmake --build build-visionos-sim --config Release -- -quiet
  423. # Add tvOS builds (might need the same u_int definitions as watchOS and visionOS)
  424. echo "Building for tvOS simulator..."
  425. cmake -B build-tvos-sim -G Xcode \
  426. "${COMMON_CMAKE_ARGS[@]}" \
  427. -DCMAKE_OSX_DEPLOYMENT_TARGET=${TVOS_MIN_OS_VERSION} \
  428. -DCMAKE_SYSTEM_NAME=tvOS \
  429. -DCMAKE_OSX_SYSROOT=appletvsimulator \
  430. -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
  431. -DGGML_METAL=ON \
  432. -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=appletvsimulator \
  433. -DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
  434. -DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
  435. -DLLAMA_CURL=OFF \
  436. -S .
  437. cmake --build build-tvos-sim --config Release -- -quiet
  438. echo "Building for tvOS devices..."
  439. cmake -B build-tvos-device -G Xcode \
  440. "${COMMON_CMAKE_ARGS[@]}" \
  441. -DCMAKE_OSX_DEPLOYMENT_TARGET=${TVOS_MIN_OS_VERSION} \
  442. -DCMAKE_SYSTEM_NAME=tvOS \
  443. -DCMAKE_OSX_SYSROOT=appletvos \
  444. -DCMAKE_OSX_ARCHITECTURES="arm64" \
  445. -DGGML_METAL=ON \
  446. -DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=appletvos \
  447. -DCMAKE_C_FLAGS="${COMMON_C_FLAGS}" \
  448. -DCMAKE_CXX_FLAGS="${COMMON_CXX_FLAGS}" \
  449. -DLLAMA_CURL=OFF \
  450. -S .
  451. cmake --build build-tvos-device --config Release -- -quiet
  452. # Setup frameworks and copy binaries and headers
  453. echo "Setting up framework structures..."
  454. setup_framework_structure "build-ios-sim" ${IOS_MIN_OS_VERSION} "ios"
  455. setup_framework_structure "build-ios-device" ${IOS_MIN_OS_VERSION} "ios"
  456. setup_framework_structure "build-macos" ${MACOS_MIN_OS_VERSION} "macos"
  457. setup_framework_structure "build-visionos" ${VISIONOS_MIN_OS_VERSION} "visionos"
  458. setup_framework_structure "build-visionos-sim" ${VISIONOS_MIN_OS_VERSION} "visionos"
  459. setup_framework_structure "build-tvos-sim" ${TVOS_MIN_OS_VERSION} "tvos"
  460. setup_framework_structure "build-tvos-device" ${TVOS_MIN_OS_VERSION} "tvos"
  461. # Create dynamic libraries from static libraries
  462. echo "Creating dynamic libraries from static libraries..."
  463. combine_static_libraries "build-ios-sim" "Release-iphonesimulator" "ios" "true"
  464. combine_static_libraries "build-ios-device" "Release-iphoneos" "ios" "false"
  465. combine_static_libraries "build-macos" "Release" "macos" "false"
  466. combine_static_libraries "build-visionos" "Release-xros" "visionos" "false"
  467. combine_static_libraries "build-visionos-sim" "Release-xrsimulator" "visionos" "true"
  468. combine_static_libraries "build-tvos-sim" "Release-appletvsimulator" "tvos" "true"
  469. combine_static_libraries "build-tvos-device" "Release-appletvos" "tvos" "false"
  470. # Create XCFramework with correct debug symbols paths
  471. echo "Creating XCFramework..."
  472. xcodebuild -create-xcframework \
  473. -framework $(pwd)/build-ios-sim/framework/llama.framework \
  474. -debug-symbols $(pwd)/build-ios-sim/dSYMs/llama.dSYM \
  475. -framework $(pwd)/build-ios-device/framework/llama.framework \
  476. -debug-symbols $(pwd)/build-ios-device/dSYMs/llama.dSYM \
  477. -framework $(pwd)/build-macos/framework/llama.framework \
  478. -debug-symbols $(pwd)/build-macos/dSYMS/llama.dSYM \
  479. -framework $(pwd)/build-visionos/framework/llama.framework \
  480. -debug-symbols $(pwd)/build-visionos/dSYMs/llama.dSYM \
  481. -framework $(pwd)/build-visionos-sim/framework/llama.framework \
  482. -debug-symbols $(pwd)/build-visionos-sim/dSYMs/llama.dSYM \
  483. -framework $(pwd)/build-tvos-device/framework/llama.framework \
  484. -debug-symbols $(pwd)/build-tvos-device/dSYMs/llama.dSYM \
  485. -framework $(pwd)/build-tvos-sim/framework/llama.framework \
  486. -debug-symbols $(pwd)/build-tvos-sim/dSYMs/llama.dSYM \
  487. -output $(pwd)/build-apple/llama.xcframework