Detect musl at build time without a predefined macro
musl defines no identifying macro, so detect it by asking the compiler for its target triple with -dumpmachine, then fall back to a negative glibc test.
glibc defines __GLIBC__ in <features.h>. musl deliberately defines nothing equivalent. Its FAQ explains that code should test for the feature it needs rather than for the libc, so there is no __MUSL__ to check in a header. When a build script really must know, for example to pick a vendored backtrace library, the compiler's target triple is the most direct answer.
triple=$(${CC:-cc} -dumpmachine)
case "$triple" in
*-musl*) echo "libc: musl ($triple)" ;;
*)
if printf '#include <features.h>\n#ifdef __GLIBC__\nglibc\n#endif\n' \
| ${CC:-cc} -E -P -x c - | grep -qx glibc; then
echo "libc: glibc ($triple)"
else
echo "libc: unknown ($triple)"
fi ;;
esaclibc: musl (x86_64-alpine-linux-musl)The first branch works for Alpine and for the usual musl cross toolchains, whose triples end in -musl or -musleabihf. Some distributions name the triple without it, so the second branch asks the preprocessor whether __GLIBC__ is set. If neither says glibc, report unknown rather than assuming musl. In C source, prefer #ifdef on the feature, such as _GNU_SOURCE functions, and keep the libc test to the build system.