#!/bin/sh # SPDX-License-Identifier: GPL-2.0 # This is not an autoconf generated configure # # Output file which is input to Makefile CONFIG_FINAL=config.mk CONFIG=".${CONFIG}.tmp" # Make a temp directory in build tree. TMPDIR=$(mktemp -d config.XXXXXX) trap 'status=$?; rm -rf $TMPDIR; rm -f $CONFIG; exit $status' EXIT HUP INT QUIT TERM check_toolchain() { local clang_version : ${PKG_CONFIG:=pkg-config} : ${CC=gcc} : ${CLANG=clang} : ${LLC=llc} for TOOL in $PKG_CONFIG $CC $CLANG $LLC; do if [ ! $(command -v ${TOOL} 2>/dev/null) ]; then echo "*** ERROR: Cannot find tool ${TOOL}" ; exit 1; fi; done clang_version=$($CLANG --version | sed -n '/clang version/ s/^.*clang version // p') clang_major_version=$(echo "$clang_version" | sed 's/\..*$//') if [ "$clang_major_version" -lt "11" ]; then echo "ERROR: Need clang version >= 11, found $clang_major_version ($clang_version)" exit 1 fi echo "clang: $clang_version" echo "PKG_CONFIG:=${PKG_CONFIG}" >>$CONFIG echo "CC:=${CC}" >>$CONFIG echo "CLANG:=${CLANG}" >>$CONFIG echo "LLC:=${LLC}" >>$CONFIG } check_elf() { if ${PKG_CONFIG} libelf --exists; then echo "HAVE_ELF:=y" >>$CONFIG echo "yes" echo 'CFLAGS += -DHAVE_ELF' `${PKG_CONFIG} libelf --cflags` >> $CONFIG echo 'LDLIBS += ' `${PKG_CONFIG} libelf --libs` >>$CONFIG else echo "missing - this is required" return 1 fi } check_zlib() { if ${PKG_CONFIG} zlib --exists; then echo "HAVE_ZLIB:=y" >>$CONFIG echo "yes" echo 'CFLAGS += -DHAVE_ZLIB' `${PKG_CONFIG} zlib --cflags` >> $CONFIG echo 'LDLIBS += ' `${PKG_CONFIG} zlib --libs` >>$CONFIG else echo "missing - this is required" return 1 fi } check_libbpf() { local libbpf_err if [ "${FORCE_SUBMODULE_LIBBPF:-0}" -ne "1" ] && ${PKG_CONFIG} libbpf --exists || [ -n "$LIBBPF_DIR" ]; then if [ -n "$LIBBPF_DIR" ]; then LIBBPF_CFLAGS="-I${LIBBPF_DIR}/include -L${LIBBPF_DIR}/lib" LIBBPF_LDLIBS="-lbpf" else LIBBPF_CFLAGS=$(${PKG_CONFIG} libbpf --cflags) LIBBPF_LDLIBS=$(${PKG_CONFIG} libbpf --libs) fi cat >$TMPDIR/libbpftest.c < int main(int argc, char **argv) { void *ptr; DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, .pin_root_path = "/path"); DECLARE_LIBBPF_OPTS(bpf_xdp_set_link_opts, lopts, .old_fd = -1); (void) bpf_object__open_file("file", &opts); (void) bpf_program__name(ptr); (void) bpf_map__set_initial_value(ptr, ptr, 0); (void) bpf_set_link_xdp_fd_opts(0, 0, 0, &lopts); (void) bpf_tc_attach(ptr, ptr); return 0; } EOF libbpf_err=$($CC -o $TMPDIR/libbpftest $TMPDIR/libbpftest.c $LIBBPF_CFLAGS -lbpf 2>&1) if [ "$?" -eq "0" ]; then echo "SYSTEM_LIBBPF:=y" >>$CONFIG echo 'CFLAGS += ' $LIBBPF_CFLAGS >> $CONFIG echo 'LDLIBS += ' $LIBBPF_LDLIBS >>$CONFIG echo 'OBJECT_LIBBPF = ' >>$CONFIG echo system return 0 fi else libbpf_err="${PKG_CONFIG} couldn't find libbpf" fi if [ "${FORCE_SYSTEM_LIBBPF:-0}" -eq "1" ]; then echo "FORCE_SYSTEM_LIBBPF is set, but no usable libbpf found on system" echo "error: $libbpf_err" rm -f "$CONFIG" exit 1 fi echo submodule echo "SYSTEM_LIBBPF:=n" >> $CONFIG echo 'CFLAGS += -I$(LIB_DIR)/libbpf-install/usr/include' >>$CONFIG echo 'BPF_CFLAGS += -I$(LIB_DIR)/libbpf-install/usr/include' >>$CONFIG echo 'LDFLAGS += -L$(LIB_DIR)/libbpf/src' >>$CONFIG echo 'LDLIBS += -l:libbpf.a' >>$CONFIG echo 'OBJECT_LIBBPF = $(LIB_DIR)/libbpf/src/libbpf.a' >>$CONFIG if ! [ -d "lib/libbpf/src" ] && [ -f ".gitmodules" ] && [ -e ".git" ]; then git submodule init && git submodule update fi echo -n "ELF support: " check_elf || exit 1 echo -n "zlib support: " check_zlib || exit 1 # For the build submodule library we know it does support this API, so we # hard code it. Also due to the fact it's hard to build a test app as # libbpf.a has not been build at configure time. echo "HAVE_LIBBPF_PERF_BUFFER__CONSUME:=y" >>"$CONFIG" } check_bpf_use_errno() { local compile_err # Clang BPF-progs when compiled with proper -target bpf cause # build dependencies to include file. # cat >$TMPDIR/bpf_use_errno_test.c < int dummy(void *ctx) { return 0; } EOF compile_err=$($CLANG -target bpf -c $TMPDIR/bpf_use_errno_test.c 2>&1) if [ "$?" -ne "0" ]; then echo "*** ERROR - Clang BPF-prog cannot include " echo " - Install missing userspace header file" echo "" echo "Compile error: $compile_err" echo "" echo " On Fedora install:" echo " dnf install glibc-devel.i686" echo " On Debian install:" echo " apt install libc6-dev-i386" echo "" exit 1 fi } quiet_config() { cat <$CONFIG quiet_config >> $CONFIG check_toolchain echo -n "libbpf support: " check_libbpf check_bpf_use_errno if [ -n "$KERNEL_HEADERS" ]; then echo "kernel headers: $KERNEL_HEADERS" echo "CFLAGS += -I$KERNEL_HEADERS" >>$CONFIG echo "BPF_CFLAGS += -I$KERNEL_HEADERS" >>$CONFIG fi mv $CONFIG $CONFIG_FINAL