Implementation:Mlc ai Mlc llm Prepare libs
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Mobile_Deployment |
| Last Updated | 2026-02-09 00:00 GMT |
Overview
Concrete tools for setting up cross-compilation toolchains and native build environments for mobile platforms provided by MLC-LLM.
Description
MLC-LLM provides two platform-specific build scripts that automate the cross-compilation of the inference engine into native static libraries:
ios/prepare_libs.sh-- A Bash script that configures CMake for iOS cross-compilation, registers the appropriate Rust target, and builds themlc_llm_staticlibrary with Metal GPU support. It supports building for both physical devices (arm64 with iphoneos sysroot) and simulators (arm64 or x86_64 with iphonesimulator sysroot). The script also creates a symbolic link fromios/MLCSwift/tvm_hometo the TVM third-party dependency so that the Swift package can locate TVM headers.
android/mlc4j/prepare_libs.py-- A Python script that configures CMake for Android cross-compilation using the Android NDK toolchain. It registers theaarch64-linux-androidRust target, invokes CMake with OpenCL GPU support enabled, builds thetvm4j_runtime_packedtarget, and installs the resulting libraries. The script requires theANDROID_NDKenvironment variable to be set and optionally acceptsTVM_SOURCE_DIRfor custom TVM builds.
Usage
Use these scripts when:
- Initially setting up the MLC-LLM mobile build environment on a new development machine
- Rebuilding the native inference libraries after updating the MLC-LLM source code
- Switching between iOS device and simulator targets
- The
mlc_llm packagecommand needs to invoke the platform-specific build step as part of the full packaging pipeline
Code Reference
Source Location
- Repository: MLC-LLM
- File (iOS):
ios/prepare_libs.sh(Lines 3-79) - File (Android):
android/mlc4j/prepare_libs.py(Lines 86-107)
Signature (iOS)
# ios/prepare_libs.sh
# OPTION:
# -s, --simulator Build for Simulator
# -a, --arch x86_64 | arm64 Simulator arch
# -h, --help Prints this help
#
# Environment Variables:
# MLC_LLM_SOURCE_DIR Path to MLC-LLM source (default: "..")
bash ios/prepare_libs.sh [-s|--simulator] [-a|--arch x86_64|arm64]
Signature (Android)
def main(mlc_llm_source_dir: Path):
"""Build MLC LLM and tvm4j native libraries for Android.
Sets up Rust target, creates build directory, configures CMake
with Android NDK toolchain, and builds tvm4j_runtime_packed.
"""
Command-line invocation:
python android/mlc4j/prepare_libs.py --mlc-llm-source-dir <path>
I/O Contract
Inputs (iOS)
| Name | Type | Required | Description |
|---|---|---|---|
-s / --simulator |
flag | No | When set, builds for the iOS Simulator instead of a physical device |
-a / --arch |
string | No | Simulator architecture: x86_64 or arm64 (default: arm64)
|
MLC_LLM_SOURCE_DIR |
env var | No | Path to MLC-LLM source root (default: ..)
|
Inputs (Android)
| Name | Type | Required | Description |
|---|---|---|---|
--mlc-llm-source-dir |
Path | No | Path to MLC-LLM source root (default: auto-detected or from MLC_LLM_SOURCE_DIR env var)
|
ANDROID_NDK |
env var | Yes | Path to the Android NDK installation directory |
TVM_SOURCE_DIR |
env var | No | Optional path to a custom TVM source directory |
Outputs
| Name | Type | Description |
|---|---|---|
build/lib/libmlc_llm_static.a (iOS) |
Static library | Compiled MLC-LLM inference engine for iOS with Metal GPU support |
build/lib/*.a (iOS) |
Static libraries | Additional TVM runtime static libraries |
ios/MLCSwift/tvm_home (iOS) |
Symlink | Symbolic link to 3rdparty/tvm for Swift package resolution
|
build/output/ (Android) |
Directory | Compiled shared libraries and JNI bindings for Android |
Usage Examples
Basic Usage (iOS Device)
# Build for a physical iOS device (arm64)
cd ios
export MLC_LLM_SOURCE_DIR=..
bash prepare_libs.sh
iOS Simulator (Apple Silicon)
# Build for iOS Simulator on Apple Silicon Mac
cd ios
bash prepare_libs.sh --simulator --arch arm64
iOS Simulator (Intel)
# Build for iOS Simulator on Intel Mac
cd ios
bash prepare_libs.sh --simulator --arch x86_64
Basic Usage (Android)
# Build for Android (requires ANDROID_NDK to be set)
export ANDROID_NDK=/path/to/android-ndk
python android/mlc4j/prepare_libs.py --mlc-llm-source-dir /path/to/mlc-llm
Related Pages
Implements Principle
Environment Links
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment