|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Install all the necessary necessary include files from wasi-libc |
| 4 | +# (`$WASI_LIBC`) into the sysroot include directory (`$SYSROOT_INC`). It uses a |
| 5 | +# passed target triple (`$TARGET_TRIPLE`) to condition some of the |
| 6 | +# copied/generated files. |
| 7 | +# |
| 8 | +# Usage: SYSROOT_INC=... TARGET_TRIPLE=... ./install-include-headers.sh |
| 9 | + |
| 10 | +set -euo pipefail |
| 11 | + |
| 12 | +PARENT_DIR=$(dirname $(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)) |
| 13 | +WASI_LIBC="${WASI_LIBC:-$PARENT_DIR}" |
| 14 | +if [[ -z "${SYSROOT_INC}" || -z "${TARGET_TRIPLE}" ]]; then |
| 15 | + echo "usage: SYSROOT_INC=... TARGET_TRIPLE=... ./install-include-headers.sh" |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | +# The commands are available for override to allow dry runs. |
| 19 | +CP="${CP:-cp -v}" |
| 20 | +RM="${RM:-rm -v}" |
| 21 | +MKDIR="${MKDIR:-mkdir}" |
| 22 | +SED="${SED:-sed}" |
| 23 | + |
| 24 | +# Copy in the bottom half's public headers. |
| 25 | +$MKDIR -p $SYSROOT_INC |
| 26 | +$CP -r $WASI_LIBC/libc-bottom-half/headers/public/* $SYSROOT_INC |
| 27 | + |
| 28 | +# Copy in the bulk of musl's public header files. |
| 29 | +$CP -r $WASI_LIBC/libc-top-half/musl/include/* $SYSROOT_INC |
| 30 | + |
| 31 | +# Copy in the musl's "bits" header files. |
| 32 | +$MKDIR -p $SYSROOT_INC/bits |
| 33 | +$CP -r $WASI_LIBC/libc-top-half/musl/arch/generic/bits/* $SYSROOT_INC/bits |
| 34 | +$CP -r $WASI_LIBC/libc-top-half/musl/arch/wasm32/bits/* $SYSROOT_INC/bits |
| 35 | + |
| 36 | +# Generate musl's bits/alltypes.h header. |
| 37 | +(set -x; $SED -f $WASI_LIBC/libc-top-half/musl/tools/mkalltypes.sed \ |
| 38 | + $WASI_LIBC/libc-top-half/musl/arch/wasm32/bits/alltypes.h.in \ |
| 39 | + $WASI_LIBC/libc-top-half/musl/include/alltypes.h.in \ |
| 40 | + > $SYSROOT_INC/bits/alltypes.h) |
| 41 | + |
| 42 | +# Copy in the fts header files. |
| 43 | +$CP $WASI_LIBC/fts/musl-fts/fts.h $SYSROOT_INC/fts.h |
| 44 | + |
| 45 | +# Remove selected header files. These are files from musl's include directory |
| 46 | +# that we don't want to install in the sysroot's include directory. |
| 47 | +MUSL_OMIT_HEADERS=() |
| 48 | +# Remove files which aren't headers (we generate `alltypes.h` above). |
| 49 | +MUSL_OMIT_HEADERS+=("bits/syscall.h.in" "bits/alltypes.h.in" "alltypes.h.in") |
| 50 | +# Use the compiler's version of these headers. |
| 51 | +MUSL_OMIT_HEADERS+=("stdarg.h" "stddef.h") |
| 52 | +# Use the WASI errno definitions. |
| 53 | +MUSL_OMIT_HEADERS+=("bits/errno.h") |
| 54 | +# Remove headers that aren't supported yet or that aren't relevant for WASI. |
| 55 | +MUSL_OMIT_HEADERS+=("sys/procfs.h" "sys/user.h" "sys/kd.h" "sys/vt.h" \ |
| 56 | + "sys/soundcard.h" "sys/sem.h" "sys/shm.h" "sys/msg.h" "sys/ipc.h" \ |
| 57 | + "sys/ptrace.h" "sys/statfs.h" "bits/kd.h" "bits/vt.h" "bits/soundcard.h" \ |
| 58 | + "bits/sem.h" "bits/shm.h" "bits/msg.h" "bits/ipc.h" "bits/ptrace.h" \ |
| 59 | + "bits/statfs.h" "sys/vfs.h" "syslog.h" "sys/syslog.h" "wait.h" \ |
| 60 | + "sys/wait.h" "ucontext.h" "sys/ucontext.h" "paths.h" "utmp.h" "utmpx.h" \ |
| 61 | + "lastlog.h" "sys/acct.h" "sys/cachectl.h" "sys/epoll.h" "sys/reboot.h" \ |
| 62 | + "sys/swap.h" "sys/sendfile.h" "sys/inotify.h" "sys/quota.h" "sys/klog.h" \ |
| 63 | + "sys/fsuid.h" "sys/io.h" "sys/prctl.h" "sys/mtio.h" "sys/mount.h" \ |
| 64 | + "sys/fanotify.h" "sys/personality.h" "elf.h" "link.h" "bits/link.h" \ |
| 65 | + "scsi/scsi.h" "scsi/scsi_ioctl.h" "scsi/sg.h" "sys/auxv.h" "pwd.h" \ |
| 66 | + "shadow.h" "grp.h" "mntent.h" "resolv.h" "pty.h" "ulimit.h" "sys/xattr.h" \ |
| 67 | + "wordexp.h" "spawn.h" "sys/membarrier.h" "sys/signalfd.h" "termios.h" \ |
| 68 | + "sys/termios.h" "bits/termios.h" "net/if.h" "net/if_arp.h" \ |
| 69 | + "net/ethernet.h" "net/route.h" "netinet/if_ether.h" "netinet/ether.h" \ |
| 70 | + "sys/timerfd.h" "libintl.h" "sys/sysmacros.h" "aio.h") |
| 71 | +# Exclude `netdb.h` from all of the p1 targets. |
| 72 | +if [[ $TARGET_TRIPLE == *"wasi" || $TARGET_TRIPLE == *"wasi-threads" || \ |
| 73 | + $TARGET_TRIPLE == *"wasip1" || $TARGET_TRIPLE == *"wasip1-threads" ]]; then |
| 74 | + MUSL_OMIT_HEADERS+=("netdb.h") |
| 75 | +fi |
| 76 | + |
| 77 | +# Remove all the `MUSL_OMIT_HEADERS` previously copied over. |
| 78 | +for OMIT_HEADER in "${MUSL_OMIT_HEADERS[@]}"; do |
| 79 | + $RM -f $SYSROOT_INC/$OMIT_HEADER |
| 80 | +done |
| 81 | + |
| 82 | +# Update the `__wasi_snapshot.h` with some additional p2 definitions. |
| 83 | +if [[ $TARGET_TRIPLE == *"p2" ]]; then |
| 84 | + printf '#ifndef __wasilibc_use_wasip2\n#define __wasilibc_use_wasip2\n#endif\n' \ |
| 85 | + > $SYSROOT_INC/__wasi_snapshot.h |
| 86 | +fi |
0 commit comments