Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for building AArch64 with Clang #374

Merged
merged 6 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 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 @@ -63,11 +63,9 @@ ldp \ra, \rb, [sp], #16
mov x1, #8
bl arch_clean_invalidate_cache_range

.if \phys_offset != 0
/* clear page */
/* calculate virtual address */
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
Expand Down
39 changes: 27 additions & 12 deletions engine.mk
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ 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
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 @@ -181,6 +181,32 @@ ifndef TOOLCHAIN_PREFIX
$(error TOOLCHAIN_PREFIX not set in the arch rules.mk)
endif

# default to no ccache
CCACHE ?=
CC := $(CCACHE) $(TOOLCHAIN_PREFIX)gcc
LD := $(TOOLCHAIN_PREFIX)ld
OBJDUMP := $(TOOLCHAIN_PREFIX)objdump
OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy
CPPFILT := $(TOOLCHAIN_PREFIX)c++filt
SIZE := $(TOOLCHAIN_PREFIX)size
NM := $(TOOLCHAIN_PREFIX)nm
STRIP := $(TOOLCHAIN_PREFIX)strip

# Now that CC is defined we can check if warning flags are supported and add
# them to GLOBAL_COMPILEFLAGS if they are.
ifeq ($(call is_warning_flag_supported,-Wnonnull-compare),yes)
GLOBAL_COMPILEFLAGS += -Wno-nonnull-compare
endif
# Ideally we would move this check to arm64/rules.mk, but we can only check
# for supported warning flags once CC is defined.
ifeq ($(ARCH),arm64)
# Clang incorrectly diagnoses msr operations as need a 64-bit operand even if
# the underlying register is actually 32 bits. Silence this common warning.
ifeq ($(call is_warning_flag_supported,-Wasm-operand-widths),yes)
ARCH_COMPILEFLAGS += -Wno-asm-operand-widths
endif
endif

$(info PROJECT = $(PROJECT))
$(info PLATFORM = $(PLATFORM))
$(info TARGET = $(TARGET))
Expand Down Expand Up @@ -230,17 +256,6 @@ ifneq ($(DEFINES),)
$(error DEFINES variable set, please move to GLOBAL_DEFINES: $(DEFINES))
endif

# default to no ccache
CCACHE ?=
CC := $(CCACHE) $(TOOLCHAIN_PREFIX)gcc
LD := $(TOOLCHAIN_PREFIX)ld
OBJDUMP := $(TOOLCHAIN_PREFIX)objdump
OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy
CPPFILT := $(TOOLCHAIN_PREFIX)c++filt
SIZE := $(TOOLCHAIN_PREFIX)size
NM := $(TOOLCHAIN_PREFIX)nm
STRIP := $(TOOLCHAIN_PREFIX)strip

# try to have the compiler output colorized error messages if available
export GCC_COLORS ?= 1

Expand Down
5 changes: 4 additions & 1 deletion external/lib/libm/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ LOCAL_DIR := $(GET_LOCAL_DIR)

MODULE := $(LOCAL_DIR)

MODULE_CFLAGS += -Wno-unused-variable -Wno-sign-compare -Wno-parentheses -Wno-double-promotion -Wno-maybe-uninitialized
MODULE_CFLAGS += -Wno-unused-variable -Wno-sign-compare -Wno-parentheses -Wno-double-promotion
ifeq ($(call is_warning_flag_supported,-Wmaybe-uninitialized),yes)
MODULE_CFLAGS += -Wno-maybe-uninitialized
endif
MODULE_OPTIONS := float

MODULE_SRCS += \
Expand Down
7 changes: 4 additions & 3 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) {}

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,9 +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
2 changes: 1 addition & 1 deletion make/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $(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) \
$(NOECHO)$(LD) $(GLOBAL_LDFLAGS) $(ARCH_LDFLAGS) -d -T $(LINKER_SCRIPT) \
$(addprefix -T,$(EXTRA_LINKER_SCRIPTS)) \
$(ALLMODULE_OBJS) $(EXTRA_OBJS) $(LIBGCC) -Map=$(OUTELF).map -o $@

Expand Down
14 changes: 14 additions & 0 deletions make/macros.mk
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ define MAKECONFIGHEADER
echo $3 >> $1.tmp; \
$(call TESTANDREPLACEFILE,$1.tmp,$1)
endef

check_compiler_flag = $(shell $(CC) -c -xc /dev/null -o /dev/null $(1) 2>/dev/null && echo yes || echo no)
# Due to GCC's behaviour with regard to unknown warning flags this macro can
# only be used to detect warning-enable options (-Wfoo) but not for warning
# disable flags such as -Wno-foo.
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#:~:text=When%20an%20unrecognized%20warning%20option%20is%20requested
is_warning_flag_supported = $(strip \
$(if $(findstring -Wno-,$(1)),$(error "Cannot use -Wno- flags here: $(1)"),) \
$(if $(CC),,$(error "CC is not set, this macro cannot be used yet!")) \
$(if $($(call MAKECVAR,$(1))), \
$(info Using cached result for $(1): $($(call MAKECVAR,$(1)))), \
$(eval $(call MAKECVAR,$(1)) := $(call check_compiler_flag,-Werror -fsyntax-only $(1))) \
$(info Checking if $(1) is supported: $($(call MAKECVAR,$(1)))) \
)$($(call MAKECVAR,$(1))))
9 changes: 6 additions & 3 deletions top/include/lk/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,22 @@
#define __WARN_UNUSED_RESULT
#endif

#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
#if defined(__clang__)
/* Clang does not support externally_visible, used should be similar. */
#define __EXTERNALLY_VISIBLE __attribute__((used))
#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
#define __EXTERNALLY_VISIBLE __attribute__((externally_visible))
#else
#define __EXTERNALLY_VISIBLE
#endif

#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) || defined(__clang__)
#define __UNREACHABLE __builtin_unreachable()
#else
#define __UNREACHABLE
#endif

#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || defined(__clang__)
#ifdef __cplusplus
#define STATIC_ASSERT(e) static_assert(e, #e)
#else
Expand Down