From db0c4dbe5ff241845655f8b344a72c3134f99cbf Mon Sep 17 00:00:00 2001
From: Patrick Oppenlander <patrick.oppenlander@gmail.com>
Date: Fri, 29 Jan 2021 12:47:59 +1100
Subject: [PATCH] clang support

---
 Makefile                                      |  58 ++++--
 hashes/llvm-project-11.0.0.tar.xz.sha1        |   1 +
 litecross/Makefile.clang                      | 188 ++++++++++++++++++
 litecross/{Makefile => Makefile.gcc}          |   0
 ...add-builtins-to-dynamic-library-link.patch |  30 +++
 ...able-crt-support-on-more-arm-targets.patch |  25 +++
 ...panding-target-triple-in-sysroot-pat.patch |  39 ++++
 7 files changed, 322 insertions(+), 19 deletions(-)
 create mode 100644 hashes/llvm-project-11.0.0.tar.xz.sha1
 create mode 100644 litecross/Makefile.clang
 rename litecross/{Makefile => Makefile.gcc} (100%)
 create mode 100644 patches/llvm-project-11.0.0/0001-libcxxabi-add-builtins-to-dynamic-library-link.patch
 create mode 100644 patches/llvm-project-11.0.0/0002-compiler-rt-enable-crt-support-on-more-arm-targets.patch
 create mode 100644 patches/llvm-project-11.0.0/0003-clang-support-expanding-target-triple-in-sysroot-pat.patch

diff --git a/Makefile b/Makefile
index a412f60..0e29588 100644
--- a/Makefile
+++ b/Makefile
@@ -1,14 +1,7 @@
 
 SOURCES = sources
 
-CONFIG_SUB_REV = 3d5db9ebe860
-BINUTILS_VER = 2.33.1
-GCC_VER = 9.2.0
-MUSL_VER = 1.2.1
-GMP_VER = 6.1.2
-MPC_VER = 1.1.0
-MPFR_VER = 4.0.2
-LINUX_VER = headers-4.19.88
+COMPILER = gcc
 
 GNU_SITE = https://ftp.gnu.org/pub/gnu
 GCC_SITE = $(GNU_SITE)/gcc
@@ -17,6 +10,7 @@ GMP_SITE = $(GNU_SITE)/gmp
 MPC_SITE = $(GNU_SITE)/mpc
 MPFR_SITE = $(GNU_SITE)/mpfr
 ISL_SITE = http://isl.gforge.inria.fr/
+LLVM_SITE = https://github.com/llvm/llvm-project/releases/download
 
 MUSL_SITE = https://www.musl-libc.org/releases
 MUSL_REPO = git://git.musl-libc.org/musl
@@ -30,24 +24,47 @@ SHA1_CMD = sha1sum -c
 COWPATCH = $(CURDIR)/cowpatch.sh
 
 HOST = $(if $(NATIVE),$(TARGET))
-BUILD_DIR = build/$(if $(HOST),$(HOST),local)/$(TARGET)
+BUILD_DIR = build-$(COMPILER)/$(if $(HOST),$(HOST),local)/$(TARGET)
 OUTPUT = $(CURDIR)/output$(if $(HOST),-$(HOST))
 
-REL_TOP = ../../..
+REL_TOP = ../..$(if $(TARGET),/..)
 
 -include config.mak
 
-SRC_DIRS = gcc-$(GCC_VER) binutils-$(BINUTILS_VER) musl-$(MUSL_VER) \
+MUSL_VER ?= 1.2.1
+LINUX_VER ?= headers-4.19.88
+
+ifeq ($(COMPILER),gcc)
+
+CONFIG_SUB_REV ?= 3d5db9ebe860
+BINUTILS_VER ?= 2.33.1
+GCC_VER ?= 9.2.0
+GMP_VER ?= 6.1.2
+MPC_VER ?= 1.1.0
+MPFR_VER ?= 4.0.2
+
+endif
+
+ifeq ($(COMPILER),clang)
+
+LLVM_VER ?= 11.0.0
+
+endif
+
+SRC_DIRS = musl-$(MUSL_VER) \
+	$(if $(GCC_VER),gcc-$(GCC_VER)) \
+	$(if $(BINUTILS_VER),binutils-$(BINUTILS_VER)) \
 	$(if $(GMP_VER),gmp-$(GMP_VER)) \
 	$(if $(MPC_VER),mpc-$(MPC_VER)) \
 	$(if $(MPFR_VER),mpfr-$(MPFR_VER)) \
 	$(if $(ISL_VER),isl-$(ISL_VER)) \
-	$(if $(LINUX_VER),linux-$(LINUX_VER))
+	$(if $(LINUX_VER),linux-$(LINUX_VER)) \
+	$(if $(LLVM_VER),llvm-project-$(LLVM_VER))
 
 all:
 
 clean:
-	rm -rf gcc-* binutils-* musl-* gmp-* mpc-* mpfr-* isl-* build build-* linux-*
+	rm -rf gcc-* binutils-* musl-* gmp-* mpc-* mpfr-* isl-* build build-* linux-* llvm-project-*
 
 distclean: clean
 	rm -rf sources
@@ -70,6 +87,7 @@ $(patsubst hashes/%.sha1,$(SOURCES)/%,$(wildcard hashes/linux-4*)): SITE = $(LIN
 $(patsubst hashes/%.sha1,$(SOURCES)/%,$(wildcard hashes/linux-3*)): SITE = $(LINUX_SITE)/v3.x
 $(patsubst hashes/%.sha1,$(SOURCES)/%,$(wildcard hashes/linux-2.6*)): SITE = $(LINUX_SITE)/v2.6
 $(patsubst hashes/%.sha1,$(SOURCES)/%,$(wildcard hashes/linux-headers-*)): SITE = $(LINUX_HEADERS_SITE)
+$(patsubst hashes/%.sha1,$(SOURCES)/%,$(wildcard hashes/llvm-project-*)): SITE = $(LLVM_SITE)/llvmorg-$(patsubst llvm-project-%,%,$(basename $(basename $(notdir $@))))
 
 $(SOURCES):
 	mkdir -p $@
@@ -150,10 +168,10 @@ extract_all: | $(SRC_DIRS)
 
 # Rules for building.
 
-ifeq ($(TARGET),)
+ifeq ($(COMPILER)$(TARGET),gcc)
 
 all:
-	@echo TARGET must be set via config.mak or command line.
+	@echo TARGET must be set for gcc build via config.mak or command line.
 	@exit 1
 
 else
@@ -162,20 +180,22 @@ $(BUILD_DIR):
 	mkdir -p $@
 
 $(BUILD_DIR)/Makefile: | $(BUILD_DIR)
-	ln -sf $(REL_TOP)/litecross/Makefile $@
+	ln -sf $(REL_TOP)/litecross/Makefile.$(COMPILER) $@
 
 $(BUILD_DIR)/config.mak: | $(BUILD_DIR)
 	printf >$@ '%s\n' \
-	"TARGET = $(TARGET)" \
+	$(if $(TARGET),"TARGET = $(TARGET)") \
 	"HOST = $(HOST)" \
 	"MUSL_SRCDIR = $(REL_TOP)/musl-$(MUSL_VER)" \
-	"GCC_SRCDIR = $(REL_TOP)/gcc-$(GCC_VER)" \
-	"BINUTILS_SRCDIR = $(REL_TOP)/binutils-$(BINUTILS_VER)" \
+	$(if $(GCC_VER),"GCC_SRCDIR = $(REL_TOP)/gcc-$(GCC_VER)") \
+	$(if $(BINUTILS_VER),"BINUTILS_SRCDIR = $(REL_TOP)/binutils-$(BINUTILS_VER)") \
 	$(if $(GMP_VER),"GMP_SRCDIR = $(REL_TOP)/gmp-$(GMP_VER)") \
 	$(if $(MPC_VER),"MPC_SRCDIR = $(REL_TOP)/mpc-$(MPC_VER)") \
 	$(if $(MPFR_VER),"MPFR_SRCDIR = $(REL_TOP)/mpfr-$(MPFR_VER)") \
 	$(if $(ISL_VER),"ISL_SRCDIR = $(REL_TOP)/isl-$(ISL_VER)") \
 	$(if $(LINUX_VER),"LINUX_SRCDIR = $(REL_TOP)/linux-$(LINUX_VER)") \
+	$(if $(LLVM_VER),"LLVM_SRCDIR = $(REL_TOP)/llvm-project-$(LLVM_VER)") \
+	$(if $(LLVM_VER),"LLVM_VER = $(LLVM_VER)") \
 	"-include $(REL_TOP)/config.mak"
 
 all: | $(SRC_DIRS) $(BUILD_DIR) $(BUILD_DIR)/Makefile $(BUILD_DIR)/config.mak
diff --git a/hashes/llvm-project-11.0.0.tar.xz.sha1 b/hashes/llvm-project-11.0.0.tar.xz.sha1
new file mode 100644
index 0000000..ef4b704
--- /dev/null
+++ b/hashes/llvm-project-11.0.0.tar.xz.sha1
@@ -0,0 +1 @@
+3c88390b19ac6779c8d9c89256892d690903412b  llvm-project-11.0.0.tar.xz
diff --git a/litecross/Makefile.clang b/litecross/Makefile.clang
new file mode 100644
index 0000000..d57095c
--- /dev/null
+++ b/litecross/Makefile.clang
@@ -0,0 +1,188 @@
+
+OUTPUT = $(CURDIR)/output
+
+LLVM_SRCDIR = LLVM_SRCDIR_not_set
+MUSL_SRCDIR = MUSL_SRCDIR_not_set
+
+TARGETS = \
+	aarch64-linux-musleabi \
+	armv7-linux-musleabihf \
+	armv7m-linux-musleabi \
+	armv7em-linux-musleabihf \
+	x86_64-linux-musl \
+	# end
+
+TARGET_CONFIG_armv7-linux-musleabihf = -mfpu=vfpv3
+TARGET_CONFIG_armv7em-linux-musleabihf = -mfpu=fpv5-d16
+
+DEFAULT_TARGET = x86_64-linux-musl
+
+TOOLS = \
+	llvm-ar \
+	llvm-nm \
+	llvm-objcopy \
+	llvm-objdump \
+	llvm-ranlib \
+	llvm-size \
+	llvm-strip \
+	# end
+
+-include config.mak
+
+# TODO: determine LLVM_TARGETS_TO_BUILD from TARGETS?
+FULL_LLVM_CONFIG = $(LLVM_CONFIG) \
+	-DCLANG_DEFAULT_CXX_STDLIB=libc++ \
+	-DCLANG_DEFAULT_LINKER=lld \
+	-DCLANG_DEFAULT_OBJCOPY=llvm-objcopy \
+	-DCLANG_DEFAULT_RTLIB=compiler-rt \
+	-DCMAKE_BUILD_TYPE=MinSizeRel \
+	-DCMAKE_INSTALL_PREFIX=/clang \
+	-DDEFAULT_SYSROOT=../../TARGET \
+	-DLLVM_BUILTIN_TARGETS='$(subst $(subst ,, ),;,$(TARGETS))' \
+	-DLLVM_DEFAULT_TARGET_TRIPLE=$(DEFAULT_TARGET) \
+	-DLLVM_DISTRIBUTION_COMPONENTS='clang;clang-resource-headers;lld;LTO;$(subst $(subst ,, ),;,$(TOOLS))' \
+	-DLLVM_ENABLE_PROJECTS='clang;lld' \
+	-DLLVM_ENABLE_RUNTIMES='compiler-rt;libcxx;libcxxabi;libunwind' \
+	-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON \
+	-DLLVM_RUNTIME_TARGETS='$(subst $(subst ,, ),;,$(TARGETS))' \
+	-DLLVM_TARGETS_TO_BUILD='AArch64;ARM;Mips;PowerPC;X86' \
+	$(foreach t,$(TARGETS),-DBUILTINS_$(t)_CMAKE_SYSROOT=$(CURDIR)/obj_sysroot/$(t)) \
+	$(foreach t,$(TARGETS),-DBUILTINS_$(t)_CMAKE_C_FLAGS='$(TARGET_CONFIG_$(t))') \
+	$(foreach t,$(TARGETS),-DBUILTINS_$(t)_CMAKE_ASM_FLAGS='$(TARGET_CONFIG_$(t))') \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_CMAKE_CXX_FLAGS='-L$(CURDIR)/obj_musl/$(t)/lib -B$(CURDIR)/obj_musl/$(t)/lib -Qunused-arguments') \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_CMAKE_C_FLAGS='-L$(CURDIR)/obj_musl/$(t)/lib -B$(CURDIR)/obj_musl/$(t)/lib -Qunused-arguments') \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_CMAKE_SYSROOT=$(CURDIR)/obj_sysroot/$(t)) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_COMPILER_RT_BUILD_LIBFUZZER=OFF) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_COMPILER_RT_BUILD_SANITIZERS=OFF) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_COMPILER_RT_BUILD_XRAY=OFF) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXXABI_ENABLE_SHARED=OFF) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXXABI_ENABLE_STATIC_UNWINDER=ON) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXXABI_INSTALL_LIBRARY=OFF) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXXABI_USE_COMPILER_RT=ON) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXXABI_USE_LLVM_UNWINDER=ON) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY=ON) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXX_HAS_MUSL_LIBC=ON) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBCXX_USE_COMPILER_RT=ON) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBUNWIND_ENABLE_SHARED=OFF) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBUNWIND_INSTALL_LIBRARY=OFF) \
+	$(foreach t,$(TARGETS),-DRUNTIMES_$(t)_LIBUNWIND_USE_COMPILER_RT=ON) \
+	# end
+
+all: llvm musl
+
+install: install-llvm install-musl install-kernel-headers install-configs install-symlinks
+
+llvm: | obj_llvm/.lc_built_3
+musl: | $(foreach t,$(TARGETS),obj_musl/$(t)/.lc_built)
+install-musl: $(foreach t,$(TARGETS),install-musl_$(t))
+install-kernel-headers: $(foreach t,$(TARGETS),install-kernel-headers_$(t))
+install-configs: $(foreach t,$(TARGETS),install-configs_$(t))
+install-symlinks: $(foreach t,$(TARGETS),install-symlinks_$(t))
+
+.PHONY: all llvm musl install-llvm install-musl install-kernel-headers install-configs install-symlinks clean
+
+src_llvm: | $(LLVM_SRCDIR)
+	ln -sf $(LLVM_SRCDIR) $@
+
+src_musl: | $(MUSL_SRCDIR)
+	ln -sf $(MUSL_SRCDIR) $@
+
+src_kernel_headers: | $(LINUX_SRCDIR)
+	ln -sf "$(LINUX_SRCDIR)" $@
+
+%/:
+	mkdir -p $@
+
+obj_llvm/.lc_configured: | obj_llvm/ src_llvm
+	cd obj_llvm && cmake -G "Ninja" $(FULL_LLVM_CONFIG) ../src_llvm/llvm
+	touch $@
+
+obj_llvm/.lc_built_1: | obj_llvm/.lc_configured
+	cd obj_llvm && ninja distribution
+	touch $@
+
+obj_musl/%/.lc_configured: | obj_musl/%/ src_musl obj_llvm/.lc_built_1
+	cd obj_musl/$* && ../../src_musl/configure \
+		--prefix= \
+		--target=$* \
+		--disable-wrapper \
+		LIBCC=../../obj_llvm/lib/clang/$(LLVM_VER)/lib/$*/libclang_rt.builtins.a \
+		CC="../../obj_llvm/bin/clang --target=$* $(TARGET_CONFIG_$*)" \
+		$(MUSL_CONFIG) $(MUSL_CONFIG_$*)
+	touch $@
+
+obj_sysroot/%/.lc_headers: | obj_sysroot/ obj_musl/%/.lc_configured
+	cd obj_musl/$* && $(MAKE) DESTDIR=$(CURDIR)/obj_sysroot/$* install-headers
+	touch $@
+
+obj_llvm/.lc_built_2: | $(foreach t,$(TARGETS),obj_sysroot/$(t)/.lc_headers)
+	cd obj_llvm && ninja builtins
+	touch $@
+
+obj_musl/%/.lc_built: | obj_musl/%/.lc_configured obj_llvm/.lc_built_2
+	cd obj_musl/$* && $(MAKE) AR=../../obj_llvm/bin/llvm-ar \
+				  RANLIB=../../obj_llvm/bin/llvm-ranlib
+	touch $@
+
+obj_sysroot/%/.lc_crt_workaround:
+	# Some configure tests for llvm runtimes fail without crtbegin and
+	# crtend files in the sysroot. The runtimes build generates these files
+	# leading to a chicken-egg problem.
+	mkdir -p obj_sysroot/$*/lib
+	cd obj_sysroot/$*/lib && touch crtbeginT.o crtbeginS.o crtbegin.o crtend.o crtendS.o
+	touch $@
+
+obj_kernel_headers/%/.lc_built: | src_kernel_headers
+	mkdir -p $(CURDIR)/obj_kernel_headers/$*/staged
+	cd src_kernel_headers && $(MAKE) ARCH=$(call fn_linux_arch,$*) O=$(CURDIR)/obj_kernel_headers/$* INSTALL_HDR_PATH=$(CURDIR)/obj_kernel_headers/$*/staged headers_install
+	find obj_kernel_headers/$*/staged/include '(' -name .install -o -name ..install.cmd ')' -exec rm {} +
+	touch $@
+
+obj_sysroot/%/.lc_linux_headers: | obj_kernel_headers/%/.lc_built
+	mkdir -p obj_sysroot/$*/include
+	cp -R obj_kernel_headers/$*/staged/include/* obj_sysroot/$*/include
+
+obj_llvm/.lc_built_3: | $(foreach t,$(TARGETS),obj_musl/$(t)/.lc_built \
+					       obj_sysroot/$(t)/.lc_crt_workaround \
+					       obj_sysroot/$(t)/.lc_linux_headers)
+	cd obj_llvm && ninja runtimes
+	touch $@
+
+install-llvm: | obj_llvm/.lc_built_3
+	cd obj_llvm && DESTDIR=$(DESTDIR)$(OUTPUT) ninja install-distribution install-builtins install-runtimes
+
+install-musl_%: | obj_musl/%/.lc_built
+	cd obj_musl/$* && $(MAKE) $(MUSL_VARS) DESTDIR=$(DESTDIR)$(OUTPUT)/$* install
+
+install-kernel-headers_%: | obj_kernel_headers/%/.lc_built
+	mkdir -p $(DESTDIR)$(OUTPUT)/$*/include
+	cp -R obj_kernel_headers/$*/staged/include/* $(DESTDIR)$(OUTPUT)/$*/include
+
+install-configs_%: | $(DESDIR)$(OUTPUT)/clang/bin/
+	echo "--target=$* $(TARGET_CONFIG_$*)" > $(DESTDIR)$(OUTPUT)/clang/bin/$*-clang.cfg
+	echo "--target=$* $(TARGET_CONFIG_$*)" > $(DESTDIR)$(OUTPUT)/clang/bin/$*-clang++.cfg
+
+install-symlinks_%: | $(DESTDIR)$(OUTPUT)/bin/
+	cd $(DESTDIR)$(OUTPUT)/bin && ln -sf ../clang/bin/clang $*-clang
+	cd $(DESTDIR)$(OUTPUT)/bin && ln -sf ../clang/bin/clang++ $*-clang++
+	cd $(DESTDIR)$(OUTPUT)/bin && ln -sf ../clang/bin/clang-cpp $*-cpp
+	cd $(DESTDIR)$(OUTPUT)/bin && ln -sf ../clang/bin/clang $*-cc
+	cd $(DESTDIR)$(OUTPUT)/bin && ln -sf ../clang/bin/clang++ $*-c++
+	cd $(DESTDIR)$(OUTPUT)/bin && ln -sf ../clang/bin/ld.lld $*-ld
+	$(foreach t,$(TOOLS),cd $(DESTDIR)$(OUTPUT)/bin && ln -sf ../clang/bin/$(t) $*-$(subst llvm-,,$(t);))
+
+clean:
+	rm -rf src_* obj_*
+
+# $(call fn_linux_arch,target-triple)
+#     Determine linux architecture from target triple
+ifeq ($(LINUX_SRCDIR),)
+$(error LINUX_SRCDIR is required for llvm libc++ build)
+endif
+LINUX_ARCH_LIST := $(sort $(notdir $(wildcard $(LINUX_SRCDIR)/arch/*)))
+fn_target_arch = $(firstword $(subst -, ,$(1)))
+fn_target_arch_mangled = $(patsubst i%86,x86,$(patsubst aarch64%,arm64%,$(call fn_target_arch,$(1))))
+fn_linux_arch = $(lastword $(foreach a,$(LINUX_ARCH_LIST),$(if $(filter $(a)%,$(call fn_target_arch_mangled,$(1))),$(a))))
+
+.SECONDARY:
diff --git a/litecross/Makefile b/litecross/Makefile.gcc
similarity index 100%
rename from litecross/Makefile
rename to litecross/Makefile.gcc
diff --git a/patches/llvm-project-11.0.0/0001-libcxxabi-add-builtins-to-dynamic-library-link.patch b/patches/llvm-project-11.0.0/0001-libcxxabi-add-builtins-to-dynamic-library-link.patch
new file mode 100644
index 0000000..d1c3734
--- /dev/null
+++ b/patches/llvm-project-11.0.0/0001-libcxxabi-add-builtins-to-dynamic-library-link.patch
@@ -0,0 +1,30 @@
+From 6f437b42e66c7552c3b92fd1bd46c96ed484dfbf Mon Sep 17 00:00:00 2001
+From: Patrick Oppenlander <patrick.oppenlander@gmail.com>
+Date: Fri, 8 Jan 2021 16:46:37 +1100
+Subject: [PATCH] libcxxabi: add builtins to dynamic library link
+
+Otherwise libc++abi.so fails to link on arm with undefined references to
+some __aeabi_ builtins.
+---
+ libcxxabi/src/CMakeLists.txt | 5 +++++
+ 1 file changed, 5 insertions(+)
+
+diff --git a/libcxxabi/src/CMakeLists.txt b/libcxxabi/src/CMakeLists.txt
+index 42bec421d2be..9dba22ea3d43 100644
+--- a/libcxxabi/src/CMakeLists.txt
++++ b/libcxxabi/src/CMakeLists.txt
+@@ -73,6 +73,11 @@ else()
+   add_library_flags_if(LIBCXXABI_HAS_C_LIB c)
+ endif()
+ 
++if (LIBCXXABI_USE_COMPILER_RT)
++  find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY)
++  list(APPEND LIBCXXABI_SHARED_LIBRARIES "${LIBCXXABI_BUILTINS_LIBRARY}")
++endif ()
++
+ if (LIBCXXABI_USE_LLVM_UNWINDER)
+   # Prefer using the in-tree version of libunwind, either shared or static. If
+   # none are found fall back to using -lunwind.
+-- 
+2.30.0
+
diff --git a/patches/llvm-project-11.0.0/0002-compiler-rt-enable-crt-support-on-more-arm-targets.patch b/patches/llvm-project-11.0.0/0002-compiler-rt-enable-crt-support-on-more-arm-targets.patch
new file mode 100644
index 0000000..efc5092
--- /dev/null
+++ b/patches/llvm-project-11.0.0/0002-compiler-rt-enable-crt-support-on-more-arm-targets.patch
@@ -0,0 +1,25 @@
+From 2425a718046a82e705d05400bd9204b265ca3262 Mon Sep 17 00:00:00 2001
+From: Patrick Oppenlander <patrick.oppenlander@gmail.com>
+Date: Fri, 8 Jan 2021 21:34:39 +1100
+Subject: [PATCH] compiler-rt: enable crt support on more arm targets
+
+---
+ compiler-rt/cmake/config-ix.cmake | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
+index 2edc1dabd90d..7d8b95146e9a 100644
+--- a/compiler-rt/cmake/config-ix.cmake
++++ b/compiler-rt/cmake/config-ix.cmake
+@@ -255,7 +255,7 @@ function(is_valid_apple_platform platform is_valid_out)
+ endfunction()
+ 
+ set(ARM64 aarch64)
+-set(ARM32 arm armhf)
++set(ARM32 arm armhf armv7 armv7m armv7em)
+ set(HEXAGON hexagon)
+ set(X86 i386)
+ set(X86_64 x86_64)
+-- 
+2.30.0
+
diff --git a/patches/llvm-project-11.0.0/0003-clang-support-expanding-target-triple-in-sysroot-pat.patch b/patches/llvm-project-11.0.0/0003-clang-support-expanding-target-triple-in-sysroot-pat.patch
new file mode 100644
index 0000000..08a7dec
--- /dev/null
+++ b/patches/llvm-project-11.0.0/0003-clang-support-expanding-target-triple-in-sysroot-pat.patch
@@ -0,0 +1,39 @@
+From f0ecb3850d7825cd967d22c09ed73d990605c70b Mon Sep 17 00:00:00 2001
+From: Patrick Oppenlander <patrick.oppenlander@gmail.com>
+Date: Mon, 11 Jan 2021 10:39:27 +1100
+Subject: [PATCH] clang: support expanding target triple in sysroot path
+
+This allows clang to switch sysroot (or, more usefully, default sysroot)
+based on target triple.
+
+For example, configuring with
+
+-DDEFAULT_SYSROOT=../TARGET
+
+will automatically select a target specific sysroot one level up from
+the clang binary location.
+---
+ clang/lib/Driver/Driver.cpp | 7 +++++++
+ 1 file changed, 7 insertions(+)
+
+diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
+index ece8222dcf24..8a3b068d86b9 100644
+--- a/clang/lib/Driver/Driver.cpp
++++ b/clang/lib/Driver/Driver.cpp
+@@ -1121,6 +1121,13 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
+                     .Default(SaveTempsCwd);
+   }
+ 
++  // expand TARGET in sysroot path
++  if (!SysRoot.empty()) {
++    size_t pos{0};
++    while ((pos = SysRoot.find("TARGET", pos)) != std::string::npos)
++      SysRoot.replace(pos, 6, TargetTriple);
++  }
++
+   setLTOMode(Args);
+ 
+   // Process -fembed-bitcode= flags.
+-- 
+2.30.0
+
-- 
2.30.0

