Oxidized NDK (ONDK)

repository·master·Indexed 42 days ago

https://github.com/topjohnwu/ondk

An unofficial repackaged Android NDK that includes a compatible Rust toolchain. ONDK is designed to enable Cross-Language LTO (Link Time Optimization) between C/C++ and Rust by ensuring both use the same LLVM version, allowing mixed-language projects to be optimized together during the linking stage.

Tokens
721
Snippets
2
Records
5
Agent score
4%

What's inside ONDK

  1. Why use ONDK for Cross-Language LTO

    master

    Standard Rust toolchains and the Android NDK use different LLVM versions, which often makes their LTO (Link Time Optimization) output formats incompatible.

    ONDK solves this by building both rustc and clang from the same LLVM source code. This ensures compatibility for Cross-Language LTO, allowing C/C++ and Rust code to be optimized together during the linking stage, which is critical for high-performance mixed-language projects.

  2. Why `std` is not prebuilt in ONDK

    master

    The std crate is intentionally not prebuilt to allow developers to:

    1. Minimize binary size: You can disable features (like unwind for panics) during the build process.
    2. Enable LTO: Building std alongside your project allows the standard library to be LTO-ed with your application code, enabling better optimizations.
  3. Build Rust code for Android using ONDK

    master

    Because the std crate is not prebuilt in ONDK, you must build it along with your project using the -Z build-std flag. You also need to explicitly point Cargo to the NDK linker via the CARGO_TARGET_<TARGET>_LINKER environment variable.

    To build a project targeting API 21 for ARM64, follow this pattern:

    1. Set LLVM_BIN to the path of the ONDK LLVM prebuilt binaries.
    2. Export the CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER environment variable pointing to the specific clang executable (e.g., aarch64-linux-android21-clang).
    3. Run cargo build with the -Z build-std flag and the appropriate --target.

    Note: You can also configure the linker in your config.toml instead of using environment variables.

    LLVM_BIN="<ondk>/toolchains/llvm/prebuilt/<os>-x86_64/bin"
    
    # We need to tell cargo where to find the NDK linker for Android.
    # You can also set this in config.toml, check the official documentation.
    export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="$LLVM_BIN/aarch64-linux-android21-clang"
    
    # Finally, build our project with -Z build-std
    cargo build -Z build-std --target aarch64-linux-android
  4. Link ONDK toolchain with rustup

    master

    To use the ONDK Rust toolchain, link it to your rustup installation using the following command. Replace <name> with your desired toolchain name and <ondk> with the path to your ONDK installation.

    rustup toolchain link <name> <ondk>/toolchains/rust