Subject: Re: [PATCHv3 iproute2-next 1/5] configure: add check_libbpf() for later libbpf support

From: Toke Høiland-Jørgensen <[email-masked]>
Date: Thu, 29 Oct 2020 16:26:12 +0100

Hangbin Liu <[email-masked]> writes:

> This patch adds a check to see if we support libbpf. By default the
> system libbpf will be used, but static linking against a custom libbpf
> version can be achieved by passing LIBBPF_DIR to configure. FORCE_LIBBPF
> can be set to force configure to abort if no suitable libbpf is found,
> which is useful for automatic packaging that wants to enforce the
> dependency.
>
> Signed-off-by: Hangbin Liu <[email-masked]>

With one nit below, feel free to add back my:

Reviewed-by: Toke Høiland-Jørgensen <[email-masked]>

> ---
> v3:
> Check function bpf_program__section_name() separately and only use it
> on higher libbpf version.
>
> v2:
> No update
> ---
>  configure | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 94 insertions(+)
>
> diff --git a/configure b/configure
> index 307912aa..58a7176e 100755
> --- a/configure
> +++ b/configure
> @@ -240,6 +240,97 @@ check_elf()
>      fi
>  }
>  
> +have_libbpf_basic()
> +{
> +    cat >$TMPDIR/libbpf_test.c <<EOF
> +#include <bpf/libbpf.h>
> +int main(int argc, char **argv) {
> +    bpf_program__set_autoload(NULL, false);
> +    bpf_map__ifindex(NULL);
> +    bpf_map__set_pin_path(NULL, NULL);
> +    bpf_object__open_file(NULL, NULL);
> +    return 0;
> +}
> +EOF
> +
> +    $CC -o $TMPDIR/libbpf_test $TMPDIR/libbpf_test.c $LIBBPF_CFLAGS $LIBBPF_LDLIBS >/dev/null 2>&1
> +    local ret=$?
> +
> +    rm -f $TMPDIR/libbpf_test.c $TMPDIR/libbpf_test
> +    return $ret
> +}
> +
> +have_libbpf_sec_name()
> +{
> +    cat >$TMPDIR/libbpf_sec_test.c <<EOF
> +#include <bpf/libbpf.h>
> +int main(int argc, char **argv) {
> +    void *ptr;
> +    bpf_program__section_name(NULL);
> +    return 0;
> +}
> +EOF
> +
> +    $CC -o $TMPDIR/libbpf_sec_test $TMPDIR/libbpf_sec_test.c $LIBBPF_CFLAGS $LIBBPF_LDLIBS >/dev/null 2>&1
> +    local ret=$?
> +
> +    rm -f $TMPDIR/libbpf_sec_test.c $TMPDIR/libbpf_sec_test
> +    return $ret
> +}
> +
> +check_force_libbpf()
> +{
> +    # if set FORCE_LIBBPF but no libbpf support, just exist the config
> +    # process to make sure we don't build without libbpf.
> +    if [ -n "$FORCE_LIBBPF" ]; then
> +        echo "FORCE_LIBBPF set, but couldn't find a usable libbpf"
> +        exit 1
> +    fi
> +}
> +
> +check_libbpf()
> +{
> +    if ! ${PKG_CONFIG} libbpf --exists && [ -z "$LIBBPF_DIR" ] ; then
> +        echo "no"
> +        check_force_libbpf
> +        return
> +    fi
> +
> +    if [ $(uname -m) == x86_64 ]; then
> +        local LIBSUBDIR=lib64
> +    else
> +        local LIBSUBDIR=lib
> +    fi
> +
> +    if [ -n "$LIBBPF_DIR" ]; then
> +        LIBBPF_CFLAGS="-I${LIBBPF_DIR}/include -L${LIBBPF_DIR}/${LIBSUBDIR}"
> +        LIBBPF_LDLIBS="${LIBBPF_DIR}/${LIBSUBDIR}/libbpf.a -lz -lelf"
> +    else
> +        LIBBPF_CFLAGS=$(${PKG_CONFIG} libbpf --cflags)
> +        LIBBPF_LDLIBS=$(${PKG_CONFIG} libbpf --libs)
> +    fi
> +
> +    if ! have_libbpf_basic; then
> +        echo "no"
> +        echo "	libbpf version is too low, please update it to at least 0.1.0"
> +        check_force_libbpf
> +        return
> +    else
> +        echo "HAVE_LIBBPF:=y" >>$CONFIG
> +        echo 'CFLAGS += -DHAVE_LIBBPF ' $LIBBPF_CFLAGS >> $CONFIG
> +        echo 'LDLIBS += ' $LIBBPF_LDLIBS >>$CONFIG
> +    fi
> +
> +    # bpf_program__title() is deprecated since libbpf 0.2.0, use
> +    # bpf_program__section_name() instead if we support
> +    if have_libbpf_sec_name; then
> +        echo "HAVE_LIBBPF_SECTION_NAME:=y" >>$CONFIG
> +        echo 'CFLAGS += -DHAVE_LIBBPF_SECTION_NAME ' $LIBBPF_CFLAGS >> $CONFIG

You already added $LIBBPF_CFLAGS above, so with this it ends up being
duplicated, doesn't it?

-Toke