Bill Wendling
2020-Mar-27 22:29 UTC
[klibc] [PATCH v2 1/5] Kbuild: add support for clang builds
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) 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
Ben Hutchings
2020-Mar-28 15:35 UTC
[klibc] [PATCH v2 1/5] Kbuild: add support for clang builds
On Fri, 2020-03-27 at 15:29 -0700, Bill Wendling wrote:> From: Michael Davidson <md at google.com> > > Add cc-name to klibc/scripts/Kbuild.include.[...]> --- 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.This seems to be an internal bug number. It doesn't belong here.> +KLIBCCPPFLAGS :> +else > +KLIBCCPPFLAGS := -nostdinc -iwithprefix include > +endifIt might make more sense to include these options only if cc-name is "gcc". (Doesn't make any difference now, but if we ever add a third compiler then it seems like it ought to work without this.)> +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) > +endifSeems like this should also check that cc-name is "gcc", then.> 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 > +endifSame here. It would be more readable to write: ifeq ($(cc-name),gcc) KLIBCOPTFLAGS += gcc-specific-optimisations endif ifeq ($(cc-name),clang) KLIBCOPTFLAGS += clang-specific-optimisations endif And this shouldn't need a comment. Ben.> KLIBCBITSIZE = 64 > KLIBCLDFLAGS = -m elf_x86_64-- Ben Hutchings This sentence contradicts itself - no actually it doesn't. -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: This is a digitally signed message part URL: <https://lists.zytor.com/archives/klibc/attachments/20200328/7c5abfaa/attachment.sig>
Thorsten Glaser
2020-Mar-29 00:00 UTC
[klibc] [PATCH v2 1/5] Kbuild: add support for clang builds
>> +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. >> +KLIBCCPPFLAGS :>> +else >> +KLIBCCPPFLAGS := -nostdinc -iwithprefix include >> +endifIs this even a good idea? The -nostdinc is required so that the normal system libc includes are not used. Omitting it will cause them to be detected e.g. by autoconf-like machinery, and subsequent use may cause trouble and/or failure. I?d suggest to add -nostdinc together with whatever other options are needed to the clang case; surely the ?compiler-specific header? path is discoverable? bye, //mirabilos -- Solange man keine schmutzigen Tricks macht, und ich meine *wirklich* schmutzige Tricks, wie bei einer doppelt verketteten Liste beide Pointer XORen und in nur einem Word speichern, funktioniert Boehm ganz hervorragend. -- Andreas Bogk ?ber boehm-gc in d.a.s.r