Skip to content

Build musl from sources - #97452

Merged
thevar1able merged 72 commits into
masterfrom
use-musl
Jul 14, 2026
Merged

thevar1able merged 72 commits into
masterfrom
use-musl

Conversation

@thevar1able

@thevar1able thevar1able commented Feb 20, 2026 •

Copy link
Copy Markdown
Member

Relates: #75847

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Build musl from sources instead of using vendored binaries.

Add contrib/musl submodule and a CMake build system for it.
Modify toolchains, default_libs, and contrib CMake files to support
USE_MUSL flag for building ClickHouse with musl libc.

Assisted-by: Claude Sonnet 4.5 via GitHub Copilot
Add complete musl source file coverage and move native tool builds
to build-time custom commands so protoc/grpc_cpp_plugin are built
without musl's -nostartfiles flag.

Assisted-by: Claude Sonnet 4.5 via GitHub Copilot
Add USE_MUSL=1 to AMD_RELEASE, ARM_RELEASE, and AMD_MUSL CI build
configurations using the standard toolchain files instead of the
standalone musl toolchain. Add USE_MUSL support to the aarch64
toolchain, mirroring the x86_64 implementation.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
@clickhouse-gh

clickhouse-gh Bot commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [764dd83]

Summary: ✅


AI Review

Summary

This PR moves USE_MUSL builds from the vendored musl sysroot to an in-tree musl source build, adds musl-specific toolchain/header wiring, and updates native helper selection for protobuf/Rust/self-extracting build steps. Most of the earlier build-order and link-order problems appear fixed in the current code, but I still do not think the new aarch64 musl path has enough proof to merge.

Missing context / blind spots
  • ⚠️ I could not run a local configure/build check for the new musl toolchains in this environment because cmake is not installed here, so the only available proof for new toolchain entry points is the repository's own build wiring and CI discussion.
Findings

⚠️ Majors

  • [cmake/linux/toolchain-aarch64-musl.cmake:1] [dismissed by author -- https://github.com/Build musl from sources #97452#discussion_r3572463852] The PR still adds a brand-new aarch64 musl toolchain entry point and matching jemalloc config without routing any in-repo build through it. In the current code, ci/jobs/build_clickhouse.py still uses cmake/linux/toolchain-aarch64.cmake for ARM_RELEASE, so this new architecture-specific contract is unexercised at merge time. For a toolchain file that changes header search, linker startup files, and allocator config, "intentional but unused" is not enough proof.
    Suggested fix: either add the smallest CI/local configure+build evidence for cmake/linux/toolchain-aarch64-musl.cmake, or defer the new aarch64 musl-only files from this PR until that path is actually exercised.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: prove the new aarch64 musl toolchain path with at least one real build/configure invocation in-repo, or remove/defer that unexercised path from this PR.

@clickhouse-gh clickhouse-gh Bot added pr-build Pull request with build/testing/packaging improvement submodule changed At least one submodule changed in this PR. labels Feb 20, 2026
Reword a comment in contrib/llvm-project-cmake/CMakeLists.txt to not
mention 'check_symbol_exists', which triggers the cpp style check that
bans dynamic compiler checks in contrib cmake files.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
Add toolchain file and -DUSE_MUSL=1 to FastTest cmake configuration,
and add contrib/musl to the submodule list. This replaces the old
commented-out reference to toolchain-x86_64-musl.cmake.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
…mand

Revert the native tool build from build-time (add_custom_command) back to
configure-time (execute_process) to match the upstream approach. The
add_custom_command approach only declared a stamp file as OUTPUT but not the
actual native binary paths, causing ninja to fail with 'missing and no known
rule to make it' for any cross-compilation build (not just musl).

The condition is extended to also trigger native builds when USE_MUSL is set,
since musl's -nostartfiles breaks standalone executables like protoc.

Remove the now-unnecessary NATIVE_TOOLS_TARGET target-level dependencies from
all proto generator files, since with execute_process the native binaries
already exist on disk before ninja starts.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
Add 14 math functions (log, log2, exp, exp2, expm1, pow, floor, fabs,
scalbn, logf, fabsl, scalbnl, copysignl, fmodl) that were previously
provided by glibc but are needed when building with musl. Also add
explogxf.cpp as a shared dependency for exp/exp2/expm1.

Includes both the source files in the SRCS list and corresponding
x86_64 runtime dispatch entries for CPU-optimized variant selection.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
lexer_test uses plain add_executable instead of clickhouse_add_executable,
so it was missing musl CRT objects (crt1, crti, crtn) while the global
-nostartfiles flag prevented linking glibc's CRT. This caused the binary
to have no _start entry point, resulting in a segfault.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
Include all 232 musl math source files instead of a partial subset.
Previously 129 math files were excluded, assuming LLVM-libc would
provide them, but LLVM-libc only covers ~45 functions. This left 84
math functions (mostly float-precision variants like sinf, cosf, expf)
with no implementation, causing 'undefined symbol: nan' linker errors.

LLVM-libc's optimized versions still take precedence via link ordering
(linked before musl in global-libs).

Also fix ksigaction.h include-override for aarch64: only x86_64 has an
arch-specific ksigaction.h in musl; all other architectures should use
the generic version from src/internal/ksigaction.h.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
@thevar1able
thevar1able marked this pull request as draft February 20, 2026 06:07
Create include_linux_aarch64_musl with JEMALLOC_STRERROR_R_RETURNS_CHAR_WITH_GNU_SOURCE
undef'd and JEMALLOC_HAVE_PTHREAD_GETNAME_NP disabled. Without this, the aarch64
build uses the glibc header which enables GNU strerror_r (returns char*), but musl
always provides POSIX strerror_r (returns int), causing -Wint-conversion error in
malloc_io.c.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
With USE_MUSL, -nostartfiles is set globally but only clickhouse_add_executable
adds musl CRT objects (crt1, crti, crtn). Fix decompressor, parquet tools, and
perf_checksum which use plain add_executable and would fail to link with
'cannot find entry symbol _start'.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
With USE_MUSL, the target-built compressor binary cannot run on the host
because it's statically linked against musl. Extend the cross-compilation
check to also trigger when USE_MUSL is set, matching the native build
condition in the root CMakeLists.txt. This ensures the native-built
pre_compressor (without musl) is used on the host, while the target-built
decompressor (with musl) is passed separately for embedding.

Assisted-by: Claude Opus 4.6 via GitHub Copilot
Assisted-by: Claude Opus 4.6 via GitHub Copilot
Assisted-by: Claude Opus 4.6 via GitHub Copilot
# Conflicts:
#	ci/jobs/build_clickhouse.py
Assisted-by: Claude Opus 4.6 via GitHub Copilot
Assisted-by: Claude Opus 4.6 via GitHub Copilot
thevar1able added a commit that referenced this pull request Mar 9, 2026
Add 18 additional math functions from LLVM-libc: `exp`, `exp2`,
`expm1`, `fabs`, `fabsl`, `floor`, `fmodl`, `log`, `log2`,
`logf`, `pow`, `scalbn`, `scalbnl`, `copysignl`, `nan`, `nanf`,
`nanl`, and the `explogxf` shared constants. Also add corresponding
x86_64 runtime dispatch entries.

Taken from #97452

Assisted-by: Claude Opus 4.6 via GitHub Copilot
Comment thread cmake/build_clang_builtin.cmake Resolved
Comment thread contrib/corrosion-cmake/CMakeLists.txt Resolved
Comment thread contrib/corrosion-cmake/CMakeLists.txt Resolved

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

@alexey-milovidov alexey-milovidov self-assigned this Jul 7, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Only need to take care of ThreadSanitizer: data race (STID: 1464-31ed)...

@groeneai

groeneai commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

@alexey-milovidov STID 1464-31ed is the uniqExact two-level parallel-merge data race (use_count()-based COW TOCTOU in UniqExactSet), unrelated to this PR - the amd_tsan stress job is just the carrier.

It is already fixed on master by #109389 (merged 2026-07-07T10:49, commit 6984449), which replaces the racy use_count() guard with a monotonic is_shared atomic. The prior half was #108928.

This PR head (5793278, 2026-07-03) predates that merge, so the fix is not in the failing build. Merging master into this branch will pick it up and clear the race.

CI report with the race: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=97452&sha=57932788529d4d69be2910737c673ab325c2235f&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29

…pe flags in corrosion

The `USE_MUSL` branch in `contrib/corrosion-cmake/CMakeLists.txt` strips every
`-isystem ...musl...` entry from `CMAKE_*_FLAGS` and rebuilds them so that
libc++ headers precede musl's. It reconstructed only four of the six `-isystem`
directories that `cmake/linux/toolchain-*-musl.cmake` (`MUSL_INCLUDE_FLAGS`)
adds, dropping the stub-header directory `contrib/musl-cmake/include` (which
provides the `execinfo.h` stub) and the `kernel-headers` directory. As a result
Cargo `cc`/`cxx` builds under `USE_MUSL` saw a narrower header surface than the
rest of the tree. Reconstruct the full set, in the toolchain's order.

Also, the non-`DEBUG` branch hard-coded `CMAKE_*_FLAGS_RELWITHDEBINFO` for every
build type, so `Release` and `MinSizeRel` musl builds compiled the Cargo
`cc`/`cxx` shims with the wrong optimisation/debug profile. Mirror the
per-build-type selection used by the non-musl branch.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread contrib/musl-cmake/CMakeLists.txt Resolved
alexey-milovidov and others added 4 commits July 7, 2026 23:42
The AI review flagged a clean-parallel-build ordering hazard: `bits/alltypes.h`
and `bits/syscall.h` were produced by the `musl-headers` build target, but both
`cmake/linux/toolchain-*-musl.cmake` add `${CMAKE_BINARY_DIR}/contrib/musl-cmake/include`
to the global compile flags of every C/C++/ASM target via `MUSL_INCLUDE_FLAGS`.
Only `musl`, `musl-crt1`, `cxx`, `cxxabi` and `unwind` depend on `musl-headers`,
so any other target that transitively includes a musl libc header (e.g. via
`<stdint.h>` or `<sys/types.h>`) could start compiling before those files exist
and fail on a clean parallel build.

Generate both headers eagerly at configure time with `execute_process`/`file`,
mirroring how `version.h` is already produced, so they are unconditionally
present before any compilation starts. A `CMAKE_CONFIGURE_DEPENDS` entry re-runs
configure when the generation inputs change (e.g. after a musl submodule bump).
The generated output is byte-identical to the previous build-time generation.
Comment thread contrib/corrosion-cmake/CMakeLists.txt Resolved
thevar1able and others added 3 commits July 13, 2026 23:24
…eference

With musl built from sources, the `USE_MUSL` link no longer pulls in a
sysroot `libc.a` (`cmake/linux/default_libs.cmake` links the in-tree
`musl` target instead of `-lc`), so probing the toolchain with
`-print-file-name=libc.a` in `clickhouse_config_crate_flags` resolved to
nothing: `CH_RUST_LOCALIZE_MATHLIB` ended up empty and Rust's libm
exports not covered by `libllvmlibc` (`round`, `copysign`, `ceil`,
`rint`, `trunc`, ...) stayed global, free to shadow musl's
implementations in the final binary.

For `USE_MUSL`, use the built musl archive itself
($<TARGET_FILE:musl>) as the reference library for
`localize_rust_c_symbols.sh` and add a build dependency on the `musl`
target; keep the `-print-file-name` probe only for the glibc toolchains.

Co-Authored-By: Claude Fable 5 <[email protected]>
@thevar1able
thevar1able added this pull request to the merge queue Jul 14, 2026
Merged via the queue into master with commit 77c0473 Jul 14, 2026
347 of 348 checks passed
@thevar1able
thevar1able deleted the use-musl branch July 14, 2026 16:37
thevar1able added a commit that referenced this pull request Jul 14, 2026
The master merge (PR #97452) brought an `AND NOT USE_MUSL` clause into the
native-tools build guard, forcing a separate native sub-build whenever
USE_MUSL is set. That sub-build reuses the same compiler without a toolchain
file; on a host whose clang defaults to a musl target the sub-build is itself
a musl build, so it re-triggers the same guard and recurses forever, creating
build/*/native/native/native/... and never finishing configure. (Master's own
CI does not hit this because its clang defaults to glibc.)

Static musl tool binaries run natively, so a native host build (host == target)
never needs a separate native build regardless of libc. Restore the guard to
key only on host != target, as it was before the merge.

Co-Authored-By: Claude Fable 5 <[email protected]>
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 14, 2026
thevar1able added a commit that referenced this pull request Jul 15, 2026
The self-extracting packaging selected the native-build pre_compressor
when cross-compiling OR when USE_MUSL (an auto-merged hunk from master's
#97452, where musl is a cross-option). But the native sub-build is only
produced when actually cross-compiling (host != target); a native musl
host build has no native/ subtree, so every non-cross build failed at
packaging with "pre_compressor: not found" (all six arm_* and coverage
build failures at 1411c0c). A statically-linked musl pre_compressor
runs on the host, so tie the selection to the same condition as the
native-build guard, matching the protoc/grpc fix in 1411c0c.

Co-Authored-By: Claude Fable 5 <[email protected]>
thevar1able added a commit that referenced this pull request Aug 19, 2026
Review feedback: `cmake/musl.cmake` put the whole `${CMAKE_SYSROOT}/usr/include`
tree on the include path to reach the kernel (uapi) headers, so any header musl
does not provide could silently resolve to the glibc copy, hiding accidental
glibc dependencies inside a supposed musl build.

Port master's solution (from the PR #97452 review): compile everything with
-nostdlibinc, pass musl's include directories explicitly in the toolchain
files, and whitelist only the kernel header directories (linux/, asm/,
asm-generic/ and peripheral ones) through a symlink directory built at
configure time in `cmake/musl.cmake`. A header musl lacks is now a compile
error, not a silent fallback to glibc declarations.

Validated: full tsan-musl build (18164 targets, zero errors, the binary runs
and stays fully static), aarch64 cross configure + `musl`/`cxx`/`cxxabi`/
`unwind`, and the aarch64 native musl sub-build (protoc closure) - no
native-build recursion.

Co-Authored-By: Claude Fable 5 <[email protected]>
thevar1able added a commit that referenced this pull request Aug 20, 2026
`-nostdlibinc` correctly surfaced a glibc dependency: openldap's
`ac/fdset.h` includes `<bits/types.h>` when the pre-generated
`portable.h` defines `HAVE_BITS_TYPES_H`, and musl has no such header.
The `linux_x86_64` variant already guards the define with
`!defined(USE_MUSL)` (from PR #97452); `linux_aarch64` was missed because
master never builds aarch64 with musl. Apply the identical guard; openldap
then takes the plain `FD_SETSIZE` path, as on the other non-glibc platforms.

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Build `memcmp`, `memcpy`, `memmove`, `memset`, `bcmp` and `memmem` from
LLVM-libc into the existing `libllvmlibc` target (which already supplies the
math functions), replacing the previous hand-rolled `memcmp`.

The wrapper TU `contrib/libllvmlibc-cmake/string/mem_functions.cpp` includes
the LLVM-libc implementation files and marks each exported C symbol `weak`
via the `LLVM_LIBC_FUNCTION_ATTR_<name>` hook, so sanitizer interceptors can
override them and so the existing strong `memcpy` from
`base/glibc-compatibility/memcpy` keeps precedence. `memmem` is a strong
symbol.

On x86_64, `x86_64_mem_functions.cpp` provides `memcpy`/`memmove`/`memset`
with runtime AVX-512 dispatch (a lazily-resolved function pointer, the same
shape as a glibc IFUNC) plus an AVX-512/AVX2/scalar `memmem`. On aarch64,
`aarch64_mem_functions.cpp` adds a NEON `memmove` and `memmem`.

The library is built with `-ffreestanding -fno-builtin` so clang's loop-idiom
recognition does not rewrite the inner loops into calls to the very functions
being defined. SIMD tiers are selected from `X86_ARCH_LEVEL` (SSE2 / v3 /
v4 with `-mprefer-vector-width=512`).

Ported from the `use-musl` branch (PR ClickHouse#97452).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
subkanthi pushed a commit to subkanthi/ClickHouse that referenced this pull request Nov 18, 2026
Build musl libc from the contrib/musl submodule (fork branch ClickHouse/v1.2.6)
instead of using vendored binaries from the sysroot. The glibc sysroot is used
only for kernel headers; musl provides all libc headers, the static library and
the CRT objects. Supported on x86_64 and aarch64 via the toolchain-*-musl.cmake
toolchains.

Squashed content of ClickHouse#97452.

Co-Authored-By: Claude Fable 5 <[email protected]>
@thevar1able thevar1able mentioned this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-build Pull request with build/testing/packaging improvement pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants