]> git.proxmox.com Git - wasi-libc.git/blobdiff - Makefile
Use MUSL's `weak*` feature in bottom half (#306)
[wasi-libc.git] / Makefile
index b85495294258b6313c6608353eba3b00771cb586..26a4ae5a821b002d46ed719a5fddd7016fa711c3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,13 @@
 # These variables are specifically meant to be overridable via the make
 # command-line.
-CC ?= clang
+# ?= doesn't work for CC and AR because make has a default value for them.
+ifeq ($(origin CC), default)
+CC := clang
+endif
 NM ?= $(patsubst %clang,%llvm-nm,$(filter-out ccache sccache,$(CC)))
-AR ?= $(patsubst %clang,%llvm-ar,$(filter-out ccache sccache,$(CC)))
+ifeq ($(origin AR), default)
+AR = $(patsubst %clang,%llvm-ar,$(filter-out ccache sccache,$(CC)))
+endif
 EXTRA_CFLAGS ?= -O2 -DNDEBUG
 # The directory where we build the sysroot.
 SYSROOT ?= $(CURDIR)/sysroot
@@ -17,6 +22,13 @@ BUILD_LIBC_TOP_HALF ?= yes
 # The directory where we're store intermediate artifacts.
 OBJDIR ?= $(CURDIR)/build
 
+# When the length is no larger than this threshold, we consider the
+# overhead of bulk memory opcodes to outweigh the performance benefit,
+# and fall back to the original musl implementation. See
+# https://github.com/WebAssembly/wasi-libc/pull/263 for relevant
+# discussion
+BULK_MEMORY_THRESHOLD ?= 32
+
 # Variables from this point on are not meant to be overridable via the
 # make command-line.
 
@@ -133,6 +145,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
         env/unsetenv.c \
         unistd/posix_close.c \
         stat/futimesat.c \
+        legacy/getpagesize.c \
     ) \
     $(filter-out %/procfdname.c %/syscall.c %/syscall_ret.c %/vdso.c %/version.c, \
                  $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/*.c)) \
@@ -178,6 +191,10 @@ MUSL_PRINTSCAN_SOURCES = \
     $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfscanf.c \
     $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/strtod.c \
     $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/wcstod.c
+BULK_MEMORY_SOURCES = \
+    $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memcpy.c \
+    $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memmove.c \
+    $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memset.c
 LIBC_TOP_HALF_HEADERS_PRIVATE = $(LIBC_TOP_HALF_DIR)/headers/private
 LIBC_TOP_HALF_SOURCES = $(LIBC_TOP_HALF_DIR)/sources
 LIBC_TOP_HALF_ALL_SOURCES = \
@@ -248,6 +265,7 @@ endif
 MUSL_PRINTSCAN_OBJS = $(call objs,$(MUSL_PRINTSCAN_SOURCES))
 MUSL_PRINTSCAN_LONG_DOUBLE_OBJS = $(patsubst %.o,%.long-double.o,$(MUSL_PRINTSCAN_OBJS))
 MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS = $(patsubst %.o,%.no-floating-point.o,$(MUSL_PRINTSCAN_OBJS))
+BULK_MEMORY_OBJS = $(call objs,$(BULK_MEMORY_SOURCES))
 LIBWASI_EMULATED_MMAN_OBJS = $(call objs,$(LIBWASI_EMULATED_MMAN_SOURCES))
 LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS = $(call objs,$(LIBWASI_EMULATED_PROCESS_CLOCKS_SOURCES))
 LIBWASI_EMULATED_GETPID_OBJS = $(call objs,$(LIBWASI_EMULATED_GETPID_SOURCES))
@@ -379,6 +397,14 @@ $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS): CFLAGS += \
            -D__wasilibc_printscan_no_floating_point \
            -D__wasilibc_printscan_floating_point_support_option="\"remove -lc-printscan-no-floating-point from the link command\""
 
+# TODO: apply -mbulk-memory globally, once
+# https://github.com/llvm/llvm-project/issues/52618 is resolved
+$(BULK_MEMORY_OBJS): CFLAGS += \
+        -mbulk-memory
+
+$(BULK_MEMORY_OBJS): CFLAGS += \
+        -DBULK_MEMORY_THRESHOLD=$(BULK_MEMORY_THRESHOLD)
+
 $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
            -D_WASI_EMULATED_SIGNAL
 
@@ -402,7 +428,9 @@ $(DLMALLOC_OBJS): CFLAGS += \
 startup_files $(LIBC_BOTTOM_HALF_ALL_OBJS): CFLAGS += \
     -I$(LIBC_BOTTOM_HALF_HEADERS_PRIVATE) \
     -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC) \
-    -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
+    -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) \
+    -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
+    -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal
 
 $(LIBC_TOP_HALF_ALL_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
     -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
@@ -508,10 +536,10 @@ check-symbols: startup_files libc
            > "$(SYSROOT_LIB)/libc.imports"
 
        #
-       # Generate a test file that includes all public header files.
+       # Generate a test file that includes all public header files.
        #
        cd "$(SYSROOT_INC)" && \
-         for header in $$(find . -type f -not -name mman.h -not -name signal.h -not -name times.h -not -name resource.h |grep -v /bits/); do \
+         for header in $$(find . -type f -not -name mman.h -not -name signal.h -not -name times.h -not -name resource.h |grep -v /bits/ |grep -v /c++/); do \
              echo '#include <'$$header'>' | sed 's/\.\///' ; \
        done |LC_ALL=C sort >$(SYSROOT_SHARE)/include-all.c ; \
        cd - >/dev/null
@@ -535,6 +563,9 @@ check-symbols: startup_files libc
        @#
        @# TODO: Undefine __FLOAT128__ for now since it's not in clang 8.0.
        @# TODO: Filter out __FLT16_* for now, as not all versions of clang have these.
+       @# TODO: Filter out __NO_MATH_ERRNO_ and a few __*WIDTH__ that are new to clang 14.
+       @# TODO: clang defined __FLT_EVAL_METHOD__ until clang 15, so we force-undefine it
+       @# for older versions.
        $(CC) $(CFLAGS) "$(SYSROOT_SHARE)/include-all.c" \
            -isystem $(SYSROOT_INC) \
            -std=gnu17 \
@@ -553,8 +584,12 @@ check-symbols: startup_files libc
            -U__GNUC_PATCHLEVEL__ \
            -U__VERSION__ \
            -U__FLOAT128__ \
+           -U__NO_MATH_ERRNO__ \
+           -U__BITINT_MAXWIDTH__ \
+           -U__FLT_EVAL_METHOD__ -Wno-builtin-macro-redefined \
            | sed -e 's/__[[:upper:][:digit:]]*_ATOMIC_\([[:upper:][:digit:]_]*\)_LOCK_FREE/__compiler_ATOMIC_\1_LOCK_FREE/' \
            | grep -v '^#define __FLT16_' \
+           | grep -v '^#define __\(BOOL\|INT_\(LEAST\|FAST\)\(8\|16\|32\|64\)\|INT\|LONG\|LLONG\|SHRT\)_WIDTH__' \
            > "$(SYSROOT_SHARE)/predefined-macros.txt"
 
        # Check that the computed metadata matches the expected metadata.