]>
Commit | Line | Data |
---|---|---|
41af0db3 DG |
1 | # These variables are specifically meant to be overridable via the make |
2 | # command-line. | |
255e6f7c TS |
3 | WASM_CC ?= clang |
4 | WASM_NM ?= $(patsubst %clang,%llvm-nm,$(WASM_CC)) | |
5 | WASM_AR ?= $(patsubst %clang,%llvm-ar,$(WASM_CC)) | |
7fcc4f29 | 6 | WASM_CFLAGS ?= -O2 -DNDEBUG |
320054e8 | 7 | # The directory where we build the sysroot. |
255e6f7c | 8 | SYSROOT ?= $(CURDIR)/sysroot |
320054e8 | 9 | # A directory to install to for "make install". |
255e6f7c | 10 | INSTALL_DIR ?= /usr/local |
dbfccac2 | 11 | # single or posix |
41af0db3 | 12 | THREAD_MODEL ?= single |
320054e8 | 13 | # yes or no |
41af0db3 DG |
14 | BUILD_DLMALLOC ?= yes |
15 | BUILD_LIBC_BOTTOM_HALF ?= yes | |
16 | BUILD_LIBC_TOP_HALF ?= yes | |
320054e8 | 17 | # The directory where we're store intermediate artifacts. |
41af0db3 | 18 | OBJDIR ?= $(CURDIR)/build |
320054e8 DG |
19 | |
20 | # Check dependencies. | |
21 | ifeq ($(BUILD_LIBC_TOP_HALF),yes) | |
22 | ifneq ($(BUILD_LIBC_BOTTOM_HALF),yes) | |
23 | $(error BUILD_LIBC_TOP_HALF=yes depends on BUILD_LIBC_BOTTOM_HALF=yes) | |
24 | endif | |
25 | endif | |
26 | ifeq ($(BUILD_LIBC_BOTTOM_HALF),yes) | |
27 | ifneq ($(BUILD_DLMALLOC),yes) | |
28 | $(error BUILD_LIBC_BOTTOM_HALF=yes depends on BUILD_DLMALLOC=yes) | |
29 | endif | |
30 | endif | |
dcf13b6f | 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. | |
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. | |
320054e8 | 42 | BASICS_DIR = $(CURDIR)/basics |
dbfccac2 | 43 | BASICS_INC = $(BASICS_DIR)/include |
ad03c82d | 44 | BASICS_CRT_SOURCES = $(wildcard $(BASICS_DIR)/crt/*.c) |
a280fead DG |
45 | BASICS_SOURCES = \ |
46 | $(wildcard $(BASICS_DIR)/sources/*.c) \ | |
47 | $(wildcard $(BASICS_DIR)/sources/math/*.c) | |
320054e8 | 48 | DLMALLOC_DIR = $(CURDIR)/dlmalloc |
dbfccac2 | 49 | DLMALLOC_SRC_DIR = $(DLMALLOC_DIR)/src |
320054e8 | 50 | DLMALLOC_SOURCES = $(DLMALLOC_SRC_DIR)/dlmalloc.c |
dbfccac2 | 51 | DLMALLOC_INC = $(DLMALLOC_DIR)/include |
320054e8 DG |
52 | LIBC_BOTTOM_HALF_DIR = $(CURDIR)/libc-bottom-half |
53 | LIBC_BOTTOM_HALF_CLOUDLIBC_SRC = $(LIBC_BOTTOM_HALF_DIR)/cloudlibc/src | |
54 | LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC = $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)/include | |
55 | LIBC_BOTTOM_HALF_HEADERS_PUBLIC = $(LIBC_BOTTOM_HALF_DIR)/headers/public | |
56 | LIBC_BOTTOM_HALF_HEADERS_PRIVATE = $(LIBC_BOTTOM_HALF_DIR)/headers/private | |
57 | LIBC_BOTTOM_HALF_LIBPREOPEN_DIR = $(LIBC_BOTTOM_HALF_DIR)/libpreopen | |
320054e8 DG |
58 | LIBC_BOTTOM_HALF_SOURCES = $(LIBC_BOTTOM_HALF_DIR)/sources |
59 | LIBC_BOTTOM_HALF_ALL_SOURCES = \ | |
60 | $(shell find $(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) -name \*.c) \ | |
7fcc4f29 | 61 | $(LIBC_BOTTOM_HALF_LIBPREOPEN_DIR)/libpreopen.c \ |
320054e8 DG |
62 | $(shell find $(LIBC_BOTTOM_HALF_SOURCES) -name \*.c) |
63 | LIBWASI_EMULATED_MMAN_SOURCES = \ | |
64 | $(shell find $(LIBC_BOTTOM_HALF_DIR)/mman -name \*.c) | |
65 | LIBC_BOTTOM_HALF_CRT_SOURCES = $(wildcard $(LIBC_BOTTOM_HALF_DIR)/crt/*.c) | |
66 | LIBC_TOP_HALF_DIR = $(CURDIR)/libc-top-half | |
67 | LIBC_TOP_HALF_MUSL_DIR = $(LIBC_TOP_HALF_DIR)/musl | |
68 | LIBC_TOP_HALF_MUSL_SRC_DIR = $(LIBC_TOP_HALF_MUSL_DIR)/src | |
69 | LIBC_TOP_HALF_MUSL_INC = $(LIBC_TOP_HALF_MUSL_DIR)/include | |
70 | LIBC_TOP_HALF_MUSL_SOURCES = \ | |
41af0db3 DG |
71 | $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \ |
72 | misc/a64l.c \ | |
73 | misc/basename.c \ | |
74 | misc/dirname.c \ | |
75 | misc/ffs.c \ | |
76 | misc/ffsl.c \ | |
77 | misc/ffsll.c \ | |
78 | misc/fmtmsg.c \ | |
79 | misc/getdomainname.c \ | |
80 | misc/gethostid.c \ | |
81 | misc/getopt.c \ | |
82 | misc/getopt_long.c \ | |
83 | misc/getsubopt.c \ | |
84 | misc/uname.c \ | |
85 | misc/nftw.c \ | |
86 | errno/strerror.c \ | |
87 | network/htonl.c \ | |
88 | network/htons.c \ | |
89 | network/ntohl.c \ | |
90 | network/ntohs.c \ | |
91 | network/inet_ntop.c \ | |
92 | network/inet_pton.c \ | |
93 | network/inet_aton.c \ | |
94 | network/in6addr_any.c \ | |
95 | network/in6addr_loopback.c \ | |
96 | fenv/fenv.c \ | |
97 | fenv/fesetround.c \ | |
98 | fenv/feupdateenv.c \ | |
99 | fenv/fesetexceptflag.c \ | |
100 | fenv/fegetexceptflag.c \ | |
101 | fenv/feholdexcept.c \ | |
102 | exit/exit.c \ | |
103 | exit/atexit.c \ | |
104 | exit/assert.c \ | |
105 | exit/quick_exit.c \ | |
106 | exit/at_quick_exit.c \ | |
107 | time/strftime.c \ | |
108 | time/asctime.c \ | |
109 | time/asctime_r.c \ | |
110 | time/ctime.c \ | |
111 | time/ctime_r.c \ | |
112 | time/wcsftime.c \ | |
113 | time/strptime.c \ | |
114 | time/difftime.c \ | |
115 | time/timegm.c \ | |
116 | time/ftime.c \ | |
117 | time/gmtime.c \ | |
118 | time/gmtime_r.c \ | |
119 | time/timespec_get.c \ | |
120 | time/getdate.c \ | |
121 | time/localtime.c \ | |
122 | time/localtime_r.c \ | |
123 | time/mktime.c \ | |
124 | time/__tm_to_secs.c \ | |
125 | time/__month_to_secs.c \ | |
126 | time/__secs_to_tm.c \ | |
127 | time/__year_to_secs.c \ | |
128 | time/__tz.c \ | |
129 | fcntl/creat.c \ | |
130 | dirent/alphasort.c \ | |
131 | dirent/versionsort.c \ | |
132 | env/clearenv.c \ | |
133 | env/getenv.c \ | |
134 | env/putenv.c \ | |
135 | env/setenv.c \ | |
136 | env/unsetenv.c \ | |
137 | unistd/posix_close.c \ | |
138 | ) \ | |
320054e8 DG |
139 | $(filter-out %/procfdname.c %/syscall.c %/syscall_ret.c %/vdso.c %/version.c, \ |
140 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/*.c)) \ | |
141 | $(filter-out %/flockfile.c %/funlockfile.c %/__lockfile.c %/ftrylockfile.c \ | |
142 | %/rename.c \ | |
143 | %/tmpnam.c %/tmpfile.c %/tempnam.c \ | |
144 | %/popen.c %/pclose.c \ | |
145 | %/remove.c \ | |
146 | %/gets.c, \ | |
147 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/*.c)) \ | |
148 | $(filter-out %/strsignal.c, \ | |
149 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/string/*.c)) \ | |
8048aeb5 | 150 | $(filter-out %/dcngettext.c %/textdomain.c %/bind_textdomain_codeset.c, \ |
320054e8 DG |
151 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/locale/*.c)) \ |
152 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/*.c) \ | |
153 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/search/*.c) \ | |
154 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/multibyte/*.c) \ | |
155 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/regex/*.c) \ | |
320054e8 DG |
156 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/prng/*.c) \ |
157 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/conf/*.c) \ | |
158 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/ctype/*.c) \ | |
900b8032 | 159 | $(filter-out %/__signbit.c %/__signbitf.c %/__signbitl.c \ |
16450763 DG |
160 | %/__fpclassify.c %/__fpclassifyf.c %/__fpclassifyl.c \ |
161 | %/ceilf.c %/ceil.c \ | |
162 | %/floorf.c %/floor.c \ | |
163 | %/truncf.c %/trunc.c \ | |
164 | %/rintf.c %/rint.c \ | |
165 | %/nearbyintf.c %/nearbyint.c \ | |
166 | %/sqrtf.c %/sqrt.c \ | |
167 | %/fabsf.c %/fabs.c \ | |
cf81683e DG |
168 | %/copysignf.c %/copysign.c \ |
169 | %/fminf.c %/fmaxf.c \ | |
170 | %/fmin.c %/fmax.c, \ | |
900b8032 | 171 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/math/*.c)) \ |
7d5074ff DG |
172 | $(filter-out %/crealf.c %/creal.c \ |
173 | %/cimagf.c %/cimag.c, \ | |
174 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \ | |
320054e8 DG |
175 | $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c) |
176 | MUSL_PRINTSCAN_SOURCES = \ | |
177 | $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/floatscan.c \ | |
178 | $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfprintf.c \ | |
2201343c | 179 | $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfwprintf.c \ |
320054e8 DG |
180 | $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdio/vfscanf.c \ |
181 | $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/strtod.c \ | |
182 | $(LIBC_TOP_HALF_MUSL_SRC_DIR)/stdlib/wcstod.c | |
183 | LIBC_TOP_HALF_HEADERS_PRIVATE = $(LIBC_TOP_HALF_DIR)/headers/private | |
184 | LIBC_TOP_HALF_SOURCES = $(LIBC_TOP_HALF_DIR)/sources | |
185 | LIBC_TOP_HALF_ALL_SOURCES = \ | |
186 | $(LIBC_TOP_HALF_MUSL_SOURCES) \ | |
187 | $(shell find $(LIBC_TOP_HALF_SOURCES) -name \*.c) | |
188 | ||
320054e8 | 189 | # Set the target. |
41af0db3 | 190 | WASM_CFLAGS += --target=$(TARGET_TRIPLE) |
320054e8 DG |
191 | # WebAssembly floating-point match doesn't trap. |
192 | # TODO: Add -fno-signaling-nans when the compiler supports it. | |
41af0db3 | 193 | WASM_CFLAGS += -fno-trapping-math |
dbfccac2 DG |
194 | |
195 | # Configure support for threads. | |
320054e8 | 196 | ifeq ($(THREAD_MODEL), single) |
41af0db3 | 197 | WASM_CFLAGS += -mthread-model single |
dbfccac2 | 198 | endif |
320054e8 | 199 | ifeq ($(THREAD_MODEL), posix) |
41af0db3 | 200 | WASM_CFLAGS += -mthread-model posix -pthread |
320054e8 DG |
201 | endif |
202 | ||
203 | # Set the sysroot. | |
41af0db3 | 204 | WASM_CFLAGS += --sysroot="$(SYSROOT)" |
320054e8 | 205 | |
41af0db3 DG |
206 | # These variables describe the locations of various files and directories in |
207 | # the build tree. | |
320054e8 | 208 | objs = $(patsubst $(CURDIR)/%.c,$(OBJDIR)/%.o,$(1)) |
41af0db3 DG |
209 | BASICS_OBJS = $(call objs,$(BASICS_SOURCES)) |
210 | DLMALLOC_OBJS = $(call objs,$(DLMALLOC_SOURCES)) | |
211 | LIBC_BOTTOM_HALF_ALL_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_ALL_SOURCES)) | |
212 | LIBC_TOP_HALF_ALL_OBJS = $(call objs,$(LIBC_TOP_HALF_ALL_SOURCES)) | |
213 | LIBC_OBJS := $(BASICS_OBJS) | |
320054e8 | 214 | ifeq ($(BUILD_DLMALLOC),yes) |
41af0db3 | 215 | LIBC_OBJS += $(DLMALLOC_OBJS) |
dbfccac2 | 216 | endif |
320054e8 DG |
217 | ifeq ($(BUILD_LIBC_BOTTOM_HALF),yes) |
218 | # Override basics' string.o with libc-bottom-half's. | |
41af0db3 | 219 | LIBC_OBJS := $(filter-out %/string.o,$(LIBC_OBJS)) |
320054e8 | 220 | # Add libc-bottom-half's objects. |
41af0db3 | 221 | LIBC_OBJS += $(LIBC_BOTTOM_HALF_ALL_OBJS) |
320054e8 DG |
222 | endif |
223 | ifeq ($(BUILD_LIBC_TOP_HALF),yes) | |
224 | # Override libc-bottom-half's string.o with libc-top-half's. | |
41af0db3 | 225 | LIBC_OBJS := $(filter-out %/string.o,$(LIBC_OBJS)) |
320054e8 | 226 | # Override libc-bottom-half's qsort.o with libc-top-half's. |
41af0db3 | 227 | LIBC_OBJS := $(filter-out %/qsort.o,$(LIBC_OBJS)) |
320054e8 | 228 | # libc-top-half is musl. |
41af0db3 | 229 | LIBC_OBJS += $(LIBC_TOP_HALF_ALL_OBJS) |
320054e8 | 230 | endif |
41af0db3 DG |
231 | MUSL_PRINTSCAN_OBJS = $(call objs,$(MUSL_PRINTSCAN_SOURCES)) |
232 | MUSL_PRINTSCAN_LONG_DOUBLE_OBJS = $(patsubst %.o,%.long-double.o,$(MUSL_PRINTSCAN_OBJS)) | |
233 | MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS = $(patsubst %.o,%.no-floating-point.o,$(MUSL_PRINTSCAN_OBJS)) | |
234 | LIBWASI_EMULATED_MMAN_OBJS = $(call objs,$(LIBWASI_EMULATED_MMAN_SOURCES)) | |
235 | ||
236 | # These variables describe the locations of various files and | |
237 | # directories in the generated sysroot tree. | |
238 | SYSROOT_LIB := $(SYSROOT)/lib/$(MULTIARCH_TRIPLE) | |
239 | SYSROOT_INC = $(SYSROOT)/include | |
240 | SYSROOT_SHARE = $(SYSROOT)/share/$(MULTIARCH_TRIPLE) | |
320054e8 | 241 | |
1f409842 DG |
242 | # Files from musl's include directory that we don't want to install in the |
243 | # sysroot's include directory. | |
41af0db3 | 244 | MUSL_OMIT_HEADERS := |
1f409842 DG |
245 | |
246 | # Remove files which aren't headers (we generate alltypes.h below). | |
41af0db3 | 247 | MUSL_OMIT_HEADERS += \ |
1f409842 DG |
248 | "bits/syscall.h.in" \ |
249 | "bits/alltypes.h.in" \ | |
250 | "alltypes.h.in" | |
251 | ||
33e8b518 | 252 | # Use the compiler's version of these headers. |
41af0db3 | 253 | MUSL_OMIT_HEADERS += \ |
33e8b518 DG |
254 | "stdarg.h" \ |
255 | "stddef.h" | |
256 | ||
1f409842 | 257 | # Use the WASI errno definitions. |
41af0db3 | 258 | MUSL_OMIT_HEADERS += \ |
1f409842 DG |
259 | "bits/errno.h" |
260 | ||
261 | # Remove headers that aren't supported yet or that aren't relevant for WASI. | |
41af0db3 | 262 | MUSL_OMIT_HEADERS += \ |
1f409842 DG |
263 | "sys/procfs.h" \ |
264 | "sys/user.h" \ | |
265 | "sys/kd.h" "sys/vt.h" "sys/soundcard.h" "sys/sem.h" \ | |
266 | "sys/shm.h" "sys/msg.h" "sys/ipc.h" "sys/ptrace.h" \ | |
267 | "sys/statfs.h" \ | |
268 | "bits/kd.h" "bits/vt.h" "bits/soundcard.h" "bits/sem.h" \ | |
269 | "bits/shm.h" "bits/msg.h" "bits/ipc.h" "bits/ptrace.h" \ | |
270 | "bits/statfs.h" \ | |
271 | "sys/vfs.h" \ | |
272 | "sys/statvfs.h" \ | |
273 | "syslog.h" "sys/syslog.h" \ | |
274 | "wait.h" "sys/wait.h" \ | |
275 | "ucontext.h" "sys/ucontext.h" \ | |
276 | "paths.h" \ | |
277 | "utmp.h" "utmpx.h" \ | |
278 | "lastlog.h" \ | |
279 | "sys/acct.h" \ | |
280 | "sys/cachectl.h" \ | |
281 | "sys/epoll.h" "sys/reboot.h" "sys/swap.h" \ | |
282 | "sys/sendfile.h" "sys/inotify.h" \ | |
283 | "sys/quota.h" \ | |
284 | "sys/klog.h" \ | |
285 | "sys/fsuid.h" \ | |
286 | "sys/io.h" \ | |
287 | "sys/prctl.h" \ | |
288 | "sys/mtio.h" \ | |
289 | "sys/mount.h" \ | |
290 | "sys/fanotify.h" \ | |
291 | "sys/personality.h" \ | |
292 | "elf.h" "link.h" "bits/link.h" \ | |
293 | "scsi/scsi.h" "scsi/scsi_ioctl.h" "scsi/sg.h" \ | |
294 | "sys/auxv.h" \ | |
295 | "pwd.h" "shadow.h" "grp.h" \ | |
296 | "mntent.h" \ | |
297 | "netdb.h" \ | |
298 | "resolv.h" \ | |
299 | "pty.h" \ | |
300 | "dlfcn.h" \ | |
301 | "setjmp.h" \ | |
302 | "ulimit.h" \ | |
303 | "sys/xattr.h" \ | |
304 | "wordexp.h" \ | |
305 | "spawn.h" \ | |
306 | "sys/membarrier.h" \ | |
307 | "sys/signalfd.h" \ | |
308 | "termios.h" \ | |
309 | "sys/termios.h" \ | |
310 | "bits/termios.h" \ | |
311 | "net/if.h" \ | |
312 | "net/if_arp.h" \ | |
313 | "net/ethernet.h" \ | |
314 | "net/route.h" \ | |
315 | "netinet/if_ether.h" \ | |
316 | "netinet/ether.h" \ | |
eb7230cf DG |
317 | "sys/timerfd.h" \ |
318 | "libintl.h" \ | |
319 | "sys/sysmacros.h" \ | |
320 | "utime.h" | |
1f409842 DG |
321 | |
322 | ifeq ($(THREAD_MODEL), single) | |
323 | # Remove headers not supported in single-threaded mode. | |
41af0db3 | 324 | MUSL_OMIT_HEADERS += "aio.h" "pthread.h" |
1f409842 DG |
325 | endif |
326 | ||
41af0db3 | 327 | default: finish |
320054e8 DG |
328 | |
329 | $(SYSROOT_LIB)/libc.a: $(LIBC_OBJS) | |
330 | ||
331 | $(SYSROOT_LIB)/libc-printscan-long-double.a: $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) | |
332 | ||
333 | $(SYSROOT_LIB)/libc-printscan-no-floating-point.a: $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS) | |
334 | ||
335 | $(SYSROOT_LIB)/libwasi-emulated-mman.a: $(LIBWASI_EMULATED_MMAN_OBJS) | |
336 | ||
337 | %.a: | |
338 | @mkdir -p "$(@D)" | |
255e6f7c TS |
339 | # On Windows, the commandline for the ar invocation got too long, so it needs to be split up. |
340 | $(WASM_AR) crs $@ $(wordlist 1, 199, $^) | |
341 | $(WASM_AR) crs $@ $(wordlist 200, 399, $^) | |
342 | $(WASM_AR) crs $@ $(wordlist 400, 599, $^) | |
343 | $(WASM_AR) crs $@ $(wordlist 600, 799, $^) | |
344 | # This might eventually overflow again, but at least it'll do so in a loud way instead of | |
345 | # silently dropping the tail. | |
346 | $(WASM_AR) crs $@ $(wordlist 800, 100000, $^) | |
320054e8 | 347 | |
41af0db3 | 348 | $(MUSL_PRINTSCAN_OBJS): WASM_CFLAGS += \ |
320054e8 DG |
349 | -D__wasilibc_printscan_no_long_double \ |
350 | -D__wasilibc_printscan_full_support_option="\"add -lc-printscan-long-double to the link command\"" | |
351 | ||
41af0db3 | 352 | $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS): WASM_CFLAGS += \ |
320054e8 DG |
353 | -D__wasilibc_printscan_no_floating_point \ |
354 | -D__wasilibc_printscan_floating_point_support_option="\"remove -lc-printscan-no-floating-point from the link command\"" | |
355 | ||
8e8cc34a | 356 | $(OBJDIR)/%.long-double.o: $(CURDIR)/%.c include_dirs |
320054e8 DG |
357 | @mkdir -p "$(@D)" |
358 | "$(WASM_CC)" $(WASM_CFLAGS) -MD -MP -o $@ -c $< | |
dcf13b6f | 359 | |
8e8cc34a | 360 | $(OBJDIR)/%.no-floating-point.o: $(CURDIR)/%.c include_dirs |
320054e8 DG |
361 | @mkdir -p "$(@D)" |
362 | "$(WASM_CC)" $(WASM_CFLAGS) -MD -MP -o $@ -c $< | |
363 | ||
8e8cc34a | 364 | $(OBJDIR)/%.o: $(CURDIR)/%.c include_dirs |
320054e8 DG |
365 | @mkdir -p "$(@D)" |
366 | "$(WASM_CC)" $(WASM_CFLAGS) -MD -MP -o $@ -c $< | |
367 | ||
255e6f7c | 368 | -include $(shell find $(OBJDIR) -name \*.d) |
320054e8 | 369 | |
41af0db3 | 370 | $(DLMALLOC_OBJS): WASM_CFLAGS += \ |
320054e8 DG |
371 | -I$(DLMALLOC_INC) |
372 | ||
41af0db3 | 373 | startup_files $(LIBC_BOTTOM_HALF_ALL_OBJS): WASM_CFLAGS += \ |
320054e8 DG |
374 | -I$(LIBC_BOTTOM_HALF_HEADERS_PRIVATE) \ |
375 | -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC_INC) \ | |
7fcc4f29 | 376 | -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC) |
320054e8 | 377 | |
41af0db3 | 378 | $(LIBC_TOP_HALF_ALL_OBJS) $(MUSL_PRINTSCAN_LONG_DOUBLE_OBJS) $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS): WASM_CFLAGS += \ |
320054e8 DG |
379 | -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/include \ |
380 | -I$(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal \ | |
381 | -I$(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32 \ | |
382 | -I$(LIBC_TOP_HALF_MUSL_DIR)/arch/generic \ | |
383 | -I$(LIBC_TOP_HALF_HEADERS_PRIVATE) \ | |
384 | -Wno-parentheses \ | |
385 | -Wno-shift-op-parentheses \ | |
386 | -Wno-bitwise-op-parentheses \ | |
387 | -Wno-logical-op-parentheses \ | |
388 | -Wno-string-plus-int \ | |
389 | -Wno-dangling-else \ | |
390 | -Wno-unknown-pragmas | |
391 | ||
8e8cc34a | 392 | include_dirs: |
dcf13b6f | 393 | $(RM) -r "$(SYSROOT)" |
dcf13b6f DG |
394 | |
395 | # | |
396 | # Install the include files. | |
397 | # | |
320054e8 DG |
398 | mkdir -p "$(SYSROOT_INC)" |
399 | cp -r "$(BASICS_INC)" "$(SYSROOT)" | |
400 | cp -r "$(LIBC_BOTTOM_HALF_HEADERS_PUBLIC)"/* "$(SYSROOT_INC)" | |
401 | ||
402 | # Generate musl's bits/alltypes.h header. | |
403 | mkdir -p "$(SYSROOT_INC)/bits" | |
404 | sed -f $(LIBC_TOP_HALF_MUSL_DIR)/tools/mkalltypes.sed \ | |
405 | $(LIBC_TOP_HALF_MUSL_DIR)/arch/wasm32/bits/alltypes.h.in \ | |
406 | $(LIBC_TOP_HALF_MUSL_DIR)/include/alltypes.h.in \ | |
407 | > "$(SYSROOT_INC)/bits/alltypes.h" | |
408 | ||
409 | # Copy in the bulk of musl's public header files. | |
410 | cp -r "$(LIBC_TOP_HALF_MUSL_INC)"/* "$(SYSROOT_INC)" | |
411 | # Copy in the musl's "bits" header files. | |
412 | cp -r "$(LIBC_TOP_HALF_MUSL_DIR)"/arch/generic/bits/* "$(SYSROOT_INC)/bits" | |
413 | cp -r "$(LIBC_TOP_HALF_MUSL_DIR)"/arch/wasm32/bits/* "$(SYSROOT_INC)/bits" | |
414 | ||
1f409842 DG |
415 | # Remove selected header files. |
416 | $(RM) $(patsubst %,$(SYSROOT_INC)/%,$(MUSL_OMIT_HEADERS)) | |
320054e8 DG |
417 | |
418 | ifeq ($(BUILD_LIBC_BOTTOM_HALF),no) | |
41af0db3 | 419 | CRT_SOURCES = $(BASICS_CRT_SOURCES) |
320054e8 | 420 | else |
41af0db3 | 421 | CRT_SOURCES = $(LIBC_BOTTOM_HALF_CRT_SOURCES) |
320054e8 | 422 | endif |
dcf13b6f | 423 | |
8e8cc34a | 424 | startup_files: include_dirs |
dcf13b6f | 425 | # |
320054e8 | 426 | # Build the startup files. |
dcf13b6f | 427 | # |
320054e8 DG |
428 | @mkdir -p "$(OBJDIR)" |
429 | cd "$(OBJDIR)" && \ | |
430 | "$(WASM_CC)" $(WASM_CFLAGS) -c $(CRT_SOURCES) -MD -MP && \ | |
431 | mkdir -p "$(SYSROOT_LIB)" && \ | |
432 | mv *.o "$(SYSROOT_LIB)" | |
dcf13b6f | 433 | |
8e8cc34a | 434 | libc: include_dirs \ |
320054e8 DG |
435 | $(SYSROOT_LIB)/libc.a \ |
436 | $(SYSROOT_LIB)/libc-printscan-long-double.a \ | |
437 | $(SYSROOT_LIB)/libc-printscan-no-floating-point.a \ | |
438 | $(SYSROOT_LIB)/libwasi-emulated-mman.a | |
439 | ||
8e8cc34a | 440 | finish: startup_files libc |
dcf13b6f | 441 | # |
320054e8 | 442 | # Create empty placeholder libraries. |
dcf13b6f | 443 | # |
1f7d798a | 444 | for name in m rt pthread crypt util xnet resolv dl; do \ |
320054e8 DG |
445 | $(WASM_AR) crs "$(SYSROOT_LIB)/lib$${name}.a"; \ |
446 | done | |
dbfccac2 DG |
447 | |
448 | # | |
320054e8 | 449 | # Collect metadata on the sysroot and perform sanity checks. |
dbfccac2 | 450 | # |
320054e8 DG |
451 | mkdir -p "$(SYSROOT_SHARE)" |
452 | ||
41af0db3 | 453 | # |
320054e8 | 454 | # Collect symbol information. |
41af0db3 DG |
455 | # |
456 | @# TODO: Use llvm-nm --extern-only instead of grep. This is blocked on | |
457 | @# LLVM PR40497, which is fixed in 9.0, but not in 8.0. | |
458 | @# Ignore certain llvm builtin symbols such as those starting with __mul | |
459 | @# since these dependencies can vary between llvm versions. | |
255e6f7c | 460 | "$(WASM_NM)" --defined-only "$(SYSROOT_LIB)"/libc.a "$(SYSROOT_LIB)"/*.o \ |
fad009dc | 461 | |grep ' [[:upper:]] ' |sed 's/.* [[:upper:]] //' |LC_ALL=C sort > "$(SYSROOT_SHARE)/defined-symbols.txt" |
255e6f7c | 462 | for undef_sym in $$("$(WASM_NM)" --undefined-only "$(SYSROOT_LIB)"/*.a "$(SYSROOT_LIB)"/*.o \ |
fad009dc | 463 | |grep ' U ' |sed 's/.* U //' |LC_ALL=C sort |uniq); do \ |
320054e8 | 464 | grep -q '\<'$$undef_sym'\>' "$(SYSROOT_SHARE)/defined-symbols.txt" || echo $$undef_sym; \ |
c35f0f5d | 465 | done | grep -v "^__mul" > "$(SYSROOT_SHARE)/undefined-symbols.txt" |
320054e8 DG |
466 | grep '^_*wasi_' "$(SYSROOT_SHARE)/undefined-symbols.txt" \ |
467 | > "$(SYSROOT_LIB)/libc.imports" | |
468 | ||
41af0db3 | 469 | # |
320054e8 | 470 | # Generate a test file that includes all public header files. |
41af0db3 | 471 | # |
320054e8 | 472 | cd "$(SYSROOT)" && \ |
41af0db3 DG |
473 | for header in $$(find include -type f -not -name mman.h |grep -v /bits/); do \ |
474 | echo '#include <'$$header'>' | sed 's/include\///' ; \ | |
fad009dc | 475 | done |LC_ALL=C sort >share/$(MULTIARCH_TRIPLE)/include-all.c ; \ |
320054e8 DG |
476 | cd - >/dev/null |
477 | ||
41af0db3 | 478 | # |
320054e8 | 479 | # Test that it compiles. |
41af0db3 | 480 | # |
320054e8 DG |
481 | "$(WASM_CC)" $(WASM_CFLAGS) -fsyntax-only "$(SYSROOT_SHARE)/include-all.c" -Wno-\#warnings |
482 | ||
8df0d4cd | 483 | # |
41af0db3 DG |
484 | # Collect all the predefined macros, except for compiler version macros |
485 | # which we don't need to track here. | |
8df0d4cd | 486 | # |
41af0db3 DG |
487 | @# |
488 | @# For the __*_ATOMIC_*_LOCK_FREE macros, squash individual compiler names | |
489 | @# to attempt, toward keeping these files compiler-independent. | |
490 | @# | |
491 | @# We have to add `-isystem $(SYSROOT_INC)` because otherwise clang puts | |
492 | @# its builtin include path first, which produces compiler-specific | |
493 | @# output. | |
494 | @# | |
495 | @# TODO: Undefine __FLOAT128__ for now since it's not in clang 8.0. | |
496 | @# TODO: Filter out __FLT16_* for now, as not all versions of clang have these. | |
320054e8 | 497 | "$(WASM_CC)" $(WASM_CFLAGS) "$(SYSROOT_SHARE)/include-all.c" \ |
8df0d4cd | 498 | -isystem $(SYSROOT_INC) \ |
38b930a2 | 499 | -std=gnu17 \ |
320054e8 | 500 | -E -dM -Wno-\#warnings \ |
9f0d8e85 | 501 | -D_ALL_SOURCE \ |
320054e8 DG |
502 | -U__llvm__ \ |
503 | -U__clang__ \ | |
504 | -U__clang_major__ \ | |
505 | -U__clang_minor__ \ | |
506 | -U__clang_patchlevel__ \ | |
507 | -U__clang_version__ \ | |
508 | -U__GNUC__ \ | |
509 | -U__GNUC_MINOR__ \ | |
510 | -U__GNUC_PATCHLEVEL__ \ | |
511 | -U__VERSION__ \ | |
512 | -U__FLOAT128__ \ | |
513 | | sed -e 's/__[[:upper:][:digit:]]*_ATOMIC_\([[:upper:][:digit:]_]*\)_LOCK_FREE/__compiler_ATOMIC_\1_LOCK_FREE/' \ | |
f74124a6 | 514 | | grep -v '^#define __FLT16_' \ |
320054e8 DG |
515 | > "$(SYSROOT_SHARE)/predefined-macros.txt" |
516 | ||
320054e8 | 517 | # Check that the computed metadata matches the expected metadata. |
255e6f7c TS |
518 | # This ignores whitespace because on Windows the output has CRLF line endings. |
519 | diff -wur "$(CURDIR)/expected/$(MULTIARCH_TRIPLE)" "$(SYSROOT_SHARE)" | |
320054e8 | 520 | |
41af0db3 DG |
521 | # |
522 | # The build succeeded! The generated sysroot is in $(SYSROOT). | |
523 | # | |
524 | ||
8e8cc34a | 525 | install: finish |
320054e8 DG |
526 | mkdir -p "$(INSTALL_DIR)" |
527 | cp -r "$(SYSROOT)/lib" "$(SYSROOT)/share" "$(SYSROOT)/include" "$(INSTALL_DIR)" | |
528 | ||
41af0db3 | 529 | .PHONY: default startup_files libc finish install include_dirs |