Building LLVM/Clang/LLDB for RISC-V on Milk-V Megrez

I successfully built a native LLVM toolchain (Clang, LLD, LLDB) for our RISC-V boards and wanted to share the process. This build took approximately 8.5 hours on my Megrez, so please plan accordingly and ensure stable power.

Here are the step-by-step instructions:

  1. Install required build tools:
    sudo apt-get update
    sudo apt-get install -y build-essential cmake ninja-build git python3 libz-dev libxml2-dev libedit-dev libncurses-dev swig gcc g++ make curl

  2. Clone the LLVM project:
    cd ~
    git clone GitHub - llvm/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
    cd llvm-project

  3. Checkout the stable version we used (20.1.8):
    git fetch --tags
    git checkout llvmorg-20.1.8

  4. Configure the build for RISC-V:
    rm -rf build
    mkdir build && cd build
    cmake -G Ninja ../llvm
    -DCMAKE_BUILD_TYPE=Release
    -DLLVM_TARGETS_TO_BUILD=“RISCV”
    -DLLVM_ENABLE_PROJECTS=“clang;lld;lldb”
    -DLLVM_TARGET_ARCH=“riscv64”
    -DLLVM_DEFAULT_TARGET_TRIPLE=“riscv64-unknown-linux-gnu”
    -DCMAKE_CXX_FLAGS=“-Wno-attributes -march=rv64gc -mabi=lp64d -mcmodel=medany -fPIC”
    -DCMAKE_C_FLAGS=“-march=rv64gc -mabi=lp64d -mcmodel=medany -fPIC”
    -DLLVM_ENABLE_RTTI=ON
    -DLLVM_ENABLE_ZSTD=OFF
    -DLLVM_INCLUDE_TESTS=OFF
    -DLLVM_ENABLE_PIC=ON
    -DLLVM_ENABLE_TERMINFO=OFF
    -DLLVM_BUILD_LLVM_DYLIB=ON
    -DLLVM_LINK_LLVM_DYLIB=ON
    -DLLDB_ENABLE_PROCESS_LINUX=OFF

  5. Start the build (this will take 8.5+ hours):
    ninja -j$(nproc)

  6. Verify the build was successful:
    ./bin/llvm-config --version
    ./bin/clang --version | grep “riscv64”
    ./bin/ld.lld --version
    ./bin/lldb --version

You should see output confirming LLVM 20.1.8 with riscv64 target. The tools will be available in the ~/llvm-project/build/bin/ directory.

Optionally, you can install system-wide with:
sudo ninja install

This provides a complete native RISC-V toolchain for development on our Milk-V Megrez boards. Note that LLDB functionality may be somewhat limited as RISC-V support is still evolving.

1 Like