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