Skip to content

Commit 150d753

Browse files
mdepinetclaude
andcommitted
Build for iOS devices and the iOS simulator
Map arm64 iOS builds to aarch64-apple-ios, or to aarch64-apple-ios-sim when the sysroot is the simulator SDK. LiveKit's WebRTC builds for iOS are arm64 only. As on Android, livekit-ffi trusts its bundled webpki roots, and there's no system libcurl, so HTTP token sources return an error. iOS has no AppKit, OpenGL or ScreenCaptureKit, so they're left out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d614163 commit 150d753

3 files changed

Lines changed: 84 additions & 20 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ if(_lk_capture_features)
211211
list(JOIN _lk_capture_features "," LIVEKIT_CARGO_FEATURES)
212212
set(LIVEKIT_CAPTURE_ENABLED 1)
213213
endif()
214-
# Android keeps its CA certificates where rustls-native-certs doesn't look, so use the bundled webpki roots there, as
215-
# LiveKit's own Android FFI builds do.
214+
# rustls-native-certs can't load Android's or iOS's CA certificates, so use the bundled webpki roots there, as
215+
# LiveKit's own mobile FFI builds do.
216216
set(LIVEKIT_CARGO_NO_DEFAULT_FEATURES OFF)
217-
if(ANDROID)
217+
if(ANDROID OR IOS)
218218
set(LIVEKIT_CARGO_NO_DEFAULT_FEATURES ON)
219219
set(_lk_cargo_features ${_lk_capture_features} room-apis rustls-tls-webpki-roots)
220220
list(SORT _lk_cargo_features)
@@ -270,11 +270,22 @@ configure_file(
270270

271271
find_program(CARGO_EXECUTABLE NAMES cargo REQUIRED)
272272

273-
# Determine Rust target triple for cross-compilation on macOS and Android
273+
# Determine Rust target triple for cross-compilation on macOS, iOS and Android
274274
set(RUST_TARGET_TRIPLE "")
275275
set(LIVEKIT_ANDROID_NDK "")
276276
set(LIVEKIT_ANDROID_CC "")
277-
if(APPLE AND CMAKE_OSX_ARCHITECTURES)
277+
if(IOS)
278+
# LiveKit's WebRTC builds for iOS are arm64 only, for devices and for the simulator.
279+
if(NOT CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
280+
message(FATAL_ERROR "iOS builds need CMAKE_OSX_ARCHITECTURES=arm64, not '${CMAKE_OSX_ARCHITECTURES}'")
281+
endif()
282+
# CMake builds for the simulator when the sysroot is the simulator SDK, given by name or by path.
283+
if(CMAKE_OSX_SYSROOT MATCHES "[Ss]imulator")
284+
set(RUST_TARGET_TRIPLE "aarch64-apple-ios-sim")
285+
else()
286+
set(RUST_TARGET_TRIPLE "aarch64-apple-ios")
287+
endif()
288+
elseif(APPLE AND CMAKE_OSX_ARCHITECTURES)
278289
if(CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
279290
set(RUST_TARGET_TRIPLE "x86_64-apple-darwin")
280291
elseif(CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
@@ -593,7 +604,7 @@ target_link_libraries(livekit
593604

594605
if(WIN32)
595606
target_link_libraries(livekit PRIVATE winhttp)
596-
elseif(NOT ANDROID)
607+
elseif(NOT ANDROID AND NOT IOS)
597608
find_package(CURL REQUIRED)
598609
target_link_libraries(livekit PRIVATE CURL::libcurl)
599610
endif()
@@ -697,13 +708,16 @@ if(APPLE)
697708
find_library(FW_AVFOUNDATION AVFoundation REQUIRED)
698709
find_library(FW_COREVIDEO CoreVideo REQUIRED)
699710
find_library(FW_FOUNDATION Foundation REQUIRED)
700-
find_library(FW_APPKIT AppKit REQUIRED)
701711
find_library(FW_QUARTZCORE QuartzCore REQUIRED)
702-
find_library(FW_OPENGL OpenGL REQUIRED)
703712
find_library(FW_IOSURFACE IOSurface REQUIRED)
704713
find_library(FW_METAL Metal REQUIRED)
705714
find_library(FW_METALKIT MetalKit REQUIRED)
706-
find_library(FW_SCREENCAPTUREKIT ScreenCaptureKit REQUIRED)
715+
# iOS has none of these.
716+
if(NOT IOS)
717+
find_library(FW_APPKIT AppKit REQUIRED)
718+
find_library(FW_OPENGL OpenGL REQUIRED)
719+
find_library(FW_SCREENCAPTUREKIT ScreenCaptureKit REQUIRED)
720+
endif()
707721

708722
target_link_libraries(livekit PRIVATE
709723
${FW_COREAUDIO}
@@ -716,14 +730,14 @@ if(APPLE)
716730
${FW_AVFOUNDATION}
717731
${FW_COREVIDEO}
718732
${FW_FOUNDATION}
719-
${FW_APPKIT}
720733
${FW_QUARTZCORE}
721-
${FW_OPENGL}
722734
${FW_IOSURFACE}
723735
${FW_METAL}
724736
${FW_METALKIT}
725-
${FW_SCREENCAPTUREKIT}
726737
)
738+
if(NOT IOS)
739+
target_link_libraries(livekit PRIVATE ${FW_APPKIT} ${FW_OPENGL} ${FW_SCREENCAPTUREKIT})
740+
endif()
727741

728742
target_link_options(livekit PRIVATE "LINKER:-ObjC")
729743
endif()

‎docs/building.md‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ sources, or support optional repository targets.
3434
| spdlog | All | 1.15.1 vendored; vcpkg-managed on Windows | Vendored / vcpkg | Implements the internal `LK_LOG_*` macros, default stderr logger, log-level filtering, and the public log callback adapter. |
3535
| nlohmann/json | All | 3.12.0 vendored; vcpkg-managed on Windows | Vendored / vcpkg | Parses token-endpoint JSON responses and JWT payload claims. |
3636
| Chromium trace-event code | All | Pinned source snapshot | Vendored source | Implements trace-event macros used by the production tracing API. Benchmark utilities consume the resulting Chromium-format traces, but Chromium is not a benchmark library dependency. |
37-
| libcurl | Mac, Linux (not Android) | System-provided | System library | Implements HTTP requests made by `TokenSource`. |
37+
| libcurl | Mac, Linux (not Android or iOS) | System-provided | System library | Implements HTTP requests made by `TokenSource`. |
3838
| OpenSSL | Linux (not Android) | System-provided | System library | Linked into `liblivekit`. Current production C++ sources do not call OpenSSL APIs directly. |
39-
| Apple system frameworks | Mac | OS-provided | System frameworks | Supplies the audio, video, graphics, security, UI, and ScreenCaptureKit frameworks required by the media stack. |
39+
| Apple system frameworks | Mac, iOS | OS-provided | System frameworks | Supplies the audio, video, graphics, security, UI, and ScreenCaptureKit frameworks required by the media stack. iOS builds leave out AppKit, OpenGL and ScreenCaptureKit, which iOS doesn't have. |
4040
| WinHTTP | Windows | OS-provided | System library | Implements HTTP requests made by `TokenSource`. |
4141

4242
### Capture Dependencies
@@ -271,6 +271,48 @@ On Android:
271271
- **No HTTP token sources.** There's no system libcurl, so `TokenSource`'s HTTP
272272
requests return an error.
273273

274+
## Cross-compiling for iOS
275+
276+
iOS builds use CMake's iOS support from a macOS host with Xcode. Devices and the
277+
simulator need separate builds, both arm64, since LiveKit's WebRTC builds for
278+
iOS are arm64 only. They need:
279+
280+
- **The Rust target:** `aarch64-apple-ios` for devices, or
281+
`aarch64-apple-ios-sim` for the simulator. Run `rustup target add` from
282+
`client-sdk-rust`, which pins the toolchain.
283+
- **A host `protoc` 25.3**, as for Android.
284+
285+
```bash
286+
(cd client-sdk-rust && rustup target add aarch64-apple-ios)
287+
export IPHONEOS_DEPLOYMENT_TARGET=14.0
288+
cmake -S . -B build-ios -G Ninja \
289+
-DCMAKE_SYSTEM_NAME=iOS \
290+
-DCMAKE_SYSTEM_PROCESSOR=arm64 \
291+
-DCMAKE_OSX_SYSROOT=iphoneos \
292+
-DCMAKE_OSX_ARCHITECTURES=arm64 \
293+
-DCMAKE_OSX_DEPLOYMENT_TARGET=14.0 \
294+
-DCMAKE_BUILD_TYPE=Release \
295+
-DProtobuf_PROTOC_EXECUTABLE=/path/to/protoc
296+
cmake --build build-ios
297+
cmake --install build-ios --prefix sdk-out/livekit-sdk-ios-arm64
298+
```
299+
300+
For the simulator, use `-DCMAKE_OSX_SYSROOT=iphonesimulator`. Set
301+
`CMAKE_SYSTEM_PROCESSOR`, since CMake leaves it unset when cross-compiling and
302+
Abseil then picks x86 compiler flags, and set `IPHONEOS_DEPLOYMENT_TARGET` to
303+
the deployment target, so that cargo builds the Rust code for the same iOS
304+
version.
305+
306+
On iOS:
307+
308+
- **Certificates.** As on Android, `livekit_ffi` trusts the Mozilla CA roots it
309+
bundles, through `rustls-tls-webpki-roots`.
310+
- **No HTTP token sources.** There's no system libcurl, so `TokenSource`'s HTTP
311+
requests return an error.
312+
- **Packaging.** Apps embed `liblivekit` and `liblivekit_ffi` as frameworks. If
313+
they're packaged under new install names, references between the libraries
314+
need updating with `install_name_tool` too, and each library re-signed.
315+
274316
## Advanced: CMake presets
275317

276318
For more control, drive CMake directly via the presets in

‎src/token_source_http.cpp‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,29 @@
2121

2222
#include "token_source_internal.h"
2323

24+
#if defined(__APPLE__)
25+
#include <TargetConditionals.h>
26+
#endif
27+
28+
// Android and iOS have no system libcurl, and bundling one would mean bundling a TLS library too.
29+
#if defined(__ANDROID__) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
30+
#define LIVEKIT_NO_HTTP_TOKEN_SOURCE 1
31+
#endif
32+
2433
#if defined(_WIN32)
2534
#ifndef NOMINMAX
2635
#define NOMINMAX
2736
#endif
2837
#include <windows.h>
2938
#include <winhttp.h>
30-
#elif !defined(__ANDROID__)
39+
#elif !defined(LIVEKIT_NO_HTTP_TOKEN_SOURCE)
3140
#include <curl/curl.h>
3241
#endif
3342

3443
namespace livekit {
3544
namespace {
3645

37-
#if !defined(_WIN32) && !defined(__ANDROID__)
46+
#if !defined(_WIN32) && !defined(LIVEKIT_NO_HTTP_TOKEN_SOURCE)
3847
size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* user_data) {
3948
const size_t total_size = size * nmemb;
4049
auto* response = static_cast<std::string*>(user_data);
@@ -43,7 +52,7 @@ size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* user_d
4352
}
4453
#endif
4554

46-
#if !defined(__ANDROID__)
55+
#if !defined(LIVEKIT_NO_HTTP_TOKEN_SOURCE)
4756
std::string normalizeHttpMethod(std::string method) {
4857
if (method.empty()) {
4958
return "POST";
@@ -200,14 +209,13 @@ Result<std::string, std::string> tokenSourceHttpRequest(const std::string& metho
200209
std::chrono::milliseconds timeout) {
201210
#if defined(_WIN32)
202211
return winHttpRequest(method, url, headers, json_body, timeout);
203-
#elif defined(__ANDROID__)
204-
// Android has no system libcurl, and bundling one would mean bundling a TLS library too.
212+
#elif defined(LIVEKIT_NO_HTTP_TOKEN_SOURCE)
205213
static_cast<void>(method);
206214
static_cast<void>(url);
207215
static_cast<void>(headers);
208216
static_cast<void>(json_body);
209217
static_cast<void>(timeout);
210-
return Result<std::string, std::string>::failure("HTTP token sources aren't supported on Android");
218+
return Result<std::string, std::string>::failure("HTTP token sources aren't supported on Android or iOS");
211219
#else
212220
CURL* curl = curl_easy_init();
213221
if (curl == nullptr) {

0 commit comments

Comments
 (0)