Files
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

35 lines
1.9 KiB
CMake

# Link musl libc built from sources (contrib/musl-cmake) into everything.
# The CRT startup files are wired into every executable's link line in
# cmake/linux/default_libs.cmake; see the ordering comment there.
#
# Headers: the musl include directories (stubs for headers musl lacks, generated
# headers, arch-specific bits/*.h, generic arch fallbacks, public headers) are
# PUBLIC on the `musl` target and reach every target through `global-group`; the
# toolchain file additionally passes them for code compiled outside the normal
# target graph (configure-time compile checks). The toolchain also compiles
# everything with -nostdlibinc, so the glibc userspace headers from the sysroot
# are never searched: a header musl does not provide is a compile error, not a
# silent fallback to glibc declarations.
target_link_libraries(global-libs INTERFACE musl)
# The only part of the glibc sysroot that must stay reachable is the Linux kernel
# (uapi) headers: linux/, asm/, asm-generic/ and a few peripheral directories.
# Expose them through a directory of symlinks so that the sysroot's userspace
# headers remain unreachable. The `asm` directory lives in the multiarch
# subdirectory on some sysroots and at the top level on others. The toolchain
# file puts this directory on the include path of every compilation.
set (MUSL_KERNEL_HEADERS_DIR "${CMAKE_BINARY_DIR}/contrib/musl-cmake/kernel-headers")
file (MAKE_DIRECTORY "${MUSL_KERNEL_HEADERS_DIR}")
foreach (dir asm asm-generic linux drm misc mtd rdma scsi sound video xen)
set (link "${MUSL_KERNEL_HEADERS_DIR}/${dir}")
if (IS_SYMLINK "${link}" AND NOT EXISTS "${link}")
file (REMOVE "${link}")
endif ()
foreach (base "${CMAKE_SYSROOT}/usr/include/${MUSL_ARCH}-linux-gnu" "${CMAKE_SYSROOT}/usr/include")
if (EXISTS "${base}/${dir}" AND NOT EXISTS "${link}")
file (CREATE_LINK "${base}/${dir}" "${link}" SYMBOLIC)
endif ()
endforeach ()
endforeach ()