All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ondrej Mosnacek <omosnace@redhat.com>
To: selinux@vger.kernel.org
Subject: [PATCH testsuite 2/2] tests/module_load: use the right compiler to build kernel modules
Date: Mon,  6 Sep 2021 12:54:35 +0200	[thread overview]
Message-ID: <20210906105435.96417-2-omosnace@redhat.com> (raw)
In-Reply-To: <20210906105435.96417-1-omosnace@redhat.com>

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 --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
-- 
2.31.1


  reply	other threads:[~2021-09-06 10:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-06 10:54 [PATCH testsuite 1/2] tests/module_load: simplify the clean target Ondrej Mosnacek
2021-09-06 10:54 ` Ondrej Mosnacek [this message]
2021-09-11 18:34 ` Ondrej Mosnacek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210906105435.96417-2-omosnace@redhat.com \
    --to=omosnace@redhat.com \
    --cc=selinux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.