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

Bill Wendling morbo at google.com
Fri Mar 27 15:12:43 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>. See b/35394554 for more details.

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)
 KLIBCOBJDUMP     := $(OBJDUMP)
 
 # 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    :=
+else
+KLIBCCPPFLAGS    := -nostdinc -iwithprefix include
+endif
+KLIBCCPPFLAGS    += -I$(KLIBCINC)/arch/$(KLIBCARCHDIR)	\
                     -I$(KLIBCINC)/bits$(KLIBCBITSIZE)	\
 		    -I$(KLIBCOBJ)/../include		\
                     -I$(KLIBCINC)
@@ -128,7 +135,13 @@ KLIBCCFLAGS      += $(KLIBCCPPFLAGS) $(KLIBCREQFLAGS) $(KLIBCARCHREQFLAGS)  \
 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)
+endif
 KLIBCLIBGCC	 ?= $(KLIBCLIBGCC_DEF)
 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.
 #
 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
 ifeq ($(DEBUG),y)
 KLIBCOPTFLAGS     += -g
 else
 KLIBCOPTFLAGS     += -fno-asynchronous-unwind-tables
 endif
+
+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
+endif
+
 KLIBCBITSIZE      = 64
 KLIBCLDFLAGS      = -m elf_x86_64
 
-- 
2.26.0.rc2.310.g2932bb562d-goog



More information about the klibc mailing list