]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
build: Add proper compiler detection
authorStefan Jumarea <stefanjumarea02@gmail.com>
Mon, 22 May 2023 10:06:32 +0000 (13:06 +0300)
committerUnikraft <monkey@unikraft.io>
Thu, 1 Jun 2023 11:44:04 +0000 (11:44 +0000)
Checking for the output of `$(CC) -v` may fail depending on compiler
versions (e.g. `clang-15 -v` on ubuntu will output `Ubuntu clang ...`,
making `CC_NAME` `Ubuntu` instead of `clang`).

Use a preprocessor program to find the proper compiler name and version.

The `cc-version.sh` script is taken from the linux kernel tree, and will
return a string containing the compiler name and version:
`GCC|Clang MAJOR_V.MINOR_V`.

Signed-off-by: Stefan Jumarea <stefanjumarea02@gmail.com>
Reviewed-by: Marco Schlumpp <marco@unikraft.io>
Reviewed-by: Maria Sfiraiala <maria.sfiraiala@gmail.com>
Approved-by: Simon Kuenzer <simon@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #902

Makefile
support/build/Makefile.rules
support/build/cc-version.sh [new file with mode: 0755]

index e727d3f464be1c4b8951f02ad3a2f42fa00ca26b..40ba709717765b3bda38e2662faeef6eb9614c01 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -624,20 +624,21 @@ DTC               := dtc
 TIME           := $(shell which time)
 LIFTOFF                := liftoff -e -s
 override ARFLAGS:= rcs
-CC_VERSION     := $(shell $(CC) --version | \
-                  sed -n -r 's/^.* ([0-9]*)\.([0-9]*)\.([0-9]*)[ ]*.*/\1.\2/p')
+
+CC_INFO := $(shell $(CONFIG_UK_BASE)/support/build/cc-version.sh $(CC))
+CC_NAME := $(word 1,$(CC_INFO))
+
 # Retrieve GCC major and minor number from CC_VERSION. They would be used
 # to select correct optimization parameters for target CPUs.
-CC_VER_MAJOR   := $(word 1,$(subst ., ,$(CC_VERSION)))
-CC_VER_MINOR   := $(word 2,$(subst ., ,$(CC_VERSION)))
+CC_VER_MAJOR   := $(word 2,$(subst ., ,$(CC_INFO)))
+CC_VER_MINOR   := $(word 3,$(subst ., ,$(CC_INFO)))
+CC_VERSION     := $(CC_VER_MAJOR).$(CC_VER_MINOR)
 
 ASFLAGS                += -DCC_VERSION=$(CC_VERSION)
 CFLAGS         += -DCC_VERSION=$(CC_VERSION)
 CXXFLAGS       += -DCC_VERSION=$(CC_VERSION)
 GOCFLAGS       += -DCC_VERSION=$(CC_VERSION)
 
-CC_NAME                := $(shell $(CC) -v 2>&1 | grep -E "\\w+ version" | sed 's/\s.*$$//')
-
 # ensure $(BUILD_DIR)/kconfig, $(BUILD_DIR)/include and $(BUILD_DIR)/include/uk exists
 $(call mk_sub_build_dir,kconfig)
 $(call mk_sub_build_dir,include)
index c1d84b89d9fc4e94aa4926d07fb349ddc179d8d8..4806e863c7dd8e785879a7c6b28a30f7969be2cb 100644 (file)
@@ -31,12 +31,12 @@ uc = $(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(s
 
 # test if GCC is set as a compiler
 define have_gcc =
-$(if $(filter gcc,$(CC_NAME)),y,n)
+$(if $(filter GCC,$(CC_NAME)),y,n)
 endef
 
 # test if CLANG is set as a compiler
 define have_clang =
-$(if $(filter clang,$(CC_NAME)),y,n)
+$(if $(filter Clang,$(CC_NAME)),y,n)
 endef
 
 # test whether GCC version is greater than or equal to the minimum requirement
diff --git a/support/build/cc-version.sh b/support/build/cc-version.sh
new file mode 100755 (executable)
index 0000000..6f0ef51
--- /dev/null
@@ -0,0 +1,42 @@
+#!/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
+}
+
+# $@ 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
+
+echo $name $version