linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kbuild: allow to use GCC toolchain not in Clang search path
@ 2018-09-18  2:31 Stefan Agner
  2018-09-18  3:06 ` Masahiro Yamada
  2018-09-18 17:16 ` Nick Desaulniers
  0 siblings, 2 replies; 6+ messages in thread
From: Stefan Agner @ 2018-09-18  2:31 UTC (permalink / raw)
  To: yamada.masahiro, michal.lkml
  Cc: ndesaulniers, arnd, linux-kbuild, linux-kernel, Stefan Agner

When using a GCC cross toolchain which is not in a compiled in
Clang search path, Clang reverts to the system assembler and
linker. This leads to assembler or linker errors, depending on
which tool is first used for a given architecture.

It seems that Clang is not searching $PATH for a matching
assembler or linker.

Make sure that Clang picks up the correct assembler or linker by
passing the cross compilers bin directory as search path.

This allows to use Clang provided by distributions with GCC
toolchains not in /usr/bin.

Link: https://github.com/ClangBuiltLinux/linux/issues/78
Signed-off-by: Stefan Agner <stefan@agner.ch>
---
Nick, I removed your Reviewed-by since I had to change variable
assignment slightly...

 Makefile | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 4d5c883a98e5..d5de2db4b549 100644
--- a/Makefile
+++ b/Makefile
@@ -495,13 +495,15 @@ endif
 ifeq ($(cc-name),clang)
 ifneq ($(CROSS_COMPILE),)
 CLANG_TARGET	:= --target=$(notdir $(CROSS_COMPILE:%-=%))
-GCC_TOOLCHAIN	:= $(realpath $(dir $(shell which $(LD)))/..)
+GCC_TOOLCHAIN_DIR := $(dir $(shell which $(LD)))
+CLANG_PREFIX	:= --prefix=$(GCC_TOOLCHAIN_DIR)
+GCC_TOOLCHAIN	:= $(realpath $(GCC_TOOLCHAIN_DIR)/..)
 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_CFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
+KBUILD_AFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
 KBUILD_CFLAGS += $(call cc-option, -no-integrated-as)
 KBUILD_AFLAGS += $(call cc-option, -no-integrated-as)
 endif
-- 
2.19.0


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

* Re: [PATCH] kbuild: allow to use GCC toolchain not in Clang search path
  2018-09-18  2:31 [PATCH] kbuild: allow to use GCC toolchain not in Clang search path Stefan Agner
@ 2018-09-18  3:06 ` Masahiro Yamada
  2018-09-18  3:11   ` Stefan Agner
  2018-09-18 17:16 ` Nick Desaulniers
  1 sibling, 1 reply; 6+ messages in thread
From: Masahiro Yamada @ 2018-09-18  3:06 UTC (permalink / raw)
  To: Stefan Agner
  Cc: Michal Marek, Nick Desaulniers, Arnd Bergmann,
	Linux Kbuild mailing list, Linux Kernel Mailing List

Hi Stefan,

2018-09-18 11:31 GMT+09:00 Stefan Agner <stefan@agner.ch>:
> When using a GCC cross toolchain which is not in a compiled in
> Clang search path, Clang reverts to the system assembler and
> linker. This leads to assembler or linker errors, depending on
> which tool is first used for a given architecture.
>
> It seems that Clang is not searching $PATH for a matching
> assembler or linker.


The current code passes the absolute path of toolchains
to --gcc-toolchain option.
In my understanding, this is enough for Clang
to find the proper linker and the assembler.


Why is the --prefix option also needed ?


Am I missing something?



> Make sure that Clang picks up the correct assembler or linker by
> passing the cross compilers bin directory as search path.
>
> This allows to use Clang provided by distributions with GCC
> toolchains not in /usr/bin.
>
> Link: https://github.com/ClangBuiltLinux/linux/issues/78
> Signed-off-by: Stefan Agner <stefan@agner.ch>
> ---
> Nick, I removed your Reviewed-by since I had to change variable
> assignment slightly...
>
>  Makefile | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 4d5c883a98e5..d5de2db4b549 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -495,13 +495,15 @@ endif
>  ifeq ($(cc-name),clang)
>  ifneq ($(CROSS_COMPILE),)
>  CLANG_TARGET   := --target=$(notdir $(CROSS_COMPILE:%-=%))
> -GCC_TOOLCHAIN  := $(realpath $(dir $(shell which $(LD)))/..)
> +GCC_TOOLCHAIN_DIR := $(dir $(shell which $(LD)))
> +CLANG_PREFIX   := --prefix=$(GCC_TOOLCHAIN_DIR)
> +GCC_TOOLCHAIN  := $(realpath $(GCC_TOOLCHAIN_DIR)/..)
>  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_CFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
> +KBUILD_AFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
>  KBUILD_CFLAGS += $(call cc-option, -no-integrated-as)
>  KBUILD_AFLAGS += $(call cc-option, -no-integrated-as)
>  endif
> --
> 2.19.0
>



-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH] kbuild: allow to use GCC toolchain not in Clang search path
  2018-09-18  3:06 ` Masahiro Yamada
@ 2018-09-18  3:11   ` Stefan Agner
  2018-09-18  7:43     ` Masahiro Yamada
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Agner @ 2018-09-18  3:11 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: Michal Marek, Nick Desaulniers, Arnd Bergmann,
	Linux Kbuild mailing list, Linux Kernel Mailing List

On 17.09.2018 20:06, Masahiro Yamada wrote:
> Hi Stefan,
> 
> 2018-09-18 11:31 GMT+09:00 Stefan Agner <stefan@agner.ch>:
>> When using a GCC cross toolchain which is not in a compiled in
>> Clang search path, Clang reverts to the system assembler and
>> linker. This leads to assembler or linker errors, depending on
>> which tool is first used for a given architecture.
>>
>> It seems that Clang is not searching $PATH for a matching
>> assembler or linker.
> 
> 
> The current code passes the absolute path of toolchains
> to --gcc-toolchain option.
> In my understanding, this is enough for Clang
> to find the proper linker and the assembler.
> 
> 
> Why is the --prefix option also needed ?
> 

It seems that Clang makes a difference between "toolchain" and search path for toolchain binaries.

Without this patch building fails for the described use case, see the github link for more context.

--
Stefan


> 
> Am I missing something?
> 
> 
> 
>> Make sure that Clang picks up the correct assembler or linker by
>> passing the cross compilers bin directory as search path.
>>
>> This allows to use Clang provided by distributions with GCC
>> toolchains not in /usr/bin.
>>
>> Link: https://github.com/ClangBuiltLinux/linux/issues/78
>> Signed-off-by: Stefan Agner <stefan@agner.ch>
>> ---
>> Nick, I removed your Reviewed-by since I had to change variable
>> assignment slightly...
>>
>>  Makefile | 8 +++++---
>>  1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/Makefile b/Makefile
>> index 4d5c883a98e5..d5de2db4b549 100644
>> --- a/Makefile
>> +++ b/Makefile
>> @@ -495,13 +495,15 @@ endif
>>  ifeq ($(cc-name),clang)
>>  ifneq ($(CROSS_COMPILE),)
>>  CLANG_TARGET   := --target=$(notdir $(CROSS_COMPILE:%-=%))
>> -GCC_TOOLCHAIN  := $(realpath $(dir $(shell which $(LD)))/..)
>> +GCC_TOOLCHAIN_DIR := $(dir $(shell which $(LD)))
>> +CLANG_PREFIX   := --prefix=$(GCC_TOOLCHAIN_DIR)
>> +GCC_TOOLCHAIN  := $(realpath $(GCC_TOOLCHAIN_DIR)/..)
>>  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_CFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
>> +KBUILD_AFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
>>  KBUILD_CFLAGS += $(call cc-option, -no-integrated-as)
>>  KBUILD_AFLAGS += $(call cc-option, -no-integrated-as)
>>  endif
>> --
>> 2.19.0
>>

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

* Re: [PATCH] kbuild: allow to use GCC toolchain not in Clang search path
  2018-09-18  3:11   ` Stefan Agner
@ 2018-09-18  7:43     ` Masahiro Yamada
  0 siblings, 0 replies; 6+ messages in thread
From: Masahiro Yamada @ 2018-09-18  7:43 UTC (permalink / raw)
  To: Stefan Agner
  Cc: Michal Marek, Nick Desaulniers, Arnd Bergmann,
	Linux Kbuild mailing list, Linux Kernel Mailing List

Hi Stefan,

2018-09-18 12:11 GMT+09:00 Stefan Agner <stefan@agner.ch>:
> On 17.09.2018 20:06, Masahiro Yamada wrote:
>> Hi Stefan,
>>
>> 2018-09-18 11:31 GMT+09:00 Stefan Agner <stefan@agner.ch>:
>>> When using a GCC cross toolchain which is not in a compiled in
>>> Clang search path, Clang reverts to the system assembler and
>>> linker. This leads to assembler or linker errors, depending on
>>> which tool is first used for a given architecture.
>>>
>>> It seems that Clang is not searching $PATH for a matching
>>> assembler or linker.
>>
>>
>> The current code passes the absolute path of toolchains
>> to --gcc-toolchain option.
>> In my understanding, this is enough for Clang
>> to find the proper linker and the assembler.
>>
>>
>> Why is the --prefix option also needed ?
>>
>
> It seems that Clang makes a difference between "toolchain" and search path for toolchain binaries.
>
> Without this patch building fails for the described use case, see the github link for more context.


OK, I confirmed this patch fixed the problem.


I will wait a little more just in case Nick has comments about this.



Thanks.



-- 
Best Regards
Masahiro Yamada

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

* Re: [PATCH] kbuild: allow to use GCC toolchain not in Clang search path
  2018-09-18  2:31 [PATCH] kbuild: allow to use GCC toolchain not in Clang search path Stefan Agner
  2018-09-18  3:06 ` Masahiro Yamada
@ 2018-09-18 17:16 ` Nick Desaulniers
  2018-09-19 14:57   ` Masahiro Yamada
  1 sibling, 1 reply; 6+ messages in thread
From: Nick Desaulniers @ 2018-09-18 17:16 UTC (permalink / raw)
  To: Stefan Agner
  Cc: Masahiro Yamada, Michal Marek, Arnd Bergmann,
	Linux Kbuild mailing list, LKML, Stephen Hines

On Mon, Sep 17, 2018 at 7:32 PM Stefan Agner <stefan@agner.ch> wrote:
>
> When using a GCC cross toolchain which is not in a compiled in
> Clang search path, Clang reverts to the system assembler and
> linker. This leads to assembler or linker errors, depending on
> which tool is first used for a given architecture.
>
> It seems that Clang is not searching $PATH for a matching
> assembler or linker.

Yes, Clang uses what's called a sysroot, which is meant to be more
hygenic than $PATH, IIUC.  This is surprising to folks familiar with
$PATH, but really helps include the correct headers and use the
correct toolchain when cross compiling, at the cost of freedom to put
the cross toolchain wherever in the filesystem. (this is my
understanding of sysroot, I just learned about it yesterday, so take
this with a grain of salt).

>
> Make sure that Clang picks up the correct assembler or linker by
> passing the cross compilers bin directory as search path.
>
> This allows to use Clang provided by distributions with GCC
> toolchains not in /usr/bin.

IIUC, its just utilities in binutils we're using (as, bfd.ld,
objcopy), not GCC, but maybe that's just being pedantic.

>
> Link: https://github.com/ClangBuiltLinux/linux/issues/78
> Signed-off-by: Stefan Agner <stefan@agner.ch>
> ---
> Nick, I removed your Reviewed-by since I had to change variable
> assignment slightly...

Yes, thank you, that's the right thing to do.

>
>  Makefile | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index 4d5c883a98e5..d5de2db4b549 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -495,13 +495,15 @@ endif
>  ifeq ($(cc-name),clang)
>  ifneq ($(CROSS_COMPILE),)
>  CLANG_TARGET   := --target=$(notdir $(CROSS_COMPILE:%-=%))
> -GCC_TOOLCHAIN  := $(realpath $(dir $(shell which $(LD)))/..)
> +GCC_TOOLCHAIN_DIR := $(dir $(shell which $(LD)))
> +CLANG_PREFIX   := --prefix=$(GCC_TOOLCHAIN_DIR)

Yep: https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-b-dir

> +GCC_TOOLCHAIN  := $(realpath $(GCC_TOOLCHAIN_DIR)/..)
>  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_CFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
> +KBUILD_AFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
>  KBUILD_CFLAGS += $(call cc-option, -no-integrated-as)
>  KBUILD_AFLAGS += $(call cc-option, -no-integrated-as)
>  endif
> --
> 2.19.0
>

Thanks for this patch Stefan.
Reviewed-and-tested-by: Nick Desaulniers <ndesaulniers@google.com>
-- 
Thanks,
~Nick Desaulniers

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

* Re: [PATCH] kbuild: allow to use GCC toolchain not in Clang search path
  2018-09-18 17:16 ` Nick Desaulniers
@ 2018-09-19 14:57   ` Masahiro Yamada
  0 siblings, 0 replies; 6+ messages in thread
From: Masahiro Yamada @ 2018-09-19 14:57 UTC (permalink / raw)
  To: Nick Desaulniers
  Cc: Stefan Agner, Michal Marek, Arnd Bergmann,
	Linux Kbuild mailing list, LKML, Stephen Hines

2018-09-19 2:16 GMT+09:00 Nick Desaulniers <ndesaulniers@google.com>:
> On Mon, Sep 17, 2018 at 7:32 PM Stefan Agner <stefan@agner.ch> wrote:
>>
>> When using a GCC cross toolchain which is not in a compiled in
>> Clang search path, Clang reverts to the system assembler and
>> linker. This leads to assembler or linker errors, depending on
>> which tool is first used for a given architecture.
>>
>> It seems that Clang is not searching $PATH for a matching
>> assembler or linker.
>
> Yes, Clang uses what's called a sysroot, which is meant to be more
> hygenic than $PATH, IIUC.  This is surprising to folks familiar with
> $PATH, but really helps include the correct headers and use the
> correct toolchain when cross compiling, at the cost of freedom to put
> the cross toolchain wherever in the filesystem. (this is my
> understanding of sysroot, I just learned about it yesterday, so take
> this with a grain of salt).
>
>>
>> Make sure that Clang picks up the correct assembler or linker by
>> passing the cross compilers bin directory as search path.
>>
>> This allows to use Clang provided by distributions with GCC
>> toolchains not in /usr/bin.
>
> IIUC, its just utilities in binutils we're using (as, bfd.ld,
> objcopy), not GCC, but maybe that's just being pedantic.
>
>>
>> Link: https://github.com/ClangBuiltLinux/linux/issues/78
>> Signed-off-by: Stefan Agner <stefan@agner.ch>
>> ---
>> Nick, I removed your Reviewed-by since I had to change variable
>> assignment slightly...
>
> Yes, thank you, that's the right thing to do.
>
>>
>>  Makefile | 8 +++++---
>>  1 file changed, 5 insertions(+), 3 deletions(-)
>>
>> diff --git a/Makefile b/Makefile
>> index 4d5c883a98e5..d5de2db4b549 100644
>> --- a/Makefile
>> +++ b/Makefile
>> @@ -495,13 +495,15 @@ endif
>>  ifeq ($(cc-name),clang)
>>  ifneq ($(CROSS_COMPILE),)
>>  CLANG_TARGET   := --target=$(notdir $(CROSS_COMPILE:%-=%))
>> -GCC_TOOLCHAIN  := $(realpath $(dir $(shell which $(LD)))/..)
>> +GCC_TOOLCHAIN_DIR := $(dir $(shell which $(LD)))
>> +CLANG_PREFIX   := --prefix=$(GCC_TOOLCHAIN_DIR)
>
> Yep: https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-b-dir
>
>> +GCC_TOOLCHAIN  := $(realpath $(GCC_TOOLCHAIN_DIR)/..)
>>  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_CFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
>> +KBUILD_AFLAGS += $(CLANG_TARGET) $(CLANG_GCC_TC) $(CLANG_PREFIX)
>>  KBUILD_CFLAGS += $(call cc-option, -no-integrated-as)
>>  KBUILD_AFLAGS += $(call cc-option, -no-integrated-as)
>>  endif
>> --
>> 2.19.0
>>
>
> Thanks for this patch Stefan.
> Reviewed-and-tested-by: Nick Desaulniers <ndesaulniers@google.com>
> --
> Thanks,
> ~Nick Desaulniers




Applied to linux-kbuild/fixes. Thanks!

-- 
Best Regards
Masahiro Yamada

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

end of thread, other threads:[~2018-09-19 14:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-18  2:31 [PATCH] kbuild: allow to use GCC toolchain not in Clang search path Stefan Agner
2018-09-18  3:06 ` Masahiro Yamada
2018-09-18  3:11   ` Stefan Agner
2018-09-18  7:43     ` Masahiro Yamada
2018-09-18 17:16 ` Nick Desaulniers
2018-09-19 14:57   ` Masahiro Yamada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).