diff mbox series

[testsuite,2/2] tests/module_load: use the right compiler to build kernel modules

Message ID 20210906105435.96417-2-omosnace@redhat.com (mailing list archive)
State Accepted
Delegated to: Ondrej Mosnáček
Headers show
Series [testsuite,1/2] tests/module_load: simplify the clean target | expand

Commit Message

Ondrej Mosnacek Sept. 6, 2021, 10:54 a.m. UTC
Kernel modules need to be built with the same compiler as the kernel was
built with. Thus, try to explicitly pass the right compiler (detected
from kernel config) to Make when compiling the modules, so that the
testsuite can be successfully built also on systems with clang-built
kernel, but with GCC as the default compiler.

Note that the rest of the testsuite doesn't currently build with clang,
so just changing CC for the whole testuite is not enough as a
workaround. And even after fixing testsuite to build with clang, it will
be beneficial to be able to use a different compiler to build the
testsuite than the one needed for the kernel modules - for example to
find more bugs by building the test programs with multiple compilers in
CI.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
---
 tests/module_load/Makefile | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/tests/module_load/Makefile b/tests/module_load/Makefile
index ce34241..272872d 100644
--- a/tests/module_load/Makefile
+++ b/tests/module_load/Makefile
@@ -4,9 +4,28 @@  TARGETS = finit_load init_load
 LDLIBS += -lselinux
 KDIR = /lib/modules/$(shell uname -r)/build
 
+# Make sure to use the same compiler as the kernel was built with.
+# If the compilers don't match, the build will fail on unsupported compiler
+# flags and even if not, the resulting module would likely fail to load.
+# If the kernel was compiled with neither GCC nor clang (currently the only
+# supported compilers), fall back to the default compiler and hope for the best.
+# In all cases allow the user to override the compiler via the KCC variable.
+DETECTED_KCC = unknown
+ifeq ($(shell grep -qFx CONFIG_CC_IS_GCC=y $(KDIR)/.config && echo true),true)
+	DETECTED_KCC = gcc
+endif
+ifeq ($(shell grep -qFx CONFIG_CC_IS_CLANG=y $(KDIR)/.config && echo true),true)
+	DETECTED_KCC = clang
+endif
+ifneq ($(DETECTED_KCC),unknown)
+	KCC ?= $(DETECTED_KCC)
+else
+	KCC ?= $(CC)
+endif
+
 all: $(TARGETS)
-	$(MAKE) -C $(KDIR) M=$(PWD)
+	$(MAKE) -C $(KDIR) CC=$(KCC) M=$(PWD)
 
 clean:
 	rm -f $(TARGETS)
-	$(MAKE) -C $(KDIR) M=$(PWD) clean
+	$(MAKE) -C $(KDIR) CC=$(KCC) M=$(PWD) clean