Skip to content

wasi-libc: Deduplicate sources and headers with regards to upstream musl - #22905

Merged
alexrp merged 1 commit into
ziglang:masterfrom
alexrp:trim-libc-fat
Feb 21, 2025
Merged

alexrp merged 1 commit into
ziglang:masterfrom
alexrp:trim-libc-fat

Conversation

@alexrp

@alexrp alexrp commented Feb 16, 2025

Copy link
Copy Markdown
Member

Unfortunately some duplicate files must remain in lib/libc/wasi/libc-top-half because they include internal headers in the same directory which have edits relative to upstream musl. Because C is an amazing language, there is no way to make it so that e.g. upstream musl's src/stdio/fputc.c includes wasi-libc's src/stdio/putc.h instead of the upstream putc.h. The preprocessor always searches the current directory first for quote includes.

Anyway, this still takes us from 2.9M to 1.4M for the combination of lib/libc/wasi and lib/libc/include/wasm-wasi-musl, so I still call it a win.

Updated instructions will follow soon on the wiki page.

@alexrp

alexrp commented Feb 16, 2025 •

Copy link
Copy Markdown
Member Author

I should note that the way we have things set up right now with regards to lib/libc/include/generic-musl and lib/libc/include/wasm-wasi-musl is very weird. You can, for example, #include <elf.h> when targeting wasm32-wasi-musl because elf.h is provided in the former directory, even though upstream wasi-libc does not provide it. This situation is really the only reason why deduplicating headers between the two directories makes sense at all. If we want to instead prevent lib/libc/include/generic-musl from 'infecting' wasm32-wasi-musl, then I can revert this portion of the PR and limit deduplication to source files only.

List of headers from generic-musl infecting wasm32-wasi-musl
Only in lib/libc/include/generic-musl: aio.h
Only in lib/libc/include/generic-musl/bits: errno.h
Only in lib/libc/include/generic-musl/bits: ipc.h
Only in lib/libc/include/generic-musl/bits: kd.h
Only in lib/libc/include/generic-musl/bits: link.h
Only in lib/libc/include/generic-musl/bits: msg.h
Only in lib/libc/include/generic-musl/bits: ptrace.h
Only in lib/libc/include/generic-musl/bits: sem.h
Only in lib/libc/include/generic-musl/bits: shm.h
Only in lib/libc/include/generic-musl/bits: soundcard.h
Only in lib/libc/include/generic-musl/bits: statfs.h
Only in lib/libc/include/generic-musl/bits: termios.h
Only in lib/libc/include/generic-musl/bits: user.h
Only in lib/libc/include/generic-musl/bits: vt.h
Only in lib/libc/include/generic-musl: elf.h
Only in lib/libc/include/generic-musl: grp.h
Only in lib/libc/include/generic-musl: lastlog.h
Only in lib/libc/include/generic-musl: libintl.h
Only in lib/libc/include/generic-musl: link.h
Only in lib/libc/include/generic-musl: mntent.h
Only in lib/libc/include/generic-musl: net
Only in lib/libc/include/generic-musl: netdb.h
Only in lib/libc/include/generic-musl/netinet: ether.h
Only in lib/libc/include/generic-musl/netinet: if_ether.h
Only in lib/libc/include/generic-musl: paths.h
Only in lib/libc/include/generic-musl: pthread.h
Only in lib/libc/include/generic-musl: pty.h
Only in lib/libc/include/generic-musl: pwd.h
Only in lib/libc/include/generic-musl: resolv.h
Only in lib/libc/include/generic-musl: scsi
Only in lib/libc/include/generic-musl: shadow.h
Only in lib/libc/include/generic-musl: spawn.h
Only in lib/libc/include/generic-musl: stdarg.h
Only in lib/libc/include/generic-musl: stddef.h
Only in lib/libc/include/generic-musl/sys: acct.h
Only in lib/libc/include/generic-musl/sys: auxv.h
Only in lib/libc/include/generic-musl/sys: cachectl.h
Only in lib/libc/include/generic-musl/sys: epoll.h
Only in lib/libc/include/generic-musl/sys: fanotify.h
Only in lib/libc/include/generic-musl/sys: fsuid.h
Only in lib/libc/include/generic-musl/sys: inotify.h
Only in lib/libc/include/generic-musl/sys: io.h
Only in lib/libc/include/generic-musl/sys: ipc.h
Only in lib/libc/include/generic-musl/sys: kd.h
Only in lib/libc/include/generic-musl/sys: klog.h
Only in lib/libc/include/generic-musl/sys: membarrier.h
Only in lib/libc/include/generic-musl/sys: mount.h
Only in lib/libc/include/generic-musl/sys: msg.h
Only in lib/libc/include/generic-musl/sys: mtio.h
Only in lib/libc/include/generic-musl/sys: personality.h
Only in lib/libc/include/generic-musl/sys: prctl.h
Only in lib/libc/include/generic-musl/sys: procfs.h
Only in lib/libc/include/generic-musl/sys: ptrace.h
Only in lib/libc/include/generic-musl/sys: quota.h
Only in lib/libc/include/generic-musl/sys: reboot.h
Only in lib/libc/include/generic-musl/sys: sem.h
Only in lib/libc/include/generic-musl/sys: sendfile.h
Only in lib/libc/include/generic-musl/sys: shm.h
Only in lib/libc/include/generic-musl/sys: signalfd.h
Only in lib/libc/include/generic-musl/sys: soundcard.h
Only in lib/libc/include/generic-musl/sys: statfs.h
Only in lib/libc/include/generic-musl/sys: swap.h
Only in lib/libc/include/generic-musl/sys: syslog.h
Only in lib/libc/include/generic-musl/sys: sysmacros.h
Only in lib/libc/include/generic-musl/sys: termios.h
Only in lib/libc/include/generic-musl/sys: timerfd.h
Only in lib/libc/include/generic-musl/sys: ucontext.h
Only in lib/libc/include/generic-musl/sys: user.h
Only in lib/libc/include/generic-musl/sys: vfs.h
Only in lib/libc/include/generic-musl/sys: vt.h
Only in lib/libc/include/generic-musl/sys: wait.h
Only in lib/libc/include/generic-musl/sys: xattr.h
Only in lib/libc/include/generic-musl: syslog.h
Only in lib/libc/include/generic-musl: termios.h
Only in lib/libc/include/generic-musl: ucontext.h
Only in lib/libc/include/generic-musl: ulimit.h
Only in lib/libc/include/generic-musl: utmp.h
Only in lib/libc/include/generic-musl: utmpx.h
Only in lib/libc/include/generic-musl: wait.h
Only in lib/libc/include/generic-musl: wordexp.h

@alexrp
alexrp requested review from andrewrk and jedisct1 February 16, 2025 02:53
@alexrp alexrp added this to the 0.14.0 milestone Feb 16, 2025

@andrewrk andrewrk 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.

I love it!

I don't consider the "infecting" thing to be a problem worth solving, because it's not going to break anything that otherwise compiles correctly directly against the libc in question, and I think the failure mode of accidentally importing something extra is not a realistic thing to worry about.

…usl.

Unfortunately some duplicate files must remain in lib/libc/wasi/libc-top-half
because they include internal headers *in the same directory* which have edits
relative to upstream musl. Because C is an amazing language, there is no way to
make it so that e.g. upstream musl's src/stdio/fputc.c includes wasi-libc's
src/stdio/putc.h instead of the upstream putc.h. The preprocessor always
searches the current directory first for quote includes.

Anyway, this still takes us from 2.9M to 1.4M for the combination of
lib/libc/wasi and lib/libc/include/wasm-wasi-musl, so I still call it a win.
@alexrp
alexrp enabled auto-merge (rebase) February 20, 2025 23:40
@alexrp
alexrp merged commit 05d8b56 into ziglang:master Feb 21, 2025
@alexrp
alexrp deleted the trim-libc-fat branch February 21, 2025 08:42
@alexrp alexrp removed this from the 0.14.0 milestone Feb 21, 2025
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.

2 participants