From 9e0c7c4b8737dca311ed43633ea9cdaf66c6197f Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 31 Mar 2022 20:23:06 -0700 Subject: [PATCH] [build] make LK buildable with LLVM/Clang Add an environment setting LLVM=1 that replaces usage of GNU tools with the LLVM equivalents. Current status is that it builds on arm64 for QEMU with several warnings and then boots. I didn't hook up any other architectures but the arm64 setup should serve as a guideline. --- arch/arm64/include/arch/asm_macros.h | 4 +--- arch/arm64/rules.mk | 2 ++ arch/arm64/toolchain.mk | 4 ++++ engine.mk | 20 +++++++++++++++++++- lib/heap/heap_wrapper.c | 8 ++++---- make/build.mk | 4 ++-- make/help.mk | 2 ++ 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/arch/asm_macros.h b/arch/arm64/include/arch/asm_macros.h index 94df594ff2..5a49153eb5 100644 --- a/arch/arm64/include/arch/asm_macros.h +++ b/arch/arm64/include/arch/asm_macros.h @@ -36,7 +36,7 @@ ldp \ra, \rb, [sp], #16 .endif .endm -.macro calloc_bootmem_aligned, new_ptr, new_ptr_end, tmp, size_shift, phys_offset=0 +.macro calloc_bootmem_aligned, new_ptr, new_ptr_end, tmp, size_shift, phys_offset .if \size_shift < 4 .error "calloc_bootmem_aligned: Unsupported size_shift, \size_shift" .endif @@ -63,11 +63,9 @@ ldp \ra, \rb, [sp], #16 mov x1, #8 bl arch_clean_invalidate_cache_range -.if \phys_offset != 0 /* clear page */ sub \new_ptr, \new_ptr, \phys_offset sub \new_ptr_end, \new_ptr_end, \phys_offset -.endif /* clean and invalidate new page */ mov x0, \new_ptr diff --git a/arch/arm64/rules.mk b/arch/arm64/rules.mk index 173a75fd61..b44f8668a7 100644 --- a/arch/arm64/rules.mk +++ b/arch/arm64/rules.mk @@ -98,7 +98,9 @@ ARCH_COMPILEFLAGS_FLOAT := ARCH_LDFLAGS += -z max-page-size=4096 +ifeq ($(LLVM),) LIBGCC := $(shell $(TOOLCHAIN_PREFIX)gcc $(GLOBAL_COMPILEFLAGS) $(ARCH_COMPILEFLAGS) -print-libgcc-file-name) +endif # make sure some bits were set up MEMVARS_SET := 0 diff --git a/arch/arm64/toolchain.mk b/arch/arm64/toolchain.mk index 2986ea9e22..c088f69137 100644 --- a/arch/arm64/toolchain.mk +++ b/arch/arm64/toolchain.mk @@ -1,6 +1,9 @@ ifndef ARCH_arm64_TOOLCHAIN_INCLUDED ARCH_arm64_TOOLCHAIN_INCLUDED := 1 +ifneq ($(LLVM),) +LLVM_TARGET_TRIPLE := aarch64-elf +else ifndef ARCH_arm64_TOOLCHAIN_PREFIX ARCH_arm64_TOOLCHAIN_PREFIX := aarch64-elf- FOUNDTOOL=$(shell which $(ARCH_arm64_TOOLCHAIN_PREFIX)gcc) @@ -14,3 +17,4 @@ endif endif endif endif +endif diff --git a/engine.mk b/engine.mk index 6536e366f1..3ba089b4a4 100644 --- a/engine.mk +++ b/engine.mk @@ -63,7 +63,10 @@ GLOBAL_INCLUDES := $(BUILDDIR) $(addsuffix /include,$(LKINC)) GLOBAL_OPTFLAGS ?= $(ARCH_OPTFLAGS) GLOBAL_COMPILEFLAGS := -g -include $(CONFIGHEADER) GLOBAL_COMPILEFLAGS += -Wextra -Wall -Werror=return-type -Wshadow -Wdouble-promotion -GLOBAL_COMPILEFLAGS += -Wno-multichar -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wno-nonnull-compare +GLOBAL_COMPILEFLAGS += -Wno-multichar -Wno-unused-parameter -Wno-unused-function -Wno-unused-label +ifeq ($(LLVM),) +GLOBAL_COMPILEFLAGS += -Wno-nonnull-compare +endif GLOBAL_COMPILEFLAGS += -fno-common GLOBAL_CFLAGS := --std=gnu11 -Werror-implicit-function-declaration -Wstrict-prototypes -Wwrite-strings GLOBAL_CPPFLAGS := --std=c++14 -fno-exceptions -fno-rtti -fno-threadsafe-statics @@ -159,9 +162,11 @@ ifndef ARCH $(error couldn't find arch or platform doesn't define arch) endif include arch/$(ARCH)/rules.mk +ifeq ($(LLVM),) ifndef TOOLCHAIN_PREFIX $(error TOOLCHAIN_PREFIX not set in the arch rules.mk) endif +endif $(info PROJECT = $(PROJECT)) $(info PLATFORM = $(PLATFORM)) @@ -215,6 +220,18 @@ endif # default to no ccache CCACHE ?= + +ifneq ($(LLVM),) +CC := $(CCACHE) clang --target=$(LLVM_TARGET_TRIPLE) +LIBGCC := $(shell $(CC) $(GLOBAL_COMPILEFLAGS) $(ARCH_COMPILEFLAGS) -print-libgcc-file-name) +LD := ld.lld +OBJDUMP := llvm-objdump +OBJCOPY := llvm-objcopy +CPPFILT := llvm-cxxfilt +SIZE := llvm-size +NM := llvm-nm +STRIP := llvm-strip +else CC := $(CCACHE) $(TOOLCHAIN_PREFIX)gcc LD := $(TOOLCHAIN_PREFIX)ld OBJDUMP := $(TOOLCHAIN_PREFIX)objdump @@ -223,6 +240,7 @@ CPPFILT := $(TOOLCHAIN_PREFIX)c++filt SIZE := $(TOOLCHAIN_PREFIX)size NM := $(TOOLCHAIN_PREFIX)nm STRIP := $(TOOLCHAIN_PREFIX)strip +endif # try to have the compiler output colorized error messages if available export GCC_COLORS ?= 1 diff --git a/lib/heap/heap_wrapper.c b/lib/heap/heap_wrapper.c index 162f643c16..aba5109e78 100644 --- a/lib/heap/heap_wrapper.c +++ b/lib/heap/heap_wrapper.c @@ -89,6 +89,10 @@ static inline void *HEAP_CALLOC(size_t n, size_t s) { #define HEAP_FREE(p) dlfree(p) static inline void HEAP_INIT(void) {} +static inline void dump_callback(void *start, void *end, size_t used_bytes, void *arg) { + printf("\t\tstart %p end %p used_bytes %zu\n", start, end, used_bytes); +} + static inline void HEAP_DUMP(void) { struct mallinfo minfo = dlmallinfo(); @@ -102,10 +106,6 @@ static inline void HEAP_DUMP(void) { printf("\t\treleasable space 0x%zx\n", minfo.keepcost); printf("\theap block list:\n"); - void dump_callback(void *start, void *end, size_t used_bytes, void *arg) { - printf("\t\tstart %p end %p used_bytes %zu\n", start, end, used_bytes); - } - dlmalloc_inspect_all(&dump_callback, NULL); } diff --git a/make/build.mk b/make/build.mk index 84dca4a198..d62bc9e42f 100644 --- a/make/build.mk +++ b/make/build.mk @@ -24,8 +24,8 @@ $(OUTELF).hex: $(OUTELF) $(OUTELF): $(ALLMODULE_OBJS) $(EXTRA_OBJS) $(LINKER_SCRIPT) $(EXTRA_LINKER_SCRIPTS) $(info linking $@) $(NOECHO)$(SIZE) -t --common $(sort $(ALLMODULE_OBJS)) $(EXTRA_OBJS) - $(NOECHO)$(LD) $(GLOBAL_LDFLAGS) $(ARCH_LDFLAGS) -dT $(LINKER_SCRIPT) \ - $(addprefix -T,$(EXTRA_LINKER_SCRIPTS)) \ + $(NOECHO)$(LD) $(GLOBAL_LDFLAGS) $(ARCH_LDFLAGS) \ + $(addprefix -T,$(EXTRA_LINKER_SCRIPTS)) -T $(LINKER_SCRIPT) \ $(ALLMODULE_OBJS) $(EXTRA_OBJS) $(LIBGCC) -Map=$(OUTELF).map -o $@ $(OUTELF).sym: $(OUTELF) diff --git a/make/help.mk b/make/help.mk index c71ab3b322..145e52ef02 100644 --- a/make/help.mk +++ b/make/help.mk @@ -15,6 +15,8 @@ help: @echo "Environment or command line variables controlling build:" @echo "PROJECT = " @echo "TOOLCHAIN_PREFIX = " + @echo "LLVM = 1 # use LLVM tools instead of GCC and binutils;" + @echo " # user should add tools to PATH instead of specifying TOOLCHAIN_PREFIX" @echo "" @echo "Special make targets:" @echo "make help: This help"