Skip to content

Commit 29c22a4

Browse files
authored
Move installation of include headers into a separate script (NFC) (#552)
This change is not meant to change any functionality, only move some Bash-specific logic out of the `Makefile` into its own script: `install-include-headers.sh`. This reduces the perceived complexity of the `Makefile` but the complexity is still there, tucked away in this script. This script also has the advantage that it can be run separately if needed. This commit comes after a few different attempts at building up `Makefile` lists of headers to copy over along with the various locations they must be copied from. It is certainly possible to do this, but due to how we need to remove some headers from the list, it ends up being easier to just `cp` and then `rm`, which this script retains.
1 parent 33c2083 commit 29c22a4

File tree

2 files changed

+88
-107
lines changed

2 files changed

+88
-107
lines changed

Makefile

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -520,86 +520,6 @@ endif
520520
SYSROOT_INC = $(SYSROOT)/include/$(TARGET_TRIPLE)
521521
SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE)
522522

523-
# Files from musl's include directory that we don't want to install in the
524-
# sysroot's include directory.
525-
MUSL_OMIT_HEADERS :=
526-
527-
# Remove files which aren't headers (we generate alltypes.h below).
528-
MUSL_OMIT_HEADERS += \
529-
"bits/syscall.h.in" \
530-
"bits/alltypes.h.in" \
531-
"alltypes.h.in"
532-
533-
# Use the compiler's version of these headers.
534-
MUSL_OMIT_HEADERS += \
535-
"stdarg.h" \
536-
"stddef.h"
537-
538-
# Use the WASI errno definitions.
539-
MUSL_OMIT_HEADERS += \
540-
"bits/errno.h"
541-
542-
# Remove headers that aren't supported yet or that aren't relevant for WASI.
543-
MUSL_OMIT_HEADERS += \
544-
"sys/procfs.h" \
545-
"sys/user.h" \
546-
"sys/kd.h" "sys/vt.h" "sys/soundcard.h" "sys/sem.h" \
547-
"sys/shm.h" "sys/msg.h" "sys/ipc.h" "sys/ptrace.h" \
548-
"sys/statfs.h" \
549-
"bits/kd.h" "bits/vt.h" "bits/soundcard.h" "bits/sem.h" \
550-
"bits/shm.h" "bits/msg.h" "bits/ipc.h" "bits/ptrace.h" \
551-
"bits/statfs.h" \
552-
"sys/vfs.h" \
553-
"syslog.h" "sys/syslog.h" \
554-
"wait.h" "sys/wait.h" \
555-
"ucontext.h" "sys/ucontext.h" \
556-
"paths.h" \
557-
"utmp.h" "utmpx.h" \
558-
"lastlog.h" \
559-
"sys/acct.h" \
560-
"sys/cachectl.h" \
561-
"sys/epoll.h" "sys/reboot.h" "sys/swap.h" \
562-
"sys/sendfile.h" "sys/inotify.h" \
563-
"sys/quota.h" \
564-
"sys/klog.h" \
565-
"sys/fsuid.h" \
566-
"sys/io.h" \
567-
"sys/prctl.h" \
568-
"sys/mtio.h" \
569-
"sys/mount.h" \
570-
"sys/fanotify.h" \
571-
"sys/personality.h" \
572-
"elf.h" "link.h" "bits/link.h" \
573-
"scsi/scsi.h" "scsi/scsi_ioctl.h" "scsi/sg.h" \
574-
"sys/auxv.h" \
575-
"pwd.h" "shadow.h" "grp.h" \
576-
"mntent.h" \
577-
"resolv.h" \
578-
"pty.h" \
579-
"ulimit.h" \
580-
"sys/xattr.h" \
581-
"wordexp.h" \
582-
"spawn.h" \
583-
"sys/membarrier.h" \
584-
"sys/signalfd.h" \
585-
"termios.h" \
586-
"sys/termios.h" \
587-
"bits/termios.h" \
588-
"net/if.h" \
589-
"net/if_arp.h" \
590-
"net/ethernet.h" \
591-
"net/route.h" \
592-
"netinet/if_ether.h" \
593-
"netinet/ether.h" \
594-
"sys/timerfd.h" \
595-
"libintl.h" \
596-
"sys/sysmacros.h" \
597-
"aio.h"
598-
599-
ifeq ($(WASI_SNAPSHOT), p1)
600-
MUSL_OMIT_HEADERS += "netdb.h"
601-
endif
602-
603523
default: finish
604524

605525
LIBC_SO_OBJS = $(patsubst %.o,%.pic.o,$(filter-out $(MUSL_PRINTSCAN_OBJS),$(LIBC_OBJS)))
@@ -811,33 +731,8 @@ $(INCLUDE_DIRS): $(ALL_POSSIBLE_HEADERS)
811731
#
812732
# Install the include files.
813733
#
814-
mkdir -p "$(SYSROOT_INC)"
815-
cp -r "$(LIBC_BOTTOM_HALF_HEADERS_PUBLIC)"/* "$(SYSROOT_INC)"
816-
817-
# Generate musl's bits/alltypes.h header.
818-
mkdir -p "$(SYSROOT_INC)/bits"
819-
sed -f $(LIBC_TOP_HALF_MUSL_DIR)/tools/mkalltypes.sed \
820-
$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32/bits/alltypes.h.in \
821-
$(LIBC_TOP_HALF_MUSL_DIR)/include/alltypes.h.in \
822-
> "$(SYSROOT_INC)/bits/alltypes.h"
823-
824-
# Copy in the bulk of musl's public header files.
825-
cp -r "$(LIBC_TOP_HALF_MUSL_INC)"/* "$(SYSROOT_INC)"
826-
827-
# Copy in the musl's "bits" header files.
828-
cp -r "$(LIBC_TOP_HALF_MUSL_DIR)"/arch/generic/bits/* "$(SYSROOT_INC)/bits"
829-
cp -r "$(LIBC_TOP_HALF_MUSL_DIR)"/arch/wasm32/bits/* "$(SYSROOT_INC)/bits"
830-
831-
# Copy in the fts header files.
832-
cp "$(MUSL_FTS_SRC_DIR)/fts.h" "$(SYSROOT_INC)/fts.h"
833-
834-
# Remove selected header files.
835-
$(RM) $(patsubst %,$(SYSROOT_INC)/%,$(MUSL_OMIT_HEADERS))
836-
ifeq ($(WASI_SNAPSHOT), p2)
837-
printf '#ifndef __wasilibc_use_wasip2\n#define __wasilibc_use_wasip2\n#endif\n' \
838-
> "$(SYSROOT_INC)/__wasi_snapshot.h"
839-
endif
840-
734+
SYSROOT_INC=$(SYSROOT_INC) TARGET_TRIPLE=$(TARGET_TRIPLE) \
735+
$(CURDIR)/scripts/install-include-headers.sh
841736
# Stamp the include installation.
842737
@mkdir -p $(@D)
843738
touch $@

scripts/install-include-headers.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)