Skip to contentSkip to conversation

selftests/lib.mk: LLVM=1, CC=clang, and warnings

PATCH In review John Hubbard 5 participants 15 messages May 31, 2024 – Jun 7, 2024 Kselftest

lore
Share & tools
lore
#0 lore raw

v2 cover selftests/lib.mk: LLVM=1, CC=clang, and warnings

The kselftests may be built in a couple different ways:
make LLVM=1
make CC=clang

In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of lib.mk, and any Makefiles that include lib.mk, can base decisions solely on whether or not LLVM is set.

Then, build upon that to disable a pair of clang warnings that are already silenced on gcc.

Doing it this way is much better than the piecemeal approach that I started with in [1] and [2]. Thanks to Nathan Chancellor for the patch reviews that led to this approach.

Changes since the first version:

1) Wrote a detailed explanation for suppressing two clang warnings, in both a lib.mk comment, and the commit description.

2) Added a Reviewed-by tag to the first patch.

[1] https://lore.kernel.org/[email protected]
[2] https://lore.kernel.org/[email protected]

John Hubbard (2):
selftests/lib.mk: handle both LLVM=1 and CC=clang builds selftests/lib.mk: silence some clang warnings that gcc already ignores

 tools/testing/selftests/lib.mk | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

base-commit: e0cce98fe279b64f4a7d81b7f5c3a23d80b92fbc

Signature · 1 line
2.45.1
#1 lore raw

PATCH v2 1/2 selftests/lib.mk: handle both LLVM=1 and CC=clang builds

The kselftests may be built in a couple different ways:
make LLVM=1
make CC=clang

In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of lib.mk, and any Makefiles that include lib.mk, can base decisions solely on whether or not LLVM is set.

---
 tools/testing/selftests/lib.mk | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/testing/selftests/lib.mk +12 -0
index 429535816dbd..2902787b89b2 100644
@@ -1,5 +1,17 @@
 # This mimics the top-level Makefile. We do it explicitly here so that this
 # Makefile can operate with or without the kbuild infrastructure.
+
+# The kselftests may be built in a couple different ways:
+#     make LLVM=1
+#     make CC=clang
+#
+# In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of
+# lib.mk, and any Makefiles that include lib.mk, can base decisions solely on
+# whether or not LLVM is set.
+ifeq ($(CC),clang)
+    LLVM := 1
+endif
+
 ifneq ($(LLVM),)
 ifneq ($(filter %/,$(LLVM)),)
 LLVM_PREFIX := $(LLVM)
Signature · 1 line
2.45.1
#2 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

Quoted 7 lines · John Hubbard

The kselftests may be built in a couple different ways:
make LLVM=1
make CC=clang

In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of lib.mk, and any Makefiles that include lib.mk, can base decisions solely on whether or not LLVM is set.

ICBW but I believe there are still some architectures with clang but not lld support where there's a use case for using CC=clang.

#3 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

Quoted 11 lines · Mark Brown
Quoted 7 lines · John Hubbard

The kselftests may be built in a couple different ways:
make LLVM=1
make CC=clang

In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of lib.mk, and any Makefiles that include lib.mk, can base decisions solely on whether or not LLVM is set.

ICBW but I believe there are still some architectures with clang but not lld support where there's a use case for using CC=clang.

I'm inclined to wait for those to make themselves known... :)

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#4 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

Quoted 6 lines · John Hubbard
Quoted 5 lines · Mark Brown
Quoted 4 lines · John Hubbard

The kselftests may be built in a couple different ways:
     make LLVM=1
     make CC=clang

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of lib.mk, and any Makefiles that include lib.mk, can base decisions solely on whether or not LLVM is set.

ICBW but I believe there are still some architectures with clang but not lld support where there's a use case for using CC=clang.

I'm inclined to wait for those to make themselves known... :)

...but thinking about this some more, maybe this patch is actually a Bad Idea. Because it is encouraging weirdness and divergence from how kbuild does it. And kbuild is very clear [1]:

Building with LLVM

Invoke make via:

make LLVM=1

to compile for the host target. For cross compiling:

make LLVM=1 ARCH=arm64

The LLVM= argument

LLVM has substitutes for GNU binutils utilities. They can be enabled individually. The full list of supported make variables:

make CC=clang LD=ld.lld AR=llvm-ar NM=llvm-nm STRIP=llvm-strip \
OBJCOPY=llvm-objcopy OBJDUMP=llvm-objdump READELF=llvm-readelf \
HOSTCC=clang HOSTCXX=clang++ HOSTAR=llvm-ar HOSTLD=ld.lld

LLVM=1 expands to the above.

[1] https://docs.kernel.org/kbuild/llvm.html

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#5 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

In reply to Mark Brown
Quoted 11 lines · Mark Brown
Quoted 7 lines · John Hubbard

The kselftests may be built in a couple different ways:
make LLVM=1
make CC=clang

In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of lib.mk, and any Makefiles that include lib.mk, can base decisions solely on whether or not LLVM is set.

ICBW but I believe there are still some architectures with clang but not lld support where there's a use case for using CC=clang.

Does CC=clang even work for the selftests? lib.mk here uses 'CC :=' so won't CC=clang get overridden to CC=$(CROSS_COMPILE)gcc?

Cheers,
Nathan

#6 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

Quoted 10 lines · Nathan Chancellor
Quoted 9 lines · Mark Brown
Quoted 7 lines · John Hubbard

The kselftests may be built in a couple different ways:
make LLVM=1
make CC=clang

In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of lib.mk, and any Makefiles that include lib.mk, can base decisions solely on whether or not LLVM is set.

Quoted 6 lines
Quoted 2 lines

ICBW but I believe there are still some architectures with clang but not lld support where there's a use case for using CC=clang.

Does CC=clang even work for the selftests? lib.mk here uses 'CC :=' so won't CC=clang get overridden to CC=$(CROSS_COMPILE)gcc?

I received a report that someone (I forget who or what) was definitely attempting to just set CC=clang. But yes, it definitely doesn't work properly for CROSS_COMPILE.

And the more we talk it through, the less I like this direction that I went off on. Let's just drop this patch and instead consider moving kselftest builds closer to kbuild, instead of making it more different.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#7 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

Quoted 14 lines · John Hubbard
Quoted 13 lines · Nathan Chancellor
Quoted 11 lines · Mark Brown
Quoted 7 lines · John Hubbard

The kselftests may be built in a couple different ways:
     make LLVM=1
     make CC=clang

In order to handle both cases, set LLVM=1 if CC=clang. That way,the rest of lib.mk, and any Makefiles that include lib.mk, can base decisions solely on whether or not LLVM is set.

ICBW but I believe there are still some architectures with clang but not lld support where there's a use case for using CC=clang.

Quoted 7 lines
Quoted 3 lines

Does CC=clang even work for the selftests? lib.mk here uses 'CC :=' so won't CC=clang get overridden to CC=$(CROSS_COMPILE)gcc?

I received a report that someone (I forget who or what) was definitely attempting to just set CC=clang. But yes, it definitely doesn't work properly for CROSS_COMPILE.

This history as I recall, is that I got a bug report [1] stating that:

# tools/testing/selftests/fchmodat2$ make CC=clang

and

# tools/testing/selftests/openat2$ make CC=clang

were both failing due to the -static-libsan / -static-libasan difference between gcc and clang. I attempted to fix that with [2], which used cc-option to determine which variant to use. That never got picked up, and John coincidentally did a similar fix, but relying on LLVM=1 instead.

If we are concluding that CC=clang is an invalid way to do this, then I guess we should report that back to [1]?

[1] https://lore.kernel.org/all/[email protected]/
[2]
https://lore.kernel.org/linux-kselftest/[email protected]/

Thanks,
Ryan

Quoted 7 lines

And the more we talk it through, the less I like this direction that I went off on. Let's just drop this patch and instead consider moving kselftest builds closer to kbuild, instead of making it more different.

thanks,

#8 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

Quoted 23 lines · Ryan Roberts
Quoted 8 lines · John Hubbard
Quoted 3 lines · Nathan Chancellor

Does CC=clang even work for the selftests? lib.mk here uses 'CC :=' so won't CC=clang get overridden to CC=$(CROSS_COMPILE)gcc?

I received a report that someone (I forget who or what) was definitely attempting to just set CC=clang. But yes, it definitely doesn't work properly for CROSS_COMPILE.

This history as I recall, is that I got a bug report [1] stating that:

# tools/testing/selftests/fchmodat2$ make CC=clang

and

# tools/testing/selftests/openat2$ make CC=clang

were both failing due to the -static-libsan / -static-libasan difference between gcc and clang. I attempted to fix that with [2], which used cc-option to determine which variant to use. That never got picked up, and John coincidentally did a similar fix, but relying on LLVM=1 instead.

Quoted 6 lines

If we are concluding that CC=clang is an invalid way to do this, then I guess we should report that back to [1]?

[1] https://lore.kernel.org/all/[email protected]/
[2]
https://lore.kernel.org/linux-kselftest/[email protected]/

I can only speak from the perspective of the main kernel build, as I don't really know much if anything about the selftests, but I think CC=clang and LLVM=1 should both be valid. Ideally, they would behave as they do for the main kernel build (i.e., CC=clang just uses clang for the compiler and LLVM=1 uses the entire LLVM tools). I realize that for the selftests, there is probably little use for tools other than the compiler, assembler, and linker but I think consistency is desirable here.

I am not at all familiar with the selftests build system, which is completely different from Kbuild, but I would ack a patch that does that. Otherwise, I think having a different meaning or handling of CC=clang or LLVM=1 is the end of the world, but I do think that it should be documented.

Cheers,
Nathan

#9 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

Quoted 3 lines · Nathan Chancellor
Quoted 2 lines · Ryan Roberts
Quoted 1 line · John Hubbard

On 6/3/24 3:47 PM, Nathan Chancellor wrote:

...

Quoted 16 lines
Quoted 6 lines

If we are concluding that CC=clang is an invalid way to do this, then I guess we should report that back to [1]?

[1] https://lore.kernel.org/all/[email protected]/
[2]
https://lore.kernel.org/linux-kselftest/[email protected]/

I can only speak from the perspective of the main kernel build, as I don't really know much if anything about the selftests, but I think CC=clang and LLVM=1 should both be valid. Ideally, they would behave as they do for the main kernel build (i.e., CC=clang just uses clang for the compiler and LLVM=1 uses the entire LLVM tools). I realize that for the selftests, there is probably little use for tools other than the compiler, assembler, and linker but I think consistency is desirable here.

Quoted 6 lines

I am not at all familiar with the selftests build system, which is completely different from Kbuild, but I would ack a patch that does that. Otherwise, I think having a different meaning or handling of CC=clang or LLVM=1 is the end of the world, but I do think that it should be documented.

OK, that can be easily done, as shown below. And there are so far only a handful of selftests that key off of LLVM (plus a few of my pending patches). I can post this, plus a few patches (and patch updates for pending patches) to use the new CC_IS_CLANG where appropriate:

tools/testing/selftests/lib.mk +9 -0
index 429535816dbd..ea643d1a65dc 100644
@@ -43,6 +43,15 @@ else
 CC := $(CROSS_COMPILE)gcc
 endif # LLVM
 
+# CC might have been set above (by inferring it from LLVM==1), or CC might have
+# been set from the environment. In either case, if CC is an invocation of clang
+# in any form, set CC_IS_CLANG. This allows subsystem selftests to selectively
+# control clang-specific behavior, such as, in particular, compiler warnings.
+CC_IS_CLANG := 0
+ifeq ($(findstring clang,$(CC)),clang)
+    CC_IS_CLANG := 1
+endif
+
 ifeq (0,$(MAKELEVEL))
     ifeq ($(OUTPUT),)
        OUTPUT := $(shell pwd)

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#10 lore raw

Re: [PATCH v2 1/2] selftests/lib.mk: handle both LLVM=1 and CC=clang builds

In reply to Nathan Chancellor
Quoted 7 lines · Nathan Chancellor
Quoted 2 lines · Mark Brown

ICBW but I believe there are still some architectures with clang but not lld support where there's a use case for using CC=clang.

Does CC=clang even work for the selftests? lib.mk here uses 'CC :=' so won't CC=clang get overridden to CC=$(CROSS_COMPILE)gcc?

No idea, it's not a configuration I'm interested in myself.

#11 lore raw

PATCH v2 2/2 selftests/lib.mk: silence some clang warnings that gcc already ignores

In reply to John Hubbard

gcc defaults to silence (off) for the following warnings, but clang defaults to the opposite. The warnings are not useful for the kernel itself, which is why they have remained disabled in gcc for the main kernel build. And it is only due to including kernel data structures in the selftests, that we get the warnings from clang.

-Waddress-of-packed-member
-Wgnu-variable-sized-type-not-at-end

In other words, the warnings are not unique to the selftests: there is nothing that the selftests' code does that triggers these warnings, other than the act of including the kernel's data structures. Therefore, silence them for the clang builds as well.

This eliminates warnings for the net/ and user_events/ kselftest subsystems, in these files:

    ./net/af_unix/scm_rights.c
    ./net/timestamping.c
    ./net/ipsec.c
    ./user_events/perf_test.c
Cc Nathan Chancellor <[email protected]>
---
 tools/testing/selftests/lib.mk | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/testing/selftests/lib.mk +8 -0
index 2902787b89b2..c179c02281e9 100644
@@ -50,6 +50,14 @@ else
 CLANG_FLAGS     += --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif # CROSS_COMPILE
 
+# gcc defaults to silence (off) for the following warnings, but clang defaults
+# to the opposite. The warnings are not useful for the kernel itself, which is
+# why they have remained disabled in gcc for the main kernel build. And it is
+# only due to including kernel data structures in the selftests, that we get the
+# warnings from clang. Therefore, disable the warnings for clang builds.
+CFLAGS += -Wno-address-of-packed-member
+CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
+
 CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
 else
 CC := $(CROSS_COMPILE)gcc
Signature · 1 line
2.45.1
#12 lore raw

Re: [PATCH v2 2/2] selftests/lib.mk: silence some clang warnings that gcc already ignores

Quoted 8 lines · John Hubbard

gcc defaults to silence (off) for the following warnings, but clang defaults to the opposite. The warnings are not useful for the kernel itself, which is why they have remained disabled in gcc for the main kernel build. And it is only due to including kernel data structures in the selftests, that we get the warnings from clang.

-Waddress-of-packed-member
-Wgnu-variable-sized-type-not-at-end

Even if patch 1/1 here is not merged, I would still like to get this one reviewed and merged. It still solves the problem for LLVM=1 builds.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 39 lines

In other words, the warnings are not unique to the selftests: there is nothing that the selftests' code does that triggers these warnings, other than the act of including the kernel's data structures. Therefore, silence them for the clang builds as well.

This eliminates warnings for the net/ and user_events/ kselftest subsystems, in these files:

     ./net/af_unix/scm_rights.c
     ./net/timestamping.c
     ./net/ipsec.c
     ./user_events/perf_test.c
Cc Nathan Chancellor <[email protected]>
---
  tools/testing/selftests/lib.mk | 8 ++++++++
  1 file changed, 8 insertions(+)
tools/testing/selftests/lib.mk +8 -0
index 2902787b89b2..c179c02281e9 100644
@@ -50,6 +50,14 @@ else
  CLANG_FLAGS     += --target=$(notdir $(CROSS_COMPILE:%-=%))
  endif # CROSS_COMPILE
 
+# gcc defaults to silence (off) for the following warnings, but clang defaults
+# to the opposite. The warnings are not useful for the kernel itself, which is
+# why they have remained disabled in gcc for the main kernel build. And it is
+# only due to including kernel data structures in the selftests, that we get the
+# warnings from clang. Therefore, disable the warnings for clang builds.
+CFLAGS += -Wno-address-of-packed-member
+CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
+
  CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
  else
  CC := $(CROSS_COMPILE)gcc
Acked-by on v2 2/2
#13 lore raw

Re: [PATCH v2 2/2] selftests/lib.mk: silence some clang warnings that gcc already ignores

In reply to John Hubbard
Quoted 17 lines · John Hubbard

gcc defaults to silence (off) for the following warnings, but clang defaults to the opposite. The warnings are not useful for the kernel itself, which is why they have remained disabled in gcc for the main kernel build. And it is only due to including kernel data structures in the selftests, that we get the warnings from clang.

-Waddress-of-packed-member
-Wgnu-variable-sized-type-not-at-end

In other words, the warnings are not unique to the selftests: there is nothing that the selftests' code does that triggers these warnings, other than the act of including the kernel's data structures. Therefore, silence them for the clang builds as well.

This eliminates warnings for the net/ and user_events/ kselftest subsystems, in these files:

Quoted 7 lines
    ./net/af_unix/scm_rights.c
    ./net/timestamping.c
    ./net/ipsec.c
    ./user_events/perf_test.c
Cc Nathan Chancellor <[email protected]>

This seems reasonable to me.

Quoted 27 lines
---
 tools/testing/selftests/lib.mk | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/testing/selftests/lib.mk +8 -0
index 2902787b89b2..c179c02281e9 100644
@@ -50,6 +50,14 @@ else
 CLANG_FLAGS     += --target=$(notdir $(CROSS_COMPILE:%-=%))
 endif # CROSS_COMPILE
 
+# gcc defaults to silence (off) for the following warnings, but clang defaults
+# to the opposite. The warnings are not useful for the kernel itself, which is
+# why they have remained disabled in gcc for the main kernel build. And it is
+# only due to including kernel data structures in the selftests, that we get the
+# warnings from clang. Therefore, disable the warnings for clang builds.
+CFLAGS += -Wno-address-of-packed-member
+CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
+
 CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
 else
 CC := $(CROSS_COMPILE)gcc
Signature · 1 line
2.45.1
maintainer
#14 lore raw

Re: [PATCH v2 2/2] selftests/lib.mk: silence some clang warnings that gcc already ignores

Quoted 42 lines · Nathan Chancellor
Quoted 17 lines · John Hubbard

gcc defaults to silence (off) for the following warnings, but clang defaults to the opposite. The warnings are not useful for the kernel itself, which is why they have remained disabled in gcc for the main kernel build. And it is only due to including kernel data structures in the selftests, that we get the warnings from clang.

-Waddress-of-packed-member
-Wgnu-variable-sized-type-not-at-end

In other words, the warnings are not unique to the selftests: there is nothing that the selftests' code does that triggers these warnings, other than the act of including the kernel's data structures. Therefore, silence them for the clang builds as well.

This eliminates warnings for the net/ and user_events/ kselftest subsystems, in these files:

Quoted 7 lines
     ./net/af_unix/scm_rights.c
     ./net/timestamping.c
     ./net/ipsec.c
     ./user_events/perf_test.c
Cc Nathan Chancellor <[email protected]>

This seems reasonable to me.

Quoted 12 lines
---
  tools/testing/selftests/lib.mk | 8 ++++++++
  1 file changed, 8 insertions(+)
tools/testing/selftests/lib.mk +0 -0
index 2902787b89b2..c179c02281e9 100644
@@ -50,6 +50,14 @@ else
  CLANG_FLAGS     += --target=$(notdir $(CROSS_COMPILE:%-=%))
  endif # CROSS_COMPILE
 
Quoted 8 lines

tools/testing/selftests/lib.mk else · line 60

Quoted 8 lines
tools/testing/selftests/lib.mk +8 -0
+# gcc defaults to silence (off) for the following warnings, but clang defaults
+# to the opposite. The warnings are not useful for the kernel itself, which is
+# why they have remained disabled in gcc for the main kernel build. And it is
+# only due to including kernel data structures in the selftests, that we get the
+# warnings from clang. Therefore, disable the warnings for clang builds.
+CFLAGS += -Wno-address-of-packed-member
+CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
+

Thank you for adding this comment block.

Quoted 8 lines
Quoted 7 lines
tools/testing/selftests/lib.mk +0 -0
  CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
  else
  CC := $(CROSS_COMPILE)gcc
Signature · 1 line
2.45.1

Thank you both. I will apply this for the next release. I want this change soaking in next for a bit.

thanks,
-- Shuah