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