@@ -556,11 +556,48 @@ PIC_OBJS = \
556
556
$(LIBC_BOTTOM_HALF_CRT_OBJS ) \
557
557
$(FTS_SO_OBJS )
558
558
559
+ # Figure out what to do about compiler-rt.
560
+ #
561
+ # The compiler-rt library is not built here in the wasi-libc repository, but it
562
+ # is required to link artifacts. Notably `libc.so` and test and such all require
563
+ # it to exist. Currently the ways this is handled are:
564
+ #
565
+ # * If `BUILTINS_LIB` is defined at build time then that's assumed to be a path
566
+ # to the libcompiler-rt.a. That's then ingested into the build here and copied
567
+ # around to special locations to get the `*.so` rules below to work (see docs
568
+ # there).
569
+ #
570
+ # * If `BUILTINS_LIB` is not defined then a known-good copy is downloaded from
571
+ # wasi-sdk CI and used instead.
572
+ #
573
+ # In the future this may also want some form of configuration to support
574
+ # assuming the system compiler has a compiler-rt, e.g. if $(SYSTEM_BUILTINS_LIB)
575
+ # exists that should be used instead.
559
576
SYSTEM_BUILTINS_LIB := $(shell ${CC} ${CFLAGS} --print-libgcc-file-name)
560
577
SYSTEM_RESOURCE_DIR := $(shell ${CC} ${CFLAGS} -print-resource-dir)
561
578
BUILTINS_LIB_REL := $(subst $(SYSTEM_RESOURCE_DIR ) ,,$(SYSTEM_BUILTINS_LIB ) )
562
- RESOURCE_DIR := $(OBJDIR ) /resource-dir
563
- BUILTINS_LIB ?= $(RESOURCE_DIR ) /$(BUILTINS_LIB_REL )
579
+ TMP_RESOURCE_DIR := $(OBJDIR ) /resource-dir
580
+ BUILTINS_LIB_PATH := $(TMP_RESOURCE_DIR ) /$(BUILTINS_LIB_REL )
581
+ BUILTINS_LIB_DIR := $(dir $(BUILTINS_LIB_PATH ) )
582
+
583
+ ifneq ($(BUILTINS_LIB ) ,)
584
+ $(BUILTINS_LIB_PATH ) : $(BUILTINS_LIB )
585
+ mkdir -p $(BUILTINS_LIB_DIR )
586
+ cp $(BUILTINS_LIB ) $(BUILTINS_LIB_PATH )
587
+ else
588
+
589
+ BUILTINS_URL := https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/libclang_rt.builtins-wasm32-wasi-25.0.tar.gz
590
+
591
+ $(BUILTINS_LIB_PATH ) :
592
+ mkdir -p $(BUILTINS_LIB_DIR )
593
+ curl -sSfL $(BUILTINS_URL ) | \
594
+ tar xzf - -C $(BUILTINS_LIB_DIR ) --strip-components 1
595
+ if [ ! -f $( BUILTINS_LIB_PATH) ]; then \
596
+ mv $(BUILTINS_LIB_DIR ) /* .a $(BUILTINS_LIB_PATH ) ; \
597
+ fi
598
+ endif
599
+
600
+ builtins : $(BUILTINS_LIB_PATH )
564
601
565
602
# TODO: Specify SDK version, e.g. libc.so.wasi-sdk-21, as SO_NAME once `wasm-ld`
566
603
# supports it.
@@ -570,22 +607,34 @@ BUILTINS_LIB ?= $(RESOURCE_DIR)/$(BUILTINS_LIB_REL)
570
607
# to CC. This is a workaround for a Windows command line size limitation. See
571
608
# the `%.a` rule below for details.
572
609
573
- # Note: libc.so is special because it shouldn't link to libc.so.
610
+ # Note: libc.so is special because it shouldn't link to libc.so, and the
611
+ # -nodefaultlibs flag here disables the default `-lc` logic that clang
612
+ # has. Note though that this also disables linking of compiler-rt
613
+ # libraries so that is explicitly passed in via `$(BUILTINS_LIB_PATH)`
614
+ #
574
615
# Note: --allow-undefined-file=linker-provided-symbols.txt is
575
616
# a workaround for https://github.com/llvm/llvm-project/issues/103592
576
- $(SYSROOT_LIB ) /libc.so : $(OBJDIR ) /libc.so.a $(BUILTINS_LIB )
577
- $(CC ) $( EXTRA_CFLAGS ) --target=${TARGET_TRIPLE} -nodefaultlibs \
617
+ $(SYSROOT_LIB ) /libc.so : $(OBJDIR ) /libc.so.a $(BUILTINS_LIB_PATH )
618
+ $(CC ) --target=${TARGET_TRIPLE} -nodefaultlibs \
578
619
-shared --sysroot=$(SYSROOT ) \
579
- -o $@ -Wl,--whole-archive $< -Wl,--no-whole-archive $( BUILTINS_LIB ) \
620
+ -o $@ -Wl,--whole-archive $< -Wl,--no-whole-archive \
580
621
-Wl,--allow-undefined-file=linker-provided-symbols.txt \
581
- -resource-dir $(RESOURCE_DIR )
582
-
622
+ $(BUILTINS_LIB_PATH ) \
623
+ $(EXTRA_CFLAGS ) $(LDFLAGS )
624
+
625
+ # Note that unlike `libc.so` above this rule does not pass `-nodefaultlibs`
626
+ # which means that libc will be linked by default. Additionally clang will try
627
+ # to find, locate, and link compiler-rt. To get compiler-rt to work a
628
+ # `-resource-dir` argument is passed to ensure that our custom
629
+ # `TMP_RESOURCE_DIR` built here locally is used instead of the system directory
630
+ # which may or may not already have compiler-rt.
583
631
$(SYSROOT_LIB ) /% .so : $(OBJDIR ) /% .so.a $(SYSROOT_LIB ) /libc.so
584
- $(CC ) $( EXTRA_CFLAGS ) --target=${TARGET_TRIPLE} \
632
+ $(CC ) --target=${TARGET_TRIPLE} \
585
633
-shared --sysroot=$(SYSROOT ) \
586
634
-o $@ -Wl,--whole-archive $< -Wl,--no-whole-archive \
587
635
-Wl,--allow-undefined-file=linker-provided-symbols.txt \
588
- -resource-dir $(RESOURCE_DIR )
636
+ -resource-dir $(TMP_RESOURCE_DIR ) \
637
+ $(EXTRA_CFLAGS ) $(LDFLAGS )
589
638
590
639
$(OBJDIR ) /libc.so.a : $(LIBC_SO_OBJS ) $(MUSL_PRINTSCAN_LONG_DOUBLE_SO_OBJS )
591
640
0 commit comments