Skip to contentSkip to conversation

tools/build: Add clang cross-compilation flags to feature detection

PATCH In review Björn Töpel 3 participants 5 messages Nov 2, 2023 – May 2, 2024 BPF

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/build: Add clang cross-compilation flags to feature detection

From: Björn Töpel <[email protected]>

When a tool cross-build has LLVM=1 set, the clang cross-compilation flags are not passed to the feature detection build system. This results in the host's features are detected instead of the targets.

E.g, triggering a cross-build of bpftool:

cd tools/bpf/bpftool
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- LLVM=1

would report the host's, and not the target's features.

Correct the issue by passing the CLANG_CROSS_FLAGS variable to the feature detection makefile.

Fixes cebdb7374577 ("tools: Help cross-building with clang")
---
 tools/build/Makefile.feature |  2 +-
 tools/build/feature/Makefile | 12 ++++++------
 2 files changed, 7 insertions(+), 7 deletions(-)
tools/build/Makefile.feature +1 -1
index 934e2777a2db..25b009a6c05f 100644
@@ -8,7 +8,7 @@ endif
 
 feature_check = $(eval $(feature_check_code))
 define feature_check_code
-  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CC="$(CC)" CXX="$(CXX)" CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" CXXFLAGS="$(EXTRA_CXXFLAGS) $(FEATURE_CHECK_CXXFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
+  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CC="$(CC)" CXX="$(CXX)" CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" CXXFLAGS="$(EXTRA_CXXFLAGS) $(FEATURE_CHECK_CXXFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" CLANG_CROSS_FLAGS="$(CLANG_CROSS_FLAGS)" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
 endef
 
 feature_set = $(eval $(feature_set_code))
tools/build/feature/Makefile +6 -6
index dad79ede4e0a..c4458345e564 100644
@@ -84,12 +84,12 @@ PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
 
 all: $(FILES)
 
-__BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(LDFLAGS)
+__BUILD = $(CC) $(CFLAGS) $(CLANG_CROSS_FLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(LDFLAGS)
   BUILD = $(__BUILD) > $(@:.bin=.make.output) 2>&1
   BUILD_BFD = $(BUILD) -DPACKAGE='"perf"' -lbfd -ldl
   BUILD_ALL = $(BUILD) -fstack-protector-all -O2 -D_FORTIFY_SOURCE=2 -ldw -lelf -lnuma -lelf -lslang $(FLAGS_PERL_EMBED) $(FLAGS_PYTHON_EMBED) -DPACKAGE='"perf"' -lbfd -ldl -lz -llzma -lzstd -lcap
 
-__BUILDXX = $(CXX) $(CXXFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
+__BUILDXX = $(CXX) $(CXXFLAGS) $(CLANG_CROSS_FLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
   BUILDXX = $(__BUILDXX) > $(@:.bin=.make.output) 2>&1
 
 ###############################
@@ -259,10 +259,10 @@ $(OUTPUT)test-reallocarray.bin:
 	$(BUILD)
 
 $(OUTPUT)test-libbfd-liberty.bin:
-	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
+	$(CC) $(CFLAGS) $(CLANG_CROSS_FLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
 
 $(OUTPUT)test-libbfd-liberty-z.bin:
-	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty -lz
+	$(CC) $(CFLAGS) $(CLANG_CROSS_FLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty -lz
 
 $(OUTPUT)test-cplus-demangle.bin:
 	$(BUILD) -liberty
@@ -283,10 +283,10 @@ $(OUTPUT)test-libbabeltrace.bin:
 	$(BUILD) # -lbabeltrace provided by $(FEATURE_CHECK_LDFLAGS-libbabeltrace)
 
 $(OUTPUT)test-compile-32.bin:
-	$(CC) -m32 -o $@ test-compile.c
+	$(CC) $(CLANG_CROSS_FLAGS) -m32 -o $@ test-compile.c
 
 $(OUTPUT)test-compile-x32.bin:
-	$(CC) -mx32 -o $@ test-compile.c
+	$(CC) $(CLANG_CROSS_FLAGS) -mx32 -o $@ test-compile.c
 
 $(OUTPUT)test-zlib.bin:
 	$(BUILD) -lz

base-commit: 21e80f3841c01aeaf32d7aee7bbc87b3db1aa0c6

Signature · 1 line
2.40.1
Acked-by on v2 1/1
#1 lore raw
Quoted 11 lines · Björn Töpel

From: Björn Töpel <[email protected]>

When a tool cross-build has LLVM=1 set, the clang cross-compilation flags are not passed to the feature detection build system. This results in the host's features are detected instead of the targets.

E.g, triggering a cross-build of bpftool:

cd tools/bpf/bpftool
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- LLVM=1

Quoted 7 lines

would report the host's, and not the target's features.

Correct the issue by passing the CLANG_CROSS_FLAGS variable to the feature detection makefile.

Fixes cebdb7374577 ("tools: Help cross-building with clang")

jirka

Quoted 68 lines
---
 tools/build/Makefile.feature |  2 +-
 tools/build/feature/Makefile | 12 ++++++------
 2 files changed, 7 insertions(+), 7 deletions(-)
tools/build/Makefile.feature +1 -1
index 934e2777a2db..25b009a6c05f 100644
@@ -8,7 +8,7 @@ endif
 
 feature_check = $(eval $(feature_check_code))
 define feature_check_code
-  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CC="$(CC)" CXX="$(CXX)" CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" CXXFLAGS="$(EXTRA_CXXFLAGS) $(FEATURE_CHECK_CXXFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
+  feature-$(1) := $(shell $(MAKE) OUTPUT=$(OUTPUT_FEATURES) CC="$(CC)" CXX="$(CXX)" CFLAGS="$(EXTRA_CFLAGS) $(FEATURE_CHECK_CFLAGS-$(1))" CXXFLAGS="$(EXTRA_CXXFLAGS) $(FEATURE_CHECK_CXXFLAGS-$(1))" LDFLAGS="$(LDFLAGS) $(FEATURE_CHECK_LDFLAGS-$(1))" CLANG_CROSS_FLAGS="$(CLANG_CROSS_FLAGS)" -C $(feature_dir) $(OUTPUT_FEATURES)test-$1.bin >/dev/null 2>/dev/null && echo 1 || echo 0)
 endef
 
 feature_set = $(eval $(feature_set_code))
tools/build/feature/Makefile +6 -6
index dad79ede4e0a..c4458345e564 100644
@@ -84,12 +84,12 @@ PKG_CONFIG ?= $(CROSS_COMPILE)pkg-config
 
 all: $(FILES)
 
-__BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(LDFLAGS)
+__BUILD = $(CC) $(CFLAGS) $(CLANG_CROSS_FLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(LDFLAGS)
   BUILD = $(__BUILD) > $(@:.bin=.make.output) 2>&1
   BUILD_BFD = $(BUILD) -DPACKAGE='"perf"' -lbfd -ldl
   BUILD_ALL = $(BUILD) -fstack-protector-all -O2 -D_FORTIFY_SOURCE=2 -ldw -lelf -lnuma -lelf -lslang $(FLAGS_PERL_EMBED) $(FLAGS_PYTHON_EMBED) -DPACKAGE='"perf"' -lbfd -ldl -lz -llzma -lzstd -lcap
 
-__BUILDXX = $(CXX) $(CXXFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
+__BUILDXX = $(CXX) $(CXXFLAGS) $(CLANG_CROSS_FLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
   BUILDXX = $(__BUILDXX) > $(@:.bin=.make.output) 2>&1
 
 ###############################
@@ -259,10 +259,10 @@ $(OUTPUT)test-reallocarray.bin:
 	$(BUILD)
 
 $(OUTPUT)test-libbfd-liberty.bin:
-	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
+	$(CC) $(CFLAGS) $(CLANG_CROSS_FLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty
 
 $(OUTPUT)test-libbfd-liberty-z.bin:
-	$(CC) $(CFLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty -lz
+	$(CC) $(CFLAGS) $(CLANG_CROSS_FLAGS) -Wall -Werror -o $@ test-libbfd.c -DPACKAGE='"perf"' $(LDFLAGS) -lbfd -ldl -liberty -lz
 
 $(OUTPUT)test-cplus-demangle.bin:
 	$(BUILD) -liberty
@@ -283,10 +283,10 @@ $(OUTPUT)test-libbabeltrace.bin:
 	$(BUILD) # -lbabeltrace provided by $(FEATURE_CHECK_LDFLAGS-libbabeltrace)
 
 $(OUTPUT)test-compile-32.bin:
-	$(CC) -m32 -o $@ test-compile.c
+	$(CC) $(CLANG_CROSS_FLAGS) -m32 -o $@ test-compile.c
 
 $(OUTPUT)test-compile-x32.bin:
-	$(CC) -mx32 -o $@ test-compile.c
+	$(CC) $(CLANG_CROSS_FLAGS) -mx32 -o $@ test-compile.c
 
 $(OUTPUT)test-zlib.bin:
 	$(BUILD) -lz

base-commit: 21e80f3841c01aeaf32d7aee7bbc87b3db1aa0c6

Signature · 1 line
2.40.1
#2 lore raw
Quoted 14 lines · Jiri Olsa
Quoted 13 lines · Björn Töpel

From: Björn Töpel <[email protected]>

When a tool cross-build has LLVM=1 set, the clang cross-compilation flags are not passed to the feature detection build system. This results in the host's features are detected instead of the targets.

E.g, triggering a cross-build of bpftool:

cd tools/bpf/bpftool
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- LLVM=1

would report the host's, and not the target's features.

Quoted 7 lines
Quoted 5 lines

Correct the issue by passing the CLANG_CROSS_FLAGS variable to the feature detection makefile.

Fixes cebdb7374577 ("tools: Help cross-building with clang")

Waking up the dead!

Arnaldo, Jean-Philippe: I'm still stung by what this patch fixes. LMK what you need from me/this patch to pick it up.

Cheers,
Björn

#3 lore raw
Quoted 22 lines · Björn Töpel
Quoted 20 lines · Jiri Olsa
Quoted 18 lines · Björn Töpel

From: Björn Töpel <[email protected]>

When a tool cross-build has LLVM=1 set, the clang cross-compilation flags are not passed to the feature detection build system. This results in the host's features are detected instead of the targets.

E.g, triggering a cross-build of bpftool:

cd tools/bpf/bpftool
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- LLVM=1

would report the host's, and not the target's features.

Correct the issue by passing the CLANG_CROSS_FLAGS variable to the feature detection makefile.

Fixes cebdb7374577 ("tools: Help cross-building with clang")
Quoted 6 lines
Quoted 1 line

Waking up the dead!

Arnaldo, Jean-Philippe: I'm still stung by what this patch fixes. LMK what you need from me/this patch to pick it up.

I guess the problem is these files don't have a specific tree. Since you mention BPF maybe it should go through the BPF tree, in which case you could resend to the tools/bpf maintainers (and "PATCH bpf" subject prefix)

FWIW the change looks good to me:

#4 lore raw
Quoted 30 lines · Jean-Philippe Brucker
Quoted 22 lines · Björn Töpel
Quoted 20 lines · Jiri Olsa
Quoted 18 lines · Björn Töpel

From: Björn Töpel <[email protected]>

When a tool cross-build has LLVM=1 set, the clang cross-compilation flags are not passed to the feature detection build system. This results in the host's features are detected instead of the targets.

E.g, triggering a cross-build of bpftool:

cd tools/bpf/bpftool
make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- LLVM=1

would report the host's, and not the target's features.

Correct the issue by passing the CLANG_CROSS_FLAGS variable to the feature detection makefile.

Fixes cebdb7374577 ("tools: Help cross-building with clang")
Quoted 6 lines
Quoted 1 line

Waking up the dead!

Arnaldo, Jean-Philippe: I'm still stung by what this patch fixes. LMK what you need from me/this patch to pick it up.

Quoted 7 lines

I guess the problem is these files don't have a specific tree. Since you mention BPF maybe it should go through the BPF tree, in which case you could resend to the tools/bpf maintainers (and "PATCH bpf" subject prefix)

FWIW the change looks good to me:

Thanks for the review. Yes, maybe it's a orphan files issue! It does seem a bit weird to route it via BPF, since it's just a regular cross-build fix -- Not directly related to BPF. Regardless; I'll do a respin targetted at *some* tree. ;-)

Björn