]> git.proxmox.com Git - wasi-libc.git/blame - Makefile
Beginning porting Emscripten's emmalloc to wasi-libc.
[wasi-libc.git] / Makefile
CommitLineData
41af0db3
DG
1# These variables are specifically meant to be overridable via the make
2# command-line.
202034fd
MH
3# ?= doesn't work for CC and AR because make has a default value for them.
4ifeq ($(origin CC), default)
5CC := clang
6endif
a279514a 7NM ?= $(patsubst %clang,%llvm-nm,$(filter-out ccache sccache,$(CC)))
202034fd
MH
8ifeq ($(origin AR), default)
9AR = $(patsubst %clang,%llvm-ar,$(filter-out ccache sccache,$(CC)))
10endif
8852e15a 11EXTRA_CFLAGS ?= -O2 -DNDEBUG
320054e8 12# The directory where we build the sysroot.
255e6f7c 13SYSROOT ?= $(CURDIR)/sysroot
320054e8 14# A directory to install to for "make install".
255e6f7c 15INSTALL_DIR ?= /usr/local
dcd28cf8 16# single or posix; note that pthread support is still a work-in-progress.
41af0db3 17THREAD_MODEL ?= single
c7465d22
AL
18# dlmalloc or none
19MALLOC_IMPL ?= dlmalloc
320054e8 20# yes or no
41af0db3 21BUILD_LIBC_TOP_HALF ?= yes
320054e8 22# The directory where we're store intermediate artifacts.
41af0db3 23OBJDIR ?= $(CURDIR)/build
320054e8 24
ba81b409
CS
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
30BULK_MEMORY_THRESHOLD ?= 32
31
41af0db3
DG
32# Variables from this point on are not meant to be overridable via the
33# make command-line.
34
35# Set the target variables. Multiarch triples notably omit the vendor field,
36# which happens to be what we do for the main target triple too.
37TARGET_TRIPLE = wasm32-wasi
38MULTIARCH_TRIPLE = wasm32-wasi
39
40# These variables describe the locations of various files and directories in
41# the source tree.
320054e8 42DLMALLOC_DIR = $(CURDIR)/dlmalloc
dbfccac2 43DLMALLOC_SRC_DIR = $(DLMALLOC_DIR)/src
320054e8 44DLMALLOC_SOURCES = $(DLMALLOC_SRC_DIR)/dlmalloc.c
dbfccac2 45DLMALLOC_INC = $(DLMALLOC_DIR)/include
320054e8
DG
46LIBC_BOTTOM_HALF_DIR = $(CURDIR)/libc-bottom-half
47LIBC_BOTTOM_HALF_CLOUDLIBC_SRC = $(LIBC_BOTTOM_HALF_DIR)/cloudlibc/src
48LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC = $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)/include
49LIBC_BOTTOM_HALF_HEADERS_PUBLIC = $(LIBC_BOTTOM_HALF_DIR)/headers/public
50LIBC_BOTTOM_HALF_HEADERS_PRIVATE = $(LIBC_BOTTOM_HALF_DIR)/headers/private
320054e8
DG
51LIBC_BOTTOM_HALF_SOURCES = $(LIBC_BOTTOM_HALF_DIR)/sources
52LIBC_BOTTOM_HALF_ALL_SOURCES = \
69031b63 53 $(sort \
320054e8 54 $(shell find $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) -name \*.c) \
69031b63 55 $(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c))
5b148b61
AC
56
57# FIXME(https://reviews.llvm.org/D85567) - due to a bug in LLD the weak
58# references to a function defined in `chdir.c` only work if `chdir.c` is at the
59# end of the archive, but once that LLD review lands and propagates into LLVM
60# then we don't have to do this.
61LIBC_BOTTOM_HALF_ALL_SOURCES := $(filter-out $(LIBC_BOTTOM_HALF_SOURCES)/chdir.c,$(LIBC_BOTTOM_HALF_ALL_SOURCES))
62LIBC_BOTTOM_HALF_ALL_SOURCES := $(LIBC_BOTTOM_HALF_ALL_SOURCES) $(LIBC_BOTTOM_HALF_SOURCES)/chdir.c
63
320054e8 64LIBWASI_EMULATED_MMAN_SOURCES = \
69031b63 65 $(sort $(shell find $(LIBC_BOTTOM_HALF_DIR)/mman -name \*.c))
b9b64a69 66LIBWASI_EMULATED_PROCESS_CLOCKS_SOURCES = \
69031b63 67 $(sort $(shell find $(LIBC_BOTTOM_HALF_DIR)/clocks -name \*.c))
659ff414 68LIBWASI_EMULATED_GETPID_SOURCES = \
69031b63 69 $(sort $(shell find $(LIBC_BOTTOM_HALF_DIR)/getpid -name \*.c))
d1cd4f48 70LIBWASI_EMULATED_SIGNAL_SOURCES = \
69031b63 71 $(sort $(shell find $(LIBC_BOTTOM_HALF_DIR)/signal -name \*.c))
d1cd4f48
DG
72LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES = \
73 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/signal/psignal.c \
74 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/strsignal.c
320054e8
DG
75LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c)
76LIBC_TOP_HALF_DIR = $(CURDIR)/libc-top-half
77LIBC_TOP_HALF_MUSL_DIR = $(LIBC_TOP_HALF_DIR)/musl
78LIBC_TOP_HALF_MUSL_SRC_DIR = $(LIBC_TOP_HALF_MUSL_DIR)/src
79LIBC_TOP_HALF_MUSL_INC = $(LIBC_TOP_HALF_MUSL_DIR)/include
80LIBC_TOP_HALF_MUSL_SOURCES = \
41af0db3
DG
81 $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
82 misc/a64l.c \
83 misc/basename.c \
84 misc/dirname.c \
85 misc/ffs.c \
86 misc/ffsl.c \
87 misc/ffsll.c \
88 misc/fmtmsg.c \
89 misc/getdomainname.c \
90 misc/gethostid.c \
91 misc/getopt.c \
92 misc/getopt_long.c \
93 misc/getsubopt.c \
94 misc/uname.c \
95 misc/nftw.c \
96 errno/strerror.c \
97 network/htonl.c \
98 network/htons.c \
99 network/ntohl.c \
100 network/ntohs.c \
101 network/inet_ntop.c \
102 network/inet_pton.c \
103 network/inet_aton.c \
104 network/in6addr_any.c \
105 network/in6addr_loopback.c \
106 fenv/fenv.c \
107 fenv/fesetround.c \
108 fenv/feupdateenv.c \
109 fenv/fesetexceptflag.c \
110 fenv/fegetexceptflag.c \
111 fenv/feholdexcept.c \
112 exit/exit.c \
113 exit/atexit.c \
114 exit/assert.c \
115 exit/quick_exit.c \
116 exit/at_quick_exit.c \
117 time/strftime.c \
118 time/asctime.c \
119 time/asctime_r.c \
120 time/ctime.c \
121 time/ctime_r.c \
122 time/wcsftime.c \
123 time/strptime.c \
124 time/difftime.c \
125 time/timegm.c \
126 time/ftime.c \
127 time/gmtime.c \
128 time/gmtime_r.c \
129 time/timespec_get.c \
130 time/getdate.c \
131 time/localtime.c \
132 time/localtime_r.c \
133 time/mktime.c \
134 time/__tm_to_secs.c \
135 time/__month_to_secs.c \
136 time/__secs_to_tm.c \
137 time/__year_to_secs.c \
138 time/__tz.c \
139 fcntl/creat.c \
140 dirent/alphasort.c \
141 dirent/versionsort.c \
142 env/clearenv.c \
143 env/getenv.c \
144 env/putenv.c \
145 env/setenv.c \
146 env/unsetenv.c \
147 unistd/posix_close.c \
079adff8 148 stat/futimesat.c \
e066a8b9 149 legacy/getpagesize.c \
dcd28cf8 150 thread/thrd_sleep.c \
41af0db3 151 ) \
320054e8
DG
152 $(filter-out %/procfdname.c %/syscall.c %/syscall_ret.c %/vdso.c %/version.c, \
153 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/*.c)) \
154 $(filter-out %/flockfile.c %/funlockfile.c %/__lockfile.c %/ftrylockfile.c \
155 %/rename.c \
156 %/tmpnam.c %/tmpfile.c %/tempnam.c \
157 %/popen.c %/pclose.c \
158 %/remove.c \
159 %/gets.c, \
160 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/*.c)) \
161 $(filter-out %/strsignal.c, \
162 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/*.c)) \
8048aeb5 163 $(filter-out %/dcngettext.c %/textdomain.c %/bind_textdomain_codeset.c, \
320054e8
DG
164 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/locale/*.c)) \
165 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/*.c) \
166 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/search/*.c) \
167 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/multibyte/*.c) \
168 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/regex/*.c) \
320054e8
DG
169 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/prng/*.c) \
170 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/conf/*.c) \
171 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/ctype/*.c) \
900b8032 172 $(filter-out %/__signbit.c %/__signbitf.c %/__signbitl.c \
16450763
DG
173 %/__fpclassify.c %/__fpclassifyf.c %/__fpclassifyl.c \
174 %/ceilf.c %/ceil.c \
175 %/floorf.c %/floor.c \
176 %/truncf.c %/trunc.c \
177 %/rintf.c %/rint.c \
178 %/nearbyintf.c %/nearbyint.c \
179 %/sqrtf.c %/sqrt.c \
180 %/fabsf.c %/fabs.c \
cf81683e
DG
181 %/copysignf.c %/copysign.c \
182 %/fminf.c %/fmaxf.c \
183 %/fmin.c %/fmax.c, \
900b8032 184 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/math/*.c)) \
5ccebd31
DG
185 $(filter-out %/crealf.c %/creal.c %creall.c \
186 %/cimagf.c %/cimag.c %cimagl.c, \
7d5074ff 187 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \
320054e8 188 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c)
dcd28cf8 189
33c3753c
AB
190ifeq ($(THREAD_MODEL), posix)
191LIBC_TOP_HALF_MUSL_SOURCES += \
192 $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
d02cec69 193 thread/__lock.c \
33c3753c
AB
194 thread/__wait.c \
195 thread/__timedwait.c \
c40403f9
MK
196 thread/pthread_cleanup_push.c \
197 thread/pthread_cond_broadcast.c \
198 thread/pthread_cond_destroy.c \
199 thread/pthread_cond_init.c \
200 thread/pthread_cond_signal.c \
201 thread/pthread_cond_timedwait.c \
202 thread/pthread_cond_wait.c \
3d00b969
MK
203 thread/pthread_condattr_destroy.c \
204 thread/pthread_condattr_init.c \
205 thread/pthread_condattr_setclock.c \
206 thread/pthread_condattr_setpshared.c \
241060c3 207 thread/pthread_create.c \
9d2f5a82 208 thread/pthread_join.c \
33c3753c
AB
209 thread/pthread_mutex_consistent.c \
210 thread/pthread_mutex_destroy.c \
211 thread/pthread_mutex_init.c \
212 thread/pthread_mutex_getprioceiling.c \
213 thread/pthread_mutex_lock.c \
214 thread/pthread_mutex_timedlock.c \
215 thread/pthread_mutex_trylock.c \
216 thread/pthread_mutex_unlock.c \
217 thread/pthread_mutexattr_destroy.c \
218 thread/pthread_mutexattr_init.c \
219 thread/pthread_mutexattr_setprotocol.c \
220 thread/pthread_mutexattr_setpshared.c \
221 thread/pthread_mutexattr_setrobust.c \
222 thread/pthread_mutexattr_settype.c \
27ba71f9
MK
223 thread/pthread_rwlock_destroy.c \
224 thread/pthread_rwlock_init.c \
225 thread/pthread_rwlock_rdlock.c \
226 thread/pthread_rwlock_timedrdlock.c \
227 thread/pthread_rwlock_timedwrlock.c \
228 thread/pthread_rwlock_tryrdlock.c \
229 thread/pthread_rwlock_trywrlock.c \
230 thread/pthread_rwlock_unlock.c \
231 thread/pthread_rwlock_wrlock.c \
232 thread/pthread_rwlockattr_destroy.c \
233 thread/pthread_rwlockattr_init.c \
234 thread/pthread_rwlockattr_setpshared.c \
241060c3 235 thread/pthread_setcancelstate.c \
2057ce92
AB
236 thread/pthread_testcancel.c \
237 thread/sem_destroy.c \
238 thread/sem_getvalue.c \
239 thread/sem_init.c \
240 thread/sem_post.c \
241 thread/sem_timedwait.c \
242 thread/sem_trywait.c \
243 thread/sem_wait.c \
33c3753c
AB
244 )
245endif
246
320054e8
DG
247MUSL_PRINTSCAN_SOURCES = \
248 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/floatscan.c \
249 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfprintf.c \
2201343c 250 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfwprintf.c \
320054e8
DG
251 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfscanf.c \
252 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/strtod.c \
253 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/wcstod.c
d3cc92a6
CS
254BULK_MEMORY_SOURCES = \
255 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memcpy.c \
256 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memmove.c \
257 $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/memset.c
320054e8
DG
258LIBC_TOP_HALF_HEADERS_PRIVATE = $(LIBC_TOP_HALF_DIR)/headers/private
259LIBC_TOP_HALF_SOURCES = $(LIBC_TOP_HALF_DIR)/sources
260LIBC_TOP_HALF_ALL_SOURCES = \
261 $(LIBC_TOP_HALF_MUSL_SOURCES) \
69031b63 262 $(sort $(shell find $(LIBC_TOP_HALF_SOURCES) -name \*.c))
320054e8 263
8852e15a
JE
264# Add any extra flags
265CFLAGS = $(EXTRA_CFLAGS)
320054e8 266# Set the target.
8852e15a 267CFLAGS += --target=$(TARGET_TRIPLE)
320054e8
DG
268# WebAssembly floating-point match doesn't trap.
269# TODO: Add -fno-signaling-nans when the compiler supports it.
5ccfab77 270CFLAGS += -fno-trapping-math
63f0f3c5
DG
271# Add all warnings, but disable a few which occur in third-party code.
272CFLAGS += -Wall -Wextra -Werror \
273 -Wno-null-pointer-arithmetic \
9520010f 274 -Wno-unused-parameter \
e69430a9 275 -Wno-sign-compare \
6273a5ff 276 -Wno-unused-variable \
ed6e0ba9 277 -Wno-unused-function \
e8270d1b 278 -Wno-ignored-attributes \
b77dcff6 279 -Wno-missing-braces \
9cbfeaaa 280 -Wno-ignored-pragmas \
a78cd329
DG
281 -Wno-unused-but-set-variable \
282 -Wno-unknown-warning-option
dbfccac2
DG
283
284# Configure support for threads.
320054e8 285ifeq ($(THREAD_MODEL), single)
d3cc92a6 286CFLAGS += -mthread-model single
dbfccac2 287endif
320054e8 288ifeq ($(THREAD_MODEL), posix)
dcd28cf8
AB
289# Specify the tls-model until LLVM 15 is released (which should contain
290# https://reviews.llvm.org/D130053).
291CFLAGS += -mthread-model posix -pthread -ftls-model=local-exec
3d00b969
MK
292
293# Include cloudlib's directory to access the structure definition of clockid_t
294CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
320054e8
DG
295endif
296
fc260f1a
JE
297# Expose the public headers to the implementation. We use `-isystem` for
298# purpose for two reasons:
299#
300# 1. It only does `<...>` not `"...."` lookup. We are making a libc,
301# which is a system library, so all public headers should be
302# accessible via `<...>` and never also need `"..."`. `-isystem` main
303# purpose is to only effect `<...>` lookup.
304#
305# 2. The `-I` for private headers added for specific C files below
306# should come earlier in the search path, so they can "override"
307# and/or `#include_next` the public headers. `-isystem` (like
308# `-idirafter`) comes later in the search path than `-I`.
309CFLAGS += -isystem "$(SYSROOT_INC)"
320054e8 310
41af0db3
DG
311# These variables describe the locations of various files and directories in
312# the build tree.
320054e8 313objs = $(patsubst $(CURDIR)/%.c,$(OBJDIR)/%.o,$(1))
41af0db3
DG
314DLMALLOC_OBJS = $(call objs,$(DLMALLOC_SOURCES))
315LIBC_BOTTOM_HALF_ALL_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_ALL_SOURCES))
316LIBC_TOP_HALF_ALL_OBJS = $(call objs,$(LIBC_TOP_HALF_ALL_SOURCES))
c7465d22 317ifeq ($(MALLOC_IMPL),dlmalloc)
41af0db3 318LIBC_OBJS += $(DLMALLOC_OBJS)
c7465d22
AL
319else ifeq ($(MALLOC_IMPL),none)
320# No object files to add.
321else
322$(error unknown malloc implementation $(MALLOC_IMPL))
dbfccac2 323endif
320054e8 324# Add libc-bottom-half's objects.
41af0db3 325LIBC_OBJS += $(LIBC_BOTTOM_HALF_ALL_OBJS)
320054e8 326ifeq ($(BUILD_LIBC_TOP_HALF),yes)
320054e8 327# libc-top-half is musl.
41af0db3 328LIBC_OBJS += $(LIBC_TOP_HALF_ALL_OBJS)
320054e8 329endif
41af0db3
DG
330MUSL_PRINTSCAN_OBJS = $(call objs,$(MUSL_PRINTSCAN_SOURCES))
331MUSL_PRINTSCAN_LONG_DOUBLE_OBJS = $(patsubst %.o,%.long-double.o,$(MUSL_PRINTSCAN_OBJS))
332MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS = $(patsubst %.o,%.no-floating-point.o,$(MUSL_PRINTSCAN_OBJS))
d3cc92a6 333BULK_MEMORY_OBJS = $(call objs,$(BULK_MEMORY_SOURCES))
41af0db3 334LIBWASI_EMULATED_MMAN_OBJS = $(call objs,$(LIBWASI_EMULATED_MMAN_SOURCES))
b9b64a69 335LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS = $(call objs,$(LIBWASI_EMULATED_PROCESS_CLOCKS_SOURCES))
659ff414 336LIBWASI_EMULATED_GETPID_OBJS = $(call objs,$(LIBWASI_EMULATED_GETPID_SOURCES))
d1cd4f48
DG
337LIBWASI_EMULATED_SIGNAL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_SOURCES))
338LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES))
41af0db3
DG
339
340# These variables describe the locations of various files and
341# directories in the generated sysroot tree.
342SYSROOT_LIB := $(SYSROOT)/lib/$(MULTIARCH_TRIPLE)
343SYSROOT_INC = $(SYSROOT)/include
344SYSROOT_SHARE = $(SYSROOT)/share/$(MULTIARCH_TRIPLE)
320054e8 345
1f409842
DG
346# Files from musl's include directory that we don't want to install in the
347# sysroot's include directory.
41af0db3 348MUSL_OMIT_HEADERS :=
1f409842
DG
349
350# Remove files which aren't headers (we generate alltypes.h below).
41af0db3 351MUSL_OMIT_HEADERS += \
1f409842
DG
352 "bits/syscall.h.in" \
353 "bits/alltypes.h.in" \
354 "alltypes.h.in"
355
33e8b518 356# Use the compiler's version of these headers.
41af0db3 357MUSL_OMIT_HEADERS += \
33e8b518
DG
358 "stdarg.h" \
359 "stddef.h"
360
1f409842 361# Use the WASI errno definitions.
41af0db3 362MUSL_OMIT_HEADERS += \
1f409842
DG
363 "bits/errno.h"
364
365# Remove headers that aren't supported yet or that aren't relevant for WASI.
41af0db3 366MUSL_OMIT_HEADERS += \
1f409842
DG
367 "sys/procfs.h" \
368 "sys/user.h" \
369 "sys/kd.h" "sys/vt.h" "sys/soundcard.h" "sys/sem.h" \
370 "sys/shm.h" "sys/msg.h" "sys/ipc.h" "sys/ptrace.h" \
371 "sys/statfs.h" \
372 "bits/kd.h" "bits/vt.h" "bits/soundcard.h" "bits/sem.h" \
373 "bits/shm.h" "bits/msg.h" "bits/ipc.h" "bits/ptrace.h" \
374 "bits/statfs.h" \
375 "sys/vfs.h" \
376 "sys/statvfs.h" \
377 "syslog.h" "sys/syslog.h" \
378 "wait.h" "sys/wait.h" \
379 "ucontext.h" "sys/ucontext.h" \
380 "paths.h" \
381 "utmp.h" "utmpx.h" \
382 "lastlog.h" \
383 "sys/acct.h" \
384 "sys/cachectl.h" \
385 "sys/epoll.h" "sys/reboot.h" "sys/swap.h" \
386 "sys/sendfile.h" "sys/inotify.h" \
387 "sys/quota.h" \
388 "sys/klog.h" \
389 "sys/fsuid.h" \
390 "sys/io.h" \
391 "sys/prctl.h" \
392 "sys/mtio.h" \
393 "sys/mount.h" \
394 "sys/fanotify.h" \
395 "sys/personality.h" \
396 "elf.h" "link.h" "bits/link.h" \
397 "scsi/scsi.h" "scsi/scsi_ioctl.h" "scsi/sg.h" \
398 "sys/auxv.h" \
399 "pwd.h" "shadow.h" "grp.h" \
400 "mntent.h" \
401 "netdb.h" \
402 "resolv.h" \
403 "pty.h" \
404 "dlfcn.h" \
405 "setjmp.h" \
406 "ulimit.h" \
407 "sys/xattr.h" \
408 "wordexp.h" \
409 "spawn.h" \
410 "sys/membarrier.h" \
411 "sys/signalfd.h" \
412 "termios.h" \
413 "sys/termios.h" \
414 "bits/termios.h" \
415 "net/if.h" \
416 "net/if_arp.h" \
417 "net/ethernet.h" \
418 "net/route.h" \
419 "netinet/if_ether.h" \
420 "netinet/ether.h" \
eb7230cf
DG
421 "sys/timerfd.h" \
422 "libintl.h" \
dcd28cf8
AB
423 "sys/sysmacros.h" \
424 "aio.h"
1f409842
DG
425
426ifeq ($(THREAD_MODEL), single)
427# Remove headers not supported in single-threaded mode.
dcd28cf8 428MUSL_OMIT_HEADERS += "pthread.h"
1f409842
DG
429endif
430
41af0db3 431default: finish
320054e8
DG
432
433$(SYSROOT_LIB)/libc.a: $(LIBC_OBJS)
434
435$(SYSROOT_LIB)/libc-printscan-long-double.a: $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS)
436
437$(SYSROOT_LIB)/libc-printscan-no-floating-point.a: $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS)
438
439$(SYSROOT_LIB)/libwasi-emulated-mman.a: $(LIBWASI_EMULATED_MMAN_OBJS)
440
b9b64a69
DG
441$(SYSROOT_LIB)/libwasi-emulated-process-clocks.a: $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS)
442
659ff414
DG
443$(SYSROOT_LIB)/libwasi-emulated-getpid.a: $(LIBWASI_EMULATED_GETPID_OBJS)
444
d1cd4f48
DG
445$(SYSROOT_LIB)/libwasi-emulated-signal.a: $(LIBWASI_EMULATED_SIGNAL_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS)
446
320054e8
DG
447%.a:
448 @mkdir -p "$(@D)"
255e6f7c 449 # On Windows, the commandline for the ar invocation got too long, so it needs to be split up.
9eb4a995
JE
450 $(AR) crs $@ $(wordlist 1, 199, $^)
451 $(AR) crs $@ $(wordlist 200, 399, $^)
452 $(AR) crs $@ $(wordlist 400, 599, $^)
453 $(AR) crs $@ $(wordlist 600, 799, $^)
255e6f7c
TS
454 # This might eventually overflow again, but at least it'll do so in a loud way instead of
455 # silently dropping the tail.
9eb4a995 456 $(AR) crs $@ $(wordlist 800, 100000, $^)
320054e8 457
5ccfab77 458$(MUSL_PRINTSCAN_OBJS): CFLAGS += \
320054e8
DG
459 -D__wasilibc_printscan_no_long_double \
460 -D__wasilibc_printscan_full_support_option="\"add -lc-printscan-long-double to the link command\""
461
5ccfab77 462$(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS): CFLAGS += \
320054e8
DG
463 -D__wasilibc_printscan_no_floating_point \
464 -D__wasilibc_printscan_floating_point_support_option="\"remove -lc-printscan-no-floating-point from the link command\""
465
5d0a7558
CS
466# TODO: apply -mbulk-memory globally, once
467# https://github.com/llvm/llvm-project/issues/52618 is resolved
d3cc92a6
CS
468$(BULK_MEMORY_OBJS): CFLAGS += \
469 -mbulk-memory
470
ba81b409
CS
471$(BULK_MEMORY_OBJS): CFLAGS += \
472 -DBULK_MEMORY_THRESHOLD=$(BULK_MEMORY_THRESHOLD)
473
5ccfab77 474$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
d1cd4f48
DG
475 -D_WASI_EMULATED_SIGNAL
476
8e8cc34a 477$(OBJDIR)/%.long-double.o: $(CURDIR)/%.c include_dirs
320054e8 478 @mkdir -p "$(@D)"
9e09e02a 479 $(CC) $(CFLAGS) -MD -MP -o $@ -c $<
dcf13b6f 480
8e8cc34a 481$(OBJDIR)/%.no-floating-point.o: $(CURDIR)/%.c include_dirs
320054e8 482 @mkdir -p "$(@D)"
9e09e02a 483 $(CC) $(CFLAGS) -MD -MP -o $@ -c $<
320054e8 484
8e8cc34a 485$(OBJDIR)/%.o: $(CURDIR)/%.c include_dirs
320054e8 486 @mkdir -p "$(@D)"
9e09e02a 487 $(CC) $(CFLAGS) -MD -MP -o $@ -c $<
320054e8 488
255e6f7c 489-include $(shell find $(OBJDIR) -name \*.d)
320054e8 490
5ccfab77 491$(DLMALLOC_OBJS): CFLAGS += \
320054e8
DG
492 -I$(DLMALLOC_INC)
493
5ccfab77 494startup_files $(LIBC_BOTTOM_HALF_ALL_OBJS): CFLAGS += \
320054e8
DG
495 -I$(LIBC_BOTTOM_HALF_HEADERS_PRIVATE) \
496 -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC) \
87c2aa04
AB
497 -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) \
498 -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
499 -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal
320054e8 500
5ccfab77 501$(LIBC_TOP_HALF_ALL_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
320054e8
DG
502 -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \
503 -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \
504 -I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 \
505 -I$(LIBC_TOP_HALF_MUSL_DIR)/arch/generic \
506 -I$(LIBC_TOP_HALF_HEADERS_PRIVATE) \
507 -Wno-parentheses \
508 -Wno-shift-op-parentheses \
509 -Wno-bitwise-op-parentheses \
510 -Wno-logical-op-parentheses \
511 -Wno-string-plus-int \
512 -Wno-dangling-else \
513 -Wno-unknown-pragmas
514
b9b64a69
DG
515$(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS): CFLAGS += \
516 -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
517
8e8cc34a 518include_dirs:
dcf13b6f
DG
519 #
520 # Install the include files.
521 #
320054e8 522 mkdir -p "$(SYSROOT_INC)"
320054e8
DG
523 cp -r "$(LIBC_BOTTOM_HALF_HEADERS_PUBLIC)"/* "$(SYSROOT_INC)"
524
525 # Generate musl's bits/alltypes.h header.
526 mkdir -p "$(SYSROOT_INC)/bits"
527 sed -f $(LIBC_TOP_HALF_MUSL_DIR)/tools/mkalltypes.sed \
528 $(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32/bits/alltypes.h.in \
529 $(LIBC_TOP_HALF_MUSL_DIR)/include/alltypes.h.in \
530 > "$(SYSROOT_INC)/bits/alltypes.h"
531
532 # Copy in the bulk of musl's public header files.
533 cp -r "$(LIBC_TOP_HALF_MUSL_INC)"/* "$(SYSROOT_INC)"
534 # Copy in the musl's "bits" header files.
535 cp -r "$(LIBC_TOP_HALF_MUSL_DIR)"/arch/generic/bits/* "$(SYSROOT_INC)/bits"
536 cp -r "$(LIBC_TOP_HALF_MUSL_DIR)"/arch/wasm32/bits/* "$(SYSROOT_INC)/bits"
537
1f409842
DG
538 # Remove selected header files.
539 $(RM) $(patsubst %,$(SYSROOT_INC)/%,$(MUSL_OMIT_HEADERS))
320054e8 540
8e8cc34a 541startup_files: include_dirs
dcf13b6f 542 #
320054e8 543 # Build the startup files.
dcf13b6f 544 #
320054e8
DG
545 @mkdir -p "$(OBJDIR)"
546 cd "$(OBJDIR)" && \
9e09e02a 547 $(CC) $(CFLAGS) -c $(LIBC_BOTTOM_HALF_CRT_SOURCES) -MD -MP && \
320054e8
DG
548 mkdir -p "$(SYSROOT_LIB)" && \
549 mv *.o "$(SYSROOT_LIB)"
dcf13b6f 550
8e8cc34a 551libc: include_dirs \
320054e8
DG
552 $(SYSROOT_LIB)/libc.a \
553 $(SYSROOT_LIB)/libc-printscan-long-double.a \
554 $(SYSROOT_LIB)/libc-printscan-no-floating-point.a \
d1cd4f48 555 $(SYSROOT_LIB)/libwasi-emulated-mman.a \
b9b64a69 556 $(SYSROOT_LIB)/libwasi-emulated-process-clocks.a \
659ff414 557 $(SYSROOT_LIB)/libwasi-emulated-getpid.a \
d1cd4f48 558 $(SYSROOT_LIB)/libwasi-emulated-signal.a
320054e8 559
8e8cc34a 560finish: startup_files libc
dcf13b6f 561 #
320054e8 562 # Create empty placeholder libraries.
dcf13b6f 563 #
1f7d798a 564 for name in m rt pthread crypt util xnet resolv dl; do \
9eb4a995 565 $(AR) crs "$(SYSROOT_LIB)/lib$${name}.a"; \
320054e8 566 done
dbfccac2 567
c7465d22
AL
568 #
569 # The build succeeded! The generated sysroot is in $(SYSROOT).
570 #
571
572# The check for defined and undefined symbols expects there to be a heap
573# alloctor (providing malloc, calloc, free, etc). Skip this step if the build
574# is done without a malloc implementation.
575ifneq ($(MALLOC_IMPL),none)
576finish: check-symbols
577endif
578
18f9e0aa
JE
579DEFINED_SYMBOLS = $(SYSROOT_SHARE)/defined-symbols.txt
580UNDEFINED_SYMBOLS = $(SYSROOT_SHARE)/undefined-symbols.txt
581
c7465d22 582check-symbols: startup_files libc
dbfccac2 583 #
320054e8 584 # Collect metadata on the sysroot and perform sanity checks.
dbfccac2 585 #
320054e8
DG
586 mkdir -p "$(SYSROOT_SHARE)"
587
41af0db3 588 #
320054e8 589 # Collect symbol information.
41af0db3
DG
590 #
591 @# TODO: Use llvm-nm --extern-only instead of grep. This is blocked on
592 @# LLVM PR40497, which is fixed in 9.0, but not in 8.0.
593 @# Ignore certain llvm builtin symbols such as those starting with __mul
594 @# since these dependencies can vary between llvm versions.
a279514a 595 "$(NM)" --defined-only "$(SYSROOT_LIB)"/libc.a "$(SYSROOT_LIB)"/libwasi-emulated-*.a "$(SYSROOT_LIB)"/*.o \
294b23c3 596 |grep ' [[:upper:]] ' |sed 's/.* [[:upper:]] //' |LC_ALL=C sort |uniq > "$(DEFINED_SYMBOLS)"
a279514a 597 for undef_sym in $$("$(NM)" --undefined-only "$(SYSROOT_LIB)"/libc.a "$(SYSROOT_LIB)"/libc-*.a "$(SYSROOT_LIB)"/*.o \
fad009dc 598 |grep ' U ' |sed 's/.* U //' |LC_ALL=C sort |uniq); do \
18f9e0aa
JE
599 grep -q '\<'$$undef_sym'\>' "$(DEFINED_SYMBOLS)" || echo $$undef_sym; \
600 done | grep -v "^__mul" > "$(UNDEFINED_SYMBOLS)"
601 grep '^_*imported_wasi_' "$(UNDEFINED_SYMBOLS)" \
320054e8
DG
602 > "$(SYSROOT_LIB)/libc.imports"
603
41af0db3 604 #
b1196b49 605 # Generate a test file that includes all public C header files.
41af0db3 606 #
4cb4b26f 607 cd "$(SYSROOT_INC)" && \
b1196b49 608 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 \
4cb4b26f
JE
609 echo '#include <'$$header'>' | sed 's/\.\///' ; \
610 done |LC_ALL=C sort >$(SYSROOT_SHARE)/include-all.c ; \
320054e8
DG
611 cd - >/dev/null
612
41af0db3 613 #
320054e8 614 # Test that it compiles.
41af0db3 615 #
9e09e02a 616 $(CC) $(CFLAGS) -fsyntax-only "$(SYSROOT_SHARE)/include-all.c" -Wno-\#warnings
320054e8 617
8df0d4cd 618 #
41af0db3
DG
619 # Collect all the predefined macros, except for compiler version macros
620 # which we don't need to track here.
8df0d4cd 621 #
41af0db3
DG
622 @#
623 @# For the __*_ATOMIC_*_LOCK_FREE macros, squash individual compiler names
624 @# to attempt, toward keeping these files compiler-independent.
625 @#
626 @# We have to add `-isystem $(SYSROOT_INC)` because otherwise clang puts
627 @# its builtin include path first, which produces compiler-specific
628 @# output.
629 @#
630 @# TODO: Undefine __FLOAT128__ for now since it's not in clang 8.0.
631 @# TODO: Filter out __FLT16_* for now, as not all versions of clang have these.
df006e12 632 @# TODO: Filter out __NO_MATH_ERRNO_ and a few __*WIDTH__ that are new to clang 14.
e9fcbdea
MH
633 @# TODO: clang defined __FLT_EVAL_METHOD__ until clang 15, so we force-undefine it
634 @# for older versions.
9e09e02a 635 $(CC) $(CFLAGS) "$(SYSROOT_SHARE)/include-all.c" \
8df0d4cd 636 -isystem $(SYSROOT_INC) \
38b930a2 637 -std=gnu17 \
320054e8 638 -E -dM -Wno-\#warnings \
9f0d8e85 639 -D_ALL_SOURCE \
320054e8
DG
640 -U__llvm__ \
641 -U__clang__ \
642 -U__clang_major__ \
643 -U__clang_minor__ \
644 -U__clang_patchlevel__ \
645 -U__clang_version__ \
ad513341
MH
646 -U__clang_literal_encoding__ \
647 -U__clang_wide_literal_encoding__ \
320054e8
DG
648 -U__GNUC__ \
649 -U__GNUC_MINOR__ \
650 -U__GNUC_PATCHLEVEL__ \
651 -U__VERSION__ \
652 -U__FLOAT128__ \
df006e12
MH
653 -U__NO_MATH_ERRNO__ \
654 -U__BITINT_MAXWIDTH__ \
e9fcbdea 655 -U__FLT_EVAL_METHOD__ -Wno-builtin-macro-redefined \
320054e8 656 | sed -e 's/__[[:upper:][:digit:]]*_ATOMIC_\([[:upper:][:digit:]_]*\)_LOCK_FREE/__compiler_ATOMIC_\1_LOCK_FREE/' \
f74124a6 657 | grep -v '^#define __FLT16_' \
df006e12 658 | grep -v '^#define __\(BOOL\|INT_\(LEAST\|FAST\)\(8\|16\|32\|64\)\|INT\|LONG\|LLONG\|SHRT\)_WIDTH__' \
320054e8
DG
659 > "$(SYSROOT_SHARE)/predefined-macros.txt"
660
320054e8 661 # Check that the computed metadata matches the expected metadata.
255e6f7c 662 # This ignores whitespace because on Windows the output has CRLF line endings.
dcd28cf8 663 diff -wur "$(CURDIR)/expected/$(MULTIARCH_TRIPLE)/$(THREAD_MODEL)" "$(SYSROOT_SHARE)"
320054e8 664
8e8cc34a 665install: finish
320054e8
DG
666 mkdir -p "$(INSTALL_DIR)"
667 cp -r "$(SYSROOT)/lib" "$(SYSROOT)/share" "$(SYSROOT)/include" "$(INSTALL_DIR)"
668
111c8729
JE
669clean:
670 $(RM) -r "$(OBJDIR)"
671 $(RM) -r "$(SYSROOT)"
672
673.PHONY: default startup_files libc finish install include_dirs clean