linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang
@ 2021-12-16 16:38 Jean-Philippe Brucker
  2021-12-16 16:38 ` [PATCH bpf-next v2 1/6] tools: Help " Jean-Philippe Brucker
                   ` (6 more replies)
  0 siblings, 7 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-12-16 16:38 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, quentin,
	bpf, linux-kselftest, llvm, Jean-Philippe Brucker

Since v1 [1], I added Quentin's acks and applied Andrii's suggestions:
* Pass CFLAGS to libbpf link in patch 3
* Substitute CLANG_CROSS_FLAGS whole in HOST_CFLAGS to avoid accidents,
  patch 4

Add support for cross-building BPF tools and selftests with clang, by
passing LLVM=1 or CC=clang to make, as well as CROSS_COMPILE. A single
clang toolchain can generate binaries for multiple architectures, so
instead of having prefixes such as aarch64-linux-gnu-gcc, clang uses the
-target parameter: `clang -target aarch64-linux-gnu'.

Patch 1 adds the parameter in Makefile.include so tools can easily
support this. Patch 2 prepares for the libbpf change from patch 3 (keep
building resolve_btfids's libbpf in the host arch, when cross-building
the kernel with clang). Patches 3-6 enable cross-building BPF tools with
clang.

[1] https://lore.kernel.org/bpf/20211122192019.1277299-1-jean-philippe@linaro.org/

Jean-Philippe Brucker (6):
  tools: Help cross-building with clang
  tools/resolve_btfids: Support cross-building the kernel with clang
  tools/libbpf: Enable cross-building with clang
  bpftool: Enable cross-building with clang
  tools/runqslower: Enable cross-building with clang
  selftests/bpf: Enable cross-building with clang

 tools/bpf/bpftool/Makefile           | 13 +++++++------
 tools/bpf/resolve_btfids/Makefile    |  1 +
 tools/bpf/runqslower/Makefile        |  4 ++--
 tools/lib/bpf/Makefile               |  3 ++-
 tools/scripts/Makefile.include       | 13 ++++++++++++-
 tools/testing/selftests/bpf/Makefile |  8 ++++----
 6 files changed, 28 insertions(+), 14 deletions(-)

-- 
2.34.1


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

* [PATCH bpf-next v2 1/6] tools: Help cross-building with clang
  2021-12-16 16:38 [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
@ 2021-12-16 16:38 ` Jean-Philippe Brucker
  2021-12-16 19:16   ` Nick Desaulniers
  2022-01-29  1:10   ` Nathan Chancellor
  2021-12-16 16:38 ` [PATCH bpf-next v2 2/6] tools/resolve_btfids: Support cross-building the kernel " Jean-Philippe Brucker
                   ` (5 subsequent siblings)
  6 siblings, 2 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-12-16 16:38 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, quentin,
	bpf, linux-kselftest, llvm, Jean-Philippe Brucker

Cross-compilation with clang uses the -target parameter rather than a
toolchain prefix. Just like the kernel Makefile, add that parameter to
CFLAGS when CROSS_COMPILE is set.

Unlike the kernel Makefile, we use the --sysroot and --gcc-toolchain
options because unlike the kernel, tools require standard libraries.
Commit c91d4e47e10e ("Makefile: Remove '--gcc-toolchain' flag") provides
some background about --gcc-toolchain. Normally clang finds on its own
the additional utilities and libraries that it needs (for example GNU ld
or glibc). On some systems however, this autodetection doesn't work.
There, our only recourse is asking GCC directly, and pass the result to
--sysroot and --gcc-toolchain. Of course that only works when a cross
GCC is available.

Autodetection worked fine on Debian, but to use the aarch64-linux-gnu
toolchain from Archlinux I needed both --sysroot (for crt1.o) and
--gcc-toolchain (for crtbegin.o, -lgcc). The --prefix parameter wasn't
needed there, but it might be useful on other distributions.

Use the CLANG_CROSS_FLAGS variable instead of CLANG_FLAGS because it
allows tools such as bpftool, that need to build both host and target
binaries, to easily filter out the cross-build flags from CFLAGS.

Acked-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
Most tools I looked at needed additional changes to support cross-build
with clang. I've only done the work for bpf tools.
---
 tools/scripts/Makefile.include | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include
index 071312f5eb92..b0be5f40a3f1 100644
--- a/tools/scripts/Makefile.include
+++ b/tools/scripts/Makefile.include
@@ -87,7 +87,18 @@ LLVM_STRIP	?= llvm-strip
 
 ifeq ($(CC_NO_CLANG), 1)
 EXTRA_WARNINGS += -Wstrict-aliasing=3
-endif
+
+else ifneq ($(CROSS_COMPILE),)
+CLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
+GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc))
+ifneq ($(GCC_TOOLCHAIN_DIR),)
+CLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE))
+CLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot)
+CLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..)
+endif # GCC_TOOLCHAIN_DIR
+CFLAGS += $(CLANG_CROSS_FLAGS)
+AFLAGS += $(CLANG_CROSS_FLAGS)
+endif # CROSS_COMPILE
 
 # Hack to avoid type-punned warnings on old systems such as RHEL5:
 # We should be changing CFLAGS and checking gcc version, but this
-- 
2.34.1


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

* [PATCH bpf-next v2 2/6] tools/resolve_btfids: Support cross-building the kernel with clang
  2021-12-16 16:38 [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
  2021-12-16 16:38 ` [PATCH bpf-next v2 1/6] tools: Help " Jean-Philippe Brucker
@ 2021-12-16 16:38 ` Jean-Philippe Brucker
  2021-12-16 16:38 ` [PATCH bpf-next v2 3/6] tools/libbpf: Enable cross-building " Jean-Philippe Brucker
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-12-16 16:38 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, quentin,
	bpf, linux-kselftest, llvm, Jean-Philippe Brucker

The CROSS_COMPILE variable may be present during resolve_btfids build if
the kernel is being cross-built. Since resolve_btfids is always executed
on the host, we set CC to HOSTCC in order to use the host toolchain when
cross-building with GCC. But instead of a toolchain prefix, cross-build
with clang uses a "-target" parameter, which Makefile.include deduces
from the CROSS_COMPILE variable. In order to avoid cross-building
libbpf, clear CROSS_COMPILE before building resolve_btfids.

Acked-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 tools/bpf/resolve_btfids/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/bpf/resolve_btfids/Makefile b/tools/bpf/resolve_btfids/Makefile
index 751643f860b2..9ddeca947635 100644
--- a/tools/bpf/resolve_btfids/Makefile
+++ b/tools/bpf/resolve_btfids/Makefile
@@ -19,6 +19,7 @@ CC       = $(HOSTCC)
 LD       = $(HOSTLD)
 ARCH     = $(HOSTARCH)
 RM      ?= rm
+CROSS_COMPILE =
 
 OUTPUT ?= $(srctree)/tools/bpf/resolve_btfids/
 
-- 
2.34.1


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

* [PATCH bpf-next v2 3/6] tools/libbpf: Enable cross-building with clang
  2021-12-16 16:38 [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
  2021-12-16 16:38 ` [PATCH bpf-next v2 1/6] tools: Help " Jean-Philippe Brucker
  2021-12-16 16:38 ` [PATCH bpf-next v2 2/6] tools/resolve_btfids: Support cross-building the kernel " Jean-Philippe Brucker
@ 2021-12-16 16:38 ` Jean-Philippe Brucker
  2021-12-16 16:38 ` [PATCH bpf-next v2 4/6] bpftool: " Jean-Philippe Brucker
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-12-16 16:38 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, quentin,
	bpf, linux-kselftest, llvm, Jean-Philippe Brucker

Cross-building using clang requires passing the "-target" flag rather
than using the CROSS_COMPILE prefix. Makefile.include transforms
CROSS_COMPILE into CLANG_CROSS_FLAGS. Add them to the CFLAGS.

Acked-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 tools/lib/bpf/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile
index 5f7086fae31c..f947b61b2107 100644
--- a/tools/lib/bpf/Makefile
+++ b/tools/lib/bpf/Makefile
@@ -90,6 +90,7 @@ override CFLAGS += -Werror -Wall
 override CFLAGS += $(INCLUDES)
 override CFLAGS += -fvisibility=hidden
 override CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
+override CFLAGS += $(CLANG_CROSS_FLAGS)
 
 # flags specific for shared library
 SHLIB_FLAGS := -DSHARED -fPIC
@@ -162,7 +163,7 @@ $(BPF_HELPER_DEFS): $(srctree)/tools/include/uapi/linux/bpf.h
 $(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
 
 $(OUTPUT)libbpf.so.$(LIBBPF_VERSION): $(BPF_IN_SHARED) $(VERSION_SCRIPT)
-	$(QUIET_LINK)$(CC) $(LDFLAGS) \
+	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) \
 		--shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
 		-Wl,--version-script=$(VERSION_SCRIPT) $< -lelf -lz -o $@
 	@ln -sf $(@F) $(OUTPUT)libbpf.so
-- 
2.34.1


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

* [PATCH bpf-next v2 4/6] bpftool: Enable cross-building with clang
  2021-12-16 16:38 [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
                   ` (2 preceding siblings ...)
  2021-12-16 16:38 ` [PATCH bpf-next v2 3/6] tools/libbpf: Enable cross-building " Jean-Philippe Brucker
@ 2021-12-16 16:38 ` Jean-Philippe Brucker
  2021-12-16 16:38 ` [PATCH bpf-next v2 5/6] tools/runqslower: " Jean-Philippe Brucker
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-12-16 16:38 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, quentin,
	bpf, linux-kselftest, llvm, Jean-Philippe Brucker

Cross-building using clang requires passing the "-target" flag rather
than using the CROSS_COMPILE prefix. Makefile.include transforms
CROSS_COMPILE into CLANG_CROSS_FLAGS, and adds that to CFLAGS. Remove
the cross flags for the bootstrap bpftool, and erase the CROSS_COMPILE
flag for the bootstrap libbpf.

Acked-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 tools/bpf/bpftool/Makefile | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile
index 42eb8eee3d89..83369f55df61 100644
--- a/tools/bpf/bpftool/Makefile
+++ b/tools/bpf/bpftool/Makefile
@@ -57,7 +57,7 @@ $(LIBBPF_INTERNAL_HDRS): $(LIBBPF_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_HDRS_
 $(LIBBPF_BOOTSTRAP): $(wildcard $(BPF_DIR)/*.[ch] $(BPF_DIR)/Makefile) | $(LIBBPF_BOOTSTRAP_OUTPUT)
 	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
 		DESTDIR=$(LIBBPF_BOOTSTRAP_DESTDIR) prefix= \
-		ARCH= CC=$(HOSTCC) LD=$(HOSTLD) $@ install_headers
+		ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) $@ install_headers
 
 $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS): $(LIBBPF_BOOTSTRAP_HDRS_DIR)/%.h: $(BPF_DIR)/%.h | $(LIBBPF_BOOTSTRAP_HDRS_DIR)
 	$(call QUIET_INSTALL, $@)
@@ -152,6 +152,9 @@ CFLAGS += -DHAVE_LIBBFD_SUPPORT
 SRCS += $(BFD_SRCS)
 endif
 
+HOST_CFLAGS = $(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),\
+		$(subst $(CLANG_CROSS_FLAGS),,$(CFLAGS)))
+
 BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
 
 BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o xlated_dumper.o btf_dumper.o disasm.o)
@@ -202,7 +205,7 @@ endif
 CFLAGS += $(if $(BUILD_BPF_SKELS),,-DBPFTOOL_WITHOUT_SKELETONS)
 
 $(BOOTSTRAP_OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
-	$(QUIET_CC)$(HOSTCC) $(CFLAGS) -c -MMD $< -o $@
+	$(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
 
 $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
 	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
@@ -213,15 +216,13 @@ ifneq ($(feature-zlib), 1)
 endif
 
 $(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
-	$(QUIET_LINK)$(HOSTCC) $(CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
+	$(QUIET_LINK)$(HOSTCC) $(HOST_CFLAGS) $(LDFLAGS) $(BOOTSTRAP_OBJS) $(LIBS_BOOTSTRAP) -o $@
 
 $(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
 	$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
 
 $(BOOTSTRAP_OUTPUT)%.o: %.c $(LIBBPF_BOOTSTRAP_INTERNAL_HDRS) | $(BOOTSTRAP_OUTPUT)
-	$(QUIET_CC)$(HOSTCC) \
-		$(subst -I$(LIBBPF_INCLUDE),-I$(LIBBPF_BOOTSTRAP_INCLUDE),$(CFLAGS)) \
-		-c -MMD $< -o $@
+	$(QUIET_CC)$(HOSTCC) $(HOST_CFLAGS) -c -MMD $< -o $@
 
 $(OUTPUT)%.o: %.c
 	$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD $< -o $@
-- 
2.34.1


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

* [PATCH bpf-next v2 5/6] tools/runqslower: Enable cross-building with clang
  2021-12-16 16:38 [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
                   ` (3 preceding siblings ...)
  2021-12-16 16:38 ` [PATCH bpf-next v2 4/6] bpftool: " Jean-Philippe Brucker
@ 2021-12-16 16:38 ` Jean-Philippe Brucker
  2021-12-16 16:38 ` [PATCH bpf-next v2 6/6] selftests/bpf: " Jean-Philippe Brucker
  2021-12-16 20:32 ` [PATCH bpf-next v2 0/6] tools/bpf: " patchwork-bot+netdevbpf
  6 siblings, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-12-16 16:38 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, quentin,
	bpf, linux-kselftest, llvm, Jean-Philippe Brucker

Cross-building using clang requires passing the "-target" flag rather
than using the CROSS_COMPILE prefix. Makefile.include transforms
CROSS_COMPILE into CLANG_CROSS_FLAGS. Add them to CFLAGS, and erase
CROSS_COMPILE for the bpftool build, since it needs to be executed on
the host.

Acked-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 tools/bpf/runqslower/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/bpf/runqslower/Makefile b/tools/bpf/runqslower/Makefile
index 8791d0e2762b..da6de16a3dfb 100644
--- a/tools/bpf/runqslower/Makefile
+++ b/tools/bpf/runqslower/Makefile
@@ -12,7 +12,7 @@ BPFOBJ := $(BPFOBJ_OUTPUT)libbpf.a
 BPF_DESTDIR := $(BPFOBJ_OUTPUT)
 BPF_INCLUDE := $(BPF_DESTDIR)/include
 INCLUDES := -I$(OUTPUT) -I$(BPF_INCLUDE) -I$(abspath ../../include/uapi)
-CFLAGS := -g -Wall
+CFLAGS := -g -Wall $(CLANG_CROSS_FLAGS)
 
 # Try to detect best kernel BTF source
 KERNEL_REL := $(shell uname -r)
@@ -88,4 +88,4 @@ $(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(BPFOBJ_OU
 
 $(DEFAULT_BPFTOOL): $(BPFOBJ) | $(BPFTOOL_OUTPUT)
 	$(Q)$(MAKE) $(submake_extras) -C ../bpftool OUTPUT=$(BPFTOOL_OUTPUT)   \
-		    CC=$(HOSTCC) LD=$(HOSTLD)
+		    ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD)
-- 
2.34.1


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

* [PATCH bpf-next v2 6/6] selftests/bpf: Enable cross-building with clang
  2021-12-16 16:38 [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
                   ` (4 preceding siblings ...)
  2021-12-16 16:38 ` [PATCH bpf-next v2 5/6] tools/runqslower: " Jean-Philippe Brucker
@ 2021-12-16 16:38 ` Jean-Philippe Brucker
  2021-12-16 20:32 ` [PATCH bpf-next v2 0/6] tools/bpf: " patchwork-bot+netdevbpf
  6 siblings, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-12-16 16:38 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, quentin,
	bpf, linux-kselftest, llvm, Jean-Philippe Brucker

Cross building using clang requires passing the "-target" flag rather
than using the CROSS_COMPILE prefix. Makefile.include transforms
CROSS_COMPILE into CLANG_CROSS_FLAGS. Clear CROSS_COMPILE for bpftool
and the host libbpf, and use the clang flags for urandom_read and bench.

Acked-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
 tools/testing/selftests/bpf/Makefile | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 43f6f3159431..42ffc24e9e71 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -170,7 +170,7 @@ $(OUTPUT)/%:%.c
 
 $(OUTPUT)/urandom_read: urandom_read.c
 	$(call msg,BINARY,,$@)
-	$(Q)$(CC) $(LDFLAGS) $< $(LDLIBS) -Wl,--build-id=sha1 -o $@
+	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $< $(LDLIBS) -Wl,--build-id=sha1 -o $@
 
 $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_testmod/*.[ch])
 	$(call msg,MOD,,$@)
@@ -217,7 +217,7 @@ BPFTOOL ?= $(DEFAULT_BPFTOOL)
 $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
 		    $(HOST_BPFOBJ) | $(HOST_BUILD_DIR)/bpftool
 	$(Q)$(MAKE) $(submake_extras)  -C $(BPFTOOLDIR)			       \
-		    CC=$(HOSTCC) LD=$(HOSTLD)				       \
+		    ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) 	       \
 		    EXTRA_CFLAGS='-g -O0'				       \
 		    OUTPUT=$(HOST_BUILD_DIR)/bpftool/			       \
 		    LIBBPF_OUTPUT=$(HOST_BUILD_DIR)/libbpf/		       \
@@ -248,7 +248,7 @@ $(HOST_BPFOBJ): $(wildcard $(BPFDIR)/*.[ch] $(BPFDIR)/Makefile)		       \
 		$(APIDIR)/linux/bpf.h					       \
 		| $(HOST_BUILD_DIR)/libbpf
 	$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR)                             \
-		    EXTRA_CFLAGS='-g -O0'				       \
+		    EXTRA_CFLAGS='-g -O0' ARCH= CROSS_COMPILE=		       \
 		    OUTPUT=$(HOST_BUILD_DIR)/libbpf/ CC=$(HOSTCC) LD=$(HOSTLD) \
 		    DESTDIR=$(HOST_SCRATCH_DIR)/ prefix= all install_headers
 endif
@@ -551,7 +551,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \
 		 $(OUTPUT)/bench_bpf_loop.o \
 		 $(OUTPUT)/bench_strncmp.o
 	$(call msg,BINARY,,$@)
-	$(Q)$(CC) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
+	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
 
 EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) $(HOST_SCRATCH_DIR)	\
 	prog_tests/tests.h map_tests/tests.h verifier/tests.h		\
-- 
2.34.1


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

* Re: [PATCH bpf-next v2 1/6] tools: Help cross-building with clang
  2021-12-16 16:38 ` [PATCH bpf-next v2 1/6] tools: Help " Jean-Philippe Brucker
@ 2021-12-16 19:16   ` Nick Desaulniers
  2022-01-29  1:10   ` Nathan Chancellor
  1 sibling, 0 replies; 12+ messages in thread
From: Nick Desaulniers @ 2021-12-16 19:16 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: ast, daniel, andrii, shuah, nathan, kafai, songliubraving, yhs,
	john.fastabend, kpsingh, quentin, bpf, linux-kselftest, llvm

On Thu, Dec 16, 2021 at 8:50 AM Jean-Philippe Brucker
<jean-philippe@linaro.org> wrote:
>
> Cross-compilation with clang uses the -target parameter rather than a
> toolchain prefix. Just like the kernel Makefile, add that parameter to
> CFLAGS when CROSS_COMPILE is set.
>
> Unlike the kernel Makefile, we use the --sysroot and --gcc-toolchain
> options because unlike the kernel, tools require standard libraries.
> Commit c91d4e47e10e ("Makefile: Remove '--gcc-toolchain' flag") provides
> some background about --gcc-toolchain. Normally clang finds on its own
> the additional utilities and libraries that it needs (for example GNU ld
> or glibc). On some systems however, this autodetection doesn't work.
> There, our only recourse is asking GCC directly, and pass the result to
> --sysroot and --gcc-toolchain. Of course that only works when a cross
> GCC is available.
>
> Autodetection worked fine on Debian, but to use the aarch64-linux-gnu
> toolchain from Archlinux I needed both --sysroot (for crt1.o) and
> --gcc-toolchain (for crtbegin.o, -lgcc). The --prefix parameter wasn't
> needed there, but it might be useful on other distributions.
>
> Use the CLANG_CROSS_FLAGS variable instead of CLANG_FLAGS because it
> allows tools such as bpftool, that need to build both host and target
> binaries, to easily filter out the cross-build flags from CFLAGS.
>
> Acked-by: Quentin Monnet <quentin@isovalent.com>
> Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
> ---
> Most tools I looked at needed additional changes to support cross-build
> with clang. I've only done the work for bpf tools.
> ---
>  tools/scripts/Makefile.include | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include
> index 071312f5eb92..b0be5f40a3f1 100644
> --- a/tools/scripts/Makefile.include
> +++ b/tools/scripts/Makefile.include
> @@ -87,7 +87,18 @@ LLVM_STRIP   ?= llvm-strip
>
>  ifeq ($(CC_NO_CLANG), 1)
>  EXTRA_WARNINGS += -Wstrict-aliasing=3
> -endif
> +
> +else ifneq ($(CROSS_COMPILE),)
> +CLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
> +GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc))
> +ifneq ($(GCC_TOOLCHAIN_DIR),)
> +CLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE))
> +CLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot)

^ this is a neat trick; it does rely on having $(CROSS_COMPILE)gcc
installed though. Technically only the header include path is needed
and the path to the cross libc shared object.  I think we should take
this patch&series as a starting point, but there might be future
revisions here to support environments that don't have
$(CROSS_COMPILE)gcc (such as Android's build env).

Acked-by: Nick Desaulniers <ndesaulniers@google.com>

Thanks for the series!

> +CLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..)
> +endif # GCC_TOOLCHAIN_DIR
> +CFLAGS += $(CLANG_CROSS_FLAGS)
> +AFLAGS += $(CLANG_CROSS_FLAGS)
> +endif # CROSS_COMPILE
>
>  # Hack to avoid type-punned warnings on old systems such as RHEL5:
>  # We should be changing CFLAGS and checking gcc version, but this
> --
> 2.34.1
>


-- 
Thanks,
~Nick Desaulniers

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

* Re: [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang
  2021-12-16 16:38 [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
                   ` (5 preceding siblings ...)
  2021-12-16 16:38 ` [PATCH bpf-next v2 6/6] selftests/bpf: " Jean-Philippe Brucker
@ 2021-12-16 20:32 ` patchwork-bot+netdevbpf
  6 siblings, 0 replies; 12+ messages in thread
From: patchwork-bot+netdevbpf @ 2021-12-16 20:32 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: ast, daniel, andrii, shuah, nathan, ndesaulniers, kafai,
	songliubraving, yhs, john.fastabend, kpsingh, quentin, bpf,
	linux-kselftest, llvm

Hello:

This series was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu, 16 Dec 2021 16:38:37 +0000 you wrote:
> Since v1 [1], I added Quentin's acks and applied Andrii's suggestions:
> * Pass CFLAGS to libbpf link in patch 3
> * Substitute CLANG_CROSS_FLAGS whole in HOST_CFLAGS to avoid accidents,
>   patch 4
> 
> Add support for cross-building BPF tools and selftests with clang, by
> passing LLVM=1 or CC=clang to make, as well as CROSS_COMPILE. A single
> clang toolchain can generate binaries for multiple architectures, so
> instead of having prefixes such as aarch64-linux-gnu-gcc, clang uses the
> -target parameter: `clang -target aarch64-linux-gnu'.
> 
> [...]

Here is the summary with links:
  - [bpf-next,v2,1/6] tools: Help cross-building with clang
    https://git.kernel.org/bpf/bpf-next/c/cebdb7374577
  - [bpf-next,v2,2/6] tools/resolve_btfids: Support cross-building the kernel with clang
    https://git.kernel.org/bpf/bpf-next/c/bf1be903461a
  - [bpf-next,v2,3/6] tools/libbpf: Enable cross-building with clang
    https://git.kernel.org/bpf/bpf-next/c/4980beb4cda2
  - [bpf-next,v2,4/6] bpftool: Enable cross-building with clang
    https://git.kernel.org/bpf/bpf-next/c/bdadbb44c90a
  - [bpf-next,v2,5/6] tools/runqslower: Enable cross-building with clang
    https://git.kernel.org/bpf/bpf-next/c/bb7b75e860ee
  - [bpf-next,v2,6/6] selftests/bpf: Enable cross-building with clang
    https://git.kernel.org/bpf/bpf-next/c/ea79020a2d9e

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

* Re: [PATCH bpf-next v2 1/6] tools: Help cross-building with clang
  2021-12-16 16:38 ` [PATCH bpf-next v2 1/6] tools: Help " Jean-Philippe Brucker
  2021-12-16 19:16   ` Nick Desaulniers
@ 2022-01-29  1:10   ` Nathan Chancellor
  2022-01-31 14:10     ` Jean-Philippe Brucker
  1 sibling, 1 reply; 12+ messages in thread
From: Nathan Chancellor @ 2022-01-29  1:10 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: ast, daniel, andrii, shuah, ndesaulniers, kafai, songliubraving,
	yhs, john.fastabend, kpsingh, quentin, bpf, linux-kselftest,
	llvm

Hi Jean-Philippe,

On Thu, Dec 16, 2021 at 04:38:38PM +0000, Jean-Philippe Brucker wrote:
> Cross-compilation with clang uses the -target parameter rather than a
> toolchain prefix. Just like the kernel Makefile, add that parameter to
> CFLAGS when CROSS_COMPILE is set.
> 
> Unlike the kernel Makefile, we use the --sysroot and --gcc-toolchain
> options because unlike the kernel, tools require standard libraries.
> Commit c91d4e47e10e ("Makefile: Remove '--gcc-toolchain' flag") provides
> some background about --gcc-toolchain. Normally clang finds on its own
> the additional utilities and libraries that it needs (for example GNU ld
> or glibc). On some systems however, this autodetection doesn't work.
> There, our only recourse is asking GCC directly, and pass the result to
> --sysroot and --gcc-toolchain. Of course that only works when a cross
> GCC is available.
> 
> Autodetection worked fine on Debian, but to use the aarch64-linux-gnu
> toolchain from Archlinux I needed both --sysroot (for crt1.o) and
> --gcc-toolchain (for crtbegin.o, -lgcc). The --prefix parameter wasn't
> needed there, but it might be useful on other distributions.
> 
> Use the CLANG_CROSS_FLAGS variable instead of CLANG_FLAGS because it
> allows tools such as bpftool, that need to build both host and target
> binaries, to easily filter out the cross-build flags from CFLAGS.
> 
> Acked-by: Quentin Monnet <quentin@isovalent.com>
> Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
> ---
> Most tools I looked at needed additional changes to support cross-build
> with clang. I've only done the work for bpf tools.
> ---
>  tools/scripts/Makefile.include | 13 ++++++++++++-
>  1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include
> index 071312f5eb92..b0be5f40a3f1 100644
> --- a/tools/scripts/Makefile.include
> +++ b/tools/scripts/Makefile.include
> @@ -87,7 +87,18 @@ LLVM_STRIP	?= llvm-strip
>  
>  ifeq ($(CC_NO_CLANG), 1)
>  EXTRA_WARNINGS += -Wstrict-aliasing=3
> -endif
> +
> +else ifneq ($(CROSS_COMPILE),)
> +CLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
> +GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc))

Apologies for noticing this so late, I only ran into this recently.

This line causes a warning when running 'make clean' when
'$(CROSS_COMPILE)gcc' does not exist in PATH. For example:

$ make -skj"$(nproc)" ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- LLVM=1 LLVM_IAS=0 clean
which: no powerpc-linux-gnu-gcc in ($PATH)

I only have powerpc-linux-gnu binutils in my PATH, not GCC, as I am only
working with clang.

This happens because of the 'resolve_btfids_clean target', which always
runs when running the 'clean' target on an in-tree build (since
$(objtree) = $(srctree)).

I tried looking into the best way to fix this but I am not at all
familiar with the tools/ build system; would you mind taking a look?
I see some machinery at the top of tools/bpf/Makefile for avoiding
running some commands under certain commands but I am unsure how to
shuffle that around to make everything work.

Cheers,
Nathan

> +ifneq ($(GCC_TOOLCHAIN_DIR),)
> +CLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE))
> +CLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot)
> +CLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..)
> +endif # GCC_TOOLCHAIN_DIR
> +CFLAGS += $(CLANG_CROSS_FLAGS)
> +AFLAGS += $(CLANG_CROSS_FLAGS)
> +endif # CROSS_COMPILE
>  
>  # Hack to avoid type-punned warnings on old systems such as RHEL5:
>  # We should be changing CFLAGS and checking gcc version, but this
> -- 
> 2.34.1
> 
> 

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

* Re: [PATCH bpf-next v2 1/6] tools: Help cross-building with clang
  2022-01-29  1:10   ` Nathan Chancellor
@ 2022-01-31 14:10     ` Jean-Philippe Brucker
  2022-01-31 17:04       ` Nathan Chancellor
  0 siblings, 1 reply; 12+ messages in thread
From: Jean-Philippe Brucker @ 2022-01-31 14:10 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: ast, daniel, andrii, shuah, ndesaulniers, kafai, songliubraving,
	yhs, john.fastabend, kpsingh, quentin, bpf, linux-kselftest,
	llvm

Hi Nathan,

On Fri, Jan 28, 2022 at 06:10:24PM -0700, Nathan Chancellor wrote:
> > diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include
> > index 071312f5eb92..b0be5f40a3f1 100644
> > --- a/tools/scripts/Makefile.include
> > +++ b/tools/scripts/Makefile.include
> > @@ -87,7 +87,18 @@ LLVM_STRIP	?= llvm-strip
> >  
> >  ifeq ($(CC_NO_CLANG), 1)
> >  EXTRA_WARNINGS += -Wstrict-aliasing=3
> > -endif
> > +
> > +else ifneq ($(CROSS_COMPILE),)
> > +CLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
> > +GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc))
> 
> Apologies for noticing this so late, I only ran into this recently.
> 
> This line causes a warning when running 'make clean' when
> '$(CROSS_COMPILE)gcc' does not exist in PATH. For example:
> 
> $ make -skj"$(nproc)" ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- LLVM=1 LLVM_IAS=0 clean
> which: no powerpc-linux-gnu-gcc in ($PATH)
> 
> I only have powerpc-linux-gnu binutils in my PATH, not GCC, as I am only
> working with clang.
> 
> This happens because of the 'resolve_btfids_clean target', which always
> runs when running the 'clean' target on an in-tree build (since
> $(objtree) = $(srctree)).
> 
> I tried looking into the best way to fix this but I am not at all
> familiar with the tools/ build system; would you mind taking a look?
> I see some machinery at the top of tools/bpf/Makefile for avoiding
> running some commands under certain commands but I am unsure how to
> shuffle that around to make everything work.

I think it's simpler than that, we should just suppress the errors from
'which'. It's fine that $(CROSS_COMPILE)gcc doesn't exist and
$(GCC_TOOLCHAIN_DIR) is empty, but 'which' should keep quiet about it.
I did test this patch with cross-build and no gcc, but Debian's 'which' is
quiet by default so I missed the error. I'll send a fix shortly.

Thanks,
Jean


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

* Re: [PATCH bpf-next v2 1/6] tools: Help cross-building with clang
  2022-01-31 14:10     ` Jean-Philippe Brucker
@ 2022-01-31 17:04       ` Nathan Chancellor
  0 siblings, 0 replies; 12+ messages in thread
From: Nathan Chancellor @ 2022-01-31 17:04 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: ast, daniel, andrii, shuah, ndesaulniers, kafai, songliubraving,
	yhs, john.fastabend, kpsingh, quentin, bpf, linux-kselftest,
	llvm

On Mon, Jan 31, 2022 at 02:10:30PM +0000, Jean-Philippe Brucker wrote:
> Hi Nathan,
> 
> On Fri, Jan 28, 2022 at 06:10:24PM -0700, Nathan Chancellor wrote:
> > > diff --git a/tools/scripts/Makefile.include b/tools/scripts/Makefile.include
> > > index 071312f5eb92..b0be5f40a3f1 100644
> > > --- a/tools/scripts/Makefile.include
> > > +++ b/tools/scripts/Makefile.include
> > > @@ -87,7 +87,18 @@ LLVM_STRIP	?= llvm-strip
> > >  
> > >  ifeq ($(CC_NO_CLANG), 1)
> > >  EXTRA_WARNINGS += -Wstrict-aliasing=3
> > > -endif
> > > +
> > > +else ifneq ($(CROSS_COMPILE),)
> > > +CLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
> > > +GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc))
> > 
> > Apologies for noticing this so late, I only ran into this recently.
> > 
> > This line causes a warning when running 'make clean' when
> > '$(CROSS_COMPILE)gcc' does not exist in PATH. For example:
> > 
> > $ make -skj"$(nproc)" ARCH=powerpc CROSS_COMPILE=powerpc-linux-gnu- LLVM=1 LLVM_IAS=0 clean
> > which: no powerpc-linux-gnu-gcc in ($PATH)
> > 
> > I only have powerpc-linux-gnu binutils in my PATH, not GCC, as I am only
> > working with clang.
> > 
> > This happens because of the 'resolve_btfids_clean target', which always
> > runs when running the 'clean' target on an in-tree build (since
> > $(objtree) = $(srctree)).
> > 
> > I tried looking into the best way to fix this but I am not at all
> > familiar with the tools/ build system; would you mind taking a look?
> > I see some machinery at the top of tools/bpf/Makefile for avoiding
> > running some commands under certain commands but I am unsure how to
> > shuffle that around to make everything work.
> 
> I think it's simpler than that, we should just suppress the errors from
> 'which'. It's fine that $(CROSS_COMPILE)gcc doesn't exist and
> $(GCC_TOOLCHAIN_DIR) is empty, but 'which' should keep quiet about it.

Ha, that's what I get for looking into something at the very end of a
long work week because that is a great and simple solution I probably
would not have seen :)

> I did test this patch with cross-build and no gcc, but Debian's 'which' is
> quiet by default so I missed the error. I'll send a fix shortly.

Ah, that is an interesting observation. TIL that Debian rolls their own
which [1], versus most other distros, which use GNU which [2].

I will keep an eye out for your patch so I can review it.

[1]: https://salsa.debian.org/debian/debianutils/-/blob/de14223e5bffe15e374a441302c528ffc1cbed57/which
[2]: https://savannah.gnu.org/projects/which/

Cheers,
Nathan

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

end of thread, other threads:[~2022-01-31 17:05 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-16 16:38 [PATCH bpf-next v2 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
2021-12-16 16:38 ` [PATCH bpf-next v2 1/6] tools: Help " Jean-Philippe Brucker
2021-12-16 19:16   ` Nick Desaulniers
2022-01-29  1:10   ` Nathan Chancellor
2022-01-31 14:10     ` Jean-Philippe Brucker
2022-01-31 17:04       ` Nathan Chancellor
2021-12-16 16:38 ` [PATCH bpf-next v2 2/6] tools/resolve_btfids: Support cross-building the kernel " Jean-Philippe Brucker
2021-12-16 16:38 ` [PATCH bpf-next v2 3/6] tools/libbpf: Enable cross-building " Jean-Philippe Brucker
2021-12-16 16:38 ` [PATCH bpf-next v2 4/6] bpftool: " Jean-Philippe Brucker
2021-12-16 16:38 ` [PATCH bpf-next v2 5/6] tools/runqslower: " Jean-Philippe Brucker
2021-12-16 16:38 ` [PATCH bpf-next v2 6/6] selftests/bpf: " Jean-Philippe Brucker
2021-12-16 20:32 ` [PATCH bpf-next v2 0/6] tools/bpf: " patchwork-bot+netdevbpf

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