[PATCH 4/4] Makefile: add LLVM/clang support
Ahmad Fatoum
a.fatoum at pengutronix.de
Mon Nov 25 07:09:45 PST 2024
We can't compile all of barebox with clang yet, because we use
GCC-specific extensions, especially C-code in naked functions, which we
is judiciously used on 32-bit ARM platforms.
Nevertheless, sandbox can already be used with clang and it's useful for
incoming support for libfuzzer, so let's add support to Kbuild.
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
Makefile | 41 +++++++++++++++++++++++++++++++++++------
arch/arm/Kconfig | 3 ++-
arch/sandbox/Makefile | 4 ++++
scripts/Makefile.clang | 9 +++++++++
4 files changed, 50 insertions(+), 7 deletions(-)
create mode 100644 scripts/Makefile.clang
diff --git a/Makefile b/Makefile
index b17452ed2cb1..7f7dde9e6bc9 100644
--- a/Makefile
+++ b/Makefile
@@ -383,8 +383,19 @@ HOST_LFS_CFLAGS := $(shell getconf LFS_CFLAGS 2>/dev/null)
HOST_LFS_LDFLAGS := $(shell getconf LFS_LDFLAGS 2>/dev/null)
HOST_LFS_LIBS := $(shell getconf LFS_LIBS 2>/dev/null)
-HOSTCC = gcc
-HOSTCXX = g++
+ifneq ($(LLVM),)
+ifneq ($(filter %/,$(LLVM)),)
+LLVM_PREFIX := $(LLVM)
+else ifneq ($(filter -%,$(LLVM)),)
+LLVM_SUFFIX := $(LLVM)
+endif
+
+HOSTCC = $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
+HOSTCXX = $(LLVM_PREFIX)clang++$(LLVM_SUFFIX)
+else
+HOSTCC = gcc
+HOSTCXX = g++
+endif
KBUILD_USERHOSTCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
-O2 -fomit-frame-pointer -std=gnu11
@@ -397,15 +408,26 @@ KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
# Make variables (CC, etc...)
-
-LD = $(CROSS_COMPILE)ld
-CC = $(CROSS_COMPILE)gcc
CPP = $(CC) -E
+ifneq ($(LLVM),)
+CC = $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
+LD = $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)
+AR = $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
+NM = $(LLVM_PREFIX)llvm-nm$(LLVM_SUFFIX)
+OBJCOPY = $(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)
+OBJDUMP = $(LLVM_PREFIX)llvm-objdump$(LLVM_SUFFIX)
+READELF = $(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)
+STRIP = $(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)
+else
+CC = $(CROSS_COMPILE)gcc
+LD = $(CROSS_COMPILE)ld
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
-STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
+READELF = $(CROSS_COMPILE)readelf
+STRIP = $(CROSS_COMPILE)strip
+endif
LEX = flex
YACC = bison
AWK = awk
@@ -459,6 +481,7 @@ KBUILD_AFLAGS_KERNEL :=
KBUILD_CFLAGS_KERNEL :=
KBUILD_AFLAGS_MODULE := -DMODULE
KBUILD_CFLAGS_MODULE := -DMODULE
+CLANG_FLAGS :=
LDFLAGS_barebox := -Map barebox.map
@@ -530,6 +553,10 @@ ifdef building_out_of_srctree
{ echo "# this is build directory, ignore it"; echo "*"; } > .gitignore
endif
+ifeq ($(CONFIG_CC_IS_CLANG),y)
+include $(srctree)/scripts/Makefile.clang
+endif
+
ifdef config-build
# ===========================================================================
# *config targets only - make sure prerequisites are updated, and descend
@@ -660,6 +687,8 @@ KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
KBUILD_CFLAGS += $(call cc-disable-warning,frame-address,)
endif
+KBUILD_CFLAGS-$(CONFIG_CC_IS_CLANG) += -Wno-gnu
+
KBUILD_CFLAGS-$(CONFIG_WERROR) += -Werror
# This warning generated too much noise in a regular build.
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 0251f2dcef62..516cc721a5f6 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -287,8 +287,9 @@ config BOARD_ARM_GENERIC_DT_AARCH64
default y
config AEABI
- bool "Use the ARM EABI to compile barebox"
+ bool "Use the ARM EABI to compile barebox" if !CC_IS_CLANG
depends on !CPU_V8
+ default CC_IS_CLANG
help
This option allows for barebox to be compiled using the latest
ARM ABI (aka EABI).
diff --git a/arch/sandbox/Makefile b/arch/sandbox/Makefile
index 5c51f2de38b3..5d434bcbf19b 100644
--- a/arch/sandbox/Makefile
+++ b/arch/sandbox/Makefile
@@ -62,6 +62,10 @@ endif
ifeq ($(CONFIG_ASAN),y)
KBUILD_CPPFLAGS += -fsanitize=address
SANITIZER_LIBS += -fsanitize=address
+ifeq ($(CONFIG_CC_IS_CLANG),y)
+KBUILD_CPPFLAGS += -fno-sanitize-address-globals-dead-stripping
+BAREBOX_LDFLAGS += -fno-sanitize-address-globals-dead-stripping
+endif
endif
ifeq ($(CONFIG_UBSAN),y)
diff --git a/scripts/Makefile.clang b/scripts/Makefile.clang
new file mode 100644
index 000000000000..c0302ff82670
--- /dev/null
+++ b/scripts/Makefile.clang
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+CLANG_FLAGS += -Wno-typdef-redefinition
+CLANG_FLAGS += -Werror=unknown-warning-option
+CLANG_FLAGS += -Werror=ignored-optimization-argument
+CLANG_FLAGS += -Werror=option-ignored
+CLANG_FLAGS += -Werror=unused-command-line-argument
+KBUILD_CPPFLAGS += $(CLANG_FLAGS)
+KBUILD_USERHOSTCFLAGS += $(CLANG_FLAGS)
+export CLANG_FLAGS
--
2.39.5
More information about the barebox
mailing list