diff --git a/kbuild/kbuild.sh b/kbuild/kbuild.sh new file mode 100644 index 000000000..91b2ab0e4 --- /dev/null +++ b/kbuild/kbuild.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +is_supported_compiler_option() +{ + local compiler_bin=$1 + local option=$2 + + # $ gcc-7 -finvalid -Winvalid -xc /dev/null 2>&1 + # gcc-7: error: unrecognized command line option '-finvalid'; did you mean '-finline'? + # gcc-7: error: unrecognized command line option '-Winvalid'; did you mean '-Winline'? + # + # $ gcc-7 -Werror=attribute-alias -xc /dev/null + # cc1: error: -Werror=attribute-alias: no option -Wattribute-alias + # $ gcc-7 -Wno-error=attribute-alias -xc /dev/null + # cc1: error: -Werror=attribute-alias: no option -Wattribute-alias + # + # $ gcc-11 -Winvalid-option -xc /dev/null + # gcc-11: error: unrecognized command-line option ‘-Winvalid-option’; did you mean ‘-Winvalid-pch’? + # + # $ clang-15 -Winvalid-option -finvalid -xc /dev/null + # warning: unknown warning option '-Winvalid-option' [-Wunknown-warning-option] + # + # $ clang-15 -finvalid -xc /dev/null 2>&1 + # clang-15: error: unknown argument: '-fstrict-flex-arrays=3' + + # if an option is "-Wno-", need to remove "no" and check the the remaining flag. + # + # $ gcc-7 -Wno-attribute-alias -xc /dev/null + # /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': + # (.text+0x17): undefined reference to `main' + # collect2: error: ld returned 1 exit status + # + # $ gcc-7 -Wattribute-alias -xc /dev/null + # gcc-7: error: unrecognized command line option '-Wattribute-alias'; did you mean '-Wattributes'? + + # delete the "no-" prefix only for warning options + echo $option | grep -q -e "Werror" -e "Wno-error" || option=${option//-Wno-/-W} + + $compiler_bin $option -xc /dev/null 2>&1 | grep -q \ + -e "unrecognized command.line option" \ + -e "no option" \ + -e "unknown warning option" \ + -e "unknown argument" \ + && return 1 + + return 0 +} + +add_kcflag() +{ + local flag=$1 + is_supported_compiler_option "$compiler_bin" "$flag" && kcflags="$kcflags $flag" +} + +add_kbuild_kcflags() +{ + local kcflags_file=$1 + while read flag + do + add_kcflag "$flag" + done < <(grep -v -e "^#" -e "^$" $kcflags_file) +} diff --git a/kbuild/make.cross b/kbuild/make.cross index 484757fbd..5391793c6 100755 --- a/kbuild/make.cross +++ b/kbuild/make.cross @@ -20,6 +20,10 @@ # Author: Fengguang Wu # Credit: Tony Breeds for crosstool +lkp_src_kbuild=$(dirname $0) + +source $lkp_src_kbuild/kbuild.sh + check_install_path() { if [[ $COMPILER_INSTALL_PATH ]]; then @@ -483,50 +487,12 @@ setup_crosstool() setup_kcflags() { - local compiler_binary=$1 - - local kcflags=( - # [v6.6-rc1] 26030cb984dd ("extrawarn: move -Wrestrict into W=1 warnings") - -Wrestrict - # [v5.18-rc1] e6148767825c ("Makefile: Enable -Warray-bounds") - -Warray-bounds - # [v6.6-rc1] 6d4ab2e97dcf ("extrawarn: enable format and stringop overflow warnings in W=1") - -Wformat-overflow - -Wformat-truncation - -Wstringop-overflow - # [v6.2-rc1] 80b6093b55e3 ("kbuild: add -Wundef to KBUILD_CPPFLAGS for W=1 builds") - -Wundef - # [v6.5-rc1] df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3") - -fstrict-flex-arrays=3 - # [v6.2-rc1] 3bc753c06dd0 ("kbuild: treat char as always unsigned") - -funsigned-char - # [v6.6-rc1] e88ca24319e4 ("kbuild: consolidate warning flags in scripts/Makefile.extrawarn") - -Wenum-conversion - ) - - opt_kcflags=() - for flag in "${kcflags[@]}" - do - is_supported_compiler_option "$compiler_binary" "$flag" && opt_kcflags+=("$flag") - done - - [[ ${#opt_kcflags[@]} -gt 0 ]] && opt_kcflags="KCFLAGS=${opt_kcflags[@]}" -} - -is_supported_compiler_option() -{ - local compiler=$1 - local option=$2 - - # $ gcc -Winvalid-option -xc /dev/null - # gcc: error: unrecognized command-line option ‘-Winvalid-option’; did you mean ‘-Winvalid-pch’? + local compiler_bin=$1 + local kcflags= - # $ clang-15 -Winvalid-option -xc /dev/null - # warning: unknown warning option '-Winvalid-option' [-Wunknown-warning-option] + add_kbuild_kcflags $lkp_src_kbuild/etc/kbuild-kcflags - $compiler $option -xc /dev/null 2>&1 | grep -q -e "unrecognized command.line option" -e "unknown warning option" && return 1 - - return 0 + [[ $kcflags ]] && opt_kbuild_cflags+=(KCFLAGS="$kcflags") } make_cross_main() @@ -613,6 +579,7 @@ make_cross_main() exit 1 } + local opt_kbuild_cflags=() setup_crosstool || { echo "setup_crosstool failed" exit 1 @@ -632,11 +599,11 @@ make_cross_main() [[ -f .make-env ]] && source ./.make-env if [[ -d source && -L source ]]; then - echo make --keep-going -C source O=$PWD $make_dts $opt_arch $opt_cross $subarch $opt_jobs "$opt_kcflags" "$@" - exec make --keep-going -C source O=$PWD $make_dts $opt_arch $opt_cross $subarch $opt_jobs "$opt_kcflags" "$@" + echo make --keep-going -C source O=$PWD $make_dts $opt_arch $opt_cross $subarch $opt_jobs "${opt_kbuild_cflags[@]}" "$@" + exec make --keep-going -C source O=$PWD $make_dts $opt_arch $opt_cross $subarch $opt_jobs "${opt_kbuild_cflags[@]}" "$@" else - echo make --keep-going $O $make_dts $opt_arch $opt_cross $subarch $opt_jobs "$opt_kcflags" "$@" - exec make --keep-going $O $make_dts $opt_arch $opt_cross $subarch $opt_jobs "$opt_kcflags" "$@" + echo make --keep-going $O $make_dts $opt_arch $opt_cross $subarch $opt_jobs "${opt_kbuild_cflags[@]}" "$@" + exec make --keep-going $O $make_dts $opt_arch $opt_cross $subarch $opt_jobs "${opt_kbuild_cflags[@]}" "$@" fi }