Re: [PATCH bpf v3] bpftool: Allow to link libbpf dynamically

Daniel Borkmann
2019-11-30T00:13:00Z

On 11/29/19 3:00 PM, Toke Høiland-Jørgensen wrote:
> Daniel Borkmann <daniel@iogearbox.net> writes:
>> On 11/28/19 5:07 PM, Toke Høiland-Jørgensen wrote:
>>> From: Jiri Olsa <jolsa@kernel.org>
[...]
>>> ifeq ($(srctree),)
>>> srctree := $(patsubst %/,%,$(dir $(CURDIR)))
>>> @@ -63,6 +72,19 @@ RM ?= rm -f
>>> FEATURE_USER = .bpftool
>>> FEATURE_TESTS = libbfd disassembler-four-args reallocarray zlib
>>> FEATURE_DISPLAY = libbfd disassembler-four-args zlib
>>> +ifdef LIBBPF_DYNAMIC
>>> + FEATURE_TESTS += libbpf
>>> + FEATURE_DISPLAY += libbpf
>>> +
>>> + # for linking with debug library run:
>>> + # make LIBBPF_DYNAMIC=1 LIBBPF_DIR=/opt/libbpf
>>
>> The Makefile already has BPF_DIR which points right now to
>> '$(srctree)/tools/lib/bpf/' and LIBBPF_PATH for the final one and
>> where $(LIBBPF_PATH)libbpf.a is expected to reside. Can't we improve
>> the Makefile to reuse and work with these instead of adding yet
>> another LIBBPF_DIR var which makes future changes in this area more
>> confusing? The libbpf build spills out libbpf.{a,so*} by default
>> anyway.
>
> I see what you mean; however, LIBBPF_DIR is meant to be specifically an
> override for the dynamic library, not just the path to libbpf.
>
> Would it be less confusing to overload the LIBBPF_DYNAMIC variable
> instead? I.e.,
>
> make LIBBPF_DYNAMIC=1
>
> would dynamically link against the libbpf installed in the system, but
>
> make LIBBPF_DYNAMIC=/opt/libbpf
>
> would override that and link against whatever is in /opt/libbpf instead?
> WDYT?

Hm, given perf tool has similar LIB*_DIR vars in place for its libs, it probably
makes sense to stick with this convention as well then. Perhaps better in this
case to just rename s/BPF_DIR/BPF_SRC_DIR/, s/LIBBPF_OUTPUT/LIBBPF_BUILD_OUTPUT/,
and s/LIBBPF_PATH/LIBBPF_BUILD_PATH/ to make their purpose more clear.

One thing that would be good to do as well for this patch is to:

i) Document both LIBBPF_DYNAMIC and LIBBPF_DIR in the Makefile comment you
added at the top along with a simple usage example.

ii) Extend tools/testing/selftests/bpf/test_bpftool_build.sh with a dynamic
linking test case, e.g. building libbpf into a temp dir and pointing
LIBBPF_DIR to it for bpftool LIBBPF_DYNAMIC=1 build.

Thanks,
Daniel