1 # These variables are specifically meant to be overridable via the make
3 # ?= doesn't work for CC and AR because make has a default value for them.
4 ifeq ($(origin CC
), default
)
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
)))
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
19 MALLOC_IMPL ?
= dlmalloc
21 BUILD_LIBC_TOP_HALF ?
= yes
22 # The directory where we're store intermediate artifacts.
23 OBJDIR ?
= $(CURDIR
)/build
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
30 BULK_MEMORY_THRESHOLD ?
= 32
32 # Variables from this point on are not meant to be overridable via the
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
40 # These variables describe the locations of various files and directories in
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
= \
54 $(shell find
$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC
) -name \
*.c
) \
55 $(shell find
$(LIBC_BOTTOM_HALF_SOURCES
) -name \
*.c
))
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
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
)/, \
89 misc
/getdomainname.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 \
109 fenv
/fesetexceptflag.c \
110 fenv
/fegetexceptflag.c \
111 fenv
/feholdexcept.c \
116 exit
/at_quick_exit.c \
129 time
/timespec_get.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 \
141 dirent
/versionsort.c \
147 unistd
/posix_close.c \
149 legacy
/getpagesize.c \
150 thread
/thrd_sleep.c \
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 \
156 %/tmpnam.c
%/tmpfile.c
%/tempnam.c \
157 %/popen.c
%/pclose.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 \
175 %/floorf.c
%/floor.c \
176 %/truncf.c
%/trunc.c \
178 %/nearbyintf.c
%/nearbyint.c \
181 %/copysignf.c
%/copysign.c \
182 %/fminf.c
%/fmaxf.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
)
190 ifeq ($(THREAD_MODEL
), posix
)
191 LIBC_TOP_HALF_MUSL_SOURCES
+= \
192 $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/, \
194 thread
/__timedwait.c \
195 thread
/pthread_cleanup_push.c \
196 thread
/pthread_cond_broadcast.c \
197 thread
/pthread_cond_destroy.c \
198 thread
/pthread_cond_init.c \
199 thread
/pthread_cond_signal.c \
200 thread
/pthread_cond_timedwait.c \
201 thread
/pthread_cond_wait.c \
202 thread
/pthread_condattr_destroy.c \
203 thread
/pthread_condattr_init.c \
204 thread
/pthread_condattr_setclock.c \
205 thread
/pthread_condattr_setpshared.c \
206 thread
/pthread_create.c \
207 thread
/pthread_mutex_consistent.c \
208 thread
/pthread_mutex_destroy.c \
209 thread
/pthread_mutex_init.c \
210 thread
/pthread_mutex_getprioceiling.c \
211 thread
/pthread_mutex_lock.c \
212 thread
/pthread_mutex_timedlock.c \
213 thread
/pthread_mutex_trylock.c \
214 thread
/pthread_mutex_unlock.c \
215 thread
/pthread_mutexattr_destroy.c \
216 thread
/pthread_mutexattr_init.c \
217 thread
/pthread_mutexattr_setprotocol.c \
218 thread
/pthread_mutexattr_setpshared.c \
219 thread
/pthread_mutexattr_setrobust.c \
220 thread
/pthread_mutexattr_settype.c \
221 thread
/pthread_rwlock_destroy.c \
222 thread
/pthread_rwlock_init.c \
223 thread
/pthread_rwlock_rdlock.c \
224 thread
/pthread_rwlock_timedrdlock.c \
225 thread
/pthread_rwlock_timedwrlock.c \
226 thread
/pthread_rwlock_tryrdlock.c \
227 thread
/pthread_rwlock_trywrlock.c \
228 thread
/pthread_rwlock_unlock.c \
229 thread
/pthread_rwlock_wrlock.c \
230 thread
/pthread_rwlockattr_destroy.c \
231 thread
/pthread_rwlockattr_init.c \
232 thread
/pthread_rwlockattr_setpshared.c \
233 thread
/pthread_setcancelstate.c \
234 thread
/pthread_testcancel.c \
235 thread
/sem_destroy.c \
236 thread
/sem_getvalue.c \
239 thread
/sem_timedwait.c \
240 thread
/sem_trywait.c \
245 MUSL_PRINTSCAN_SOURCES
= \
246 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/internal
/floatscan.c \
247 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/stdio
/vfprintf.c \
248 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/stdio
/vfwprintf.c \
249 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/stdio
/vfscanf.c \
250 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/stdlib
/strtod.c \
251 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/stdlib
/wcstod.c
252 BULK_MEMORY_SOURCES
= \
253 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/string
/memcpy.c \
254 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/string
/memmove.c \
255 $(LIBC_TOP_HALF_MUSL_SRC_DIR
)/string
/memset.c
256 LIBC_TOP_HALF_HEADERS_PRIVATE
= $(LIBC_TOP_HALF_DIR
)/headers
/private
257 LIBC_TOP_HALF_SOURCES
= $(LIBC_TOP_HALF_DIR
)/sources
258 LIBC_TOP_HALF_ALL_SOURCES
= \
259 $(LIBC_TOP_HALF_MUSL_SOURCES
) \
260 $(sort $(shell find
$(LIBC_TOP_HALF_SOURCES
) -name \
*.c
))
262 # Add any extra flags
263 CFLAGS
= $(EXTRA_CFLAGS
)
265 CFLAGS
+= --target
=$(TARGET_TRIPLE
)
266 # WebAssembly floating-point match doesn't trap.
267 # TODO: Add -fno-signaling-nans when the compiler supports it.
268 CFLAGS
+= -fno-trapping-math
269 # Add all warnings, but disable a few which occur in third-party code.
270 CFLAGS
+= -Wall
-Wextra
-Werror \
271 -Wno-null-pointer-arithmetic \
272 -Wno-unused-parameter \
274 -Wno-unused-variable \
275 -Wno-unused-function \
276 -Wno-ignored-attributes \
277 -Wno-missing-braces \
278 -Wno-ignored-pragmas \
279 -Wno-unused-but-set-variable \
280 -Wno-unknown-warning-option
282 # Configure support for threads.
283 ifeq ($(THREAD_MODEL
), single
)
284 CFLAGS
+= -mthread-model single
286 ifeq ($(THREAD_MODEL
), posix
)
287 # Specify the tls-model until LLVM 15 is released (which should contain
288 # https://reviews.llvm.org/D130053).
289 CFLAGS
+= -mthread-model posix
-pthread
-ftls-model
=local-exec
291 # Include cloudlib's directory to access the structure definition of clockid_t
292 CFLAGS
+= -I
$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC
)
295 # Expose the public headers to the implementation. We use `-isystem` for
296 # purpose for two reasons:
298 # 1. It only does `<...>` not `"...."` lookup. We are making a libc,
299 # which is a system library, so all public headers should be
300 # accessible via `<...>` and never also need `"..."`. `-isystem` main
301 # purpose is to only effect `<...>` lookup.
303 # 2. The `-I` for private headers added for specific C files below
304 # should come earlier in the search path, so they can "override"
305 # and/or `#include_next` the public headers. `-isystem` (like
306 # `-idirafter`) comes later in the search path than `-I`.
307 CFLAGS
+= -isystem
"$(SYSROOT_INC)"
309 # These variables describe the locations of various files and directories in
311 objs
= $(patsubst $(CURDIR
)/%.c
,$(OBJDIR
)/%.o
,$(1))
312 DLMALLOC_OBJS
= $(call objs
,$(DLMALLOC_SOURCES
))
313 LIBC_BOTTOM_HALF_ALL_OBJS
= $(call objs
,$(LIBC_BOTTOM_HALF_ALL_SOURCES
))
314 LIBC_TOP_HALF_ALL_OBJS
= $(call objs
,$(LIBC_TOP_HALF_ALL_SOURCES
))
315 ifeq ($(MALLOC_IMPL
),dlmalloc
)
316 LIBC_OBJS
+= $(DLMALLOC_OBJS
)
317 else ifeq ($(MALLOC_IMPL
),none
)
318 # No object files to add.
320 $(error unknown malloc implementation
$(MALLOC_IMPL
))
322 # Add libc-bottom-half's objects.
323 LIBC_OBJS
+= $(LIBC_BOTTOM_HALF_ALL_OBJS
)
324 ifeq ($(BUILD_LIBC_TOP_HALF
),yes
)
325 # libc-top-half is musl.
326 LIBC_OBJS
+= $(LIBC_TOP_HALF_ALL_OBJS
)
328 MUSL_PRINTSCAN_OBJS
= $(call objs
,$(MUSL_PRINTSCAN_SOURCES
))
329 MUSL_PRINTSCAN_LONG_DOUBLE_OBJS
= $(patsubst %.o
,%.long-double.o
,$(MUSL_PRINTSCAN_OBJS
))
330 MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS
= $(patsubst %.o
,%.no-floating-point.o
,$(MUSL_PRINTSCAN_OBJS
))
331 BULK_MEMORY_OBJS
= $(call objs
,$(BULK_MEMORY_SOURCES
))
332 LIBWASI_EMULATED_MMAN_OBJS
= $(call objs
,$(LIBWASI_EMULATED_MMAN_SOURCES
))
333 LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS
= $(call objs
,$(LIBWASI_EMULATED_PROCESS_CLOCKS_SOURCES
))
334 LIBWASI_EMULATED_GETPID_OBJS
= $(call objs
,$(LIBWASI_EMULATED_GETPID_SOURCES
))
335 LIBWASI_EMULATED_SIGNAL_OBJS
= $(call objs
,$(LIBWASI_EMULATED_SIGNAL_SOURCES
))
336 LIBWASI_EMULATED_SIGNAL_MUSL_OBJS
= $(call objs
,$(LIBWASI_EMULATED_SIGNAL_MUSL_SOURCES
))
338 # These variables describe the locations of various files and
339 # directories in the generated sysroot tree.
340 SYSROOT_LIB
:= $(SYSROOT
)/lib
/$(MULTIARCH_TRIPLE
)
341 SYSROOT_INC
= $(SYSROOT
)/include
342 SYSROOT_SHARE
= $(SYSROOT
)/share
/$(MULTIARCH_TRIPLE
)
344 # Files from musl's include directory that we don't want to install in the
345 # sysroot's include directory.
348 # Remove files which aren't headers (we generate alltypes.h below).
349 MUSL_OMIT_HEADERS
+= \
350 "bits/syscall.h.in" \
351 "bits/alltypes.h.in" \
354 # Use the compiler's version of these headers.
355 MUSL_OMIT_HEADERS
+= \
359 # Use the WASI errno definitions.
360 MUSL_OMIT_HEADERS
+= \
363 # Remove headers that aren't supported yet or that aren't relevant for WASI.
364 MUSL_OMIT_HEADERS
+= \
367 "sys/kd.h" "sys/vt.h" "sys/soundcard.h" "sys/sem.h" \
368 "sys/shm.h" "sys/msg.h" "sys/ipc.h" "sys/ptrace.h" \
370 "bits/kd.h" "bits/vt.h" "bits/soundcard.h" "bits/sem.h" \
371 "bits/shm.h" "bits/msg.h" "bits/ipc.h" "bits/ptrace.h" \
375 "syslog.h" "sys/syslog.h" \
376 "wait.h" "sys/wait.h" \
377 "ucontext.h" "sys/ucontext.h" \
383 "sys/epoll.h" "sys/reboot.h" "sys/swap.h" \
384 "sys/sendfile.h" "sys/inotify.h" \
393 "sys/personality.h" \
394 "elf.h" "link.h" "bits/link.h" \
395 "scsi/scsi.h" "scsi/scsi_ioctl.h" "scsi/sg.h" \
397 "pwd.h" "shadow.h" "grp.h" \
417 "netinet/if_ether.h" \
424 ifeq ($(THREAD_MODEL
), single
)
425 # Remove headers not supported in single-threaded mode.
426 MUSL_OMIT_HEADERS
+= "pthread.h"
431 $(SYSROOT_LIB
)/libc.a
: $(LIBC_OBJS
)
433 $(SYSROOT_LIB
)/libc-printscan-long-double.a
: $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS
)
435 $(SYSROOT_LIB
)/libc-printscan-no-floating-point.a
: $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS
)
437 $(SYSROOT_LIB
)/libwasi-emulated-mman.a
: $(LIBWASI_EMULATED_MMAN_OBJS
)
439 $(SYSROOT_LIB
)/libwasi-emulated-process-clocks.a
: $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS
)
441 $(SYSROOT_LIB
)/libwasi-emulated-getpid.a
: $(LIBWASI_EMULATED_GETPID_OBJS
)
443 $(SYSROOT_LIB
)/libwasi-emulated-signal.a
: $(LIBWASI_EMULATED_SIGNAL_OBJS
) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS
)
447 # On Windows, the commandline for the ar invocation got too long, so it needs to be split up.
448 $(AR
) crs
$@
$(wordlist
1, 199, $^
)
449 $(AR
) crs
$@
$(wordlist
200, 399, $^
)
450 $(AR
) crs
$@
$(wordlist
400, 599, $^
)
451 $(AR
) crs
$@
$(wordlist
600, 799, $^
)
452 # This might eventually overflow again, but at least it'll do so in a loud way instead of
453 # silently dropping the tail.
454 $(AR
) crs
$@
$(wordlist
800, 100000, $^
)
456 $(MUSL_PRINTSCAN_OBJS
): CFLAGS
+= \
457 -D__wasilibc_printscan_no_long_double \
458 -D__wasilibc_printscan_full_support_option
="\"add -lc-printscan-long-double to the link command\""
460 $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS
): CFLAGS
+= \
461 -D__wasilibc_printscan_no_floating_point \
462 -D__wasilibc_printscan_floating_point_support_option
="\"remove -lc-printscan-no-floating-point from the link command\""
464 # TODO: apply -mbulk-memory globally, once
465 # https://github.com/llvm/llvm-project/issues/52618 is resolved
466 $(BULK_MEMORY_OBJS
): CFLAGS
+= \
469 $(BULK_MEMORY_OBJS
): CFLAGS
+= \
470 -DBULK_MEMORY_THRESHOLD
=$(BULK_MEMORY_THRESHOLD
)
472 $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS
): CFLAGS
+= \
473 -D_WASI_EMULATED_SIGNAL
475 $(OBJDIR
)/%.long-double.o
: $(CURDIR
)/%.c include_dirs
477 $(CC
) $(CFLAGS
) -MD
-MP
-o
$@
-c
$<
479 $(OBJDIR
)/%.no-floating-point.o
: $(CURDIR
)/%.c include_dirs
481 $(CC
) $(CFLAGS
) -MD
-MP
-o
$@
-c
$<
483 $(OBJDIR
)/%.o
: $(CURDIR
)/%.c include_dirs
485 $(CC
) $(CFLAGS
) -MD
-MP
-o
$@
-c
$<
487 -include $(shell find
$(OBJDIR
) -name \
*.d
)
489 $(DLMALLOC_OBJS
): CFLAGS
+= \
492 startup_files
$(LIBC_BOTTOM_HALF_ALL_OBJS
): CFLAGS
+= \
493 -I
$(LIBC_BOTTOM_HALF_HEADERS_PRIVATE
) \
494 -I
$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC
) \
495 -I
$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC
) \
496 -I
$(LIBC_TOP_HALF_MUSL_SRC_DIR
)/include \
497 -I
$(LIBC_TOP_HALF_MUSL_SRC_DIR
)/internal
499 $(LIBC_TOP_HALF_ALL_OBJS
) $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS
) $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS
) $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS
): CFLAGS
+= \
500 -I
$(LIBC_TOP_HALF_MUSL_SRC_DIR
)/include \
501 -I
$(LIBC_TOP_HALF_MUSL_SRC_DIR
)/internal \
502 -I
$(LIBC_TOP_HALF_MUSL_DIR
)/arch
/wasm32 \
503 -I
$(LIBC_TOP_HALF_MUSL_DIR
)/arch
/generic \
504 -I
$(LIBC_TOP_HALF_HEADERS_PRIVATE
) \
506 -Wno-shift-op-parentheses \
507 -Wno-bitwise-op-parentheses \
508 -Wno-logical-op-parentheses \
509 -Wno-string-plus-int \
513 $(LIBWASI_EMULATED_PROCESS_CLOCKS_OBJS
): CFLAGS
+= \
514 -I
$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC
)
518 # Install the include files.
520 mkdir
-p
"$(SYSROOT_INC)"
521 cp
-r
"$(LIBC_BOTTOM_HALF_HEADERS_PUBLIC)"/* "$(SYSROOT_INC)"
523 # Generate musl's bits/alltypes.h header.
524 mkdir
-p
"$(SYSROOT_INC)/bits"
525 sed
-f
$(LIBC_TOP_HALF_MUSL_DIR
)/tools
/mkalltypes.sed \
526 $(LIBC_TOP_HALF_MUSL_DIR
)/arch
/wasm32
/bits
/alltypes.h.in \
527 $(LIBC_TOP_HALF_MUSL_DIR
)/include/alltypes.h.in \
528 > "$(SYSROOT_INC)/bits/alltypes.h"
530 # Copy in the bulk of musl's public header files.
531 cp
-r
"$(LIBC_TOP_HALF_MUSL_INC)"/* "$(SYSROOT_INC)"
532 # Copy in the musl's "bits" header files.
533 cp
-r
"$(LIBC_TOP_HALF_MUSL_DIR)"/arch
/generic
/bits
/* "$(SYSROOT_INC)/bits"
534 cp
-r
"$(LIBC_TOP_HALF_MUSL_DIR)"/arch
/wasm32
/bits
/* "$(SYSROOT_INC)/bits"
536 # Remove selected header files.
537 $(RM
) $(patsubst %,$(SYSROOT_INC
)/%,$(MUSL_OMIT_HEADERS
))
539 startup_files
: include_dirs
541 # Build the startup files.
543 @mkdir
-p
"$(OBJDIR)"
545 $(CC
) $(CFLAGS
) -c
$(LIBC_BOTTOM_HALF_CRT_SOURCES
) -MD
-MP
&& \
546 mkdir
-p
"$(SYSROOT_LIB)" && \
547 mv
*.o
"$(SYSROOT_LIB)"
550 $(SYSROOT_LIB
)/libc.a \
551 $(SYSROOT_LIB
)/libc-printscan-long-double.a \
552 $(SYSROOT_LIB
)/libc-printscan-no-floating-point.a \
553 $(SYSROOT_LIB
)/libwasi-emulated-mman.a \
554 $(SYSROOT_LIB
)/libwasi-emulated-process-clocks.a \
555 $(SYSROOT_LIB
)/libwasi-emulated-getpid.a \
556 $(SYSROOT_LIB
)/libwasi-emulated-signal.a
558 finish
: startup_files libc
560 # Create empty placeholder libraries.
562 for name in m rt pthread crypt util xnet resolv dl
; do \
563 $(AR
) crs
"$(SYSROOT_LIB)/lib$${name}.a"; \
567 # The build succeeded! The generated sysroot is in $(SYSROOT).
570 # The check for defined and undefined symbols expects there to be a heap
571 # alloctor (providing malloc, calloc, free, etc). Skip this step if the build
572 # is done without a malloc implementation.
573 ifneq ($(MALLOC_IMPL
),none
)
574 finish
: check-symbols
577 DEFINED_SYMBOLS
= $(SYSROOT_SHARE
)/defined-symbols.txt
578 UNDEFINED_SYMBOLS
= $(SYSROOT_SHARE
)/undefined-symbols.txt
580 check-symbols
: startup_files libc
582 # Collect metadata on the sysroot and perform sanity checks.
584 mkdir
-p
"$(SYSROOT_SHARE)"
587 # Collect symbol information.
589 @
# TODO: Use llvm-nm --extern-only instead of grep. This is blocked on
590 @
# LLVM PR40497, which is fixed in 9.0, but not in 8.0.
591 @
# Ignore certain llvm builtin symbols such as those starting with __mul
592 @
# since these dependencies can vary between llvm versions.
593 "$(NM)" --defined-only
"$(SYSROOT_LIB)"/libc.a
"$(SYSROOT_LIB)"/libwasi-emulated-
*.a
"$(SYSROOT_LIB)"/*.o \
594 |grep
' [[:upper:]] ' |sed
's/.* [[:upper:]] //' |LC_ALL
=C
sort |uniq
> "$(DEFINED_SYMBOLS)"
595 for undef_sym in
$$("$(NM)" --undefined-only
"$(SYSROOT_LIB)"/libc.a
"$(SYSROOT_LIB)"/libc-
*.a
"$(SYSROOT_LIB)"/*.o \
596 |grep
' U ' |sed
's/.* U //' |LC_ALL
=C
sort |uniq
); do \
597 grep
-q
'\<'$$undef_sym'\>' "$(DEFINED_SYMBOLS)" || echo
$$undef_sym; \
598 done | grep
-v
"^__mul" > "$(UNDEFINED_SYMBOLS)"
599 grep
'^_*imported_wasi_' "$(UNDEFINED_SYMBOLS)" \
600 > "$(SYSROOT_LIB)/libc.imports"
603 # Generate a test file that includes all public C header files.
605 cd
"$(SYSROOT_INC)" && \
606 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 \
607 echo
'#include <'$$header'>' | sed
's/\.\///' ; \
608 done |LC_ALL
=C
sort >$(SYSROOT_SHARE
)/include-all.c
; \
612 # Test that it compiles.
614 $(CC
) $(CFLAGS
) -fsyntax-only
"$(SYSROOT_SHARE)/include-all.c" -Wno-\
#warnings
617 # Collect all the predefined macros, except for compiler version macros
618 # which we don't need to track here.
621 @
# For the __*_ATOMIC_*_LOCK_FREE macros, squash individual compiler names
622 @
# to attempt, toward keeping these files compiler-independent.
624 @
# We have to add `-isystem $(SYSROOT_INC)` because otherwise clang puts
625 @
# its builtin include path first, which produces compiler-specific
628 @
# TODO: Undefine __FLOAT128__ for now since it's not in clang 8.0.
629 @
# TODO: Filter out __FLT16_* for now, as not all versions of clang have these.
630 @
# TODO: Filter out __NO_MATH_ERRNO_ and a few __*WIDTH__ that are new to clang 14.
631 @
# TODO: clang defined __FLT_EVAL_METHOD__ until clang 15, so we force-undefine it
632 @
# for older versions.
633 $(CC
) $(CFLAGS
) "$(SYSROOT_SHARE)/include-all.c" \
634 -isystem
$(SYSROOT_INC
) \
636 -E
-dM
-Wno-\
#warnings \
642 -U__clang_patchlevel__ \
643 -U__clang_version__ \
644 -U__clang_literal_encoding__ \
645 -U__clang_wide_literal_encoding__ \
648 -U__GNUC_PATCHLEVEL__ \
651 -U__NO_MATH_ERRNO__ \
652 -U__BITINT_MAXWIDTH__ \
653 -U__FLT_EVAL_METHOD__
-Wno-builtin-macro-redefined \
654 | sed
-e
's/__[[:upper:][:digit:]]*_ATOMIC_\([[:upper:][:digit:]_]*\)_LOCK_FREE/__compiler_ATOMIC_\1_LOCK_FREE/' \
655 | grep
-v
'^#define __FLT16_' \
656 | grep
-v
'^#define __\(BOOL\|INT_\(LEAST\|FAST\)\(8\|16\|32\|64\)\|INT\|LONG\|LLONG\|SHRT\)_WIDTH__' \
657 > "$(SYSROOT_SHARE)/predefined-macros.txt"
659 # Check that the computed metadata matches the expected metadata.
660 # This ignores whitespace because on Windows the output has CRLF line endings.
661 diff
-wur
"$(CURDIR)/expected/$(MULTIARCH_TRIPLE)/$(THREAD_MODEL)" "$(SYSROOT_SHARE)"
664 mkdir
-p
"$(INSTALL_DIR)"
665 cp
-r
"$(SYSROOT)/lib" "$(SYSROOT)/share" "$(SYSROOT)/include" "$(INSTALL_DIR)"
669 $(RM
) -r
"$(SYSROOT)"
671 .PHONY
: default startup_files libc finish
install include_dirs
clean