Skip to content

Commit

Permalink
[build] make LK buildable with LLVM/Clang
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pcc committed Apr 1, 2022
1 parent bce9599 commit d472e17
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
4 changes: 1 addition & 3 deletions arch/arm64/include/arch/asm_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,11 +58,9 @@ ldp \ra, \rb, [sp], #16
add \new_ptr_end, \new_ptr, #(1 << \size_shift)
str \new_ptr_end, [\tmp, #:lo12:boot_alloc_end]

.if \phys_offset != 0
/* clear page */
sub \new_ptr, \new_ptr, \phys_offset
sub \new_ptr_end, \new_ptr_end, \phys_offset
.endif

/* clear page */
mov \tmp, \new_ptr
Expand Down
2 changes: 2 additions & 0 deletions arch/arm64/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ ARCH_COMPILEFLAGS += -fno-omit-frame-pointer

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
Expand Down
4 changes: 4 additions & 0 deletions arch/arm64/toolchain.mk
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -14,3 +17,4 @@ endif
endif
endif
endif
endif
20 changes: 19 additions & 1 deletion engine.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -157,9 +160,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))
Expand Down Expand Up @@ -213,6 +218,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
Expand All @@ -221,6 +238,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
Expand Down
8 changes: 4 additions & 4 deletions lib/heap/heap_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions make/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,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)
Expand Down
2 changes: 2 additions & 0 deletions make/help.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ help:
@echo "Environment or command line variables controlling build:"
@echo "PROJECT = <project name>"
@echo "TOOLCHAIN_PREFIX = <absolute path to toolchain or relative path with 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"
Expand Down

0 comments on commit d472e17

Please sign in to comment.