cc-version.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. set -e
  7. # Print the compiler name and some version components.
  8. get_compiler_info()
  9. {
  10. cat <<- EOF | "$@" -E -P -x c - 2>/dev/null
  11. #if defined(__clang__)
  12. Clang __clang_major__ __clang_minor__ __clang_patchlevel__
  13. #elif defined(__INTEL_COMPILER)
  14. ICC __INTEL_COMPILER __INTEL_COMPILER_UPDATE
  15. #elif defined(__GNUC__)
  16. GCC __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
  17. #else
  18. unknown
  19. #endif
  20. EOF
  21. }
  22. # Convert the version string x.y.z to a canonical 5 or 6-digit form.
  23. get_canonical_version()
  24. {
  25. IFS=.
  26. set -- $1
  27. echo $((10000 * $1 + 100 * $2 + $3))
  28. }
  29. # $@ instead of $1 because multiple words might be given, e.g. CC="ccache gcc".
  30. orig_args="$@"
  31. set -- $(get_compiler_info "$@")
  32. name=$1
  33. min_tool_version=$(dirname $0)/min-tool-version.sh
  34. case "$name" in
  35. GCC)
  36. version=$2.$3.$4
  37. min_version=$($min_tool_version gcc)
  38. ;;
  39. Clang)
  40. version=$2.$3.$4
  41. min_version=$($min_tool_version llvm)
  42. ;;
  43. ICC)
  44. version=$(($2 / 100)).$(($2 % 100)).$3
  45. min_version=$($min_tool_version icc)
  46. ;;
  47. *)
  48. echo "$orig_args: unknown compiler" >&2
  49. exit 1
  50. ;;
  51. esac
  52. cversion=$(get_canonical_version $version)
  53. min_cversion=$(get_canonical_version $min_version)
  54. if [ "$cversion" -lt "$min_cversion" ]; then
  55. echo >&2 "***"
  56. echo >&2 "*** Compiler is too old."
  57. echo >&2 "*** Your $name version: $version"
  58. echo >&2 "*** Minimum $name version: $min_version"
  59. echo >&2 "***"
  60. exit 1
  61. fi
  62. echo $name $cversion