Skip to content

Commit 61163ef

Browse files
committed
kbuild: LLVMLinux: Add Kbuild support for building kernel with Clang
Add support to toplevel Makefile for compiling with clang, both for HOSTCC and CC. Use cc-option to prevent gcc option from breaking clang, and from clang options from breaking gcc. Clang 3.4 semantics are the same as gcc semantics for unsupported flags. For unsupported warnings clang 3.4 returns true but shows a warning and gcc shows a warning and returns false. Signed-off-by: Behan Webster <behanw@converseincode.com> Signed-off-by: Jan-Simon Möller <dl9pf@gmx.de> Signed-off-by: Mark Charlebois <charlebm@gmail.com> Cc: PaX Team <pageexec@freemail.hu>
1 parent 39de65a commit 61163ef

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

Makefile

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ HOSTCXX = g++
248248
HOSTCFLAGS = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer
249249
HOSTCXXFLAGS = -O2
250250

251+
ifeq ($(shell $(HOSTCC) -v 2>&1 | grep -c "clang version"), 1)
252+
HOSTCFLAGS += -Wno-unused-value -Wno-unused-parameter \
253+
-Wno-missing-field-initializers -fno-delete-null-pointer-checks
254+
endif
255+
251256
# Decide whether to build built-in, modular, or both.
252257
# Normally, just do built-in.
253258

@@ -324,6 +329,14 @@ endif
324329

325330
export quiet Q KBUILD_VERBOSE
326331

332+
ifneq ($(CC),)
333+
ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1)
334+
COMPILER := clang
335+
else
336+
COMPILER := gcc
337+
endif
338+
export COMPILER
339+
endif
327340

328341
# Look for make include files relative to root of kernel src
329342
MAKEFLAGS += --include-dir=$(srctree)
@@ -383,7 +396,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
383396
-fno-strict-aliasing -fno-common \
384397
-Werror-implicit-function-declaration \
385398
-Wno-format-security \
386-
-fno-delete-null-pointer-checks
399+
$(call cc-option,-fno-delete-null-pointer-checks,)
387400
KBUILD_AFLAGS_KERNEL :=
388401
KBUILD_CFLAGS_KERNEL :=
389402
KBUILD_AFLAGS := -D__ASSEMBLY__
@@ -623,9 +636,24 @@ endif
623636
endif
624637
KBUILD_CFLAGS += $(stackp-flag)
625638

639+
ifeq ($(COMPILER),clang)
640+
KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
641+
KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
642+
KBUILD_CFLAGS += $(call cc-disable-warning, unused-variable)
643+
KBUILD_CFLAGS += $(call cc-disable-warning, format-invalid-specifier)
644+
KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
645+
# Quiet clang warning: comparison of unsigned expression < 0 is always false
646+
KBUILD_CFLAGS += $(call cc-disable-warning, tautological-compare)
647+
# CLANG uses a _MergedGlobals as optimization, but this breaks modpost, as the
648+
# source of a reference will be _MergedGlobals and not on of the whitelisted names.
649+
# See modpost pattern 2
650+
KBUILD_CFLAGS += $(call cc-option, -mno-global-merge,)
651+
else
652+
626653
# This warning generated too much noise in a regular build.
627654
# Use make W=1 to enable this warning (see scripts/Makefile.build)
628655
KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
656+
endif
629657

630658
ifdef CONFIG_FRAME_POINTER
631659
KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls

0 commit comments

Comments
 (0)