xref: /linux/scripts/cc-version.sh (revision a1c3be890440a1769ed6f822376a3e3ab0d42994)
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
7set -e
8
9# When you raise the minimum compiler version, please update
10# Documentation/process/changes.rst as well.
11gcc_min_version=4.9.0
12clang_min_version=10.0.1
13icc_min_version=16.0.3 # temporary
14
15# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63293
16# https://lore.kernel.org/r/20210107111841.GN1551@shell.armlinux.org.uk
17if [ "$SRCARCH" = arm64 ]; then
18	gcc_min_version=5.1.0
19fi
20
21# Print the compiler name and some version components.
22get_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.
38get_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".
46orig_args="$@"
47set -- $(get_compiler_info "$@")
48
49name=$1
50
51case "$name" in
52GCC)
53	version=$2.$3.$4
54	min_version=$gcc_min_version
55	;;
56Clang)
57	version=$2.$3.$4
58	min_version=$clang_min_version
59	;;
60ICC)
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	;;
68esac
69
70cversion=$(get_canonical_version $version)
71min_cversion=$(get_canonical_version $min_version)
72
73if [ "$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
80fi
81
82echo $name $cversion
83