@@ -25,6 +25,16 @@ BUILD_LIBC_TOP_HALF ?= yes
2525BUILD_LIBSETJMP ?= yes
2626# The directory where we will store intermediate artifacts.
2727OBJDIR ?= 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
2838# The directory where we store files and tools for generating WASIp2 bindings
2939BINDING_WORK_DIR ?= build/bindings
3040# URL from which to retrieve the WIT files used to generate the WASIp2 bindings
@@ -251,6 +261,11 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
251261 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR ) /complex/* .c) ) \
252262 $(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR ) /crypt/* .c)
253263
264+ LIBC_NONLTO_SOURCES = \
265+ $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR ) /, \
266+ exit/atexit.c \
267+ )
268+
254269ifeq ($(WASI_SNAPSHOT ) , p2)
255270LIBC_TOP_HALF_MUSL_SOURCES += \
256271 $(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR ) /, \
@@ -396,6 +411,18 @@ ASMFLAGS += -matomics
396411CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC )
397412endif
398413
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+
399426ifeq ($(WASI_SNAPSHOT ) , p2)
400427CFLAGS += -D__wasilibc_use_wasip2
401428endif
@@ -452,10 +479,14 @@ LIBWASI_EMULATED_SIGNAL_MUSL_OBJS = $(call objs,$(LIBWASI_EMULATED_SIGNAL_MUSL_S
452479LIBDL_OBJS = $(call objs,$(LIBDL_SOURCES ) )
453480LIBSETJMP_OBJS = $(call objs,$(LIBSETJMP_SOURCES ) )
454481LIBC_BOTTOM_HALF_CRT_OBJS = $(call objs,$(LIBC_BOTTOM_HALF_CRT_SOURCES ) )
482+ LIBC_NONLTO_OBJS = $(call objs,$(LIBC_NONLTO_SOURCES ) )
455483
456484# These variables describe the locations of various files and
457485# directories in the generated sysroot tree.
458486SYSROOT_LIB := $(SYSROOT ) /lib/$(TARGET_TRIPLE )
487+ ifneq ($(LTO ) ,no)
488+ override SYSROOT_LIB := $(SYSROOT_LIB ) /llvm-lto/$(CLANG_VERSION )
489+ endif
459490SYSROOT_INC = $(SYSROOT ) /include/$(TARGET_TRIPLE )
460491SYSROOT_SHARE = $(SYSROOT ) /share/$(TARGET_TRIPLE )
461492
@@ -627,6 +658,8 @@ $(SYSROOT_LIB)/libsetjmp.a: $(LIBSETJMP_OBJS)
627658
628659$(PIC_OBJS ) : CFLAGS += -fPIC -fvisibility=default
629660
661+ $(LIBC_NONLTO_OBJS ) : CFLAGS := $(filter-out -flto% -fno-lto, $(CFLAGS ) ) -fno-lto
662+
630663$(MUSL_PRINTSCAN_OBJS ) : CFLAGS += \
631664 -D__wasilibc_printscan_no_long_double \
632665 -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
794827 # The build succeeded! The generated sysroot is in $(SYSROOT).
795828 #
796829
830+ ifeq ($(LTO ) ,no)
797831# The check for defined and undefined symbols expects there to be a heap
798832# alloctor (providing malloc, calloc, free, etc). Skip this step if the build
799833# is done without a malloc implementation.
800834ifneq ($(MALLOC_IMPL ) ,none)
801835finish : check-symbols
802836endif
837+ endif
803838
804839DEFINED_SYMBOLS = $(SYSROOT_SHARE ) /defined-symbols.txt
805840UNDEFINED_SYMBOLS = $(SYSROOT_SHARE ) /undefined-symbols.txt
0 commit comments