Skip to contentSkip to conversation

kbuild: allow to use GCC toolchain not in Clang search path

PATCH In review Stefan Agner 3 participants 6 messages Sep 18, 2018 – Sep 19, 2018 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: allow to use GCC toolchain not in Clang search path

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.

---
Nick, I removed your Reviewed-by since I had to change variable assignment slightly...

 Makefile | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Makefile +5 -3
index 4d5c883a98e5..d5de2db4b549 100644
@@ -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
Signature · 1 line
2.19.0
#1 lore raw

Hi Stefan,

2018-09-18 11:31 GMT+09:00 Stefan Agner <[email protected]>:

Quoted 7 lines

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?

Quoted 41 lines

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.

---
Nick, I removed your Reviewed-by since I had to change variable assignment slightly...

 Makefile | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Makefile +5 -4
index 4d5c883a98e5..d5de2db4b549 100644
@@ -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

Signature · 2 lines
Best Regards
Masahiro Yamada
#2 lore raw
Quoted 12 lines · Masahiro Yamada

Hi Stefan,

2018-09-18 11:31 GMT+09:00 Stefan Agner <[email protected]>:

Quoted 7 lines

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.

Quoted 8 lines

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

Quoted 46 lines

Am I missing something?

Quoted 41 lines

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.

---
Nick, I removed your Reviewed-by since I had to change variable assignment slightly...

 Makefile | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Makefile +5 -4
index 4d5c883a98e5..d5de2db4b549 100644
@@ -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

#3 lore raw

Hi Stefan,

2018-09-18 12:11 GMT+09:00 Stefan Agner <[email protected]>:

Quoted 19 lines · Masahiro Yamada
Quoted 18 lines

Hi Stefan,

2018-09-18 11:31 GMT+09:00 Stefan Agner <[email protected]>:

Quoted 7 lines

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.

Quoted 6 lines
Quoted 2 lines

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.

Signature · 2 lines
Best Regards
Masahiro Yamada
#4 lore raw
In reply to Stefan Agner
Quoted 8 lines · 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.

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).

Quoted 6 lines

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.

Quoted 6 lines

---
Nick, I removed your Reviewed-by since I had to change variable assignment slightly...

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

Quoted 9 lines
 Makefile | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Makefile +0 -0
index 4d5c883a98e5..d5de2db4b549 100644

Makefile endif · line 499

Quoted 6 lines
Makefile +2 -1
@@ -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

Quoted 13 lines
Makefile +3 -3
+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
--
Quoted 2 lines

2.19.0

Thanks for this patch Stefan.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#5 lore raw

2018-09-19 2:16 GMT+09:00 Nick Desaulniers <[email protected]>:

Quoted 72 lines · Stefan Agner
Quoted 8 lines

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).

Quoted 6 lines

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.

Quoted 6 lines

---
Nick, I removed your Reviewed-by since I had to change variable assignment slightly...

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

Quoted 9 lines
 Makefile | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Makefile +0 -0
index 4d5c883a98e5..d5de2db4b549 100644

Makefile endif · line 499

Quoted 6 lines
Makefile +2 -1
@@ -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

Quoted 13 lines
Makefile +3 -3
+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
--
Quoted 2 lines

2.19.0

Thanks for this patch Stefan.

Quoted 4 lines

--
Thanks,
~Nick Desaulniers

Applied to linux-kbuild/fixes. Thanks!

Signature · 2 lines
Best Regards
Masahiro Yamada