6.22. llvm/clang pass 2 22.1.3
The second llvm/clang pass installs the final compiler toolchain, then refreshes the LLVM runtimes with standalone cross builds and fixes Clang's default search paths for the book's custom filesystem layout.
llvm-project-22.1.3.src.tar.xz is already present in LBI_SOURCES, and sections 5.4 and 5.5 completed successfully.
Licenses
- Apache-2.0 WITH LLVM-exception
Dependencies
- musl (libc)
- cmake
- ninja
- zlib-ng (libz)
- llvm/clang pass 1
llvm is a modular compiler infrastructure project. we need it to provide the final compiler backend, linker integration, and toolchain libraries for the target system.
clang is a C and C++ frontend for LLVM. we need it to provide the final clang and clang++ compilers for the target system.
compiler-rt is the LLVM runtime support library project. we need it to provide Clang builtins and startup objects.
libunwind is a platform-independent stack unwinding library. we need it to provide unwind support used by C++ exception handling.
libcxxabi is the LLVM C++ ABI support library. we need it to provide ABI and exception runtime support for libcxx.
libcxx is the LLVM C++ standard library implementation. we need it to provide the C++ standard library used by Clang in later stages.
Extract Sources and Enter the LLVM Build Root
cd "$LBI_SOURCES"
tar -xf llvm-project-22.1.3.src.tar.xz
cd llvm-project-22.1.3.src/llvm
Configure llvm/clang Pass 2
LLVM_ENABLE_RUNTIMES flow is intentionally not used here, because it tries to configure compiler-rt builtins with the just-built target compiler, which is not host-runnable in this setup.
lbi_cmake build-llvm-pass2 \
-G Ninja \
-DCMAKE_C_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang" \
-DCMAKE_CXX_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang++" \
-DCMAKE_AR="$LBI_ROOT/system/tools/bin/$LBI_TARGET-ar" \
-DCMAKE_RANLIB="$LBI_ROOT/system/tools/bin/$LBI_TARGET-ranlib" \
-DCMAKE_NM="$LBI_ROOT/system/tools/bin/$LBI_TARGET-nm" \
-DCMAKE_OBJCOPY="$LBI_ROOT/system/tools/bin/$LBI_TARGET-objcopy" \
-DCMAKE_OBJDUMP="$LBI_ROOT/system/tools/bin/$LBI_TARGET-objdump" \
-DCMAKE_STRIP="$LBI_ROOT/system/tools/bin/$LBI_TARGET-strip" \
-DCMAKE_SYSROOT="$LBI_ROOT" \
-DCMAKE_C_COMPILER_LAUNCHER="ccache" \
-DCMAKE_CXX_COMPILER_LAUNCHER="ccache" \
-DCMAKE_FIND_ROOT_PATH="$LBI_ROOT;$LBI_ROOT/system" \
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY \
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
-DCMAKE_C_COMPILER_TARGET="$LBI_TARGET" \
-DCMAKE_CXX_COMPILER_TARGET="$LBI_TARGET" \
-DCMAKE_ASM_COMPILER_TARGET="$LBI_TARGET" \
-DCMAKE_C_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -isystem /system/headers $LWI_CFLAGS" \
-DCMAKE_CXX_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -isystem /system/headers $LWI_CXXFLAGS" \
-DCMAKE_SHARED_LINKER_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -B/system/libraries -B/system/libraries/clang/22/lib/linux -L/system/libraries $LBI_CUSTOM_LDFLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -B/system/libraries -B/system/libraries/clang/22/lib/linux -L/system/libraries $LBI_CUSTOM_LDFLAGS" \
-DLLVM_NATIVE_TOOL_DIR="$LBI_ROOT/system/tools/bin" \
-DLLVM_TABLEGEN="$LBI_ROOT/system/tools/bin/llvm-tblgen" \
-DCLANG_TABLEGEN="$LBI_ROOT/system/tools/bin/clang-tblgen" \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_INSTALL_BINUTILS_SYMLINKS=ON \
-DLLVM_HOST_TRIPLE="$LBI_TARGET" \
-DLLVM_TARGETS_TO_BUILD="X86" \
-DLLVM_DEFAULT_TARGET_TRIPLE="$LBI_TARGET" \
-DDEFAULT_SYSROOT="/" \
-DCLANG_DEFAULT_CXX_STDLIB=libc++ \
-DCLANG_DEFAULT_LINKER=lld \
-DCLANG_DEFAULT_RTLIB=compiler-rt \
-DCLANG_DEFAULT_UNWINDLIB=libunwind \
-DLLVM_ENABLE_ZLIB=ON \
-DLLVM_ENABLE_ZSTD=OFF \
-DLLVM_ENABLE_LIBXML2=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_BUILD_TESTS=OFF \
-DCMAKE_BUILD_TYPE=Release
Build llvm/clang Pass 2
cmake --build build-llvm-pass2 $LWI_MAKE_FLAGS
Install llvm/clang Pass 2
DESTDIR="$LBI_ROOT" cmake --install build-llvm-pass2
Refresh LLVM C++ Runtimes
cd "$LBI_SOURCES/llvm-project-22.1.3.src/runtimes"
lbi_cmake build-runtimes-pass2 \
-G Ninja \
-DCMAKE_C_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang" \
-DCMAKE_CXX_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang++" \
-DLLVM_CMAKE_DIR="$LBI_SOURCES/llvm-project-22.1.3.src/llvm/build-llvm-pass2/lib/cmake/llvm" \
-DCMAKE_SYSROOT="$LBI_ROOT" \
-DCMAKE_C_COMPILER_LAUNCHER="ccache" \
-DCMAKE_CXX_COMPILER_LAUNCHER="ccache" \
-DCMAKE_FIND_ROOT_PATH="$LBI_ROOT;$LBI_ROOT/system" \
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY \
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
-DCMAKE_C_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -isystem /system/headers $LWI_CFLAGS" \
-DCMAKE_CXX_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -isystem /system/headers $LWI_CXXFLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -B/system/libraries -B/system/libraries/clang/22/lib/linux -L/system/libraries $LBI_CUSTOM_LDFLAGS" \
-DLLVM_ENABLE_RUNTIMES="libunwind;libcxxabi;libcxx" \
-DLLVM_ENABLE_LIBXML2=OFF \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_DOCS=OFF \
-DLIBUNWIND_INSTALL_LIBRARY_DIR=/system/libraries \
-DLIBUNWIND_INCLUDE_TESTS=OFF \
-DLIBUNWIND_INCLUDE_DOCS=OFF \
-DLIBCXXABI_INSTALL_LIBRARY_DIR=/system/libraries \
-DLIBCXXABI_INCLUDE_TESTS=OFF \
-DLIBCXX_INSTALL_LIBRARY_DIR=/system/libraries \
-DLIBCXX_INCLUDE_TESTS=OFF \
-DLIBCXX_INCLUDE_BENCHMARKS=OFF \
-DLIBCXX_INCLUDE_DOCS=OFF \
-DLIBCXX_HAS_MUSL_LIBC=ON \
-DLIBCXX_HAS_ATOMIC_LIB=OFF \
-DLIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL=OFF \
-DLIBCXXABI_USE_LLVM_UNWINDER=ON \
-DLIBCXX_USE_COMPILER_RT=ON \
-DLIBCXXABI_USE_COMPILER_RT=ON \
-DLIBUNWIND_USE_COMPILER_RT=ON \
-DCMAKE_BUILD_TYPE=Release
cmake --build build-runtimes-pass2 $LWI_MAKE_FLAGS
DESTDIR="$LBI_ROOT" cmake --install build-runtimes-pass2
Build compiler-rt Builtins and CRT Objects
cd "$LBI_SOURCES/llvm-project-22.1.3.src/compiler-rt"
lbi_cmake build-compiler-rt-pass2 \
-G Ninja \
-DCMAKE_C_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang" \
-DCMAKE_CXX_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang++" \
-DCMAKE_ASM_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang" \
-DCMAKE_AR="$LBI_ROOT/system/tools/bin/$LBI_TARGET-ar" \
-DCMAKE_NM="$LBI_ROOT/system/tools/bin/$LBI_TARGET-nm" \
-DCMAKE_RANLIB="$LBI_ROOT/system/tools/bin/$LBI_TARGET-ranlib" \
-DLLVM_CMAKE_DIR="$LBI_SOURCES/llvm-project-22.1.3.src/llvm/build-llvm-pass2/lib/cmake/llvm" \
-DCMAKE_SYSROOT="$LBI_ROOT" \
-DCMAKE_C_COMPILER_LAUNCHER="ccache" \
-DCMAKE_CXX_COMPILER_LAUNCHER="ccache" \
-DCMAKE_C_COMPILER_TARGET="$LBI_TARGET" \
-DCMAKE_CXX_COMPILER_TARGET="$LBI_TARGET" \
-DCMAKE_ASM_COMPILER_TARGET="$LBI_TARGET" \
-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY \
-DCMAKE_C_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -isystem /system/headers $LWI_CFLAGS" \
-DCMAKE_CXX_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -isystem /system/headers $LWI_CXXFLAGS" \
-DCMAKE_ASM_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT $LWI_CFLAGS" \
-DCMAKE_EXE_LINKER_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT -B/system/libraries -B/system/libraries/clang/22/lib/linux -L/system/libraries $LBI_CUSTOM_LDFLAGS" \
-DCOMPILER_RT_INSTALL_PATH=/system/lib/clang/22 \
-DCOMPILER_RT_BUILD_BUILTINS=ON \
-DCOMPILER_RT_BUILD_CRT=ON \
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
-DCOMPILER_RT_BUILD_MEMPROF=OFF \
-DCOMPILER_RT_BUILD_ORC=OFF \
-DCOMPILER_RT_BUILD_PROFILE=OFF \
-DCOMPILER_RT_BUILD_CTX_PROFILE=OFF \
-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
-DCOMPILER_RT_BUILD_XRAY=OFF \
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
-DCOMPILER_RT_INCLUDE_TESTS=OFF \
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
-DCMAKE_BUILD_TYPE=Release
cmake --build build-compiler-rt-pass2 --target builtins $LWI_MAKE_FLAGS
cmake --build build-compiler-rt-pass2 --target crt $LWI_MAKE_FLAGS
DESTDIR="$LBI_ROOT" cmake --install build-compiler-rt-pass2
Make Clang Resource Files Visible in the Final Tree
mkdir -p "$LBI_ROOT/system/lib/clang/22/lib/$LBI_TARGET"
if [ -f "$LBI_ROOT/system/tools/lib/clang/22/lib/$LBI_TARGET/libclang_rt.builtins.a" ]; then
ln -sf "/system/tools/lib/clang/22/lib/$LBI_TARGET/libclang_rt.builtins.a" \
"$LBI_ROOT/system/lib/clang/22/lib/$LBI_TARGET/libclang_rt.builtins.a"
fi
Create Compiler and Linker Compatibility Symlinks
cd "$LBI_ROOT/system/binaries"
ln -sf ld.lld ld
ln -sf clang cc
ln -sf clang++ c++
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 ld.lld "$LBI_TARGET-ld"
Link Clang Startup Objects for musl Compatibility
CRTBEGIN_OBJ=$(find "$LBI_ROOT/system/libraries/clang" \
-type f \( -name 'crtbeginS.o' -o -name 'clang_rt.crtbegin*.o' \) | head -n1)
CRTEND_OBJ=$(find "$LBI_ROOT/system/libraries/clang" \
-type f \( -name 'crtendS.o' -o -name 'clang_rt.crtend*.o' \) | head -n1)
CRT_DIR=$(dirname "$CRTBEGIN_OBJ")
if [ -n "$CRTBEGIN_OBJ" ] && [ -n "$CRTEND_OBJ" ]; then
ln -sf "$(basename "$CRTBEGIN_OBJ")" "$CRT_DIR/crtbeginS.o"
ln -sf "$(basename "$CRTEND_OBJ")" "$CRT_DIR/crtendS.o"
ln -sf "${CRTBEGIN_OBJ#$LBI_ROOT/system}" "$LBI_ROOT/system/libraries/crtbeginS.o"
ln -sf "${CRTEND_OBJ#$LBI_ROOT/system}" "$LBI_ROOT/system/libraries/crtendS.o"
fi
Add Clang Driver Wrapper Defaults
mv "$LBI_ROOT/system/binaries/clang" "$LBI_ROOT/system/binaries/clang.real"
mv "$LBI_ROOT/system/binaries/clang++" "$LBI_ROOT/system/binaries/clang++.real"
cat > "$LBI_ROOT/system/binaries/clang" <<'EOF'
#!/bin/sh
exec /system/binaries/clang.real \
-isystem /system/headers \
-B/system/libraries \
-Wno-unused-command-line-argument \
-B/system/libraries/clang/22/lib/linux \
-L/system/libraries \
"$@"
EOF
cat > "$LBI_ROOT/system/binaries/clang++" <<'EOF'
#!/bin/sh
exec /system/binaries/clang++.real \
-nostdinc++ \
-I/system/headers/c++/v1 \
-isystem /system/libraries/clang/22/include \
-isystem /system/headers \
-B/system/libraries \
-B/system/libraries/clang/22/lib/linux \
-L/system/libraries \
-Wl,-rpath,/system/libraries \
"$@" \
-Wno-unused-command-line-argument \
-lc++ \
-lc++abi \
-lunwind
EOF
chmod 755 "$LBI_ROOT/system/binaries/clang" "$LBI_ROOT/system/binaries/clang++"
cd "$LBI_ROOT/system/binaries"
ln -sf clang cc
ln -sf clang++ c++
ln -sf clang "$LBI_TARGET-clang"
ln -sf clang++ "$LBI_TARGET-clang++"
ln -sf clang "$LBI_TARGET-cc"
ln -sf clang++ "$LBI_TARGET-c++"
Command Explanations
LBI_HOST_NINJA=$(command -v ninja || command -v samu || true): Finds a host-runnable Ninja-compatible build tool before CMake has a chance to cache a target-side executable.if [ -z "$LBI_HOST_NINJA" ]; then ... exit 1; fi: Stops early with a clear error if the host does not haveninjaorsamu.lbi_cmake build-llvm-pass2: Uses the book's CMake helper for install layout while placing generated files in thebuild-llvm-pass2directory.-DCMAKE_C_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang"and-DCMAKE_CXX_COMPILER=...: Build pass 2 with the pass 1 cross compiler instead of the host compiler.-DCMAKE_AR,-DCMAKE_RANLIB,-DCMAKE_NM,-DCMAKE_OBJCOPY,-DCMAKE_OBJDUMP, and-DCMAKE_STRIP: Use the target-prefixed LLVM binutils-compatible tools.-DCMAKE_SYSROOT="$LBI_ROOT"and theCMAKE_FIND_ROOT_PATH*settings: Keep configure checks and dependency lookups inside the target tree while still allowing host build tools such ascmakeandninja.-DCMAKE_MAKE_PROGRAM="$LBI_HOST_NINJA": Pins the Ninja generator to a host-runnable build executor so CMake does not cache a target-side/usr/bin/ninjafromLBI_ROOT.-DCMAKE_C_COMPILER_LAUNCHER="ccache"and-DCMAKE_CXX_COMPILER_LAUNCHER="ccache": Use ccache for repeated compiler invocations when it is available.-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY: Prevent configure probes from requiring runnable target executables.-DCMAKE_C_COMPILER_TARGET,-DCMAKE_CXX_COMPILER_TARGET, and-DCMAKE_ASM_COMPILER_TARGET: Tell CMake and Clang which target triple to use for C, C++, and assembly.-DCMAKE_C_FLAGS=... -isystem /system/headersand-DCMAKE_CXX_FLAGS=... -isystem /system/headers: Make the build use the book's custom libc header location instead of assuming/usr/include.-DCMAKE_EXE_LINKER_FLAGS=... -B/system/libraries -B/system/libraries/clang/22/lib/linux -L/system/librariesand the matching shared-linker flags: Make link steps find musl startup files, Clang CRT objects, and libc in the book's custom library layout.-DLLVM_NATIVE_TOOL_DIR,-DLLVM_TABLEGEN, and-DCLANG_TABLEGEN: Reuse the pass 1 native helper tools instead of rebuilding host tools in this pass.-DLLVM_ENABLE_PROJECTS="clang;lld": Build the final frontend and linker in the main compiler pass.-DLLVM_INSTALL_BINUTILS_SYMLINKS=ON: Installs LLVM tool aliases for common binutils command names.-DLLVM_HOST_TRIPLE="$LBI_TARGET",-DLLVM_TARGETS_TO_BUILD="X86", and-DLLVM_DEFAULT_TARGET_TRIPLE="$LBI_TARGET": Keep this pass focused on the target architecture and make the installed compiler default to the book's target triple.-DDEFAULT_SYSROOT="/": Make the installed Clang use the target root layout at runtime instead of baking in the temporary build host path.-DCLANG_DEFAULT_CXX_STDLIB=libc++,-DCLANG_DEFAULT_LINKER=lld,-DCLANG_DEFAULT_RTLIB=compiler-rt, and-DCLANG_DEFAULT_UNWINDLIB=libunwind: Set the intended default runtime family for later builds.-DLLVM_ENABLE_ZLIB=ON: Enables zlib support using the target zlib-compatible library.-DLLVM_ENABLE_ZSTD=OFFand-DLLVM_ENABLE_LIBXML2=OFF: Disable optional dependencies that are not part of this pass.-DLLVM_INCLUDE_TESTS=OFFand-DLLVM_BUILD_TESTS=OFF: Skip LLVM tests in the target compiler build.-DCMAKE_BUILD_TYPE=Release: Uses release optimization for the final target compiler.cmake --build build-llvm-pass2 $LWI_MAKE_FLAGS: Builds the configured compiler tree with the shared parallel build setting.DESTDIR="$LBI_ROOT" cmake --install build-llvm-pass2: Installs the final compiler into the target root instead of the host system.LLVM_ENABLE_RUNTIMESis omitted from the main pass on purpose: the top-level runtime bootstrap tries to configure compiler-rt builtins with the just-built target compiler, which fails in this cross setup because that compiler is not host-runnable.- The standalone
build-runtimes-pass2step refresheslibunwind,libcxxabi, andlibcxxwith the final compiler and the final library/header layout. -DLLVM_CMAKE_DIR="$LBI_SOURCES/llvm-project-22.1.3.src/llvm/build-llvm-pass2/lib/cmake/llvm": Points standalone runtime and compiler-rt builds at the just-configured LLVM package files.- The runtime
CMAKE_FIND_ROOT_PATH*,CMAKE_TRY_COMPILE_TARGET_TYPE, compile flags, and linker flags repeat the same cross-build protections used for the main pass. -DLLVM_ENABLE_RUNTIMES="libunwind;libcxxabi;libcxx": Builds only the C++ runtime stack in the standalone runtime pass.-DLLVM_INCLUDE_DOCS=OFF,LIBUNWIND_INCLUDE_*,LIBCXXABI_INCLUDE_TESTS=OFF, andLIBCXX_INCLUDE_*: Skip tests, benchmarks, and documentation for the runtime rebuild.-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF: Keep runtime outputs out of target-triple subdirectories where possible so they land in the normal library layout.-DLIBUNWIND_INSTALL_LIBRARY_DIR=/system/libraries,-DLIBCXXABI_INSTALL_LIBRARY_DIR=/system/libraries, and-DLIBCXX_INSTALL_LIBRARY_DIR=/system/libraries: Force the runtime libraries into the book's custom/system/librariespath.-DLIBCXX_HAS_MUSL_LIBC=ON: Enables libcxx behavior for the musl target.-DLIBCXX_HAS_ATOMIC_LIB=OFF: Prevents an external-latomicdependency in this pass.-DLIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL=OFF: Avoids relying on the glibc-specific__cxa_thread_atexit_implsymbol.-DLIBCXXABI_USE_LLVM_UNWINDER=ON: Links libcxxabi against libunwind from this runtime set.-DLIBCXX_USE_COMPILER_RT=ON,-DLIBCXXABI_USE_COMPILER_RT=ON, and-DLIBUNWIND_USE_COMPILER_RT=ON: Use compiler-rt runtime libraries instead of libgcc-style runtime assumptions.cmake --build build-runtimes-pass2 $LWI_MAKE_FLAGSandDESTDIR="$LBI_ROOT" cmake --install build-runtimes-pass2: Build and install the refreshed C++ runtime stack into the target root.lbi_cmake build-compiler-rt-pass2: Configures compiler-rt as a standalone cross build.-DCMAKE_ASM_COMPILER="$LBI_ROOT/system/tools/bin/$LBI_TARGET-clang"and-DCMAKE_ASM_COMPILER_TARGET="$LBI_TARGET": Build assembly runtime objects with the target Clang.-DCMAKE_ASM_FLAGS="--target=$LBI_TARGET --sysroot=$LBI_ROOT $LWI_CFLAGS": Applies the target triple and sysroot to assembly compilation.-DCOMPILER_RT_INSTALL_PATH=/system/lib/clang/22: Install compiler-rt into Clang's resource-directory layout so the driver can find builtins by default.-DCOMPILER_RT_BUILD_BUILTINS=ONand-DCOMPILER_RT_BUILD_CRT=ON: Build the Clang builtins library and CRT startup objects needed by later links.-DCOMPILER_RT_BUILD_LIBFUZZER=OFF,-DCOMPILER_RT_BUILD_MEMPROF=OFF,-DCOMPILER_RT_BUILD_ORC=OFF,-DCOMPILER_RT_BUILD_PROFILE=OFF,-DCOMPILER_RT_BUILD_CTX_PROFILE=OFF,-DCOMPILER_RT_BUILD_SANITIZERS=OFF, and-DCOMPILER_RT_BUILD_XRAY=OFF: Skip runtime components that are not needed for this target bootstrap.-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON: Limits compiler-rt output to the configured target.-DCOMPILER_RT_INCLUDE_TESTS=OFF: Skips compiler-rt tests.cmake --build build-compiler-rt-pass2 --target builtins $LWI_MAKE_FLAGSand--target crt: Build only the compiler-rt targets needed by the target toolchain.DESTDIR="$LBI_ROOT" cmake --install build-compiler-rt-pass2: Installs compiler-rt outputs into the target root.mkdir -p "$LBI_ROOT/system/lib/clang/22/lib/$LBI_TARGET": Creates the Clang resource-library directory expected by the driver.- The resource-file symlink step: Covers cases where compiler-rt still lands under
/system/tools/lib/clang/...while the Clang driver looks under/system/lib/clang/.... cd "$LBI_ROOT/system/binaries": Runs compiler/linker symlink creation from the target binary directory so the relative links stay simple.ln -sf ld.lld ld,ln -sf clang cc, andln -sf clang++ c++: Provide conventional compiler and linker command names.- The
$LBI_TARGET-*symlinks: Preserve cross-style tool names for later build systems that look for target-prefixed compilers. find "$LBI_ROOT/system/libraries/clang" ... | head -n1: Locates the installed compiler-rt CRT begin/end objects regardless of their exact upstream-generated names.CRT_DIR=$(dirname "$CRTBEGIN_OBJ"): Records the resource directory containing those CRT objects.- The
crtbeginS.oandcrtendS.olinks: Make the Clang CRT objects visible both in the resource directory and in/system/libraries, where musl-oriented links and later builds expect them. mv "$LBI_ROOT/system/binaries/clang" ... clang.realand the matchingclang++move: Preserve the real compiler binaries before installing wrapper scripts at the public command names.cat > "$LBI_ROOT/system/binaries/clang" <<'EOF'and the matchingclang++block: Writes wrapper scripts with fixed default include, startup-object, and library search paths.- The Clang wrapper scripts: Add the header path and startup-object/library search paths needed by the book's non-FHS layout, so later package builds do not have to export manual
CPPFLAGS,CFLAGS, orLDFLAGSjust to find musl headers and libc. chmod 755 "$LBI_ROOT/system/binaries/clang" "$LBI_ROOT/system/binaries/clang++": Makes the wrapper scripts executable.- The final compiler symlink block: Recreates
cc,c++, and target-prefixed aliases so they point at the wrapper scripts rather than bypassing them.