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