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