Skip to content

Commit 7b4a30b

Browse files
qmonnetgregkh
authored andcommitted
samples/bpf: Install libbpf headers when building
[ Upstream commit 3f7a331 ] API headers from libbpf should not be accessed directly from the source directory. Instead, they should be exported with "make install_headers". Make sure that samples/bpf/Makefile installs the headers properly when building. The object compiled from and exported by libbpf are now placed into a subdirectory of sample/bpf/ instead of remaining in tools/lib/bpf/. We attempt to remove this directory on "make clean". However, the "clean" target re-enters the samples/bpf/ directory from the root of the repository ("$(MAKE) -C ../../ M=$(CURDIR) clean"), in such a way that $(srctree) and $(src) are not defined, making it impossible to use $(LIBBPF_OUTPUT) and $(LIBBPF_DESTDIR) in the recipe. So we only attempt to clean $(CURDIR)/libbpf, which is the default value. Add a dependency on libbpf's headers for the $(TRACE_HELPERS). We also change the output directory for bpftool, to place the generated objects under samples/bpf/bpftool/ instead of building in bpftool's directory directly. Doing so, we make sure bpftool reuses the libbpf library previously compiled and installed. Signed-off-by: Quentin Monnet <quentin@isovalent.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20211007194438.34443-10-quentin@isovalent.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c7f0c08 commit 7b4a30b

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ tprogs-y += xdp_redirect
5959
tprogs-y += xdp_monitor
6060

6161
# Libbpf dependencies
62-
LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
62+
LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
63+
LIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpf
64+
LIBBPF_DESTDIR = $(LIBBPF_OUTPUT)
65+
LIBBPF_INCLUDE = $(LIBBPF_DESTDIR)/include
66+
LIBBPF = $(LIBBPF_OUTPUT)/libbpf.a
6367

6468
CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o
6569
TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
@@ -198,7 +202,7 @@ TPROGS_CFLAGS += -Wstrict-prototypes
198202

199203
TPROGS_CFLAGS += -I$(objtree)/usr/include
200204
TPROGS_CFLAGS += -I$(srctree)/tools/testing/selftests/bpf/
201-
TPROGS_CFLAGS += -I$(srctree)/tools/lib/
205+
TPROGS_CFLAGS += -I$(LIBBPF_INCLUDE)
202206
TPROGS_CFLAGS += -I$(srctree)/tools/include
203207
TPROGS_CFLAGS += -I$(srctree)/tools/perf
204208
TPROGS_CFLAGS += -DHAVE_ATTR_TEST=0
@@ -268,16 +272,27 @@ all:
268272
clean:
269273
$(MAKE) -C ../../ M=$(CURDIR) clean
270274
@find $(CURDIR) -type f -name '*~' -delete
275+
@$(RM) -r $(CURDIR)/libbpf $(CURDIR)/bpftool
271276

272-
$(LIBBPF): FORCE
277+
$(LIBBPF): FORCE | $(LIBBPF_OUTPUT)
273278
# Fix up variables inherited from Kbuild that tools/ build system won't like
274-
$(MAKE) -C $(dir $@) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
275-
LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ O=
279+
$(MAKE) -C $(LIBBPF_SRC) RM='rm -rf' EXTRA_CFLAGS="$(TPROGS_CFLAGS)" \
280+
LDFLAGS=$(TPROGS_LDFLAGS) srctree=$(BPF_SAMPLES_PATH)/../../ \
281+
O= OUTPUT=$(LIBBPF_OUTPUT)/ DESTDIR=$(LIBBPF_DESTDIR) prefix= \
282+
$@ install_headers
276283

277284
BPFTOOLDIR := $(TOOLS_PATH)/bpf/bpftool
278-
BPFTOOL := $(BPFTOOLDIR)/bpftool
279-
$(BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)
280-
$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../
285+
BPFTOOL_OUTPUT := $(abspath $(BPF_SAMPLES_PATH))/bpftool
286+
BPFTOOL := $(BPFTOOL_OUTPUT)/bpftool
287+
$(BPFTOOL): $(LIBBPF) $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) | $(BPFTOOL_OUTPUT)
288+
$(MAKE) -C $(BPFTOOLDIR) srctree=$(BPF_SAMPLES_PATH)/../../ \
289+
OUTPUT=$(BPFTOOL_OUTPUT)/ \
290+
LIBBPF_OUTPUT=$(LIBBPF_OUTPUT)/ \
291+
LIBBPF_DESTDIR=$(LIBBPF_DESTDIR)/
292+
293+
$(LIBBPF_OUTPUT) $(BPFTOOL_OUTPUT):
294+
$(call msg,MKDIR,$@)
295+
$(Q)mkdir -p $@
281296

282297
$(obj)/syscall_nrs.h: $(obj)/syscall_nrs.s FORCE
283298
$(call filechk,offsets,__SYSCALL_NRS_H__)
@@ -309,6 +324,11 @@ verify_target_bpf: verify_cmds
309324
$(BPF_SAMPLES_PATH)/*.c: verify_target_bpf $(LIBBPF)
310325
$(src)/*.c: verify_target_bpf $(LIBBPF)
311326

327+
libbpf_hdrs: $(LIBBPF)
328+
$(obj)/$(TRACE_HELPERS): | libbpf_hdrs
329+
330+
.PHONY: libbpf_hdrs
331+
312332
$(obj)/xdp_redirect_cpu_user.o: $(obj)/xdp_redirect_cpu.skel.h
313333
$(obj)/xdp_redirect_map_multi_user.o: $(obj)/xdp_redirect_map_multi.skel.h
314334
$(obj)/xdp_redirect_map_user.o: $(obj)/xdp_redirect_map.skel.h
@@ -366,7 +386,7 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x
366386
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(SRCARCH) \
367387
-Wno-compare-distinct-pointer-types -I$(srctree)/include \
368388
-I$(srctree)/samples/bpf -I$(srctree)/tools/include \
369-
-I$(srctree)/tools/lib $(CLANG_SYS_INCLUDES) \
389+
-I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \
370390
-c $(filter %.bpf.c,$^) -o $@
371391

372392
LINKED_SKELS := xdp_redirect_cpu.skel.h xdp_redirect_map_multi.skel.h \
@@ -403,7 +423,7 @@ $(obj)/%.o: $(src)/%.c
403423
@echo " CLANG-bpf " $@
404424
$(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS) \
405425
-I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \
406-
-I$(srctree)/tools/lib/ \
426+
-I$(LIBBPF_INCLUDE) \
407427
-D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-pointer-sign \
408428
-D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-types \
409429
-Wno-gnu-variable-sized-type-not-at-end \

0 commit comments

Comments
 (0)