Skip to content

Commit 2441f3b

Browse files
committed
llama : add tvos build support to xcframework
1 parent 758fce3 commit 2441f3b

File tree

2 files changed

+90
-27
lines changed

2 files changed

+90
-27
lines changed

build-xcframework.sh

Lines changed: 87 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
IOS_MIN_OS_VERSION=16.4
55
MACOS_MIN_OS_VERSION=13.3
66
VISIONOS_MIN_OS_VERSION=1.0
7+
TVOS_MIN_OS_VERSION=16.4
78

89
BUILD_SHARED_LIBS=OFF
910
LLAMA_BUILD_EXAMPLES=OFF
@@ -65,12 +66,14 @@ rm -rf build-ios-device
6566
rm -rf build-macos
6667
rm -rf build-visionos
6768
rm -rf build-visionos-sim
69+
rm -rf build-tvos-sim
70+
rm -rf build-tvos-device
6871

6972
# Setup the xcframework build directory structure
7073
setup_framework_structure() {
7174
local build_dir=$1
7275
local min_os_version=$2
73-
local platform=$3 # "ios", "macos", or "visionos"
76+
local platform=$3 # "ios", "macos", "visionos", or "tvos"
7477
local framework_name="llama"
7578

7679
echo "Creating ${platform}-style framework structure for ${build_dir}"
@@ -92,7 +95,7 @@ setup_framework_structure() {
9295
local header_path=${build_dir}/framework/${framework_name}.framework/Versions/A/Headers/
9396
local module_path=${build_dir}/framework/${framework_name}.framework/Versions/A/Modules/
9497
else
95-
# iOS/VisionOS use a flat structure
98+
# iOS/VisionOS/tvOS use a flat structure
9699
mkdir -p ${build_dir}/framework/${framework_name}.framework/Headers
97100
mkdir -p ${build_dir}/framework/${framework_name}.framework/Modules
98101

@@ -166,6 +169,16 @@ EOF
166169
local plist_path="${build_dir}/framework/${framework_name}.framework/Info.plist"
167170
local device_family=""
168171
;;
172+
"tvos")
173+
platform_name="appletvos"
174+
sdk_name="appletvos${min_os_version}"
175+
supported_platform="AppleTVOS"
176+
local plist_path="${build_dir}/framework/${framework_name}.framework/Info.plist"
177+
local device_family=' <key>UIDeviceFamily</key>
178+
<array>
179+
<integer>3</integer>
180+
</array>'
181+
;;
169182
esac
170183

171184
# Create Info.plist
@@ -209,8 +222,8 @@ EOF
209222
combine_static_libraries() {
210223
local build_dir="$1"
211224
local release_dir="$2"
212-
local platform="$3" # "ios", "macos", or "visionos"
213-
local is_simulator="$4" # "true" or "false"
225+
local platform="$3" # "ios", "macos", "visionos", or "tvos"
226+
local is_simulator="$4"
214227
local base_dir="$(pwd)"
215228
local framework_name="llama"
216229

@@ -220,7 +233,7 @@ combine_static_libraries() {
220233
# macOS uses versioned structure
221234
output_lib="${build_dir}/framework/${framework_name}.framework/Versions/A/${framework_name}"
222235
else
223-
# Both iOS and visionOS use flat structure
236+
# iOS, visionOS, and tvOS use a directory flat structure
224237
output_lib="${build_dir}/framework/${framework_name}.framework/${framework_name}"
225238
fi
226239

@@ -279,6 +292,18 @@ combine_static_libraries() {
279292
# Use flat structure for visionOS, same as iOS
280293
install_name="@rpath/llama.framework/llama"
281294
;;
295+
"tvos")
296+
if [[ "$is_simulator" == "true" ]]; then
297+
sdk="appletvsimulator"
298+
archs="arm64 x86_64"
299+
min_version_flag="-mtvos-simulator-version-min=${TVOS_MIN_OS_VERSION}"
300+
else
301+
sdk="appletvos"
302+
archs="arm64"
303+
min_version_flag="-mtvos-version-min=${TVOS_MIN_OS_VERSION}"
304+
fi
305+
install_name="@rpath/llama.framework/llama"
306+
;;
282307
esac
283308

284309
# Build architecture flags
@@ -298,36 +323,37 @@ combine_static_libraries() {
298323
-install_name "$install_name" \
299324
-o "${base_dir}/${output_lib}"
300325

301-
# Platform-specific post-processing
302-
if [[ "$platform" == "ios" && "$is_simulator" == "false" ]]; then
303-
# For iOS device builds, ensure the binary is marked as a framework binary
326+
# Platform-specific post-processing for device builds
327+
if [[ "$is_simulator" == "false" ]]; then
304328
if command -v vtool &>/dev/null; then
305-
echo "Marking binary as a framework binary for iOS..."
306-
vtool -set-build-version ios ${IOS_MIN_OS_VERSION} ${IOS_MIN_OS_VERSION} -replace \
307-
-output "${base_dir}/${output_lib}" "${base_dir}/${output_lib}"
308-
else
309-
echo "Warning: vtool not found. Binary may not pass App Store validation."
310-
fi
311-
fi
312-
313-
# Same for visionOS device builds. TODO(danbev) Remove duplication.
314-
if [[ "$platform" == "visionos" && "$is_simulator" == "false" ]]; then
315-
if command -v vtool &>/dev/null; then
316-
echo "Marking binary as a framework binary for visionOS..."
317-
vtool -set-build-version xros ${VISIONOS_MIN_OS_VERSION} ${VISIONOS_MIN_OS_VERSION} -replace \
318-
-output "${base_dir}/${output_lib}" "${base_dir}/${output_lib}"
329+
case "$platform" in
330+
"ios")
331+
echo "Marking binary as a framework binary for iOS..."
332+
vtool -set-build-version ios ${IOS_MIN_OS_VERSION} ${IOS_MIN_OS_VERSION} -replace \
333+
-output "${base_dir}/${output_lib}" "${base_dir}/${output_lib}"
334+
;;
335+
"visionos")
336+
echo "Marking binary as a framework binary for visionOS..."
337+
vtool -set-build-version xros ${VISIONOS_MIN_OS_VERSION} ${VISIONOS_MIN_OS_VERSION} -replace \
338+
-output "${base_dir}/${output_lib}" "${base_dir}/${output_lib}"
339+
;;
340+
"tvos")
341+
echo "Marking binary as a framework binary for tvOS..."
342+
vtool -set-build-version tvos ${TVOS_MIN_OS_VERSION} ${TVOS_MIN_OS_VERSION} -replace \
343+
-output "${base_dir}/${output_lib}" "${base_dir}/${output_lib}"
344+
;;
345+
esac
319346
else
320347
echo "Warning: vtool not found. Binary may not pass App Store validation."
321348
fi
322349
fi
323350

324351
echo "Creating properly formatted dSYM..."
325-
326352
# Create a separate directory for dSYMs for all platforms
327353
mkdir -p "${base_dir}/${build_dir}/dSYMs"
328354

329355
# iOS and visionOS style dSYM (flat structure)
330-
if [[ "$platform" == "ios" || "$platform" == "visionos" ]]; then
356+
if [[ "$platform" == "ios" || "$platform" == "visionos" || "$platform" == "tvos" ]]; then
331357
# Generate dSYM in the dSYMs directory
332358
xcrun dsymutil "${base_dir}/${output_lib}" -o "${base_dir}/${build_dir}/dSYMs/llama.dSYM"
333359

@@ -424,13 +450,44 @@ cmake -B build-visionos-sim -G Xcode \
424450
-S .
425451
cmake --build build-visionos-sim --config Release -- -quiet
426452

453+
# Add tvOS builds (might need the same u_int definitions as watchOS and visionOS)
454+
echo "Building for tvOS simulator..."
455+
cmake -B build-tvos-sim -G Xcode \
456+
"${COMMON_CMAKE_ARGS[@]}" \
457+
-DCMAKE_OSX_DEPLOYMENT_TARGET=${TVOS_MIN_OS_VERSION} \
458+
-DCMAKE_SYSTEM_NAME=tvOS \
459+
-DCMAKE_OSX_SYSROOT=appletvsimulator \
460+
-DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
461+
-DGGML_METAL=ON \
462+
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=appletvsimulator \
463+
-DCMAKE_C_FLAGS="-DGGML_TVOS=1 ${COMMON_C_FLAGS}" \
464+
-DCMAKE_CXX_FLAGS="-DGGML_TVOS=1 ${COMMON_CXX_FLAGS}" \
465+
-S .
466+
cmake --build build-tvos-sim --config Release -- -quiet
467+
468+
echo "Building for tvOS devices..."
469+
cmake -B build-tvos-device -G Xcode \
470+
"${COMMON_CMAKE_ARGS[@]}" \
471+
-DCMAKE_OSX_DEPLOYMENT_TARGET=${TVOS_MIN_OS_VERSION} \
472+
-DCMAKE_SYSTEM_NAME=tvOS \
473+
-DCMAKE_OSX_SYSROOT=appletvos \
474+
-DCMAKE_OSX_ARCHITECTURES="arm64" \
475+
-DGGML_METAL=ON \
476+
-DCMAKE_XCODE_ATTRIBUTE_SUPPORTED_PLATFORMS=appletvos \
477+
-DCMAKE_C_FLAGS="-DGGML_TVOS=1 ${COMMON_C_FLAGS}" \
478+
-DCMAKE_CXX_FLAGS="-DGGML_TVOS=1 ${COMMON_CXX_FLAGS}" \
479+
-S .
480+
cmake --build build-tvos-device --config Release -- -quiet
481+
427482
# Setup frameworks and copy binaries and headers
428483
echo "Setting up framework structures..."
429484
setup_framework_structure "build-ios-sim" ${IOS_MIN_OS_VERSION} "ios"
430485
setup_framework_structure "build-ios-device" ${IOS_MIN_OS_VERSION} "ios"
431486
setup_framework_structure "build-macos" ${MACOS_MIN_OS_VERSION} "macos"
432487
setup_framework_structure "build-visionos" ${VISIONOS_MIN_OS_VERSION} "visionos"
433488
setup_framework_structure "build-visionos-sim" ${VISIONOS_MIN_OS_VERSION} "visionos"
489+
setup_framework_structure "build-tvos-sim" ${TVOS_MIN_OS_VERSION} "tvos"
490+
setup_framework_structure "build-tvos-device" ${TVOS_MIN_OS_VERSION} "tvos"
434491

435492
# Create dynamic libraries from static libraries
436493
echo "Creating dynamic libraries from static libraries..."
@@ -439,6 +496,8 @@ combine_static_libraries "build-ios-device" "Release-iphoneos" "ios" "false"
439496
combine_static_libraries "build-macos" "Release" "macos" "false"
440497
combine_static_libraries "build-visionos" "Release-xros" "visionos" "false"
441498
combine_static_libraries "build-visionos-sim" "Release-xrsimulator" "visionos" "true"
499+
combine_static_libraries "build-tvos-sim" "Release-appletvsimulator" "tvos" "true"
500+
combine_static_libraries "build-tvos-device" "Release-appletvos" "tvos" "false"
442501

443502
# Create XCFramework with correct debug symbols paths
444503
echo "Creating XCFramework..."
@@ -453,4 +512,8 @@ xcodebuild -create-xcframework \
453512
-debug-symbols $(pwd)/build-visionos/dSYMs/llama.dSYM \
454513
-framework $(pwd)/build-visionos-sim/framework/llama.framework \
455514
-debug-symbols $(pwd)/build-visionos-sim/dSYMs/llama.dSYM \
515+
-framework $(pwd)/build-tvos-device/framework/llama.framework \
516+
-debug-symbols $(pwd)/build-tvos-device/dSYMs/llama.dSYM \
517+
-framework $(pwd)/build-tvos-sim/framework/llama.framework \
518+
-debug-symbols $(pwd)/build-tvos-sim/dSYMs/llama.dSYM \
456519
-output $(pwd)/build-apple/llama.xcframework

src/llama-mmap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,9 @@ struct llama_mlock::impl {
471471

472472
char* errmsg = std::strerror(errno);
473473
bool suggest = (errno == ENOMEM);
474-
#if defined(GGML_VISIONOS)
475-
// visionOS doesn't support RLIMIT_MEMLOCK
476-
// Skip resource limit checks on visionOS
474+
#if defined(GGML_VISIONOS) || defined(GGML_TVOS)
475+
// visionOS/watchOS/tvOS dont't support RLIMIT_MEMLOCK
476+
// Skip resource limit checks on visionOS/watchOS/tvOS
477477
suggest = false;
478478
#else
479479
struct rlimit lock_limit;

0 commit comments

Comments
 (0)