Skip to contentSkip to conversation

samples: bpf: Fix cross-compiling error about bpftool

PATCH In review Pu Lehui 3 participants 7 messages Jul 7, 2022 – Jul 9, 2022

Subsystems (3)
lore
Share & tools
lore

Pu Lehui

author
#0 lore raw

PATCH 1/1 samples: bpf: Fix cross-compiling error about bpftool

Currently, when cross compiling bpf samples, the host side cannot use arch-specific bpftool to generate vmlinux.h or skeleton. We need to compile the bpftool with the host compiler.

---
 samples/bpf/Makefile | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
samples/bpf/Makefile +4 -4
index 5002a5b9a7da..fe54a8c8f312 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+-include tools/scripts/Makefile.include
 
 BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))
 TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools
@@ -283,11 +284,10 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
 BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
 BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
 BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
-$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
+$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
 	    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
-		OUTPUT=$(BPFTOOL_OUTPUT)/ \
-		LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
-		LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
+		ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \
+		OUTPUT=$(BPFTOOL_OUTPUT)/
 
 $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
 	$(call msg,MKDIR,$@)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 19 lines · Pu Lehui

Currently, when cross compiling bpf samples, the host side cannot use arch-specific bpftool to generate vmlinux.h or skeleton. We need to compile the bpftool with the host compiler.

---
samples/bpf/Makefile | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
samples/bpf/Makefile +0 -0
index 5002a5b9a7da..fe54a8c8f312 100644

samples/bpf/Makefile line 2

Quoted 2 lines
samples/bpf/Makefile +1 -0
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+-include tools/scripts/Makefile.include

Why do we need the -include here?

Thanks,
Song

Quoted 21 lines
samples/bpf/Makefile +0 -0
 BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))

TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools

samples/bpf/Makefile +0 -0
@@ -283,11 +284,10 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU

BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
-$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT) +$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)

	    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
samples/bpf/Makefile +2 -3
-		OUTPUT=$(BPFTOOL_OUTPUT)/ \
-		LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
-		LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
+		ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \
+		OUTPUT=$(BPFTOOL_OUTPUT)/

$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):

	$(call msg,MKDIR,$@)
Signature · 1 line
2.25.1
author
#2 lore raw
Quoted 14 lines · Song Liu
Quoted 14 lines · Pu Lehui

Currently, when cross compiling bpf samples, the host side cannot use arch-specific bpftool to generate vmlinux.h or skeleton. We need to compile the bpftool with the host compiler.

---
samples/bpf/Makefile | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

samples/bpf/Makefile line 2

Quoted 7 lines
samples/bpf/Makefile +1 -0
index 5002a5b9a7da..fe54a8c8f312 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+-include tools/scripts/Makefile.include

Why do we need the -include here?

HOSTLD is defined in tools/scripts/Makefile.include, we need to add it.

And for -include, mainly to resolve some conflicts:
1. If workdir is kernel_src, then 'include tools/scripts/Makefile.include' is fine when 'make M=samples/bpf'.
2. Since the trick in samples/bpf/Makefile:

# Trick to allow make to be run from this directory all:

	$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)

If workdir is samples/bpf, the compile process will first load the Makefile in samples/bpf, then change workdir to kernel_src and load the kernel_src's Makefile. So if we just add 'include tools/scripts/Makefile.include', then the first load will occur error for not found the file, so we add -include to skip the first load.

Quoted 27 lines

Thanks,
Song

Quoted 16 lines
samples/bpf/Makefile +0 -0
 BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))

TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools

samples/bpf/Makefile +0 -0
@@ -283,11 +284,10 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU

BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
-$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT) +$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)

	    $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
samples/bpf/Makefile +2 -3
-		OUTPUT=$(BPFTOOL_OUTPUT)/ \
-		LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
-		LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
+		ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \
+		OUTPUT=$(BPFTOOL_OUTPUT)/
Quoted 5 lines

$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):

	$(call msg,MKDIR,$@)
Signature · 1 line
2.25.1

.

author
#3 lore raw
Quoted 34 lines · Pu Lehui
Quoted 14 lines · Song Liu
Quoted 14 lines · Pu Lehui

Currently, when cross compiling bpf samples, the host side cannot use arch-specific bpftool to generate vmlinux.h or skeleton. We need to compile the bpftool with the host compiler.

---
samples/bpf/Makefile | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

samples/bpf/Makefile line 2

Quoted 7 lines
samples/bpf/Makefile +1 -0
index 5002a5b9a7da..fe54a8c8f312 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+-include tools/scripts/Makefile.include

Why do we need the -include here?

HOSTLD is defined in tools/scripts/Makefile.include, we need to add it.

And for -include, mainly to resolve some conflicts:
1. If workdir is kernel_src, then 'include tools/scripts/Makefile.include' is fine when 'make M=samples/bpf'.
2. Since the trick in samples/bpf/Makefile:

Quoted 5 lines

# Trick to allow make to be run from this directory all:
    $(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)

If workdir is samples/bpf, the compile process will first load the Makefile in samples/bpf, then change workdir to kernel_src and load the kernel_src's Makefile. So if we just add 'include tools/scripts/Makefile.include', then the first load will occur error for not found the file, so we add -include to skip the first load.

sorry, correct the reply, so we add -include to skip the 'tools/scripts/Makefile.include' file on the fisrt load.

Quoted 32 lines
Quoted 23 lines

Thanks,
Song

Quoted 19 lines
samples/bpf/Makefile +0 -0
 BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))

TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools

samples/bpf/Makefile +0 -0
@@ -283,11 +284,10 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] 

$(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
-$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
+$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
        $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
-        OUTPUT=$(BPFTOOL_OUTPUT)/ \
-        LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
-        LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
+        ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \
+        OUTPUT=$(BPFTOOL_OUTPUT)/

Quoted 8 lines
Quoted 5 lines

$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
    $(call msg,MKDIR,$@)

Signature · 1 line
2.25.1

.

.

Acked-by
#4 lore raw
Quoted 41 lines · Pu Lehui
Quoted 25 lines · Pu Lehui
Quoted 14 lines · Song Liu
Quoted 14 lines · Pu Lehui

Currently, when cross compiling bpf samples, the host side cannot use arch-specific bpftool to generate vmlinux.h or skeleton. We need to compile the bpftool with the host compiler.

---
samples/bpf/Makefile | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

samples/bpf/Makefile line 2

Quoted 7 lines
samples/bpf/Makefile +1 -0
index 5002a5b9a7da..fe54a8c8f312 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+-include tools/scripts/Makefile.include

Why do we need the -include here?

Quoted 8 lines

HOSTLD is defined in tools/scripts/Makefile.include, we need to add it.
And for -include, mainly to resolve some conflicts:
1. If workdir is kernel_src, then 'include tools/scripts/Makefile.include' is fine when 'make M=samples/bpf'.
2. Since the trick in samples/bpf/Makefile:
# Trick to allow make to be run from this directory
all:
$(MAKE) -C ../../ M=$(CURDIR) BPF_SAMPLES_PATH=$(CURDIR)
If workdir is samples/bpf, the compile process will first load the Makefile in samples/bpf, then change workdir to kernel_src and load the kernel_src's Makefile. So if we just add 'include tools/scripts/Makefile.include', then the first load will occur error for not found the file, so we add -include to skip the first load.

Quoted 1 line

sorry, correct the reply, so we add -include to skip the 'tools/scripts/Makefile.include' file on the fisrt load.

Thanks for the explanation.

Quoted 29 lines
Quoted 29 lines
Quoted 20 lines

Thanks,
Song

Quoted 16 lines
samples/bpf/Makefile +0 -0
 BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))

TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools

samples/bpf/Makefile +0 -0
@@ -283,11 +284,10 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU

BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
-$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT) +$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT) $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \

samples/bpf/Makefile +2 -3
-  OUTPUT=$(BPFTOOL_OUTPUT)/ \
-  LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
-  LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
+  ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \
+  OUTPUT=$(BPFTOOL_OUTPUT)/
Quoted 8 lines
Quoted 5 lines

$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
$(call msg,MKDIR,$@)

Signature · 1 line
2.25.1

.

.

#5 lore raw
In reply to Pu Lehui
Quoted 8 lines · Pu Lehui

Currently, when cross compiling bpf samples, the host side cannot use arch-specific bpftool to generate vmlinux.h or skeleton. We need to compile the bpftool with the host compiler.

---

samples/bpf use bpftool for vmlinux.h, skeleton, and static linking only. All that is supported by lightweight "bootstrap" bpftool version, so we can build just that. It will be faster, and bootstrap version should be always host-native even during cross compilation. See [0] for what I did in libbpf-bootstrap.

Also please cc Quention for bpftool-related changes. Thanks!

[0] https://github.com/libbpf/libbpf-bootstrap/commit/fc28424eb3f0e39cfb5959296b070389b9a8bd8f

Quoted 31 lines
 samples/bpf/Makefile | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
samples/bpf/Makefile +4 -5
index 5002a5b9a7da..fe54a8c8f312 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
+-include tools/scripts/Makefile.include
 
 BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))
 TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools
@@ -283,11 +284,10 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
 BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
 BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
 BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
-$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
+$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
            $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
-               OUTPUT=$(BPFTOOL_OUTPUT)/ \
-               LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
-               LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
+               ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \
+               OUTPUT=$(BPFTOOL_OUTPUT)/
 
 $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
        $(call msg,MKDIR,$@)
--

2.25.1

author
#6 lore raw
Quoted 16 lines · Andrii Nakryiko
Quoted 8 lines · Pu Lehui

Currently, when cross compiling bpf samples, the host side cannot use arch-specific bpftool to generate vmlinux.h or skeleton. We need to compile the bpftool with the host compiler.

---

samples/bpf use bpftool for vmlinux.h, skeleton, and static linking only. All that is supported by lightweight "bootstrap" bpftool version, so we can build just that. It will be faster, and bootstrap version should be always host-native even during cross compilation. See [0] for what I did in libbpf-bootstrap.

Quoted 4 lines

Also please cc Quention for bpftool-related changes. Thanks!

    [0] https://github.com/libbpf/libbpf-bootstrap/commit/fc28424eb3f0e39cfb5959296b070389b9a8bd8f

so brilliant,we can take it to other places where rely on bpftool. thanks.

Quoted 33 lines
Quoted 29 lines
  samples/bpf/Makefile | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
samples/bpf/Makefile +4 -5
index 5002a5b9a7da..fe54a8c8f312 100644
@@ -1,4 +1,5 @@
  # SPDX-License-Identifier: GPL-2.0
+-include tools/scripts/Makefile.include
 
  BPF_SAMPLES_PATH ?= $(abspath $(srctree)/$(src))
  TOOLS_PATH := $(BPF_SAMPLES_PATH)/../../tools
@@ -283,11 +284,10 @@ $(LIBBPF): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(LIBBPF_OU
  BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
  BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
  BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
-$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
+$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
             $(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
-               OUTPUT=$(BPFTOOL_OUTPUT)/ \
-               LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
-               LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
+               ARCH= CROSS_COMPILE= CC=$(HOSTCC) LD=$(HOSTLD) \
+               OUTPUT=$(BPFTOOL_OUTPUT)/
 
  $(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
         $(call msg,MKDIR,$@)
--
Quoted 2 lines

2.25.1

.