Update build instruction about how to leverage the customize flags

LiteRT-PiperOrigin-RevId: 835357060
This commit is contained in:
Google AI Edge 2025-11-21 14:20:10 -08:00 committed by Copybara-Service
parent 97ecde5f8d
commit e05b361e68

View File

@ -1,8 +1,8 @@
# LiteRT CMake Build Instructions
Use this guide to configure and build the LiteRT runtime with CMake on macOS
The instructions cover both Android cross-compilation targets
(built from Linux and Mac OS) machine and native macOS and Linux builds.
Use this guide to configure and build the LiteRT runtime with CMake on macOS The
instructions cover both Android cross-compilation targets (built from Linux and
Mac OS) machine and native macOS and Linux builds.
## Common Build Steps
@ -19,48 +19,90 @@ controlled via `-j` with the desired core count.
## Android (arm64) Cross-Compilation
1. Install the Android NDK and export the path so CMake can find it:
1. Install the Android NDK and export the path so CMake can find it:
```bash
export ANDROID_NDK_HOME=/absolute/path/to/android-ndk-r27
```
```bash
export ANDROID_NDK_HOME=/absolute/path/to/android-ndk-r27
```
2. Configure host-side flatbuffer tools
1. Configure host-side flatbuffer tools
```bash
cmake --preset android-arm64;
```
```bash
cmake --preset android-arm64;
```
3. Configure the LiteRT Android build using the provided preset and point to
the generated FlatBuffers tools:
1. Configure the LiteRT Android build using the provided preset and point to the
generated FlatBuffers tools:
```bash
cmake --preset android-arm64 \
-DTFLITE_HOST_TOOLS_DIR="$(cd ../host_flatc_build/_deps/flatbuffers-build && pwd)"
```
```bash
cmake --preset android-arm64 \
-DTFLITE_HOST_TOOLS_DIR="$(cd ../host_flatc_build/_deps/flatbuffers-build && pwd)"
```
4. Build LiteRT for Android:
1. Build LiteRT for Android:
```bash
cmake --build cmake_build_android_arm64 -j
```
```bash
cmake --build cmake_build_android_arm64 -j
```
Artifacts such as static libraries will be emitted under
`cmake_build_android_arm64`.
## Host Build from Mac OS and Linux
1. Configure the default host preset:
1. Configure the default host preset:
```bash
cmake --preset default
```
```bash
cmake --preset default
```
2. Build LiteRT:
1. Build LiteRT:
```bash
cmake --build cmake_build -j
```
```bash
cmake --build cmake_build -j
```
## Customize your build target
## Customizing CMake Builds
Use CMake options to control which toolchains and features are compiled into
your targets. E.g.:
```bash
cmake -S . -B build-release \
-DCMAKE_BUILD_TYPE=Release \
-DLITERT_AUTO_BUILD_TFLITE=ON \
-DLITERT_ENABLE_GPU=OFF \
-DLITERT_ENABLE_NPU=OFF \
-DLITERT_DISABLE_KLEIDIAI=ON \
-DLITERT_HOST_C_COMPILER=/usr/bin/clang \
-DLITERT_HOST_CXX_COMPILER=/usr/bin/clang++
cmake --build build-release --target dispatch_api_Qualcomm_so -j8
```
- `LITERT_HOST_C_COMPILER` / `LITERT_HOST_CXX_COMPILER` let you point the helper
host tools at any Clang/GCC installation without editing `CMakeLists.txt`.
- `LITERT_DISABLE_KLEIDIAI` keeps x86 host builds reproducible by skipping
KleidiAI; set it to `OFF` whenever you want to bundle the delegate.
- Always pass `-DCMAKE_BUILD_TYPE=Release` (or the equivalent preset) when you
need the optimized artifact. This makes CMake use `-O3 -DNDEBUG` for you.
### Producing Minimum-Size Vendor Libraries
At link time enable dead-stripping so dispatch libraries match Bazels size:
- macOS: add
`-DCMAKE_SHARED_LINKER_FLAGS_RELEASE="-Wl,-dead_strip -dead_strip_dylibs"`
(and the same for `CMAKE_EXE_LINKER_FLAGS_RELEASE` if you want the
executeables stripped). After building, run
`strip -x path/to/libLiteRtDispatch_Qualcomm.dylib`.
- Linux: add `-DCMAKE_SHARED_LINKER_FLAGS_RELEASE="-Wl,--gc-sections"` and run
`strip --strip-unneeded path/to/libLiteRtDispatch_Qualcomm.so` (or make it a
`POST_BUILD` command).
These flags can live directly in `cmake --preset …` entries in
`CMakePresets.json` so every developer in your team gets the same configuration.
## Troubleshooting Tips