Skip to content

Commit

Permalink
use separate include dirs for each target
Browse files Browse the repository at this point in the history
This allows each target to have a different set of header files under the same
sysroot.

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej committed Dec 12, 2023
1 parent 6be1e1a commit 24c7bb7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ jobs:

- name: Build libc
shell: bash
run: make -j4
run: |
make -j4
WASI_SNAPSHOT=preview2 make -j4
- name: Test
shell: bash
Expand All @@ -98,6 +100,8 @@ jobs:
mkdir -p $WASI_DIR
cp download/lib/wasi/libclang_rt.builtins-wasm32.a $WASI_DIR
make test
rm -r build
WASI_SNAPSHOT=preview2 make test
# The older version of Clang does not provide the expected symbol for the
# test entrypoints: `undefined symbol: __main_argc_argv`.
# The older (<15.0.7) version of wasm-ld does not provide `__heap_end`,
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES))
# These variables describe the locations of various files and
# directories in the generated sysroot tree.
SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE)
SYSROOT_INC = $(SYSROOT)/include
SYSROOT_INC = $(SYSROOT)/include/$(TARGET_TRIPLE)
SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE)

# Files from musl's include directory that we don't want to install in the
Expand Down Expand Up @@ -681,6 +681,10 @@ include_dirs:

# Remove selected header files.
$(RM) $(patsubst %,$(SYSROOT_INC)/%,$(MUSL_OMIT_HEADERS))
ifeq ($(WASI_SNAPSHOT), preview2)
printf '#ifndef __wasilibc_use_preview2\n#define __wasilibc_use_preview2\n#endif\n' \
> "$(SYSROOT_INC)/__wasi_snapshot.h"
endif

startup_files: include_dirs $(LIBC_BOTTOM_HALF_CRT_OBJS)
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ __attribute__((__import_module__("wasi_snapshot_preview1"), __import_name__("ada

static bool wasi_preview1_adapter_open_badfd(int* fd)
{
return __wasi_preview1_adapter_open_badfd((int32_t)fd) != 0;
return __wasi_preview1_adapter_open_badfd((int32_t)fd) == 0;
}

__attribute__((__import_module__("wasi_snapshot_preview1"), __import_name__("adapter_close_badfd"))) extern int32_t __wasi_preview1_adapter_close_badfd(int32_t);

static bool wasi_preview1_adapter_close_badfd(int fd)
{
return __wasi_preview1_adapter_close_badfd(fd) != 0;
return __wasi_preview1_adapter_close_badfd(fd) == 0;
}

/*
Expand Down
1 change: 1 addition & 0 deletions libc-bottom-half/headers/public/__header_sys_socket.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __wasilibc___header_sys_socket_h
#define __wasilibc___header_sys_socket_h

#include <__wasi_snapshot.h>
#include <__struct_msghdr.h>
#include <__struct_sockaddr.h>
#include <__struct_sockaddr_storage.h>
Expand Down
5 changes: 5 additions & 0 deletions libc-bottom-half/headers/public/__wasi_snapshot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* This is file is (practically) empty by default. The Makefile will replace it
with a non-empty version that defines `__wasilibc_use_preview2` if targeting
`wasm32-wasi-preview2`.
*/

3 changes: 3 additions & 0 deletions libc-top-half/musl/include/sys/socket.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifndef _SYS_SOCKET_H
#define _SYS_SOCKET_H

#include <__wasi_snapshot.h>

#ifdef __wasilibc_unmodified_upstream /* Use alternate WASI libc headers */
#else
#include <__header_sys_socket.h>
Expand Down
39 changes: 35 additions & 4 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,29 @@ test: run
OBJDIR ?= $(CURDIR)/build
DOWNDIR ?= $(CURDIR)/download

# preview1 or preview2
WASI_SNAPSHOT ?= preview1

##### DOWNLOAD #################################################################

LIBC_TEST_URL ?= https://github.com/bytecodealliance/libc-test
LIBC_TEST = $(DOWNDIR)/libc-test
LIBRT_URL ?= https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-16/libclang_rt.builtins-wasm32-wasi-16.0.tar.gz
LIBRT = $(DOWNDIR)/lib/wasi/libclang_rt.builtins-wasm32.a
WASMTIME_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/v3.0.0/wasmtime-v3.0.0-x86_64-linux.tar.xz
# TODO: switch to Wasmtime 16 once it's released
WASMTIME_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/dev/wasmtime-dev-x86_64-linux.tar.xz
WASMTIME = $(DOWNDIR)/$(shell basename $(WASMTIME_URL) .tar.xz)/wasmtime
WASM_TOOLS_URL ?= https://github.com/bytecodealliance/wasm-tools/releases/download/wasm-tools-1.0.54/wasm-tools-1.0.54-x86_64-linux.tar.gz
WASM_TOOLS = $(DOWNDIR)/$(shell basename $(WASM_TOOLS_URL) .tar.gz)/wasm-tools
ADAPTER_URL ?= https://github.com/bytecodealliance/wasmtime/releases/download/dev/wasi_snapshot_preview1.command.wasm
ADAPTER = $(DOWNDIR)/wasi_snapshot_preview1.command.wasm

TO_DOWNLOAD = $(LIBC_TEST) $(LIBRT) $(WASMTIME)
ifeq ($(WASI_SNAPSHOT), preview2)
TO_DOWNLOAD += $(ADAPTER) $(WASM_TOOLS)
endif

download: $(LIBC_TEST) $(LIBRT) $(WASMTIME)
download: $(TO_DOWNLOAD)

$(DOWNDIR):
mkdir -p download
Expand All @@ -42,6 +55,13 @@ $(WASMTIME): | $(DOWNDIR)
wget --no-clobber --directory-prefix=$(DOWNDIR) $(WASMTIME_URL)
tar --extract --file=$(DOWNDIR)/$(shell basename $(WASMTIME_URL)) --directory=$(DOWNDIR)/

$(WASM_TOOLS): | $(DOWNDIR)
wget --no-clobber --directory-prefix=$(DOWNDIR) $(WASM_TOOLS_URL)
tar --extract --file=$(DOWNDIR)/$(shell basename $(WASM_TOOLS_URL)) --directory=$(DOWNDIR)/

$(ADAPTER): | $(DOWNDIR)
wget --no-clobber --directory-prefix=$(DOWNDIR) $(ADAPTER_URL)

clean::
rm -rf download

Expand Down Expand Up @@ -110,13 +130,18 @@ WASM_OBJS += $(INFRA_WASM_OBJS)
DIRS := $(patsubst $(OBJDIR)/%/,%,$(sort $(dir $(WASM_OBJS))))
OBJDIRS := $(DIRS:%=$(OBJDIR)/%)

TARGET_TRIPLE = wasm32-wasi
ifeq ($(WASI_SNAPSHOT), preview2)
TARGET_TRIPLE = wasm32-wasi-preview2
endif

# Allow $(CC) to be set from the command line; ?= doesn't work for CC because
# make has a default value for it.
ifeq ($(origin CC), default)
CC := clang
endif
LDFLAGS ?=
CFLAGS ?= --target=wasm32-wasi --sysroot=../sysroot
CFLAGS ?= --target=$(TARGET_TRIPLE) --sysroot=../sysroot
# Always include the `libc-test` infrastructure headers.
CFLAGS += -I$(LIBC_TEST)/src/common

Expand All @@ -129,7 +154,10 @@ build: download $(WASMS)

$(WASMS): | $(OBJDIRS)
$(OBJDIR)/%.wasm: $(OBJDIR)/%.wasm.o $(INFRA_WASM_OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
ifeq ($(WASI_SNAPSHOT), preview2)
$(WASM_TOOLS) component new --adapt $(ADAPTER) $@ -o $@
endif

$(WASM_OBJS): $(LIBC_TEST)/src/common/test.h | $(OBJDIRS)
$(OBJDIR)/%.wasm.o: $(LIBC_TEST)/src/%.c
Expand All @@ -144,6 +172,9 @@ clean::
##### RUN ######################################################################

ENGINE ?= $(WASMTIME) run
ifeq ($(WASI_SNAPSHOT), preview2)
ENGINE += --wasm component-model
endif
ERRS:=$(WASMS:%.wasm=%.wasm.err)

# Use the provided Wasm engine to execute each test, emitting its output into
Expand Down

0 comments on commit 24c7bb7

Please sign in to comment.