13
13
SYSROOT ?= $(CURDIR)/sysroot
14
14
# A directory to install to for "make install".
15
15
INSTALL_DIR ?= /usr/local
16
# single or posix; note that pthread support is still a work-in-progress.
17
17
THREAD_MODEL ?= single
19
19
MALLOC_IMPL ?= dlmalloc
21
21
BUILD_LIBC_TOP_HALF ?= yes
22
# The directory where we're store intermediate artifacts.
23
OBJDIR ?= $(CURDIR)/build
22
# The directory where we will store intermediate artifacts.
23
OBJDIR ?= $(CURDIR)/build/$(TARGET_TRIPLE)
25
# When the length is no larger than this threshold, we consider the
26
# overhead of bulk memory opcodes to outweigh the performance benefit,
27
# and fall back to the original musl implementation. See
28
# https://github.com/WebAssembly/wasi-libc/pull/263 for relevant
30
BULK_MEMORY_THRESHOLD ?= 32
25
32
# Variables from this point on are not meant to be overridable via the
26
33
# make command-line.
28
# Set the target variables. Multiarch triples notably omit the vendor field,
29
# which happens to be what we do for the main target triple too.
35
# Set the default WASI target triple.
30
36
TARGET_TRIPLE = wasm32-wasi
31
MULTIARCH_TRIPLE = wasm32-wasi
38
# Threaded version necessitates a different traget, as objects from different
39
# targets can't be mixed together while linking.
40
ifeq ($(THREAD_MODEL), posix)
41
TARGET_TRIPLE = wasm32-wasi-threads
33
44
# These variables describe the locations of various files and directories in
54
68
LIBC_BOTTOM_HALF_ALL_SOURCES := $(LIBC_BOTTOM_HALF_ALL_SOURCES) $(LIBC_BOTTOM_HALF_SOURCES)/chdir.c
56
70
LIBWASI_EMULATED_MMAN_SOURCES = \
57
$(shell find $(LIBC_BOTTOM_HALF_DIR)/mman -name \*.c)
71
$(sort $(shell find $(LIBC_BOTTOM_HALF_DIR)/mman -name \*.c))
58
72
LIBWASI_EMULATED_PROCESS_CLOCKS_SOURCES = \
59
$(shell find $(LIBC_BOTTOM_HALF_DIR)/clocks -name \*.c)
73
$(sort $(shell find $(LIBC_BOTTOM_HALF_DIR)/clocks -name \*.c))
60
74
LIBWASI_EMULATED_GETPID_SOURCES = \
61
$(shell find $(LIBC_BOTTOM_HALF_DIR)/getpid -name \*.c)
75
$(sort $(shell find $(LIBC_BOTTOM_HALF_DIR)/getpid -name \*.c))
62
76
LIBWASI_EMULATED_SIGNAL_SOURCES = \
63
$(shell find $(LIBC_BOTTOM_HALF_DIR)/signal -name \*.c)
77
$(sort $(shell find $(LIBC_BOTTOM_HALF_DIR)/signal -name \*.c))
64
78
LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES = \
65
79
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/signal/psignal.c \
66
80
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/strsignal.c
176
193
%/cimagf.c %/cimag.c %cimagl.c, \
177
194
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \
178
195
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c)
197
ifeq ($(THREAD_MODEL), posix)
198
LIBC_TOP_HALF_MUSL_SOURCES += \
199
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
203
stdio/ftrylockfile.c \
204
stdio/funlockfile.c \
207
thread/__timedwait.c \
208
thread/default_attr.c \
209
thread/pthread_attr_destroy.c \
210
thread/pthread_attr_get.c \
211
thread/pthread_attr_init.c \
212
thread/pthread_attr_setstack.c \
213
thread/pthread_attr_setdetachstate.c \
214
thread/pthread_attr_setstacksize.c \
215
thread/pthread_barrier_destroy.c \
216
thread/pthread_barrier_init.c \
217
thread/pthread_barrier_wait.c \
218
thread/pthread_cleanup_push.c \
219
thread/pthread_cond_broadcast.c \
220
thread/pthread_cond_destroy.c \
221
thread/pthread_cond_init.c \
222
thread/pthread_cond_signal.c \
223
thread/pthread_cond_timedwait.c \
224
thread/pthread_cond_wait.c \
225
thread/pthread_condattr_destroy.c \
226
thread/pthread_condattr_init.c \
227
thread/pthread_condattr_setclock.c \
228
thread/pthread_condattr_setpshared.c \
229
thread/pthread_create.c \
230
thread/pthread_detach.c \
231
thread/pthread_equal.c \
232
thread/pthread_getspecific.c \
233
thread/pthread_join.c \
234
thread/pthread_key_create.c \
235
thread/pthread_mutex_consistent.c \
236
thread/pthread_mutex_destroy.c \
237
thread/pthread_mutex_init.c \
238
thread/pthread_mutex_getprioceiling.c \
239
thread/pthread_mutex_lock.c \
240
thread/pthread_mutex_timedlock.c \
241
thread/pthread_mutex_trylock.c \
242
thread/pthread_mutex_unlock.c \
243
thread/pthread_mutexattr_destroy.c \
244
thread/pthread_mutexattr_init.c \
245
thread/pthread_mutexattr_setprotocol.c \
246
thread/pthread_mutexattr_setpshared.c \
247
thread/pthread_mutexattr_setrobust.c \
248
thread/pthread_mutexattr_settype.c \
249
thread/pthread_once.c \
250
thread/pthread_rwlock_destroy.c \
251
thread/pthread_rwlock_init.c \
252
thread/pthread_rwlock_rdlock.c \
253
thread/pthread_rwlock_timedrdlock.c \
254
thread/pthread_rwlock_timedwrlock.c \
255
thread/pthread_rwlock_tryrdlock.c \
256
thread/pthread_rwlock_trywrlock.c \
257
thread/pthread_rwlock_unlock.c \
258
thread/pthread_rwlock_wrlock.c \
259
thread/pthread_rwlockattr_destroy.c \
260
thread/pthread_rwlockattr_init.c \
261
thread/pthread_rwlockattr_setpshared.c \
262
thread/pthread_setcancelstate.c \
263
thread/pthread_setspecific.c \
264
thread/pthread_self.c \
265
thread/pthread_testcancel.c \
266
thread/sem_destroy.c \
267
thread/sem_getvalue.c \
270
thread/sem_timedwait.c \
271
thread/sem_trywait.c \
273
thread/wasm32/wasi_thread_start.s \
179
277
MUSL_PRINTSCAN_SOURCES = \
180
278
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/floatscan.c \
181
279
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfprintf.c \
183
281
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfscanf.c \
184
282
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/strtod.c \
185
283
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/wcstod.c
284
BULK_MEMORY_SOURCES = \
285
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memcpy.c \
286
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memmove.c \
287
$(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memset.c
186
288
LIBC_TOP_HALF_HEADERS_PRIVATE = $(LIBC_TOP_HALF_DIR)/headers/private
187
289
LIBC_TOP_HALF_SOURCES = $(LIBC_TOP_HALF_DIR)/sources
188
290
LIBC_TOP_HALF_ALL_SOURCES = \
189
291
$(LIBC_TOP_HALF_MUSL_SOURCES) \
190
$(shell find $(LIBC_TOP_HALF_SOURCES) -name \*.c)
292
$(sort $(shell find $(LIBC_TOP_HALF_SOURCES) -name \*.[cs]))
192
294
# Add any extra flags
193
295
CFLAGS = $(EXTRA_CFLAGS)
194
296
# Set the target.
195
297
CFLAGS += --target=$(TARGET_TRIPLE)
298
ASMFLAGS += --target=$(TARGET_TRIPLE)
196
299
# WebAssembly floating-point match doesn't trap.
197
300
# TODO: Add -fno-signaling-nans when the compiler supports it.
198
301
CFLAGS += -fno-trapping-math
215
318
CFLAGS += -mthread-model single
217
320
ifeq ($(THREAD_MODEL), posix)
218
CFLAGS += -mthread-model posix -pthread
321
# Specify the tls-model until LLVM 15 is released (which should contain
322
# https://reviews.llvm.org/D130053).
323
CFLAGS += -mthread-model posix -pthread -ftls-model=local-exec
325
# Include cloudlib's directory to access the structure definition of clockid_t
326
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
221
329
# Expose the public headers to the implementation. We use `-isystem` for
235
343
# These variables describe the locations of various files and directories in
236
344
# the build tree.
237
345
objs = $(patsubst $(CURDIR)/%.c,$(OBJDIR)/%.o,$(1))
346
asmobjs = $(patsubst $(CURDIR)/%.s,$(OBJDIR)/%.o,$(1))
238
347
DLMALLOC_OBJS = $(call objs,$(DLMALLOC_SOURCES))
348
EMMALLOC_OBJS = $(call objs,$(EMMALLOC_SOURCES))
239
349
LIBC_BOTTOM_HALF_ALL_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_ALL_SOURCES))
240
LIBC_TOP_HALF_ALL_OBJS = $(call objs,$(LIBC_TOP_HALF_ALL_SOURCES))
350
LIBC_TOP_HALF_ALL_OBJS = $(call asmobjs,$(call objs,$(LIBC_TOP_HALF_ALL_SOURCES)))
241
351
ifeq ($(MALLOC_IMPL),dlmalloc)
242
352
LIBC_OBJS += $(DLMALLOC_OBJS)
353
else ifeq ($(MALLOC_IMPL),emmalloc)
354
LIBC_OBJS += $(EMMALLOC_OBJS)
243
355
else ifeq ($(MALLOC_IMPL),none)
244
356
# No object files to add.
254
366
MUSL_PRINTSCAN_OBJS = $(call objs,$(MUSL_PRINTSCAN_SOURCES))
255
367
MUSL_PRINTSCAN_LONG_DOUBLE_OBJS = $(patsubst %.o,%.long-double.o,$(MUSL_PRINTSCAN_OBJS))
256
368
MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS = $(patsubst %.o,%.no-floating-point.o,$(MUSL_PRINTSCAN_OBJS))
369
BULK_MEMORY_OBJS = $(call objs,$(BULK_MEMORY_SOURCES))
257
370
LIBWASI_EMULATED_MMAN_OBJS = $(call objs,$(LIBWASI_EMULATED_MMAN_SOURCES))
258
371
LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS = $(call objs,$(LIBWASI_EMULATED_PROCESS_CLOCKS_SOURCES))
259
372
LIBWASI_EMULATED_GETPID_OBJS = $(call objs,$(LIBWASI_EMULATED_GETPID_SOURCES))
263
376
# These variables describe the locations of various files and
264
377
# directories in the generated sysroot tree.
265
SYSROOT_LIB := $(SYSROOT)/lib/$(MULTIARCH_TRIPLE)
378
SYSROOT_LIB := $(SYSROOT)/lib/$(TARGET_TRIPLE)
266
379
SYSROOT_INC = $(SYSROOT)/include
267
SYSROOT_SHARE = $(SYSROOT)/share/$(MULTIARCH_TRIPLE)
380
SYSROOT_SHARE = $(SYSROOT)/share/$(TARGET_TRIPLE)
269
382
# Files from musl's include directory that we don't want to install in the
270
383
# sysroot's include directory.
408
534
startup_files $(LIBC_BOTTOM_HALF_ALL_OBJS): CFLAGS += \
409
535
-I$(LIBC_BOTTOM_HALF_HEADERS_PRIVATE) \
410
536
-I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC) \
411
-I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
537
-I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) \
538
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
539
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal
413
541
$(LIBC_TOP_HALF_ALL_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
414
542
-I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
512
640
@# Ignore certain llvm builtin symbols such as those starting with __mul
513
641
@# since these dependencies can vary between llvm versions.
514
642
"$(NM)" --defined-only "$(SYSROOT_LIB)"/libc.a "$(SYSROOT_LIB)"/libwasi-emulated-*.a "$(SYSROOT_LIB)"/*.o \
515
|grep ' [[:upper:]] ' |sed 's/.* [[:upper:]] //' |LC_ALL=C sort > "$(DEFINED_SYMBOLS)"
643
|grep ' [[:upper:]] ' |sed 's/.* [[:upper:]] //' |LC_ALL=C sort |uniq > "$(DEFINED_SYMBOLS)"
516
644
for undef_sym in $$("$(NM)" --undefined-only "$(SYSROOT_LIB)"/libc.a "$(SYSROOT_LIB)"/libc-*.a "$(SYSROOT_LIB)"/*.o \
517
645
|grep ' U ' |sed 's/.* U //' |LC_ALL=C sort |uniq); do \
518
646
grep -q '\<'$$undef_sym'\>' "$(DEFINED_SYMBOLS)" || echo $$undef_sym; \
546
674
@# its builtin include path first, which produces compiler-specific
549
@# TODO: Undefine __FLOAT128__ for now since it's not in clang 8.0.
550
@# TODO: Filter out __FLT16_* for now, as not all versions of clang have these.
551
677
@# TODO: Filter out __NO_MATH_ERRNO_ and a few __*WIDTH__ that are new to clang 14.
678
@# TODO: Filter out __GCC_HAVE_SYNC_COMPARE_AND_SWAP_* that are new to clang 16.
552
679
@# TODO: clang defined __FLT_EVAL_METHOD__ until clang 15, so we force-undefine it
553
680
@# for older versions.
681
@# TODO: Undefine __wasm_mutable_globals__ and __wasm_sign_ext__, that are new to
682
@# clang 16 for -mcpu=generic.
683
@# TODO: As of clang 16, __GNUC_VA_LIST is #defined without a value.
554
684
$(CC) $(CFLAGS) "$(SYSROOT_SHARE)/include-all.c" \
555
685
-isystem $(SYSROOT_INC) \
564
694
-U__clang_version__ \
565
695
-U__clang_literal_encoding__ \
566
696
-U__clang_wide_literal_encoding__ \
697
-U__wasm_mutable_globals__ \
698
-U__wasm_sign_ext__ \
568
700
-U__GNUC_MINOR__ \
569
701
-U__GNUC_PATCHLEVEL__ \
572
703
-U__NO_MATH_ERRNO__ \
573
704
-U__BITINT_MAXWIDTH__ \
574
705
-U__FLT_EVAL_METHOD__ -Wno-builtin-macro-redefined \
575
706
| sed -e 's/__[[:upper:][:digit:]]*_ATOMIC_\([[:upper:][:digit:]_]*\)_LOCK_FREE/__compiler_ATOMIC_\1_LOCK_FREE/' \
576
| grep -v '^#define __FLT16_' \
707
| sed -e 's/__GNUC_VA_LIST $$/__GNUC_VA_LIST 1/' \
577
708
| grep -v '^#define __\(BOOL\|INT_\(LEAST\|FAST\)\(8\|16\|32\|64\)\|INT\|LONG\|LLONG\|SHRT\)_WIDTH__' \
709
| grep -v '^#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_\(1\|2\|4\|8\)' \
578
710
> "$(SYSROOT_SHARE)/predefined-macros.txt"
580
712
# Check that the computed metadata matches the expected metadata.
581
713
# This ignores whitespace because on Windows the output has CRLF line endings.
582
diff -wur "$(CURDIR)/expected/$(MULTIARCH_TRIPLE)" "$(SYSROOT_SHARE)"
714
diff -wur "$(CURDIR)/expected/$(TARGET_TRIPLE)" "$(SYSROOT_SHARE)"
585
717
mkdir -p "$(INSTALL_DIR)"