Files
datastore/cmake/linux/toolchain-aarch64-musl.cmake
Konstantin BogdanovandClaude Fable 5 3e8e4ad35d Stop falling back to glibc sysroot headers in musl builds
The glibc userspace headers from the sysroot were reachable in every
compilation through clang's builtin sysroot search path, so any header
musl does not provide (or does not win in the include order) silently
fell back to glibc declarations without a link-time signal; the
execinfo.h stub was one manually-shadowed example. The AI reviewer
suggested narrowing the fallback to the kernel headers only.

Compile everything with -nostdlibinc and pass the musl include
directories explicitly in the toolchain flags. The kernel (uapi)
headers, the only part of the sysroot that must stay reachable, are
whitelisted through a directory of symlinks created at configure time
in cmake/musl.cmake. A header musl does not provide is now a compile
error: for example, #include <gnu/libc-version.h> fails with "file not
found", while execinfo.h resolves to the stub and linux/asm headers to
the whitelist.

The two target_include_directories(global-libs ...) blocks removed here
were inert: only INTERFACE_LINK_LIBRARIES of global-libs is forwarded
to global-group, so these directories never appeared on any compile
line; the fallback came from the builtin sysroot path, which
-nostdlibinc disables.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-03 01:39:24 +02:00

40 lines
2.3 KiB
CMake

# Toolchain for a fully static binary against musl libc built from sources (see contrib/musl-cmake).
# See linux/toolchain-x86_64.cmake for details about multiple load of toolchain file.
include_guard(GLOBAL)
set (CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set (CMAKE_SYSTEM_NAME "Linux")
set (CMAKE_SYSTEM_PROCESSOR "aarch64")
set (CMAKE_C_COMPILER_TARGET "aarch64-linux-musl")
set (CMAKE_CXX_COMPILER_TARGET "aarch64-linux-musl")
set (CMAKE_ASM_COMPILER_TARGET "aarch64-linux-musl")
# The glibc sysroot is used only for kernel headers (linux/, asm/, asm-generic/);
# musl's own headers are given higher priority via the -isystem flags below.
set (TOOLCHAIN_PATH "${CMAKE_CURRENT_LIST_DIR}/../../contrib/sysroot/linux-aarch64")
set (CMAKE_SYSROOT "${TOOLCHAIN_PATH}/aarch64-linux-gnu/libc")
# -nostdlibinc drops the sysroot's glibc userspace headers from the default search
# path; musl's headers are passed explicitly instead, and the kernel (uapi) headers
# are whitelisted through the kernel-headers symlink directory created in
# cmake/musl.cmake. These flags come after any target include directories, so they
# do not disturb the include order while building musl itself.
set (MUSL_SOURCE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../contrib/musl")
set (MUSL_STUB_INCLUDE_PATH "${CMAKE_CURRENT_LIST_DIR}/../../contrib/musl-cmake/include")
set (MUSL_INCLUDE_FLAGS "-nostdlibinc -isystem ${MUSL_STUB_INCLUDE_PATH} -isystem ${CMAKE_BINARY_DIR}/contrib/musl-cmake/include -isystem ${MUSL_SOURCE_PATH}/include -isystem ${MUSL_SOURCE_PATH}/arch/aarch64 -isystem ${MUSL_SOURCE_PATH}/arch/generic -isystem ${CMAKE_BINARY_DIR}/contrib/musl-cmake/kernel-headers")
# Make sure to ignore global clang configuration files which could influence the
# build environment using --no-default-config
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MUSL_INCLUDE_FLAGS} --no-default-config")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MUSL_INCLUDE_FLAGS} --no-default-config")
set (CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${MUSL_INCLUDE_FLAGS} --no-default-config")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --no-default-config")
set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --no-default-config")
set (USE_MUSL 1)
# musl's name for the target architecture: the arch/<MUSL_ARCH> directory in the musl sources.
set (MUSL_ARCH "aarch64")
add_definitions(-DUSE_MUSL=1 -D__MUSL__=1)