All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] kbuild: Add better clang cross build support
@ 2017-04-10 21:09 Matthias Kaehlcke
  2017-04-21  8:24 ` Masahiro Yamada
  0 siblings, 1 reply; 5+ messages in thread
From: Matthias Kaehlcke @ 2017-04-10 21:09 UTC (permalink / raw)
  To: Michal Marek
  Cc: linux-kbuild, linux-kernel, Behan Webster, Grant Grundler,
	Greg Hackmann, Michael Davidson, Peter Foley, Matthias Kaehlcke

Add cross target to CC if using clang. Also add custom gcc toolchain
path for fallback gcc tools.

COMPILER is previously set to "clang" if CC=clang was set from the
make command line.  So -target and -gcc-toolchain can be added to CC,
since we already know that it is set.

Clang will fallback to using things like ld, as, and libgcc if
(respectively) one of the llvm linkers isn't available, the integrated
assembler is turned off, or an appropriately cross-compiled version of
compiler-rt isn't available. To this end, you can specify the path to
this fallback gcc toolchain with GCC_TOOLCHAIN.

From: Behan Webster <behanw@converseincode.com>
Signed-off-by: Behan Webster <behanw@converseincode.com>
Reviewed-by: Jan-Simon Möller <dl9pf@gmx.de>
Reviewed-by: Mark Charlebois <charlebm@gmail.com>
Signed-off-by: Greg Hackmann <ghackmann@google.com>
Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---
Changes in v2:
- Fix GCC_TOOLCHAIN definition. $(LD) is needed to link the kernel, use
  its path to determine the installation prefix of the toolchain.
  (From: Greg Hackmann <ghackmann@google.com>)

 Makefile | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index e11989d36c87..d3a02786bb53 100644
--- a/Makefile
+++ b/Makefile
@@ -375,6 +375,16 @@ LDFLAGS_vmlinux =
 CFLAGS_GCOV	= -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized
 CFLAGS_KCOV	:= $(call cc-option,-fsanitize-coverage=trace-pc,)
 
+ifeq ($(cc-name),clang)
+ifneq ($(CROSS_COMPILE),)
+CLANG_TARGET	:= -target $(notdir $(CROSS_COMPILE:%-=%))
+GCC_TOOLCHAIN	:= $(realpath $(dir $(shell which $(LD)))/..)
+endif
+ifneq ($(GCC_TOOLCHAIN),)
+CLANG_GCC_TC	:= -gcc-toolchain $(GCC_TOOLCHAIN)
+endif
+CLANG_FLAGS	:= $(CLANG_TARGET) $(CLANG_GCC_TC)
+endif
 
 # Use USERINCLUDE when you must reference the UAPI directories only.
 USERINCLUDE    := \
@@ -395,18 +405,18 @@ LINUXINCLUDE    := \
 
 LINUXINCLUDE	+= $(filter-out $(LINUXINCLUDE),$(USERINCLUDE))
 
-KBUILD_CPPFLAGS := -D__KERNEL__
+KBUILD_CPPFLAGS := -D__KERNEL__ $(CLANG_FLAGS)
 
 KBUILD_CFLAGS   := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
 		   -fno-strict-aliasing -fno-common \
 		   -Werror-implicit-function-declaration \
 		   -Wno-format-security \
-		   -std=gnu89 $(call cc-option,-fno-PIE)
-
+		   -std=gnu89 $(call cc-option,-fno-PIE) \
+		   $(CLANG_FLAGS)
 
 KBUILD_AFLAGS_KERNEL :=
 KBUILD_CFLAGS_KERNEL :=
-KBUILD_AFLAGS   := -D__ASSEMBLY__ $(call cc-option,-fno-PIE)
+KBUILD_AFLAGS   := -D__ASSEMBLY__ $(call cc-option,-fno-PIE) $(CLANG_FLAGS)
 KBUILD_AFLAGS_MODULE  := -DMODULE
 KBUILD_CFLAGS_MODULE  := -DMODULE
 KBUILD_LDFLAGS_MODULE := -T $(srctree)/scripts/module-common.lds
-- 
2.12.2.715.g7642488e1d-goog

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] kbuild: Add better clang cross build support
  2017-04-10 21:09 [PATCH v2] kbuild: Add better clang cross build support Matthias Kaehlcke
@ 2017-04-21  8:24 ` Masahiro Yamada
  2017-04-21 17:27   ` Matthias Kaehlcke
  0 siblings, 1 reply; 5+ messages in thread
From: Masahiro Yamada @ 2017-04-21  8:24 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: Michal Marek, Linux Kbuild mailing list,
	Linux Kernel Mailing List, Behan Webster, Grant Grundler,
	Greg Hackmann, Michael Davidson, Peter Foley

Hi Matthias,


2017-04-11 6:09 GMT+09:00 Matthias Kaehlcke <mka@chromium.org>:


> COMPILER is previously set to "clang" if CC=clang was set from the
> make command line.  So -target and -gcc-toolchain can be added to CC,
> since we already know that it is set.

Can you reword or delete this paragraph?
I think this is confusing because COMPILER was replaced with cc-name
by commit 5631d9c4.




> Clang will fallback to using things like ld, as, and libgcc if
> (respectively) one of the llvm linkers isn't available, the integrated
> assembler is turned off, or an appropriately cross-compiled version of
> compiler-rt isn't available. To this end, you can specify the path to
> this fallback gcc toolchain with GCC_TOOLCHAIN.
>
> From: Behan Webster <behanw@converseincode.com>
> Signed-off-by: Behan Webster <behanw@converseincode.com>
> Reviewed-by: Jan-Simon Möller <dl9pf@gmx.de>
> Reviewed-by: Mark Charlebois <charlebm@gmail.com>
> Signed-off-by: Greg Hackmann <ghackmann@google.com>
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
> ---
> Changes in v2:
> - Fix GCC_TOOLCHAIN definition. $(LD) is needed to link the kernel, use
>   its path to determine the installation prefix of the toolchain.
>   (From: Greg Hackmann <ghackmann@google.com>)
>
>  Makefile | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index e11989d36c87..d3a02786bb53 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -375,6 +375,16 @@ LDFLAGS_vmlinux =
>  CFLAGS_GCOV    = -fprofile-arcs -ftest-coverage -fno-tree-loop-im -Wno-maybe-uninitialized
>  CFLAGS_KCOV    := $(call cc-option,-fsanitize-coverage=trace-pc,)
>
> +ifeq ($(cc-name),clang)
> +ifneq ($(CROSS_COMPILE),)
> +CLANG_TARGET   := -target $(notdir $(CROSS_COMPILE:%-=%))
> +GCC_TOOLCHAIN  := $(realpath $(dir $(shell which $(LD)))/..)
> +endif
> +ifneq ($(GCC_TOOLCHAIN),)
> +CLANG_GCC_TC   := -gcc-toolchain $(GCC_TOOLCHAIN)
> +endif
> +CLANG_FLAGS    := $(CLANG_TARGET) $(CLANG_GCC_TC)
> +endif
>
>  # Use USERINCLUDE when you must reference the UAPI directories only.
>  USERINCLUDE    := \
> @@ -395,18 +405,18 @@ LINUXINCLUDE    := \
>
>  LINUXINCLUDE   += $(filter-out $(LINUXINCLUDE),$(USERINCLUDE))
>
> -KBUILD_CPPFLAGS := -D__KERNEL__
> +KBUILD_CPPFLAGS := -D__KERNEL__ $(CLANG_FLAGS)
>
>  KBUILD_CFLAGS   := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
>                    -fno-strict-aliasing -fno-common \
>                    -Werror-implicit-function-declaration \
>                    -Wno-format-security \
> -                  -std=gnu89 $(call cc-option,-fno-PIE)
> -
> +                  -std=gnu89 $(call cc-option,-fno-PIE) \
> +                  $(CLANG_FLAGS)
>
>  KBUILD_AFLAGS_KERNEL :=
>  KBUILD_CFLAGS_KERNEL :=
> -KBUILD_AFLAGS   := -D__ASSEMBLY__ $(call cc-option,-fno-PIE)
> +KBUILD_AFLAGS   := -D__ASSEMBLY__ $(call cc-option,-fno-PIE) $(CLANG_FLAGS)


This adds multiple -gcc-toolchain because both KBUILD_CPPFLAGS and KBUILD_CFLAGS
are added to c_flags.   See scripts/Makefile.lib

orig_c_flags   = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(KBUILD_SUBDIR_CCFLAGS) \
                 $(ccflags-y) $(CFLAGS_$(basetarget).o)


Pre-processing is related to the front-end part,
so -target and -gcc-toolchain are not necessary for KBUILD_CPPFLAGS?

Maybe, adding it to KBUILD_CFLAGS and KBUILD_AFLAGS could be enough?




Is it possible to move these to around line 700?

ifeq ($(cc-name),clang)
   < add here ? >
KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)



I do not want to sprinkle ugly ifeq ($(cc-name,clang).





-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] kbuild: Add better clang cross build support
  2017-04-21  8:24 ` Masahiro Yamada
@ 2017-04-21 17:27   ` Matthias Kaehlcke
  0 siblings, 0 replies; 5+ messages in thread
From: Matthias Kaehlcke @ 2017-04-21 17:27 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Michal Marek, Linux Kbuild mailing list,
	Linux Kernel Mailing List, Behan Webster, Grant Grundler,
	Greg Hackmann, Michael Davidson, Peter Foley

Hi Masahiro,

Thanks for your comments.

El Fri, Apr 21, 2017 at 05:24:31PM +0900 Masahiro Yamada ha dit:

> 2017-04-11 6:09 GMT+09:00 Matthias Kaehlcke <mka@chromium.org>:
> 
> 
> > COMPILER is previously set to "clang" if CC=clang was set from the
> > make command line.  So -target and -gcc-toolchain can be added to CC,
> > since we already know that it is set.
> 
> Can you reword or delete this paragraph?
> I think this is confusing because COMPILER was replaced with cc-name
> by commit 5631d9c4.

Ok, I think not much is lost by deleting it.

<snip>

> > -KBUILD_CPPFLAGS := -D__KERNEL__
> > +KBUILD_CPPFLAGS := -D__KERNEL__ $(CLANG_FLAGS)
> >
> >  KBUILD_CFLAGS   := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
> >                    -fno-strict-aliasing -fno-common \
> >                    -Werror-implicit-function-declaration \
> >                    -Wno-format-security \
> > -                  -std=gnu89 $(call cc-option,-fno-PIE)
> > -
> > +                  -std=gnu89 $(call cc-option,-fno-PIE) \
> > +                  $(CLANG_FLAGS)
> >
> >  KBUILD_AFLAGS_KERNEL :=
> >  KBUILD_CFLAGS_KERNEL :=
> > -KBUILD_AFLAGS   := -D__ASSEMBLY__ $(call cc-option,-fno-PIE)
> > +KBUILD_AFLAGS   := -D__ASSEMBLY__ $(call cc-option,-fno-PIE) $(CLANG_FLAGS)
> 
> 
> This adds multiple -gcc-toolchain because both KBUILD_CPPFLAGS and KBUILD_CFLAGS
> are added to c_flags.   See scripts/Makefile.lib
> 
> orig_c_flags   = $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(KBUILD_SUBDIR_CCFLAGS) \
>                  $(ccflags-y) $(CFLAGS_$(basetarget).o)
> 
> 
> Pre-processing is related to the front-end part,
> so -target and -gcc-toolchain are not necessary for KBUILD_CPPFLAGS?
> 
> Maybe, adding it to KBUILD_CFLAGS and KBUILD_AFLAGS could be enough?

Indeed it seems it is not needed in KBUILD_CPPFLAGS, at least I
encountered no issues in a x86 and arm64 build.

> Is it possible to move these to around line 700?
> 
> ifeq ($(cc-name),clang)
>    < add here ? >
> KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
> KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
> 
> I do not want to sprinkle ugly ifeq ($(cc-name,clang).

I agree, better concentrate clang specific stuff when possible.

I'll rework the patch and send out an update shortly.

Cheers

Matthias

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] kbuild: Add better clang cross build support
  2017-04-21 18:20 Matthias Kaehlcke
@ 2017-04-23 14:26 ` Masahiro Yamada
  0 siblings, 0 replies; 5+ messages in thread
From: Masahiro Yamada @ 2017-04-23 14:26 UTC (permalink / raw)
  To: Matthias Kaehlcke
  Cc: Michal Marek, Linux Kbuild mailing list,
	Linux Kernel Mailing List, Behan Webster, Grant Grundler,
	Greg Hackmann, Michael Davidson, Peter Foley

2017-04-22 3:20 GMT+09:00 Matthias Kaehlcke <mka@chromium.org>:
> Add cross target to CC if using clang. Also add custom gcc toolchain
> path for fallback gcc tools.
>
> Clang will fallback to using things like ld, as, and libgcc if
> (respectively) one of the llvm linkers isn't available, the integrated
> assembler is turned off, or an appropriately cross-compiled version of
> compiler-rt isn't available. To this end, you can specify the path to
> this fallback gcc toolchain with GCC_TOOLCHAIN.
>
> From: Behan Webster <behanw@converseincode.com>
> Signed-off-by: Behan Webster <behanw@converseincode.com>
> Reviewed-by: Jan-Simon Möller <dl9pf@gmx.de>
> Reviewed-by: Mark Charlebois <charlebm@gmail.com>
> Signed-off-by: Greg Hackmann <ghackmann@google.com>
> Signed-off-by: Matthias Kaehlcke <mka@chromium.org>


Applied to linux-kbuild/kbuild.  Thanks!




-- 
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] kbuild: Add better clang cross build support
@ 2017-04-21 18:20 Matthias Kaehlcke
  2017-04-23 14:26 ` Masahiro Yamada
  0 siblings, 1 reply; 5+ messages in thread
From: Matthias Kaehlcke @ 2017-04-21 18:20 UTC (permalink / raw)
  To: Masahiro Yamada, Michal Marek
  Cc: linux-kbuild, linux-kernel, Behan Webster, Grant Grundler,
	Greg Hackmann, Michael Davidson, Peter Foley, Matthias Kaehlcke

Add cross target to CC if using clang. Also add custom gcc toolchain
path for fallback gcc tools.

Clang will fallback to using things like ld, as, and libgcc if
(respectively) one of the llvm linkers isn't available, the integrated
assembler is turned off, or an appropriately cross-compiled version of
compiler-rt isn't available. To this end, you can specify the path to
this fallback gcc toolchain with GCC_TOOLCHAIN.

From: Behan Webster <behanw@converseincode.com>
Signed-off-by: Behan Webster <behanw@converseincode.com>
Reviewed-by: Jan-Simon Möller <dl9pf@gmx.de>
Reviewed-by: Mark Charlebois <charlebm@gmail.com>
Signed-off-by: Greg Hackmann <ghackmann@google.com>
Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---
Changes in v2:
- Moved new 'code' into existing ifeq clang block
- Don't add -target and -gcc-toolchain to KBUILD_CPPFLAGS
- Removed intermediate CLANG_FLAGS variable
- Updated commit message, removed paragraph mentioning old COMPILER
  variable

 Makefile | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/Makefile b/Makefile
index 5039b9148d15..11c00bd3ec0c 100644
--- a/Makefile
+++ b/Makefile
@@ -698,6 +698,15 @@ endif
 KBUILD_CFLAGS += $(stackp-flag)
 
 ifeq ($(cc-name),clang)
+ifneq ($(CROSS_COMPILE),)
+CLANG_TARGET	:= -target $(notdir $(CROSS_COMPILE:%-=%))
+GCC_TOOLCHAIN	:= $(realpath $(dir $(shell which $(LD)))/..)
+endif
+ifneq ($(GCC_TOOLCHAIN),)
+CLANG_GCC_TC	:= -gcc-toolchain $(GCC_TOOLCHAIN)
+endif
+KBUILD_CFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC)
+KBUILD_AFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC)
 KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
 KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-variable)
-- 
2.12.2.816.g2cccc81164-goog

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-04-23 14:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-10 21:09 [PATCH v2] kbuild: Add better clang cross build support Matthias Kaehlcke
2017-04-21  8:24 ` Masahiro Yamada
2017-04-21 17:27   ` Matthias Kaehlcke
2017-04-21 18:20 Matthias Kaehlcke
2017-04-23 14:26 ` Masahiro Yamada

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.