Skip to content

Commit 8859b0d

Browse files
jpbruckeranakryiko
authored andcommitted
tools/bpftool: Fix cross-build
The bpftool build first creates an intermediate binary, executed on the host, to generate skeletons required by the final build. When cross-building bpftool for an architecture different from the host, the intermediate binary should be built using the host compiler (gcc) and the final bpftool using the cross compiler (e.g. aarch64-linux-gnu-gcc). Generate the intermediate objects into the bootstrap/ directory using the host toolchain. Signed-off-by: Jean-Philippe Brucker <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9e8929f commit 8859b0d

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

tools/bpf/bpftool/Makefile

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,37 @@ BPF_DIR = $(srctree)/tools/lib/bpf/
1919
ifneq ($(OUTPUT),)
2020
LIBBPF_OUTPUT = $(OUTPUT)/libbpf/
2121
LIBBPF_PATH = $(LIBBPF_OUTPUT)
22+
BOOTSTRAP_OUTPUT = $(OUTPUT)/bootstrap/
2223
else
24+
LIBBPF_OUTPUT =
2325
LIBBPF_PATH = $(BPF_DIR)
26+
BOOTSTRAP_OUTPUT = $(CURDIR)/bootstrap/
2427
endif
2528

2629
LIBBPF = $(LIBBPF_PATH)libbpf.a
30+
LIBBPF_BOOTSTRAP_OUTPUT = $(BOOTSTRAP_OUTPUT)libbpf/
31+
LIBBPF_BOOTSTRAP = $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a
2732

2833
BPFTOOL_VERSION ?= $(shell make -rR --no-print-directory -sC ../../.. kernelversion)
2934

30-
$(LIBBPF_OUTPUT):
35+
$(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT):
3136
$(QUIET_MKDIR)mkdir -p $@
3237

3338
$(LIBBPF): FORCE | $(LIBBPF_OUTPUT)
3439
$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) $(LIBBPF_OUTPUT)libbpf.a
3540

41+
$(LIBBPF_BOOTSTRAP): FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT)
42+
$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) \
43+
ARCH= CC=$(HOSTCC) LD=$(HOSTLD) $@
44+
3645
$(LIBBPF)-clean: $(LIBBPF_OUTPUT)
3746
$(call QUIET_CLEAN, libbpf)
3847
$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_OUTPUT) clean >/dev/null
3948

49+
$(LIBBPF_BOOTSTRAP)-clean: $(LIBBPF_BOOTSTRAP_OUTPUT)
50+
$(call QUIET_CLEAN, libbpf-bootstrap)
51+
$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(LIBBPF_BOOTSTRAP_OUTPUT) clean >/dev/null
52+
4053
prefix ?= /usr/local
4154
bash_compdir ?= /usr/share/bash-completion/completions
4255

@@ -94,6 +107,7 @@ CFLAGS += -DCOMPAT_NEED_REALLOCARRAY
94107
endif
95108

96109
LIBS = $(LIBBPF) -lelf -lz
110+
LIBS_BOOTSTRAP = $(LIBBPF_BOOTSTRAP) -lelf -lz
97111
ifeq ($(feature-libcap), 1)
98112
CFLAGS += -DUSE_LIBCAP
99113
LIBS += -lcap
@@ -120,9 +134,9 @@ CFLAGS += -DHAVE_LIBBFD_SUPPORT
120134
SRCS += $(BFD_SRCS)
121135
endif
122136

123-
BPFTOOL_BOOTSTRAP := $(if $(OUTPUT),$(OUTPUT)bpftool-bootstrap,./bpftool-bootstrap)
137+
BPFTOOL_BOOTSTRAP := $(BOOTSTRAP_OUTPUT)bpftool
124138

125-
BOOTSTRAP_OBJS = $(addprefix $(OUTPUT),main.o common.o json_writer.o gen.o btf.o)
139+
BOOTSTRAP_OBJS = $(addprefix $(BOOTSTRAP_OUTPUT),main.o common.o json_writer.o gen.o btf.o)
126140
OBJS = $(patsubst %.c,$(OUTPUT)%.o,$(SRCS)) $(OUTPUT)disasm.o
127141

128142
VMLINUX_BTF_PATHS ?= $(if $(O),$(O)/vmlinux) \
@@ -169,24 +183,28 @@ $(OUTPUT)disasm.o: $(srctree)/kernel/bpf/disasm.c
169183

170184
$(OUTPUT)feature.o: | zdep
171185

172-
$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF)
173-
$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) $(LIBS)
186+
$(BPFTOOL_BOOTSTRAP): $(BOOTSTRAP_OBJS) $(LIBBPF_BOOTSTRAP)
187+
$(QUIET_LINK)$(HOSTCC) $(CFLAGS) $(LDFLAGS) -o $@ $(BOOTSTRAP_OBJS) \
188+
$(LIBS_BOOTSTRAP)
174189

175190
$(OUTPUT)bpftool: $(OBJS) $(LIBBPF)
176191
$(QUIET_LINK)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
177192

193+
$(BOOTSTRAP_OUTPUT)%.o: %.c | $(BOOTSTRAP_OUTPUT)
194+
$(QUIET_CC)$(HOSTCC) $(CFLAGS) -c -MMD -o $@ $<
195+
178196
$(OUTPUT)%.o: %.c
179197
$(QUIET_CC)$(CC) $(CFLAGS) -c -MMD -o $@ $<
180198

181199
feature-detect-clean:
182200
$(call QUIET_CLEAN, feature-detect)
183201
$(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null
184202

185-
clean: $(LIBBPF)-clean feature-detect-clean
203+
clean: $(LIBBPF)-clean $(LIBBPF_BOOTSTRAP)-clean feature-detect-clean
186204
$(call QUIET_CLEAN, bpftool)
187205
$(Q)$(RM) -- $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
188-
$(Q)$(RM) -- $(BPFTOOL_BOOTSTRAP) $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
189-
$(Q)$(RM) -r -- $(OUTPUT)libbpf/
206+
$(Q)$(RM) -- $(OUTPUT)*.skel.h $(OUTPUT)vmlinux.h
207+
$(Q)$(RM) -r -- $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT)
190208
$(call QUIET_CLEAN, core-gen)
191209
$(Q)$(RM) -- $(OUTPUT)FEATURE-DUMP.bpftool
192210
$(Q)$(RM) -r -- $(OUTPUT)feature/

0 commit comments

Comments
 (0)