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