Skip to content

feat: add goffi_musl build tag for Alpine and other musl systems - #87

Open
unxed wants to merge 2 commits into
go-webgpu:mainfrom
unxed:feature/goffi-musl
Open

unxed wants to merge 2 commits into
go-webgpu:mainfrom
unxed:feature/goffi-musl

Conversation

@unxed

@unxed unxed commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Step 3 of the split agreed in #76, built on the goffi_static from #78.

A default goffi binary does not start on Alpine. Its PT_INTERP names the glibc loader, and its DT_NEEDED entries name libdl.so.2, libc.so.6 and libpthread.so.0. musl has neither: it ships one loader, /lib/ld-musl-<arch>.so.1, and one libc, libc.musl-<arch>.so.1. Both are fixed at link time, so the libc flavor has to be chosen at build time.

CGO_ENABLED=0 go build -tags goffi_musl \
    -gcflags=github.com/go-webgpu/goffi/internal/dl=-std ./...

What changes (linux/amd64 and linux/arm64 only):

  • internal/dl, internal/syscall and internal/fakecgo get musl flavors of their cgo_import_dynamic directives. The glibc libdl imports move from dl_linux.go to dl_linux_glibc.go, so the RTLD_* constants stay shared.
  • internal/dl sets the musl loader with //go:cgo_dynamic_linker. The compiler accepts that directive only in cgo-generated code, which is why the build needs the -gcflags line. If you leave the flag out, the compile fails and names the directive.
  • pthread_get_stacksize_np is left out of the musl set. It is a Darwin-only API. glibc binds lazily and never calls it, but musl binds every import at load time and would refuse to start the binary. The linker already drops its trampoline on Linux.
  • gen.go generates the fakecgo musl files from the same symbol tables as the glibc ones.
  • When both tags are set, goffi_static takes precedence. goffi_musl has no effect off Linux. FFI is fully available under goffi_musl.

Tests:

  • TestMuslDirectiveParity checks that the glibc and musl symbol sets match, apart from the one intended exclusion. It also checks the per-arch SONAMEs and the interpreter.
  • TestMuslLinkArtifacts builds linux/amd64 and linux/arm64 probes and reads PT_INTERP and DT_NEEDED with debug/elf.
  • cmd/musl-probe runs in an Alpine container (scripts/check-musl.sh, new musl-build CI job on Go 1.25 and 1.26). It covers dlopen/dlsym, integer and float calls, errno, a qsort callback, and a goroutine hammer that makes the runtime start OS threads through fakecgo's pthread imports.

Docs: docs/MUSL.md, the README linking-modes table and CHANGELOG.

The Android arm64 jobs fail in "Set up Android SDK" before any goffi code runs. That failure is unrelated to this PR and is fixed by #85.

🤖 Generated with Claude Code

Both Android arm64 jobs now fail in "Set up Android SDK", before any goffi
code runs:

    Warning: Failed to find package 'tools'
    Error: The process '/usr/local/lib/android/sdk/cmdline-tools/16.0/bin/sdkmanager' failed with exit code 1

android-actions/setup-android@v3 defaults its `packages` input to
`tools platform-tools` and runs `sdkmanager <pkg>` for each entry. Google's
SDK repository index (repository2-3.xml) no longer contains a `tools`
package, while `platform-tools` and `cmdline-tools` are still listed, so
the `tools` call exits 1. The same breakage is tracked upstream in
android-actions/setup-android#537. The last green run of these jobs on main
was for c8f74c6 on 2026-09-10; the workflow has not changed since, and the
failure is identical on a docs-only PR.

Nothing in this workflow or in scripts/check-android-arm64.sh uses the
`tools` package. Pass `packages: platform-tools`, which keeps everything
the step installed before except the package that no longer exists. The
action still accepts licenses, exports ANDROID_HOME and puts sdkmanager on
PATH, which the following NDK step relies on.
@unxed
unxed requested a review from kolkov as a code owner September 25, 2026 14:15
@codecov

codecov Bot commented Sep 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

A default goffi binary cannot start on Alpine: PT_INTERP names the glibc
loader, which musl systems do not have, and the cgo_import_dynamic
directives name libdl.so.2, libc.so.6 and libpthread.so.0, none of which
musl ships. Its whole POSIX surface lives in one arch-named object,
libc.musl-<arch>.so.1. Both facts are baked into the ELF at link time, so
the libc flavor is a build-time choice.

The goffi_musl tag selects musl flavors of the three directive groups
(internal/dl, internal/syscall, internal/fakecgo) and bakes the musl
loader path into PT_INTERP via //go:cgo_dynamic_linker. That directive is
restricted to cgo-generated code, so musl builds pass
-gcflags=github.com/go-webgpu/goffi/internal/dl=-std. Forgetting the flag
is a compile error naming the directive, not a binary that dies at
startup with a confusing ENOENT.

The glibc libdl imports move out of dl_linux.go into dl_linux_glibc.go so
the RTLD_* constants stay shared by both flavors.

One symbol is dropped from the musl set: pthread_get_stacksize_np is a
Darwin-only API that glibc's lazy PLT tolerates but musl's immediate
binding would reject at load time. Its trampoline is only reachable from
the Darwin thread-entry path, so the linker drops it on Linux. The
fakecgo musl files are generated by gen.go from the same symbol tables as
the glibc ones.

goffi_static wins over goffi_musl; the tag is inert off Linux.

Verification:
- TestMuslDirectiveParity pins glibc/musl symbol-set parity (including
  the one intentional exclusion), per-arch SONAMEs and the interpreter.
- TestMuslLinkArtifacts builds linux/{amd64,arm64} probes and checks
  PT_INTERP and DT_NEEDED with debug/elf.
- cmd/musl-probe runs against a real musl libc: dlopen/dlsym, float and
  integer calls, errno capture, qsort with a Go callback, and a
  64-goroutine hammer that makes the runtime create OS threads through
  fakecgo's pthread imports. scripts/check-musl.sh runs it inside Alpine
  and is wired into CI.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@unxed
unxed force-pushed the feature/goffi-musl branch from 34042b8 to 1e5844a Compare September 25, 2026 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant