[klibc] [PATCH v2 1/5] Kbuild: add support for clang builds

Bill Wendling morbo at google.com
Fri Mar 27 15:29:42 PDT 2020

From: Michael Davidson <md at google.com>

Add cc-name to klibc/scripts/Kbuild.include.

Make optimization flags not supported by clang conditional on cc-name.

Don't use -nostdinc when building klibc.
  While klibc doesn't (shouldn't) use the standard header files
  supplied by the toolchain, it does still need to be able to find
  the compiler-specific header file <stdarg.h>. With gcc this can
  be achieved by specifying "-iwithprefix include" in addition to
  "-nostdinc". clang behaves differently and the easiest work around
  is to simply not specify "-nostdinc" and rely on the fact that
  the various include paths have been specified correctly so that
  the only header file actually pulled in from the standard location
  is <stdarg.h>.

Disable the use of --print-libgcc when using clang.

Explictly specifying the path to compiler specific libraries
  appears to be unnecessary since klibc never specifies -nostdlib
  when linking binaries, meaning that any such libraries should
  already be available.

Signed-off-by: Bill Wendling <morbo at google.com>
 scripts/Kbuild.include        |  4 ++++
 scripts/Kbuild.klibc          | 17 +++++++++++++++--
 usr/klibc/arch/x86_64/MCONFIG | 10 ++++++++--
 3 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index a048ec7a4c89..de31a0c5efa5 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -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 afc9a546feca..411d7ea9bea6 100644
--- a/scripts/Kbuild.klibc
+++ b/scripts/Kbuild.klibc
@@ -107,8 +107,15 @@ KLIBCOBJCOPY     := $(OBJCOPY)
 # klibc include paths
-KLIBCCPPFLAGS    := -nostdinc -iwithprefix include \
-		    -I$(KLIBCINC)/arch/$(KLIBCARCHDIR)	\
+ifeq ($(cc-name),clang)
+# clang can't find the compiler-specific header <stdarg.h> if -nostdinc
+# is specified so we have to omit it along with the -iwithprefix option.
+# See b/35394554.
+KLIBCCPPFLAGS    := -nostdinc -iwithprefix include
                     -I$(KLIBCINC)/bits$(KLIBCBITSIZE)	\
 		    -I$(KLIBCOBJ)/../include		\
 KLIBCAFLAGS      += -D__ASSEMBLY__ -Wa,--noexecstack $(KLIBCCFLAGS)
 KLIBCSTRIPFLAGS  += --strip-all -R .comment -R .note
+# KLIBCLIBGCC_DEF appears to be unnecessary since klibc never uses -nostdlib
+# when linking binaries, which means that any compiler specific libraries
+# should be available automatically, no need to speciy an explicit path.
+# Don't attempt to set it if we are using clang.
+ifneq ($(cc-name),clang)
 KLIBCLIBGCC_DEF  := $(shell $(KLIBCCC) $(KLIBCCFLAGS) --print-libgcc)
 KLIBCCRT0        := $(KLIBCOBJ)/arch/$(KLIBCARCHDIR)/crt0.o
 KLIBCLIBC        := $(KLIBCOBJ)/libc.a
diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG
index c5f2fa265a26..77e2ad279884 100644
--- a/usr/klibc/arch/x86_64/MCONFIG
+++ b/usr/klibc/arch/x86_64/MCONFIG
@@ -15,13 +15,19 @@
 # debugging using gdb.
-KLIBCOPTFLAGS	+= -Os -fomit-frame-pointer -mno-sse \
-		-falign-functions=1 -falign-jumps=1 -falign-loops=1
+KLIBCOPTFLAGS	+= -Os -fomit-frame-pointer -mno-sse
 ifeq ($(DEBUG),y)
 KLIBCOPTFLAGS     += -fno-asynchronous-unwind-tables
+ifneq ($(cc-name),clang)
+# These options are not supported by clang but only result in a warning
+# so we can't use a cc-option test to filter them.
+KLIBCOPTFLAGS     += -falign-functions=1 -falign-jumps=1 -falign-loops=1
 KLIBCLDFLAGS      = -m elf_x86_64

More information about the klibc mailing list