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

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.

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


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

* [PATCH bpf-next 1/6] tools: Help cross-building with clang
  2021-11-22 19:20 [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
@ 2021-11-22 19:20 ` Jean-Philippe Brucker
  2021-11-22 19:20 ` [PATCH bpf-next 2/6] tools/resolve_btfids: Support cross-building the kernel " Jean-Philippe Brucker
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-11-22 19:20 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, 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.

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


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

* [PATCH bpf-next 2/6] tools/resolve_btfids: Support cross-building the kernel with clang
  2021-11-22 19:20 [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
  2021-11-22 19:20 ` [PATCH bpf-next 1/6] tools: Help " Jean-Philippe Brucker
@ 2021-11-22 19:20 ` Jean-Philippe Brucker
  2021-11-22 19:20 ` [PATCH bpf-next 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-11-22 19:20 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, 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.

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


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

* [PATCH bpf-next 3/6] tools/libbpf: Enable cross-building with clang
  2021-11-22 19:20 [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
  2021-11-22 19:20 ` [PATCH bpf-next 1/6] tools: Help " Jean-Philippe Brucker
  2021-11-22 19:20 ` [PATCH bpf-next 2/6] tools/resolve_btfids: Support cross-building the kernel " Jean-Philippe Brucker
@ 2021-11-22 19:20 ` Jean-Philippe Brucker
  2021-11-29 23:51   ` Andrii Nakryiko
  2021-11-22 19:20 ` [PATCH bpf-next 4/6] bpftool: " Jean-Philippe Brucker
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-11-22 19:20 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, 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.

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..fe9201862aed 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) $(CLANG_CROSS_FLAGS) $(LDFLAGS) \
 		--shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
 		-Wl,--version-script=$(VERSION_SCRIPT) $< -lelf -lz -o $@
 	@ln -sf $(@F) $(OUTPUT)libbpf.so
-- 
2.33.1


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

* [PATCH bpf-next 4/6] bpftool: Enable cross-building with clang
  2021-11-22 19:20 [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
                   ` (2 preceding siblings ...)
  2021-11-22 19:20 ` [PATCH bpf-next 3/6] tools/libbpf: Enable cross-building " Jean-Philippe Brucker
@ 2021-11-22 19:20 ` Jean-Philippe Brucker
  2021-11-29 23:53   ` Andrii Nakryiko
  2021-11-22 19:20 ` [PATCH bpf-next 5/6] tools/runqslower: " Jean-Philippe Brucker
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-11-22 19:20 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, 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. Filter
out the cross flags for the bootstrap bpftool, and erase the
CROSS_COMPILE flag for the bootstrap libbpf.

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..b0f3e17d981a 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),\
+		$(filter-out $(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.33.1


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

* [PATCH bpf-next 5/6] tools/runqslower: Enable cross-building with clang
  2021-11-22 19:20 [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
                   ` (3 preceding siblings ...)
  2021-11-22 19:20 ` [PATCH bpf-next 4/6] bpftool: " Jean-Philippe Brucker
@ 2021-11-22 19:20 ` Jean-Philippe Brucker
  2021-11-22 19:20 ` [PATCH bpf-next 6/6] selftests/bpf: " Jean-Philippe Brucker
  2021-11-26 20:36 ` [PATCH bpf-next 0/6] tools/bpf: " Quentin Monnet
  6 siblings, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-11-22 19:20 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, 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.

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


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

* [PATCH bpf-next 6/6] selftests/bpf: Enable cross-building with clang
  2021-11-22 19:20 [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
                   ` (4 preceding siblings ...)
  2021-11-22 19:20 ` [PATCH bpf-next 5/6] tools/runqslower: " Jean-Philippe Brucker
@ 2021-11-22 19:20 ` Jean-Philippe Brucker
  2021-11-26 20:36 ` [PATCH bpf-next 0/6] tools/bpf: " Quentin Monnet
  6 siblings, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-11-22 19:20 UTC (permalink / raw)
  To: ast, daniel, andrii, shuah, nathan, ndesaulniers
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, 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.

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 52383fe8ba42..6c678509e949 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,,$@)
@@ -213,7 +213,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/		       \
@@ -244,7 +244,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
@@ -542,7 +542,7 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \
 		 $(OUTPUT)/bench_ringbufs.o \
 		 $(OUTPUT)/bench_bloom_filter_map.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.33.1


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

* Re: [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang
  2021-11-22 19:20 [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
                   ` (5 preceding siblings ...)
  2021-11-22 19:20 ` [PATCH bpf-next 6/6] selftests/bpf: " Jean-Philippe Brucker
@ 2021-11-26 20:36 ` Quentin Monnet
  2021-11-26 21:15   ` Daniel Borkmann
  2021-11-29  9:42   ` Jean-Philippe Brucker
  6 siblings, 2 replies; 12+ messages in thread
From: Quentin Monnet @ 2021-11-26 20:36 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Shuah Khan,
	nathan, ndesaulniers, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, bpf,
	open list:KERNEL SELFTEST FRAMEWORK, llvm

On Mon, 22 Nov 2021 at 19:23, Jean-Philippe Brucker
<jean-philippe@linaro.org> wrote:
>
> 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.

The set looks good to me. I checked that the tools are still building
(without cross-compiling). I currently have issues building the
selftests on my setup, but they don't appear to be related to this
patchset.

Acked-by: Quentin Monnet <quentin@isovalent.com>

Note that on bpf-next, patch 5 (runqslower) has a conflict with
be79505caf3f ("tools/runqslower: Install libbpf headers when
building").

Did you consider enabling cross-compiling for the BPF samples too? I'm
asking because the build system is pretty similar to the BPF tools.

Thanks,
Quentin

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

* Re: [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang
  2021-11-26 20:36 ` [PATCH bpf-next 0/6] tools/bpf: " Quentin Monnet
@ 2021-11-26 21:15   ` Daniel Borkmann
  2021-11-29  9:42   ` Jean-Philippe Brucker
  1 sibling, 0 replies; 12+ messages in thread
From: Daniel Borkmann @ 2021-11-26 21:15 UTC (permalink / raw)
  To: Quentin Monnet, Jean-Philippe Brucker
  Cc: Alexei Starovoitov, Andrii Nakryiko, Shuah Khan, nathan,
	ndesaulniers, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, bpf,
	open list:KERNEL SELFTEST FRAMEWORK, llvm

On 11/26/21 9:36 PM, Quentin Monnet wrote:
> On Mon, 22 Nov 2021 at 19:23, Jean-Philippe Brucker
> <jean-philippe@linaro.org> wrote:
>>
>> 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.
> 
> The set looks good to me. I checked that the tools are still building
> (without cross-compiling). I currently have issues building the
> selftests on my setup, but they don't appear to be related to this
> patchset.
> 
> Acked-by: Quentin Monnet <quentin@isovalent.com>
> 
> Note that on bpf-next, patch 5 (runqslower) has a conflict with
> be79505caf3f ("tools/runqslower: Install libbpf headers when
> building").

Jean-Philippe, please do one rebase spin for addressing the above.

Thanks,
Daniel

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

* Re: [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang
  2021-11-26 20:36 ` [PATCH bpf-next 0/6] tools/bpf: " Quentin Monnet
  2021-11-26 21:15   ` Daniel Borkmann
@ 2021-11-29  9:42   ` Jean-Philippe Brucker
  1 sibling, 0 replies; 12+ messages in thread
From: Jean-Philippe Brucker @ 2021-11-29  9:42 UTC (permalink / raw)
  To: Quentin Monnet
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Shuah Khan,
	nathan, ndesaulniers, Martin KaFai Lau, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, bpf,
	open list:KERNEL SELFTEST FRAMEWORK, llvm

On Fri, Nov 26, 2021 at 08:36:24PM +0000, Quentin Monnet wrote:
> On Mon, 22 Nov 2021 at 19:23, Jean-Philippe Brucker
> <jean-philippe@linaro.org> wrote:
> >
> > 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.
> 
> The set looks good to me. I checked that the tools are still building
> (without cross-compiling). I currently have issues building the
> selftests on my setup, but they don't appear to be related to this
> patchset.
> 
> Acked-by: Quentin Monnet <quentin@isovalent.com>
> 
> Note that on bpf-next, patch 5 (runqslower) has a conflict with
> be79505caf3f ("tools/runqslower: Install libbpf headers when
> building").

Ah right, I have the series on top of "tools/runqslower: Fix cross-build",
which is in bpf/master but not yet in bpf-next. I'll wait for the branches
to synchronize before resending, since the patches conflict either way.

> Did you consider enabling cross-compiling for the BPF samples too? I'm
> asking because the build system is pretty similar to the BPF tools.

Right, the problems seem similar to the selftests (need to build two
libbpfs, pass $(HOSTCC) etc). Samples aren't as crucial as tools but I can
look into it when I find a bit of time.

Thanks,
Jean

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

* Re: [PATCH bpf-next 3/6] tools/libbpf: Enable cross-building with clang
  2021-11-22 19:20 ` [PATCH bpf-next 3/6] tools/libbpf: Enable cross-building " Jean-Philippe Brucker
@ 2021-11-29 23:51   ` Andrii Nakryiko
  0 siblings, 0 replies; 12+ messages in thread
From: Andrii Nakryiko @ 2021-11-29 23:51 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Shuah Khan,
	Nathan Chancellor, Nick Desaulniers, Martin Lau, Song Liu,
	Yonghong Song, john fastabend, KP Singh, bpf,
	open list:KERNEL SELFTEST FRAMEWORK, llvm

On Mon, Nov 22, 2021 at 11:24 AM Jean-Philippe Brucker
<jean-philippe@linaro.org> wrote:
>
> 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.
>
> 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..fe9201862aed 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) $(CLANG_CROSS_FLAGS) $(LDFLAGS) \

I haven't checked, but what would happen if we just use $(CFLAGS)
here? Would it still work?

>                 --shared -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \
>                 -Wl,--version-script=$(VERSION_SCRIPT) $< -lelf -lz -o $@
>         @ln -sf $(@F) $(OUTPUT)libbpf.so
> --
> 2.33.1
>

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

* Re: [PATCH bpf-next 4/6] bpftool: Enable cross-building with clang
  2021-11-22 19:20 ` [PATCH bpf-next 4/6] bpftool: " Jean-Philippe Brucker
@ 2021-11-29 23:53   ` Andrii Nakryiko
  0 siblings, 0 replies; 12+ messages in thread
From: Andrii Nakryiko @ 2021-11-29 23:53 UTC (permalink / raw)
  To: Jean-Philippe Brucker
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Shuah Khan,
	Nathan Chancellor, Nick Desaulniers, Martin Lau, Song Liu,
	Yonghong Song, john fastabend, KP Singh, bpf,
	open list:KERNEL SELFTEST FRAMEWORK, llvm

On Mon, Nov 22, 2021 at 11:23 AM Jean-Philippe Brucker
<jean-philippe@linaro.org> wrote:
>
> 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. Filter
> out the cross flags for the bootstrap bpftool, and erase the
> CROSS_COMPILE flag for the bootstrap libbpf.
>
> 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..b0f3e17d981a 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),\
> +               $(filter-out $(CLANG_CROSS_FLAGS),$(CFLAGS)))

Isn't this dangerous? CLANG_CROSS_FLAGS are treated as space-separated
array of values, so if you have something like '-I whatever' it will
actually filter out unrelated -Is from CFLAGS, no? I'd rather subst
into empty string instead, just like we do for LIBBPF_INCLUDE.

> +
>  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.33.1
>

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

end of thread, other threads:[~2021-11-29 23:54 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-22 19:20 [PATCH bpf-next 0/6] tools/bpf: Enable cross-building with clang Jean-Philippe Brucker
2021-11-22 19:20 ` [PATCH bpf-next 1/6] tools: Help " Jean-Philippe Brucker
2021-11-22 19:20 ` [PATCH bpf-next 2/6] tools/resolve_btfids: Support cross-building the kernel " Jean-Philippe Brucker
2021-11-22 19:20 ` [PATCH bpf-next 3/6] tools/libbpf: Enable cross-building " Jean-Philippe Brucker
2021-11-29 23:51   ` Andrii Nakryiko
2021-11-22 19:20 ` [PATCH bpf-next 4/6] bpftool: " Jean-Philippe Brucker
2021-11-29 23:53   ` Andrii Nakryiko
2021-11-22 19:20 ` [PATCH bpf-next 5/6] tools/runqslower: " Jean-Philippe Brucker
2021-11-22 19:20 ` [PATCH bpf-next 6/6] selftests/bpf: " Jean-Philippe Brucker
2021-11-26 20:36 ` [PATCH bpf-next 0/6] tools/bpf: " Quentin Monnet
2021-11-26 21:15   ` Daniel Borkmann
2021-11-29  9:42   ` Jean-Philippe Brucker

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