# How to Build LiteRT-LM from Source Using Bazel or CMake

> Learn to build LiteRT-LM from source with Bazel or CMake. Compile the inference engine easily for your AI projects. Get started today!

- Repository: [google-ai-edge/LiteRT-LM](https://github.com/google-ai-edge/LiteRT-LM)
- Tags: how-to-guide
- Published: 2026-04-06

---

**You can compile the LiteRT-LM inference engine using either Bazel 7.6.1 (the default, hermetic build system) or CMake ≥3.25 (for external toolchains), both targeting the `runtime/engine:litert_lm_main` binary.**

LiteRT-LM is a cross-platform large language model inference framework maintained by Google AI Edge in the `google-ai-edge/LiteRT-LM` repository. When you **build LiteRT-LM from source**, you choose between two build orchestrations—Bazel or CMake—that consume the same C++ source tree but use different top-level configuration files.

## Prerequisites

Both build paths require Git to clone the repository and a C++ toolchain, but they diverge on the orchestration tool:

- **Bazel**: Install **Bazelisk** to automatically respect the pinned version (7.6.1) declared in the `.bazelversion` file. Python 3.13 is required for schema generation and tooling.
- **CMake**: Install **CMake 3.25 or newer** and a native compiler (Clang for macOS/Linux, MSVC for Windows). For cross-compilation, prepare a toolchain file (e.g., for Android or iOS).

Start by cloning the repository and checking out a release tag:

```bash
git clone https://github.com/google-ai-edge/LiteRT-LM.git
cd LiteRT-LM
git fetch --tags
git checkout -b build-branch v0.8.0

```

## Building with Bazel (Official Method)

Bazel is the officially supported build system. It reads dependency declarations from the `WORKSPACE` file and build rules from `runtime/engine/BUILD` to produce hermetic, cacheable outputs.

### Basic CPU Build

Run the following command from the repository root. Bazel automatically downloads external dependencies (Abseil, LiteRT, FlatBuffers) defined in `WORKSPACE`:

```bash
bazelisk build //runtime/engine:litert_lm_main

```

The output binary appears at `bazel-bin/runtime/engine/litert_lm_main`.

### Cross-Compilation for Android

To target Android ARM64, use the `--config=android_arm64` flag. The build leverages rules defined in the internal `toolchain` configurations to locate the Android NDK:

```bash
bazelisk build --config=android_arm64 //runtime/engine:litert_lm_main

```

### GPU Support on Windows

When building for GPU execution on Windows, append the following defines to ensure proper symbol resolution:

```bash
bazelisk build --config=windows \
    --define=litert_link_capi_so=true \
    --define=resolve_symbols_in_exec=false \
    //runtime/engine:litert_lm_main

```

## Building with CMake (Alternative Toolchains)

CMake provides flexibility for integrating with external toolchains or IDE workflows. The root [`CMakeLists.txt`](https://github.com/google-ai-edge/LiteRT-LM/blob/main/CMakeLists.txt) orchestrates a two-stage process: a *host pre-build* stage to generate protobuf and FlatBuffer schemas (mirroring the logic in `schema/py/BUILD` and `schema/cc/BUILD`), followed by the engine compilation.

### Native Build

Create a build directory and invoke CMake. The [`runtime/engine/CMakeLists.txt`](https://github.com/google-ai-edge/LiteRT-LM/blob/main/runtime/engine/CMakeLists.txt) defines the `litert_lm_main` target using custom `add_litertlm_executable` functions:

```bash
cmake -S . -B build
cmake --build build --target litert_lm_main

```

### Cross-Compilation with Toolchain Files

Pass your toolchain file via the `LITERTLM_TOOLCHAIN_ARGS` variable. CMake will first run the host pre-build via `ExternalProject_Add` to generate schema files, then compile the engine using your specified toolchain:

```bash
cmake -S . -B build_android \
    -DLITERTLM_TOOLCHAIN_ARGS="-DCMAKE_TOOLCHAIN_FILE=android-arm64.cmake"
cmake --build build_android --target litert_lm_main

```

The resulting binary is located at `build/runtime/engine/litert_lm_main` (or the equivalent path inside your build directory).

## Running the Inference Engine

After building with either system, execute the binary with the `--backend` and `--model_path` flags:

**Bazel output:**

```bash
MODEL_PATH=/path/to/model.litertlm
./bazel-bin/runtime/engine/litert_lm_main \
    --backend=cpu \
    --model_path=$MODEL_PATH

```

**CMake output:**

```bash
MODEL_PATH=/path/to/model.litertlm
./build/runtime/engine/litert_lm_main \
    --backend=cpu \
    --model_path=$MODEL_PATH

```

## Architectural Differences Between Build Systems

While both systems compile identical source code from the `runtime/engine` directory, their orchestration models differ significantly:

- **Bazel** uses repository-internal `WORKSPACE` and `BUILD` files to fetch dependencies and apply platform-specific selections via `select({})` statements. The build graph is fully hermetic and automatically caches intermediate artifacts.
- **CMake** acts as a thin wrapper that mirrors the Bazel layout. It uses `ExternalProject_Add` in the root [`CMakeLists.txt`](https://github.com/google-ai-edge/LiteRT-LM/blob/main/CMakeLists.txt) to manage the host pre-build step for schema generation, then delegates to subdirectories like [`runtime/engine/CMakeLists.txt`](https://github.com/google-ai-edge/LiteRT-LM/blob/main/runtime/engine/CMakeLists.txt) for the final linking stage. This allows you to inject any CMake-compatible toolchain file.

## Summary

- **Bazel** is the default, recommended path for most users, supporting Linux, macOS, Windows, and Android via `--config` flags.
- **CMake** is ideal when you require custom compilers or cross-compilation toolchains not covered by the Bazel `rules_android` or `rules_apple` definitions.
- Both systems ultimately produce the `litert_lm_main` executable, configured through files like `WORKSPACE` (Bazel) or the root [`CMakeLists.txt`](https://github.com/google-ai-edge/LiteRT-LM/blob/main/CMakeLists.txt) (CMake).
- GPU builds require additional `--define` flags in Bazel, while CMake builds rely on `LITERTLM_TOOLCHAIN_ARGS` for cross-platform targeting.

## Frequently Asked Questions

### What version of Bazel is required for LiteRT-LM?

The repository pins **Bazel 7.6.1** in the `.bazelversion` file. Use Bazelisk to automatically download and use this exact version, ensuring reproducible builds across different development machines.

### Can I use CMake to build for Android or iOS?

Yes. Pass a toolchain file (e.g., `android-arm64.cmake`) using the `LITERTLM_TOOLCHAIN_ARGS` variable. The root [`CMakeLists.txt`](https://github.com/google-ai-edge/LiteRT-LM/blob/main/CMakeLists.txt) will handle the host pre-build step to generate necessary schema files before applying your Android or iOS toolchain to the engine compilation.

### Why does CMake require a host pre-build stage?

LiteRT-LM relies on generated code from protobuf and FlatBuffer definitions located in `schema/py/BUILD` and `schema/cc/BUILD`. Because CMake cannot natively execute these Bazel rules, the `ExternalProject_Add` block in the root [`CMakeLists.txt`](https://github.com/google-ai-edge/LiteRT-LM/blob/main/CMakeLists.txt) runs a preliminary host build to generate these artifacts before the main cross-compilation begins.

### Where is the final binary located after compilation?

For **Bazel**, the executable is placed at `bazel-bin/runtime/engine/litert_lm_main`. For **CMake**, the path is `build/runtime/engine/litert_lm_main` (or your custom build directory), as defined by the output rules in [`runtime/engine/CMakeLists.txt`](https://github.com/google-ai-edge/LiteRT-LM/blob/main/runtime/engine/CMakeLists.txt).