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