~mmach/netext73/wasi-libc

« back to all changes in this revision

Viewing changes to Makefile

  • Committer: mmach
  • Date: 2023-09-12 15:53:36 UTC
  • Revision ID: netbit73@gmail.com-20230912155336-s1r5z5xnszz99zzo
0.0~git20230113.4362b18

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
16
# single or posix; note that pthread support is still a work-in-progress.
17
17
THREAD_MODEL ?= single
18
18
# dlmalloc or none
19
19
MALLOC_IMPL ?= dlmalloc
20
20
# yes or no
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)
 
24
 
 
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
 
29
# discussion
 
30
BULK_MEMORY_THRESHOLD ?= 32
24
31
 
25
32
# Variables from this point on are not meant to be overridable via the
26
33
# make command-line.
27
34
 
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
 
37
 
 
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
 
42
endif
32
43
 
33
44
# These variables describe the locations of various files and directories in
34
45
# the source tree.
36
47
DLMALLOC_SRC_DIR = $(DLMALLOC_DIR)/src
37
48
DLMALLOC_SOURCES = $(DLMALLOC_SRC_DIR)/dlmalloc.c
38
49
DLMALLOC_INC = $(DLMALLOC_DIR)/include
 
50
EMMALLOC_DIR = $(CURDIR)/emmalloc
 
51
EMMALLOC_SOURCES = $(EMMALLOC_DIR)/emmalloc.c
39
52
LIBC_BOTTOM_HALF_DIR = $(CURDIR)/libc-bottom-half
40
53
LIBC_BOTTOM_HALF_CLOUDLIBC_SRC = $(LIBC_BOTTOM_HALF_DIR)/cloudlibc/src
41
54
LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC = $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)/include
43
56
LIBC_BOTTOM_HALF_HEADERS_PRIVATE = $(LIBC_BOTTOM_HALF_DIR)/headers/private
44
57
LIBC_BOTTOM_HALF_SOURCES = $(LIBC_BOTTOM_HALF_DIR)/sources
45
58
LIBC_BOTTOM_HALF_ALL_SOURCES = \
 
59
    $(sort \
46
60
    $(shell find $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) -name \*.c) \
47
 
    $(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c)
 
61
    $(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c))
48
62
 
49
63
# FIXME(https://reviews.llvm.org/D85567) - due to a bug in LLD the weak
50
64
# references to a function defined in `chdir.c` only work if `chdir.c` is at the
54
68
LIBC_BOTTOM_HALF_ALL_SOURCES := $(LIBC_BOTTOM_HALF_ALL_SOURCES) $(LIBC_BOTTOM_HALF_SOURCES)/chdir.c
55
69
 
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
131
145
        fcntl/creat.c \
132
146
        dirent/alphasort.c \
133
147
        dirent/versionsort.c \
 
148
        env/__stack_chk_fail.c \
134
149
        env/clearenv.c \
135
150
        env/getenv.c \
136
151
        env/putenv.c \
138
153
        env/unsetenv.c \
139
154
        unistd/posix_close.c \
140
155
        stat/futimesat.c \
 
156
        legacy/getpagesize.c \
 
157
        thread/thrd_sleep.c \
141
158
    ) \
142
159
    $(filter-out %/procfdname.c %/syscall.c %/syscall_ret.c %/vdso.c %/version.c, \
143
160
                 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/*.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)
 
196
 
 
197
ifeq ($(THREAD_MODEL), posix)
 
198
LIBC_TOP_HALF_MUSL_SOURCES += \
 
199
    $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
 
200
        env/__init_tls.c \
 
201
        stdio/__lockfile.c \
 
202
        stdio/flockfile.c \
 
203
        stdio/ftrylockfile.c \
 
204
        stdio/funlockfile.c \
 
205
        thread/__lock.c \
 
206
        thread/__wait.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 \
 
268
        thread/sem_init.c \
 
269
        thread/sem_post.c \
 
270
        thread/sem_timedwait.c \
 
271
        thread/sem_trywait.c \
 
272
        thread/sem_wait.c \
 
273
        thread/wasm32/wasi_thread_start.s \
 
274
    )
 
275
endif
 
276
 
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]))
191
293
 
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
216
319
endif
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
 
324
 
 
325
# Include cloudlib's directory to access the structure definition of clockid_t
 
326
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
219
327
endif
220
328
 
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.
245
357
else
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))
262
375
 
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)
268
381
 
269
382
# Files from musl's include directory that we don't want to install in the
270
383
# sysroot's include directory.
343
456
    "netinet/ether.h" \
344
457
    "sys/timerfd.h" \
345
458
    "libintl.h" \
346
 
    "sys/sysmacros.h"
 
459
    "sys/sysmacros.h" \
 
460
    "aio.h"
347
461
 
348
462
ifeq ($(THREAD_MODEL), single)
349
463
# Remove headers not supported in single-threaded mode.
350
 
MUSL_OMIT_HEADERS += "aio.h" "pthread.h"
 
464
MUSL_OMIT_HEADERS += "pthread.h"
351
465
endif
352
466
 
353
467
default: finish
385
499
            -D__wasilibc_printscan_no_floating_point \
386
500
            -D__wasilibc_printscan_floating_point_support_option="\"remove -lc-printscan-no-floating-point from the link command\""
387
501
 
 
502
# TODO: apply -mbulk-memory globally, once
 
503
# https://github.com/llvm/llvm-project/issues/52618 is resolved
 
504
$(BULK_MEMORY_OBJS): CFLAGS += \
 
505
        -mbulk-memory
 
506
 
 
507
$(BULK_MEMORY_OBJS): CFLAGS += \
 
508
        -DBULK_MEMORY_THRESHOLD=$(BULK_MEMORY_THRESHOLD)
 
509
 
388
510
$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
389
511
            -D_WASI_EMULATED_SIGNAL
390
512
 
400
522
        @mkdir -p "$(@D)"
401
523
        $(CC) $(CFLAGS) -MD -MP -o $@ -c $<
402
524
 
 
525
$(OBJDIR)/%.o: $(CURDIR)/%.s include_dirs
 
526
        @mkdir -p "$(@D)"
 
527
        $(CC) $(ASMFLAGS) -o $@ -c $<
 
528
 
403
529
-include $(shell find $(OBJDIR) -name \*.d)
404
530
 
405
531
$(DLMALLOC_OBJS): CFLAGS += \
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
412
540
 
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
547
675
        @# output.
548
676
        @#
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) \
556
686
            -std=gnu17 \
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__ \
567
699
            -U__GNUC__ \
568
700
            -U__GNUC_MINOR__ \
569
701
            -U__GNUC_PATCHLEVEL__ \
570
702
            -U__VERSION__ \
571
 
            -U__FLOAT128__ \
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"
579
711
 
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)"
583
715
 
584
716
install: finish
585
717
        mkdir -p "$(INSTALL_DIR)"