5.3. llvm/clang pass 1 22.1.3

The first llvm/clang pass establishes an initial toolchain in the tools prefix so later stages, including compiler-rt, can build against a controlled compiler and linker set.

Performance note: This stage will take a while. It is NOT recommended to run this on a device with fewer than 8 CPU cores.
Input assumption: llvm-project-22.1.3.src.tar.xz is already present from the chapter 4 manifest fetch step.

llvm/clang is a modular compiler and linker toolchain project. we need it to produce the pass 1 compiler components (clang, lld) used by later runtime work, including compiler-rt.

Extract Sources and Create a Build Directory

cd "$LBI_SOURCES"
tar -xf llvm-project-22.1.3.src.tar.xz
cd llvm-project-22.1.3.src
mkdir -p build-llvm
cd build-llvm

Configure llvm/clang Pass 1

cmake -G Ninja "../llvm" \
    -DCMAKE_C_COMPILER=/usr/bin/clang \
    -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
    -DCMAKE_C_COMPILER_LAUNCHER=ccache \
    -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
    -DCMAKE_INSTALL_PREFIX=$LBI_ROOT/system/tools \
    -DLLVM_ENABLE_PROJECTS="lld;clang" \
    -DLLVM_ENABLE_RUNTIMES="compiler-rt" \
    -DCOMPILER_RT_BUILD_BUILTINS=ON \
    -DCOMPILER_RT_BUILD_SANITIZERS=OFF \
    -DCOMPILER_RT_BUILD_XRAY=OFF \
    -DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
    -DCOMPILER_RT_BUILD_PROFILE=OFF \
    -DLLVM_DEFAULT_TARGET_TRIPLE="$LBI_TARGET" \
    -DLLVM_TARGETS_TO_BUILD="X86" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCLANG_DEFAULT_CXX_STDLIB=libc++ \
    -DCLANG_DEFAULT_LINKER=lld \
    -DCLANG_DEFAULT_RTLIB=compiler-rt \
    -DDEFAULT_SYSROOT=$LBI_ROOT

Build llvm/clang Pass 1

ninja $LWI_MAKE_FLAGS

Install llvm/clang Pass 1 to the temp tools directory

ninja install

Command Explanations

This pass 1 configuration keeps compiler-rt limited to builtins-only output.

ccache is recommended, but not required. If ccache is not installed on your host, remove the two CMAKE_*_COMPILER_LAUNCHER lines and re-run configure.

For accepted LLVM_TARGETS_TO_BUILD values, choose the target that matches your architecture and verify against upstream documentation:

Create Target-Prefixed llvm Tool Symlinks

Some builds expect cross-style tool names like $LBI_TARGET-ld. Create symlinks for the installed LLVM tools so those names resolve in $LBI_ROOT/system/tools/bin.

cd "$LBI_ROOT/system/tools/bin"

ln -sf clang "$LBI_TARGET-clang"
ln -sf clang++ "$LBI_TARGET-clang++"
ln -sf clang "$LBI_TARGET-cc"
ln -sf clang++ "$LBI_TARGET-c++"

ln -sf llvm-ar "$LBI_TARGET-ar"
ln -sf llvm-ranlib "$LBI_TARGET-ranlib"
ln -sf llvm-as "$LBI_TARGET-as"
ln -sf llvm-nm "$LBI_TARGET-nm"
ln -sf llvm-objcopy "$LBI_TARGET-objcopy"
ln -sf llvm-objdump "$LBI_TARGET-objdump"
ln -sf llvm-readelf "$LBI_TARGET-readelf"
ln -sf llvm-strip "$LBI_TARGET-strip"

ln -sf ld.lld "$LBI_TARGET-ld"