Re: [PATCH] kbuild: Set CLANG_VERSION only when Clang is used

From: Nathan Chancellor
Date: Sun Apr 19 2020 - 11:37:09 EST


On Sun, Apr 19, 2020 at 02:36:12PM +0200, Sedat Dilek wrote:
> Do like GCC_VERSION is set when GCC (see CC_IS_GCC) is used.
>
> Signed-off-by: Sedat Dilek <sedat.dilek@xxxxxxxxx>
> ---
> init/Kconfig | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/init/Kconfig b/init/Kconfig
> index 9e22ee8fbd75..c23f9d3d6d6c 100644
> --- a/init/Kconfig
> +++ b/init/Kconfig
> @@ -25,7 +25,7 @@ config CC_IS_CLANG
>
> config CLANG_VERSION
> int
> - default $(shell,$(srctree)/scripts/clang-version.sh $(CC))
> + default $(shell,$(srctree)/scripts/clang-version.sh $(CC)) if CC_IS_CLANG
>
> config CC_CAN_LINK
> def_bool $(success,$(srctree)/scripts/cc-can-link.sh $(CC))
> --
> 2.26.1
>

I don't understand the motivation behind this. The commit message needs
to be expanded upon or further clarification is needed.

CONFIG_GCC_VERSION is always set, either to 0 if using clang or the
actual version spit out by GCC so the commit message is not entirely
accurate.

It is done in the

default $(shell,$(srctree)/scripts/gcc-version.sh $(CC)) if CC_IS_GCC
default 0

way because it relies on the __GNUC__, __GNUC_MINOR__, and
__GNUC_PATCHLEVEL__ preprocessor macros to figure out the verison, which
clang also defines (to 4, 2, and 1 respectively) so it has to be done
this way to avoid confusing clang for GCC.

We can handle this in clang-version.sh so that CONFIG_CLANG_VERSION is
set to zero if CONFIG_CC_IS_GCC is set, just like CONFIG_GCC_VERSION:

if ! ( $compiler --version | grep -q clang) ; then
echo 0
exit 1
fi

CONFIG_CLANG_VERSION needs to be defined, otherwise we are breaking the
assumption that I made in commit df3da04880b4 ("mips: Fix unroll macro
when building with Clang").

With your patch and GCC 9.3.0 building malta_defconfig:

arch/mips/include/asm/r4kcache.h: In function 'blast_scache64_node':
arch/mips/include/asm/unroll.h:29:9: error: 'CONFIG_CLANG_VERSION' undeclared (first use in this function); did you mean 'CONFIG_LD_VERSION'?
29 | CONFIG_CLANG_VERSION >= 80000) && \
| ^~~~~~~~~~~~~~~~~~~~
/home/nathan/src/linux/include/linux/compiler.h:330:9: note: in definition of macro '__compiletime_assert'
330 | if (!(condition)) \
| ^~~~~~~~~
/home/nathan/src/linux/include/linux/compiler.h:350:2: note: in expansion of macro '_compiletime_assert'
350 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
/home/nathan/src/linux/include/linux/build_bug.h:39:37: note: in expansion of macro 'compiletime_assert'
39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
| ^~~~~~~~~~~~~~~~~~
/home/nathan/src/linux/include/linux/build_bug.h:50:2: note: in expansion of macro 'BUILD_BUG_ON_MSG'
50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
| ^~~~~~~~~~~~~~~~
/home/nathan/src/linux/arch/mips/include/asm/unroll.h:28:2: note: in expansion of macro 'BUILD_BUG_ON'
28 | BUILD_BUG_ON((CONFIG_GCC_VERSION >= 40700 || \
| ^~~~~~~~~~~~
/home/nathan/src/linux/arch/mips/include/asm/r4kcache.h:203:2: note: in expansion of macro 'unroll'
203 | unroll(times, _cache_op, insn, op, (addr) + (i++ * (lsize))); \
| ^~~~~~
/home/nathan/src/linux/arch/mips/include/asm/r4kcache.h:370:4: note: in expansion of macro 'cache_unroll'
370 | cache_unroll(32, kernel_cache, indexop, \
| ^~~~~~~~~~~~
/home/nathan/src/linux/arch/mips/include/asm/r4kcache.h:376:1: note: in expansion of macro '__BUILD_BLAST_CACHE_NODE'
376 | __BUILD_BLAST_CACHE_NODE(s, scache, Index_Writeback_Inv_SD, Hit_Writeback_Inv_SD, 64)
| ^~~~~~~~~~~~~~~~~~~~~~~~

Cheers,
Nathan