mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/4] Makefile: add LLVM/clang support
Date: Mon, 25 Nov 2024 16:09:45 +0100	[thread overview]
Message-ID: <20241125150945.166979-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20241125150945.166979-1-a.fatoum@pengutronix.de>

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@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




      parent reply	other threads:[~2024-11-25 15:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-25 15:09 [PATCH 0/4] add first " Ahmad Fatoum
2024-11-25 15:09 ` [PATCH 1/4] sandbox: use host system's UBSan library Ahmad Fatoum
2024-11-25 15:09 ` [PATCH 2/4] common: implement CC_IS_GCC and CC_IS_CLANG symbols Ahmad Fatoum
2024-11-25 15:09 ` [PATCH 3/4] fixdep: sync with Linux Ahmad Fatoum
2024-11-25 15:09 ` Ahmad Fatoum [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241125150945.166979-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox