[DocDB] LTO build fails in llvm-ar because CMake finds the system llvm-ar instead of the toolchain one
Jira Link: DB-23657
Description
A local --full-lto build fails on the first static library:
[4/1584] Linking C static library lib/libgutil_c.a
FAILED: lib/libgutil_c.a
: && /opt/cmake-3.31.0-linux-x86_64/bin/cmake -E rm -f lib/libgutil_c.a && /usr/bin/llvm-ar qc lib/libgutil_c.a src/yb/gutil/CMakeFiles/gutil_c.dir/dynamic_annotations.c.o src/yb/gutil/CMakeFiles/gutil_c.dir/utf/rune.c.o && /usr/bin/llvm-ranlib lib/libgutil_c.a && :
/usr/bin/llvm-ar: error: lib/libgutil_c.a: Not an int attribute (Producer: 'LLVM21.1.1' Reader: 'LLVM 13.0.1')Steps to reproduce
Run on an AlmaLinux 8 host which has the system llvm package installed, this package owns /usr/bin/llvm-ar:
./yb_build.sh --full-lto release --sj packagedBuild root is release-clang21-full-lto-ninja, third-party is yugabyte-db-thirdparty-v20260903234939-a6198d5839-almalinux8-x86_64-clang21-full-lto, toolchain is yb-llvm-v21.1.1-yb-2-1776376910-efca861c-almalinux8-x86_64, the system llvm package is llvm-13.0.1-1.module_el8.6.0+2864+ffe288a1.x86_64.
Reason
With LTO the object files contain LLVM bitcode instead of ELF, so llvm-ar has to parse them to build the archive symbol index. The reader of version 13.0.1 can't read bitcode produced by version 21.1.1.
CMake resolves the archiver to the wrong binary. CMakeFindBinUtils looks for llvm-ar in the directory of the compiler first, but CMAKE_C_COMPILER and CMAKE_CXX_COMPILER point to build-support/compiler-wrappers/{cc,c++} and there is no llvm-ar near the wrappers. So the search falls back to PATH, where the only llvm-ar is the system one — we don't add $YB_LLVM_TOOLCHAIN_DIR/bin to PATH.
Builds without LTO are not affected, their object files are real ELF and any ar can archive them. On the affected host all build roots — 2024.2, 2025.1, 2025.2, 2026.1 and master — have CMAKE_AR:FILEPATH=/usr/bin/llvm-ar, but only the LTO one fails.
Workaround
Fix an existing build root:
BR=$YB_SRC_ROOT/build/release-clang21-full-lto-ninja
TC=$YB_LLVM_TOOLCHAIN_DIR/bin
sed -i "s|/usr/bin/llvm-ar|$TC/llvm-ar|g; s|/usr/bin/llvm-ranlib|$TC/llvm-ranlib|g" \
$BR/CMakeFiles/*/CMakeC{,XX}Compiler.cmakeOr add the toolchain to PATH before the first CMake run in a new build root:
export PATH=$YB_LLVM_TOOLCHAIN_DIR/bin:$PATHIssue Type
kind/bug
Warning: Please confirm that this issue does not contain any sensitive information
- I confirm this issue does not contain any sensitive information.
Source: yugabyte/yugabyte-db