Skip to contentSkip to conversation

selftests: propagate CC to selftest submakes

PATCH In review Andrew Delgadillo 3 participants 5 messages Dec 3, 2020 – Dec 11, 2020 Kselftest

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: propagate CC to selftest submakes

lib.mk defaults to gcc when CC is not set. When building selftests as part of a kernel compilation, MAKEFLAGS is cleared to allow implicit build rules to be used. This has the side-effect of clearing the CC variable, which will cause selftests to be built with gcc regardless of if we are using gcc or clang. To remedy this, propagate the CC variable when clearing makeflags to ensure the correct compiler is used.

---
 tools/testing/selftests/Makefile | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/Makefile +4 -2
index d9c283503159..a4dd6d7e8276 100644
@@ -90,10 +90,12 @@ FORCE_TARGETS ?=
 
 # Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
 # implicit rules to sub-test Makefiles which avoids build failures in test
-# Makefile that don't have explicit build rules.
+# Makefile that don't have explicit build rules. Since lib.mk defaults to
+# using gcc for compilation when the CC variable is not set, we propagate the
+# CC variable so if clang is being used, selftests will build with clang.
 ifeq (,$(LINK.c))
 override LDFLAGS =
-override MAKEFLAGS =
+override MAKEFLAGS = CC=$(CC)
 endif
 
 # Append kselftest to KBUILD_OUTPUT and O to avoid cluttering
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
#1 lore raw
Quoted 9 lines · Andrew Delgadillo

lib.mk defaults to gcc when CC is not set. When building selftests as part of a kernel compilation, MAKEFLAGS is cleared to allow implicit build rules to be used. This has the side-effect of clearing the CC variable, which will cause selftests to be built with gcc regardless of if we are using gcc or clang. To remedy this, propagate the CC variable when clearing makeflags to ensure the correct compiler is used.

Hi Andrew, thanks for the patch. Can you walk me through how to build the selftests?

Documentation/dev-tools/kselftest.rst says:

$ make -C tools/testing/selftests

And if I do:

$ make CC=clang defconfig
$ make CC=clang -C tools/testing/selftests -j

I observe a spew of errors. If I apply your patch and rerun the above, I see what looks like the same spew of errors. Am I "holding it wrong" or could the docs use a refresh?

Quoted 26 lines
---
 tools/testing/selftests/Makefile | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/Makefile +4 -3
index d9c283503159..a4dd6d7e8276 100644
@@ -90,10 +90,12 @@ FORCE_TARGETS ?=
 
 # Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
 # implicit rules to sub-test Makefiles which avoids build failures in test
-# Makefile that don't have explicit build rules.
+# Makefile that don't have explicit build rules. Since lib.mk defaults to
+# using gcc for compilation when the CC variable is not set, we propagate the
+# CC variable so if clang is being used, selftests will build with clang.
 ifeq (,$(LINK.c))
 override LDFLAGS =
-override MAKEFLAGS =
+override MAKEFLAGS = CC=$(CC)
 endif
 
 # Append kselftest to KBUILD_OUTPUT and O to avoid cluttering
--

2.29.2.576.ga3fc446d84-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 18 lines · Nick Desaulniers
Quoted 9 lines · Andrew Delgadillo

lib.mk defaults to gcc when CC is not set. When building selftests as part of a kernel compilation, MAKEFLAGS is cleared to allow implicit build rules to be used. This has the side-effect of clearing the CC variable, which will cause selftests to be built with gcc regardless of if we are using gcc or clang. To remedy this, propagate the CC variable when clearing makeflags to ensure the correct compiler is used.

Hi Andrew, thanks for the patch. Can you walk me through how to build the selftests?

Documentation/dev-tools/kselftest.rst says:

$ make -C tools/testing/selftests
Quoted 8 lines

And if I do:

$ make CC=clang defconfig
$ make CC=clang -C tools/testing/selftests -j

I observe a spew of errors. If I apply your patch and rerun the above, I see what looks like the same spew of errors. Am I "holding it wrong" or could the docs use a refresh?

Hi Nick, sure thing!

I also see a slew of errors when building with make -C tools/testing/selftests. However, that is not the problem I am trying to solve. I believe we are seeing errors building that way because it is missing some make variables that are normally set up when building from the kernel's top level makefile.

From https://www.kernel.org/doc/html/latest/dev-tools/kselftest.html:
To build and run tests with a single command, use:
$ make kselftest

To illustrate what I am to fix, one can follow these steps:

Without patch:

$ cd kernel_root
$ make mrproper # Start fresh
$ make defconfig
$ make kselftest V=1 |& tee out1

At this point inspecting out1 will show that gcc is being used as the compiler. Let's try to set it to clang.

$ make mrproper
$ make defconfig
$ make kselftest V=1 CC=clang |& tee out2

Inspecting out2 shows that clang is not used, but rather gcc. You will get a similar result if you substitute CC=clang for LLVM=1. We can verify that the CC variable is not properly propagated to the submakes with the following addition to tools/testing/selftests/android/Makefile's all recipe (this is the first submake run):
...
all:

        echo "My CC compiler is $(CC)" && false

...

Following the above steps again will echo "My CC compiler is gcc" both times, despite setting CC=clang in the second run. After applying my patch, the CC variable will be properly propagated. The reason it was not propagated properly in the first place is that we clear MAKEFLAGS if implicit build rules are disabled (top level Makefile disables them, but selftests need), which has the side effect of unsetting the CC variable. Selftest's lib.mk defaults to gcc when CC is not set, which is why we see CC=gcc even when we set CC=clang on the commandline.

While this is not a problem if building with make -C tools/testing/selftests/Makefile, it does present a problem for those who build with the top level makefile like "make kselftest" or "make vmlinux kselftest". One reason for doing it the second way is that using the top level Makefile allows one to use flags like "LLVM=1".

Quoted 31 lines
Quoted 24 lines
---
 tools/testing/selftests/Makefile | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/Makefile +4 -3
index d9c283503159..a4dd6d7e8276 100644
@@ -90,10 +90,12 @@ FORCE_TARGETS ?=
 
 # Clear LDFLAGS and MAKEFLAGS when implicit rules are missing.  This provides
 # implicit rules to sub-test Makefiles which avoids build failures in test
-# Makefile that don't have explicit build rules.
+# Makefile that don't have explicit build rules. Since lib.mk defaults to
+# using gcc for compilation when the CC variable is not set, we propagate the
+# CC variable so if clang is being used, selftests will build with clang.
 ifeq (,$(LINK.c))
 override LDFLAGS =
-override MAKEFLAGS =
+override MAKEFLAGS = CC=$(CC)
 endif
 
 # Append kselftest to KBUILD_OUTPUT and O to avoid cluttering
--
Quoted 2 lines

2.29.2.576.ga3fc446d84-goog

--
Thanks,
~Nick Desaulniers

maintainer
#3 lore raw
Quoted 29 lines · Andrew Delgadillo
Quoted 18 lines · Nick Desaulniers
Quoted 9 lines · Andrew Delgadillo

lib.mk defaults to gcc when CC is not set. When building selftests as part of a kernel compilation, MAKEFLAGS is cleared to allow implicit build rules to be used. This has the side-effect of clearing the CC variable, which will cause selftests to be built with gcc regardless of if we are using gcc or clang. To remedy this, propagate the CC variable when clearing makeflags to ensure the correct compiler is used.

Hi Andrew, thanks for the patch. Can you walk me through how to build the selftests?

Documentation/dev-tools/kselftest.rst says:

$ make -C tools/testing/selftests
Quoted 8 lines

And if I do:

$ make CC=clang defconfig
$ make CC=clang -C tools/testing/selftests -j

I observe a spew of errors. If I apply your patch and rerun the above, I see what looks like the same spew of errors. Am I "holding it wrong" or could the docs use a refresh?

Quoted 8 lines

Hi Nick, sure thing!

I also see a slew of errors when building with make -C tools/testing/selftests. However, that is not the problem I am trying to solve. I believe we are seeing errors building that way because it is missing some make variables that are normally set up when building from the kernel's top level makefile.

Both options are supported and should work.

make -C tools/testing/selftests
make kselftest

That being said, I use gcc. Can you send the errors you are seeing? It is possible, a few tests aren't building and need to be fixed for clang and gcc.

thanks,
-- Shuah

#4 lore raw
Quoted 40 lines · Shuah Khan
Quoted 29 lines · Andrew Delgadillo
Quoted 18 lines · Nick Desaulniers
Quoted 9 lines · Andrew Delgadillo

lib.mk defaults to gcc when CC is not set. When building selftests as part of a kernel compilation, MAKEFLAGS is cleared to allow implicit build rules to be used. This has the side-effect of clearing the CC variable, which will cause selftests to be built with gcc regardless of if we are using gcc or clang. To remedy this, propagate the CC variable when clearing makeflags to ensure the correct compiler is used.

Hi Andrew, thanks for the patch. Can you walk me through how to build the selftests?

Documentation/dev-tools/kselftest.rst says:

$ make -C tools/testing/selftests
Quoted 8 lines

And if I do:

$ make CC=clang defconfig
$ make CC=clang -C tools/testing/selftests -j

I observe a spew of errors. If I apply your patch and rerun the above, I see what looks like the same spew of errors. Am I "holding it wrong" or could the docs use a refresh?

Quoted 8 lines

Hi Nick, sure thing!

I also see a slew of errors when building with make -C tools/testing/selftests. However, that is not the problem I am trying to solve. I believe we are seeing errors building that way because it is missing some make variables that are normally set up when building from the kernel's top level makefile.

Quoted 8 lines

Both options are supported and should work.

make -C tools/testing/selftests
make kselftest

That being said, I use gcc. Can you send the errors you are seeing? It is possible, a few tests aren't building and need to be fixed for clang and gcc.

Most of the errors I saw, I was able to fix by installing the correct packages to get some missing headers, so in those cases nothing is broken about the tests. However, after that the errors still remaining look like so (I've done my best to deduplicate similar errors):

clone3_cap_checkpoint_restore.c: In function 'clone3_cap_checkpoint_restore':
clone3_cap_checkpoint_restore.c:148:9: error: expected expression before 'return'

   XFAIL(return, "Skipping all tests as non-root\n");
         ^

make[3]: *** [../lib.mk:139:
/usr/local/google/home/adelg/projects/upstream/tools/testing/selftests/clone3/clone3_cap_checkpoint_restore] Error 1

memfd_test.c: In function 'test_seal_future_write':
memfd_test.c:783:27: error: 'F_SEAL_FUTURE_WRITE' undeclared (first use in this function)

  mfd_assert_add_seals(fd, F_SEAL_FUTURE_WRITE);
                           ^

memfd_test.c:783:27: note: each undeclared identifier is reported only once for each function it appears in

/usr/local/***/lib/../lib64/librt.so: undefined reference to `pthread_attr_setstacksize@GLIBC_2.2.5'
collect2: error: ld returned 1 exit status

There are also bpf selftest errors, but I know for a fact those are just an artifact of me not having llvm-readelf and other binaries in my PATH as I've compiled those successfully before.

Quoted 4 lines

thanks,
-- Shuah