Skip to content

Commit aec6c60

Browse files
committed
kbuild: check the minimum compiler version in Kconfig
Paul Gortmaker reported a regression in the GCC version check. [1] If you use GCC 4.8, the build breaks before showing the error message "error Sorry, your version of GCC is too old - please use 4.9 or newer." I do not want to apply his fix-up since it implies we would not be able to remove any cc-option test. Anyway, I admit checking the GCC version in <linux/compiler-gcc.h> is too late. Almost at the same time, Linus also suggested to move the compiler version error to Kconfig time. [2] I unified the two similar scripts, gcc-version.sh and clang-version.sh into cc-version.sh. The old scripts invoked the compiler multiple times (3 times for gcc-version.sh, 4 times for clang-version.sh). I refactored the code so the new one invokes the compiler just once, and also tried my best to use shell-builtin commands where possible. The new script runs faster. $ time ./scripts/clang-version.sh clang 120000 real 0m0.029s user 0m0.012s sys 0m0.021s $ time ./scripts/cc-version.sh clang Clang 120000 real 0m0.009s user 0m0.006s sys 0m0.004s cc-version.sh also shows an error message if the compiler is too old: $ make defconfig CC=clang-9 *** Default configuration is based on 'x86_64_defconfig' *** *** Compiler is too old. *** Your Clang version: 9.0.1 *** Minimum Clang version: 10.0.1 *** scripts/Kconfig.include:46: Sorry, this compiler is not supported. make[1]: *** [scripts/kconfig/Makefile:81: defconfig] Error 1 make: *** [Makefile:602: defconfig] Error 2 The new script takes care of ICC because we have <linux/compiler-intel.h> although I am not sure if building the kernel with ICC is well-supported. [1]: https://lore.kernel.org/r/[email protected] [2]: https://lore.kernel.org/r/CAHk-=wh-+TMHPTFo1qs-MYyK7tZh-OQovA=pP3=e06aCVp6_kA@mail.gmail.com Fixes: 87de84c ("kbuild: remove cc-option test of -Werror=date-time") Reported-by: Paul Gortmaker <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Tested-by: Nick Desaulniers <[email protected]> Reviewed-by: Nathan Chancellor <[email protected]> Tested-by: Nathan Chancellor <[email protected]> Reviewed-by: Miguel Ojeda <[email protected]> Tested-by: Miguel Ojeda <[email protected]> Tested-by: Sedat Dilek <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 052c805 commit aec6c60

File tree

8 files changed

+93
-65
lines changed

8 files changed

+93
-65
lines changed

MAINTAINERS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4314,7 +4314,6 @@ C: irc://chat.freenode.net/clangbuiltlinux
43144314
F: Documentation/kbuild/llvm.rst
43154315
F: include/linux/compiler-clang.h
43164316
F: scripts/clang-tools/
4317-
F: scripts/clang-version.sh
43184317
F: scripts/lld-version.sh
43194318
K: \b(?i:clang|llvm)\b
43204319

include/linux/compiler-clang.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
#error "Please don't include <linux/compiler-clang.h> directly, include <linux/compiler.h> instead."
44
#endif
55

6-
#define CLANG_VERSION (__clang_major__ * 10000 \
7-
+ __clang_minor__ * 100 \
8-
+ __clang_patchlevel__)
9-
10-
#if CLANG_VERSION < 100001
11-
#ifndef __BPF_TRACING__
12-
# error Sorry, your version of Clang is too old - please use 10.0.1 or newer.
13-
#endif
14-
#endif
15-
166
/* Compiler specific definitions for Clang compiler */
177

188
/* same as gcc, this was present in clang-2.6 so we can assume it works

include/linux/compiler-gcc.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@
1010
+ __GNUC_MINOR__ * 100 \
1111
+ __GNUC_PATCHLEVEL__)
1212

13-
/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 */
14-
#if GCC_VERSION < 40900
15-
# error Sorry, your version of GCC is too old - please use 4.9 or newer.
16-
#elif defined(CONFIG_ARM64) && GCC_VERSION < 50100
17-
/*
18-
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63293
19-
* https://lore.kernel.org/r/[email protected]
20-
*/
21-
# error Sorry, your version of GCC is too old - please use 5.1 or newer.
22-
#endif
23-
2413
/*
2514
* This macro obfuscates arithmetic on a variable address so that gcc
2615
* shouldn't recognize the original var, and make assumptions about it.

init/Kconfig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,27 @@ config CC_VERSION_TEXT
2626
and then every file will be rebuilt.
2727

2828
config CC_IS_GCC
29-
def_bool $(success,echo "$(CC_VERSION_TEXT)" | grep -q gcc)
29+
def_bool $(success,test "$(cc-name)" = GCC)
3030

3131
config GCC_VERSION
3232
int
33-
default $(shell,$(srctree)/scripts/gcc-version.sh $(CC)) if CC_IS_GCC
33+
default $(cc-version) if CC_IS_GCC
3434
default 0
3535

3636
config LD_VERSION
3737
int
3838
default $(shell,$(LD) --version | $(srctree)/scripts/ld-version.sh)
3939

4040
config CC_IS_CLANG
41-
def_bool $(success,echo "$(CC_VERSION_TEXT)" | grep -q clang)
41+
def_bool $(success,test "$(cc-name)" = Clang)
4242

4343
config LD_IS_LLD
4444
def_bool $(success,$(LD) -v | head -n 1 | grep -q LLD)
4545

4646
config CLANG_VERSION
4747
int
48-
default $(shell,$(srctree)/scripts/clang-version.sh $(CC))
48+
default $(cc-version) if CC_IS_CLANG
49+
default 0
4950

5051
config LLD_VERSION
5152
int

scripts/Kconfig.include

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ as-instr = $(success,printf "%b\n" "$(1)" | $(CC) $(CLANG_FLAGS) -c -x assembler
3939
$(error-if,$(failure,command -v $(CC)),compiler '$(CC)' not found)
4040
$(error-if,$(failure,command -v $(LD)),linker '$(LD)' not found)
4141

42+
# Get the compiler name, version, and error out if it is not supported.
43+
cc-info := $(shell,$(srctree)/scripts/cc-version.sh $(CC))
44+
$(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this compiler is not supported.)
45+
cc-name := $(shell,set -- $(cc-info) && echo $1)
46+
cc-version := $(shell,set -- $(cc-info) && echo $2)
47+
4248
# Fail if the linker is gold as it's not capable of linking the kernel proper
4349
$(error-if,$(success, $(LD) -v | grep -q gold), gold linker '$(LD)' not supported)
4450

scripts/cc-version.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Print the compiler name and its version in a 5 or 6-digit form.
5+
# Also, perform the minimum version check.
6+
7+
set -e
8+
9+
# When you raise the minimum compiler version, please update
10+
# Documentation/process/changes.rst as well.
11+
gcc_min_version=4.9.0
12+
clang_min_version=10.0.1
13+
icc_min_version=16.0.3 # temporary
14+
15+
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63293
16+
# https://lore.kernel.org/r/[email protected]
17+
if [ "$SRCARCH" = arm64 ]; then
18+
gcc_min_version=5.1.0
19+
fi
20+
21+
# Print the compiler name and some version components.
22+
get_compiler_info()
23+
{
24+
cat <<- EOF | "$@" -E -P -x c - 2>/dev/null
25+
#if defined(__clang__)
26+
Clang __clang_major__ __clang_minor__ __clang_patchlevel__
27+
#elif defined(__INTEL_COMPILER)
28+
ICC __INTEL_COMPILER __INTEL_COMPILER_UPDATE
29+
#elif defined(__GNUC__)
30+
GCC __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
31+
#else
32+
unknown
33+
#endif
34+
EOF
35+
}
36+
37+
# Convert the version string x.y.z to a canonical 5 or 6-digit form.
38+
get_canonical_version()
39+
{
40+
IFS=.
41+
set -- $1
42+
echo $((10000 * $1 + 100 * $2 + $3))
43+
}
44+
45+
# $@ instead of $1 because multiple words might be given, e.g. CC="ccache gcc".
46+
orig_args="$@"
47+
set -- $(get_compiler_info "$@")
48+
49+
name=$1
50+
51+
case "$name" in
52+
GCC)
53+
version=$2.$3.$4
54+
min_version=$gcc_min_version
55+
;;
56+
Clang)
57+
version=$2.$3.$4
58+
min_version=$clang_min_version
59+
;;
60+
ICC)
61+
version=$(($2 / 100)).$(($2 % 100)).$3
62+
min_version=$icc_min_version
63+
;;
64+
*)
65+
echo "$orig_args: unknown compiler" >&2
66+
exit 1
67+
;;
68+
esac
69+
70+
cversion=$(get_canonical_version $version)
71+
min_cversion=$(get_canonical_version $min_version)
72+
73+
if [ "$cversion" -lt "$min_cversion" ]; then
74+
echo >&2 "***"
75+
echo >&2 "*** Compiler is too old."
76+
echo >&2 "*** Your $name version: $version"
77+
echo >&2 "*** Minimum $name version: $min_version"
78+
echo >&2 "***"
79+
exit 1
80+
fi
81+
82+
echo $name $cversion

scripts/clang-version.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

scripts/gcc-version.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)