Re: [PATCH bpf-next v4 09/10] selftests: Remove tools/lib/bpf from include path

From: Andrii Nakryiko
Date: Mon Jan 20 2020 - 01:21:10 EST


On Fri, Jan 17, 2020 at 5:43 AM Toke HÃiland-JÃrgensen <toke@xxxxxxxxxx> wrote:
>
> From: Toke HÃiland-JÃrgensen <toke@xxxxxxxxxx>
>
> To make sure no new files are introduced that doesn't include the bpf/
> prefix in its #include, remove tools/lib/bpf from the include path
> entirely.
>
> Instead, we introduce a new header files directory under the scratch tools/
> dir, and add a rule to run the 'install_headers' rule from libbpf to have a
> full set of consistent libbpf headers in $(OUTPUT)/tools/include/bpf, and
> then use $(OUTPUT)/tools/include as the include path for selftests.
>
> For consistency we also make sure we put all the scratch build files from
> other bpftool and libbpf into tools/build/, so everything stays within
> selftests/.
>
> Signed-off-by: Toke HÃiland-JÃrgensen <toke@xxxxxxxxxx>
> ---
> tools/testing/selftests/bpf/.gitignore | 2 +
> tools/testing/selftests/bpf/Makefile | 49 +++++++++++++++++++++-----------
> 2 files changed, 33 insertions(+), 18 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/.gitignore b/tools/testing/selftests/bpf/.gitignore
> index 1d14e3ab70be..8c9eac626996 100644
> --- a/tools/testing/selftests/bpf/.gitignore
> +++ b/tools/testing/selftests/bpf/.gitignore
> @@ -39,4 +39,4 @@ test_cpp
> /no_alu32
> /bpf_gcc
> /tools
> -bpf_helper_defs.h

can you please also drop libbpf.pc and libbpf.so.* rules from .gitignore?

> +
> diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> index 858d4e3369ad..ac0292a82fdc 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile
> @@ -20,8 +20,8 @@ CLANG ?= clang
> LLC ?= llc
> LLVM_OBJCOPY ?= llvm-objcopy
> BPF_GCC ?= $(shell command -v bpf-gcc;)
> -CFLAGS += -g -Wall -O2 $(GENFLAGS) -I$(CURDIR) -I$(APIDIR) -I$(LIBDIR) \
> - -I$(BPFDIR) -I$(GENDIR) -I$(TOOLSINCDIR) \
> +CFLAGS += -g -Wall -O2 $(GENFLAGS) -I$(CURDIR) -I$(APIDIR) \

extra space here

> + -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) -I$(TOOLSINCDIR) \
> -Dbpf_prog_load=bpf_prog_test_load \
> -Dbpf_load_program=bpf_test_load_program
> LDLIBS += -lcap -lelf -lz -lrt -lpthread
> @@ -97,11 +97,15 @@ OVERRIDE_TARGETS := 1
> override define CLEAN
> $(call msg,CLEAN)
> $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN)
> - $(MAKE) -C $(BPFDIR) OUTPUT=$(OUTPUT)/ clean
> endef
>
> include ../lib.mk
>
> +SCRATCH_DIR := $(OUTPUT)/tools
> +BUILD_DIR := $(SCRATCH_DIR)/build
> +INCLUDE_DIR := $(SCRATCH_DIR)/include
> +INCLUDE_BPF := $(INCLUDE_DIR)/bpf
> +
> # Define simple and short `make test_progs`, `make test_sysctl`, etc targets
> # to build individual tests.
> # NOTE: Semicolon at the end is critical to override lib.mk's default static
> @@ -120,7 +124,7 @@ $(OUTPUT)/urandom_read: urandom_read.c
> $(call msg,BINARY,,$@)
> $(CC) -o $@ $< -Wl,--build-id
>
> -$(OUTPUT)/test_stub.o: test_stub.c
> +$(OUTPUT)/test_stub.o: test_stub.c $(INCLUDE_BPF)
> $(call msg,CC,,$@)
> $(CC) -c $(CFLAGS) -o $@ $<
>
> @@ -133,7 +137,7 @@ $(OUTPUT)/runqslower: force
> $(Q)$(MAKE) $(submake_extras) -C $(TOOLSDIR)/bpf/runqslower \
> OUTPUT=$(OUTPUT)/tools/ VMLINUX_BTF=$(VMLINUX_BTF)
>
> -BPFOBJ := $(OUTPUT)/libbpf.a
> +BPFOBJ := $(BUILD_DIR)/libbpf/libbpf.a
>
> $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/test_stub.o $(BPFOBJ)
>
> @@ -159,17 +163,28 @@ force:
> DEFAULT_BPFTOOL := $(OUTPUT)/tools/sbin/bpftool
> BPFTOOL ?= $(DEFAULT_BPFTOOL)
>
> -$(DEFAULT_BPFTOOL): force
> - $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \
> +$(BUILD_DIR)/libbpf $(BUILD_DIR)/bpftool $(INCLUDE_DIR):
> + $(call msg,MKDIR,,$@)
> + mkdir -p $@
> +
> +$(DEFAULT_BPFTOOL): force | $(BUILD_DIR)/bpftool
> + $(Q)$(MAKE) $(submake_extras) -C $(BPFTOOLDIR) \

slash alignment


> + OUTPUT=$(BUILD_DIR)/bpftool/ \
> prefix= DESTDIR=$(OUTPUT)/tools/ install
>
> -$(BPFOBJ): force
> - $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(OUTPUT)/
> +$(BPFOBJ): force | $(BUILD_DIR)/libbpf
> + $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \
> + OUTPUT=$(BUILD_DIR)/libbpf/
> +
> +$(INCLUDE_BPF): $(BPFOBJ) | $(INCLUDE_DIR)
> + $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) install_headers \
> + OUTPUT=$(BUILD_DIR)/libbpf/ DESTDIR=$(SCRATCH_DIR) prefix=
> +
> +BPF_HELPERS := $(or $(wildcard $(INCLUDE_BPF)/bpf_*.h),$(INCLUDE_BPF))
> +ifneq ($(BPF_HELPERS),$(INCLUDE_BPF))
> +$(BPF_HELPERS): $(INCLUDE_BPF)
> +endif
>
> -BPF_HELPERS := $(OUTPUT)/bpf_helper_defs.h $(wildcard $(BPFDIR)/bpf_*.h)
> -$(OUTPUT)/bpf_helper_defs.h: $(BPFOBJ)
> - $(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) \
> - OUTPUT=$(OUTPUT)/ $(OUTPUT)/bpf_helper_defs.h
>

I really-really didn't like this alternating dependency on directory
or a set of file, depending on current state of those temporary
directories. Then also this ugly check to avoid circular dependency.
All that seemed wrong. So I played a bit with how to achieve the same
differently, and here's what I came up with, which I like a bit
better. What do you think?

$(BPFOBJ): $(wildcard $(BPFDIR)/*.c $(BPFDIR)/*.h $(BPFDIR)/Makefile) \
../../../include/uapi/linux/bpf.h \
| $(INCLUDE_DIR) $(BUILD_DIR)/libbpf
$(Q)$(MAKE) $(submake_extras) -C $(BPFDIR) OUTPUT=$(BUILD_DIR)/libbpf/ \
DESTDIR=$(SCRATCH_DIR) prefix= all install_headers

So, essentially, just make sure that we install local copies of
headers whenever libbpf.a needs to be re-built.
../../../include/uapi/linux/bpf.h ensures we don't miss uapi header
changes as well. Now anything that uses libbpf headers will need to
depend on $(BPFOBJ) and will automatically get up-to-date local copies
of headers.

This seems much simpler. Please give it a try, thanks!

> # Get Clang's default includes on this system, as opposed to those seen by
> # '-target bpf'. This fixes "missing" files on some architectures/distros,
> @@ -189,8 +204,8 @@ MENDIAN=$(if $(IS_LITTLE_ENDIAN),-mlittle-endian,-mbig-endian)
>
> CLANG_SYS_INCLUDES = $(call get_sys_includes,$(CLANG))
> BPF_CFLAGS = -g -D__TARGET_ARCH_$(SRCARCH) $(MENDIAN) \
> - -I$(OUTPUT) -I$(CURDIR) -I$(CURDIR)/include/uapi \
> - -I$(APIDIR) -I$(LIBDIR) -I$(BPFDIR) -I$(abspath $(OUTPUT)/../usr/include)
> + -I$(INCLUDE_DIR) -I$(CURDIR) -I$(CURDIR)/include/uapi \
> + -I$(APIDIR) -I$(abspath $(OUTPUT)/../usr/include)
>
> CLANG_CFLAGS = $(CLANG_SYS_INCLUDES) \
> -Wno-compare-distinct-pointer-types
> @@ -392,7 +407,7 @@ $(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ)
> $(call msg,CXX,,$@)
> $(CXX) $(CFLAGS) $^ $(LDLIBS) -o $@
>
> -EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) \
> +EXTRA_CLEAN := $(TEST_CUSTOM_PROGS) $(SCRATCH_DIR) \
> prog_tests/tests.h map_tests/tests.h verifier/tests.h \
> feature \
> - $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc tools)
> + $(addprefix $(OUTPUT)/,*.o *.skel.h no_alu32 bpf_gcc)
>