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