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