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