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