@@ -25,6 +25,16 @@ BUILD_LIBC_TOP_HALF ?= yes
25
25
BUILD_LIBSETJMP ?= yes
26
26
# The directory where we will store intermediate artifacts.
27
27
OBJDIR ?= build/$(TARGET_TRIPLE )
28
+
29
+ # LTO; no, full, or thin
30
+ # Note: thin LTO here is just for experimentation. It has known issues:
31
+ # - https://github.com/llvm/llvm-project/issues/91700
32
+ # - https://github.com/llvm/llvm-project/issues/91711
33
+ LTO ?= no
34
+ ifneq ($(LTO ) ,no)
35
+ CLANG_VERSION ?= $(shell ${CC} -dumpversion)
36
+ override OBJDIR := $(OBJDIR ) /llvm-lto/$(CLANG_VERSION )
37
+ endif
28
38
# The directory where we store files and tools for generating WASIp2 bindings
29
39
BINDING_WORK_DIR ?= build/bindings
30
40
# URL from which to retrieve the WIT files used to generate the WASIp2 bindings
@@ -251,6 +261,11 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
251
261
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR ) /complex/* .c) ) \
252
262
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR ) /crypt/* .c)
253
263
264
+ LIBC_NONLTO_SOURCES = \
265
+ $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR ) /, \
266
+ exit/atexit.c \
267
+ )
268
+
254
269
ifeq ($(WASI_SNAPSHOT ) , p2)
255
270
LIBC_TOP_HALF_MUSL_SOURCES += \
256
271
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR ) /, \
@@ -396,6 +411,18 @@ ASMFLAGS += -matomics
396
411
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC )
397
412
endif
398
413
414
+ ifneq ($(LTO ) ,no)
415
+ ifeq ($(LTO ) ,full)
416
+ CFLAGS += -flto=full
417
+ else
418
+ ifeq ($(LTO ) ,thin)
419
+ CFLAGS += -flto=thin
420
+ else
421
+ $(error unknown LTO value : $(LTO ) )
422
+ endif
423
+ endif
424
+ endif
425
+
399
426
ifeq ($(WASI_SNAPSHOT ) , p2)
400
427
CFLAGS += -D__wasilibc_use_wasip2
401
428
endif
@@ -452,10 +479,14 @@ LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_S
452
479
LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES ) )
453
480
LIBSETJMP_OBJS = $(call objs,$(LIBSETJMP_SOURCES ) )
454
481
LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES ) )
482
+ LIBC_NONLTO_OBJS = $(call objs,$(LIBC_NONLTO_SOURCES ) )
455
483
456
484
# These variables describe the locations of various files and
457
485
# directories in the generated sysroot tree.
458
486
SYSROOT_LIB := $(SYSROOT ) /lib/$(TARGET_TRIPLE )
487
+ ifneq ($(LTO ) ,no)
488
+ override SYSROOT_LIB := $(SYSROOT_LIB ) /llvm-lto/$(CLANG_VERSION )
489
+ endif
459
490
SYSROOT_INC = $(SYSROOT ) /include/$(TARGET_TRIPLE )
460
491
SYSROOT_SHARE = $(SYSROOT ) /share/$(TARGET_TRIPLE )
461
492
@@ -627,6 +658,8 @@ $(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS)
627
658
628
659
$(PIC_OBJS ) : CFLAGS += -fPIC -fvisibility=default
629
660
661
+ $(LIBC_NONLTO_OBJS ) : CFLAGS := $(filter-out -flto% -fno-lto, $(CFLAGS ) ) -fno-lto
662
+
630
663
$(MUSL_PRINTSCAN_OBJS ) : CFLAGS += \
631
664
-D__wasilibc_printscan_no_long_double \
632
665
-D__wasilibc_printscan_full_support_option=" \" add -lc-printscan-long-double to the link command\" "
@@ -794,12 +827,14 @@ finish: startup_files libc dummy_libs
794
827
# The build succeeded! The generated sysroot is in $(SYSROOT).
795
828
#
796
829
830
+ ifeq ($(LTO ) ,no)
797
831
# The check for defined and undefined symbols expects there to be a heap
798
832
# alloctor (providing malloc, calloc, free, etc). Skip this step if the build
799
833
# is done without a malloc implementation.
800
834
ifneq ($(MALLOC_IMPL ) ,none)
801
835
finish : check-symbols
802
836
endif
837
+ endif
803
838
804
839
DEFINED_SYMBOLS = $(SYSROOT_SHARE ) /defined-symbols.txt
805
840
UNDEFINED_SYMBOLS = $(SYSROOT_SHARE ) /undefined-symbols.txt
0 commit comments