[klibc] [kvm-unit-tests PATCH v3 1/4] Kbuild: add support for clang builds
Bill Wendling
morbo at google.com
Sun Mar 29 04:38:30 PDT 2020
Add cc-name to klibc/scripts/Kbuild.include.
Make optimization flags not supported by clang conditional on cc-name.
Use "--print-file-name=include" and "--print-libgcc-file-name" when
using clang.
Use cc-option to enable compiler-specific flags.
Mark "bcmp" as not a builtin to prevent clang from using it.
Signed-off-by: Bill Wendling <morbo at google.com>
---
scripts/Kbuild.include | 8 ++++++--
scripts/Kbuild.klibc | 13 +++++++++----
usr/klibc/Kbuild | 4 ++--
usr/klibc/arch/x86_64/MCONFIG | 6 ++++--
4 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index a048ec7a..5604b3e3 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -110,12 +110,12 @@ as-instr = $(call try-run,\
# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
cc-option = $(call try-run,\
- $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -xc /dev/null -o "$$TMP",$(1),$(2))
+ $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -Werror -c -xc /dev/null -o "$$TMP",$(1),$(2))
# cc-option-yn
# Usage: flag := $(call cc-option-yn,-march=winchip-c6)
cc-option-yn = $(call try-run,\
- $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -xc /dev/null -o "$$TMP",y,n)
+ $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -Werror -c -xc /dev/null -o "$$TMP",y,n)
# cc-option-align
# Prefix align with either -falign or -malign
@@ -127,6 +127,10 @@ cc-option-align = $(subst -functions=0,,\
cc-disable-warning = $(call try-run,\
$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -xc /dev/null -o "$$TMP",-Wno-$(strip $(1)))
+# cc-name
+# Expands to either gcc or clang
+cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
+
# cc-version
# Usage gcc-ver := $(call cc-version)
cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
diff --git a/scripts/Kbuild.klibc b/scripts/Kbuild.klibc
index afc9a546..ca2539d6 100644
--- a/scripts/Kbuild.klibc
+++ b/scripts/Kbuild.klibc
@@ -69,6 +69,7 @@ include $(srctree)/scripts/Kbuild.include
KLIBCREQFLAGS := $(call cc-option, -fno-stack-protector, ) \
$(call cc-option, -fwrapv, ) \
$(call cc-option, -fno-PIE, ) \
+ $(call cc-option, -fno-builtin-bcmp, ) \
-ggdb
KLIBCARCHREQFLAGS :=
KLIBCOPTFLAGS :=
@@ -108,10 +109,14 @@ KLIBCOBJDUMP := $(OBJDUMP)
# klibc include paths
KLIBCCPPFLAGS := -nostdinc -iwithprefix include \
- -I$(KLIBCINC)/arch/$(KLIBCARCHDIR) \
+ -I$(KLIBCINC)/arch/$(KLIBCARCHDIR) \
-I$(KLIBCINC)/bits$(KLIBCBITSIZE) \
- -I$(KLIBCOBJ)/../include \
+ -I$(KLIBCOBJ)/../include \
-I$(KLIBCINC)
+ifeq ($(cc-name),clang)
+KLIBCCPPFLAGS += -I$(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-file-name=include)
+endif
+
# kernel include paths
KLIBCKERNELSRC ?= $(srctree)
KLIBCCPPFLAGS += -I$(KLIBCKERNELSRC)/include \
@@ -128,8 +133,8 @@ KLIBCCFLAGS += $(KLIBCCPPFLAGS) $(KLIBCREQFLAGS) $(KLIBCARCHREQFLAGS) \
KLIBCAFLAGS += -D__ASSEMBLY__ -Wa,--noexecstack $(KLIBCCFLAGS)
KLIBCSTRIPFLAGS += --strip-all -R .comment -R .note
-KLIBCLIBGCC_DEF := $(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-libgcc)
-KLIBCLIBGCC ?= $(KLIBCLIBGCC_DEF)
+KLIBCLIBGCC_DEF := $(shell $(KLIBCCC) $(KLIBCCFLAGS) $(if $(filter gcc,$(cc-name)),--print-libgcc,--print-libgcc-file-name))
+KLIBCLIBGCC ?= $(KLIBCLIBGCC_DEF)
KLIBCCRT0 := $(KLIBCOBJ)/arch/$(KLIBCARCHDIR)/crt0.o
KLIBCLIBC := $(KLIBCOBJ)/libc.a
KLIBCCRTSHARED := $(KLIBCOBJ)/interp.o
diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild
index 24bad07d..c3ebff99 100644
--- a/usr/klibc/Kbuild
+++ b/usr/klibc/Kbuild
@@ -85,8 +85,8 @@ endif
# These pass a huge maximum length to the corresponding length-limiting
# functions
-KLIBCCFLAGS_sprintf.o += -Wno-format-truncation
-KLIBCCFLAGS_vsprintf.o += -Wno-format-truncation
+KLIBCCFLAGS_sprintf.o += $(call cc-option,-Wno-format-truncation, )
+KLIBCCFLAGS_vsprintf.o += $(call cc-option,-Wno-format-truncation, )
# sigsuspend.c includes <klibc/havesyscall.h> generated by syscalls/
# build, so require that to build first
diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG
index c5f2fa26..7eed8847 100644
--- a/usr/klibc/arch/x86_64/MCONFIG
+++ b/usr/klibc/arch/x86_64/MCONFIG
@@ -15,8 +15,10 @@
# debugging using gdb.
#
KLIBCARCHREQFLAGS = -m64
-KLIBCOPTFLAGS += -Os -fomit-frame-pointer -mno-sse \
- -falign-functions=1 -falign-jumps=1 -falign-loops=1
+KLIBCOPTFLAGS += -Os -fomit-frame-pointer -mno-sse \
+ $(call cc-option,-falign-functions=1, ) \
+ $(call cc-option,-falign-jumps=1, ) \
+ $(call cc-option,-falign-loops=1, )
ifeq ($(DEBUG),y)
KLIBCOPTFLAGS += -g
else
--
2.26.0.rc2.310.g2932bb562d-goog
More information about the klibc
mailing list