build-xcframework.sh 21 KB

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