From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/4] common: implement CC_IS_GCC and CC_IS_CLANG symbols
Date: Mon, 25 Nov 2024 16:09:43 +0100 [thread overview]
Message-ID: <20241125150945.166979-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20241125150945.166979-1-a.fatoum@pengutronix.de>
As preparation for support LLVM+clang, we need barebox to be able to
detect what compiler is being used and its version.
Import the necessary infrastructure from Linux.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
common/Kconfig | 16 +++++++++++++
scripts/Kconfig.include | 6 +++++
scripts/cc-version.sh | 52 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 74 insertions(+)
create mode 100755 scripts/cc-version.sh
diff --git a/common/Kconfig b/common/Kconfig
index 859356038386..498d219b4de3 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -2,6 +2,22 @@
source "common/boards/Kconfig"
+config CC_IS_GCC
+ def_bool $(success,test "$(cc-name)" = GCC)
+
+config GCC_VERSION
+ int
+ default $(cc-version) if CC_IS_GCC
+ default 0
+
+config CC_IS_CLANG
+ def_bool $(success,test "$(cc-name)" = Clang)
+
+config CLANG_VERSION
+ int
+ default $(cc-version) if CC_IS_CLANG
+ default 0
+
config GREGORIAN_CALENDER
bool
diff --git a/scripts/Kconfig.include b/scripts/Kconfig.include
index a5fe72c504ff..55ec8737d0db 100644
--- a/scripts/Kconfig.include
+++ b/scripts/Kconfig.include
@@ -23,6 +23,12 @@ success = $(if-success,$(1),y,n)
# Return n if <command> exits with 0, y otherwise
failure = $(if-success,$(1),n,y)
+# Get the C compiler name, version, and error out if it is not supported.
+cc-info := $(shell,$(srctree)/scripts/cc-version.sh $(CC))
+$(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not supported.)
+cc-name := $(shell,set -- $(cc-info) && echo $1)
+cc-version := $(shell,set -- $(cc-info) && echo $2)
+
# $(cc-option,<flag>)
# Return y if the compiler supports <flag>, n otherwise
cc-option = $(success,mkdir .tmp_$$$$; trap "rm -rf .tmp_$$$$" EXIT; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$$$/tmp.o)
diff --git a/scripts/cc-version.sh b/scripts/cc-version.sh
new file mode 100755
index 000000000000..65d8a2805d2e
--- /dev/null
+++ b/scripts/cc-version.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Print the C compiler name and its version in a 5 or 6-digit form.
+# Also, perform the minimum version check.
+
+set -e
+
+# Print the C compiler name and some version components.
+get_c_compiler_info()
+{
+ cat <<- EOF | "$@" -E -P -x c - 2>/dev/null
+ #if defined(__clang__)
+ Clang __clang_major__ __clang_minor__ __clang_patchlevel__
+ #elif defined(__GNUC__)
+ GCC __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
+ #else
+ unknown
+ #endif
+ EOF
+}
+
+# Convert the version string x.y.z to a canonical 5 or 6-digit form.
+get_canonical_version()
+{
+ IFS=.
+ set -- $1
+ echo $((10000 * $1 + 100 * $2 + $3))
+}
+
+# $@ instead of $1 because multiple words might be given, e.g. CC="ccache gcc".
+orig_args="$@"
+set -- $(get_c_compiler_info "$@")
+
+name=$1
+
+case "$name" in
+GCC)
+ version=$2.$3.$4
+ ;;
+Clang)
+ version=$2.$3.$4
+ ;;
+*)
+ echo "$orig_args: unknown C compiler" >&2
+ exit 1
+ ;;
+esac
+
+cversion=$(get_canonical_version $version)
+
+echo $name $cversion
--
2.39.5
next prev 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 LLVM/clang support Ahmad Fatoum
2024-11-25 15:09 ` [PATCH 1/4] sandbox: use host system's UBSan library Ahmad Fatoum
2024-11-25 15:09 ` Ahmad Fatoum [this message]
2024-11-25 15:09 ` [PATCH 3/4] fixdep: sync with Linux Ahmad Fatoum
2024-11-25 15:09 ` [PATCH 4/4] Makefile: add LLVM/clang support Ahmad Fatoum
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-3-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