152 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)3 4case $1 in5 -h|--help)6 echo -e "$0 [-j <n>]"7 echo -e "\tTest the different ways of building bpftool."8 echo -e ""9 echo -e "\tOptions:"10 echo -e "\t\t-j <n>:\tPass -j flag to 'make'."11 exit 012 ;;13esac14 15J=$*16 17# Assume script is located under tools/testing/selftests/bpf/. We want to start18# build attempts from the top of kernel repository.19SCRIPT_REL_PATH=$(realpath --relative-to=$PWD $0)20SCRIPT_REL_DIR=$(dirname $SCRIPT_REL_PATH)21KDIR_ROOT_DIR=$(realpath $PWD/$SCRIPT_REL_DIR/../../../../)22cd $KDIR_ROOT_DIR23if [ ! -e tools/bpf/bpftool/Makefile ]; then24 echo -e "skip: bpftool files not found!\n"25 exit 4 # KSFT_SKIP=426fi27 28ERROR=029TMPDIR=30 31# If one build fails, continue but return non-0 on exit.32return_value() {33 if [ -d "$TMPDIR" ] ; then34 rm -rf -- $TMPDIR35 fi36 exit $ERROR37}38trap return_value EXIT39 40check() {41 local dir=$(realpath $1)42 43 echo -n "binary: "44 # Returns non-null if file is found (and "false" is run)45 find $dir -type f -executable -name bpftool -print -exec false {} + && \46 ERROR=1 && printf "FAILURE: Did not find bpftool\n"47}48 49make_and_clean() {50 echo -e "\$PWD: $PWD"51 echo -e "command: make -s $* >/dev/null"52 make $J -s $* >/dev/null53 if [ $? -ne 0 ] ; then54 ERROR=155 fi56 if [ $# -ge 1 ] ; then57 check ${@: -1}58 else59 check .60 fi61 (62 if [ $# -ge 1 ] ; then63 cd ${@: -1}64 fi65 make -s clean66 )67 echo68}69 70make_with_tmpdir() {71 local ARGS72 73 TMPDIR=$(mktemp -d)74 if [ $# -ge 2 ] ; then75 ARGS=${@:1:(($# - 1))}76 fi77 echo -e "\$PWD: $PWD"78 echo -e "command: make -s $ARGS ${@: -1}=$TMPDIR/ >/dev/null"79 make $J -s $ARGS ${@: -1}=$TMPDIR/ >/dev/null80 if [ $? -ne 0 ] ; then81 ERROR=182 fi83 check $TMPDIR84 rm -rf -- $TMPDIR85 echo86}87 88echo "Trying to build bpftool"89echo -e "... through kbuild\n"90 91if [ -f ".config" ] ; then92 make_and_clean tools/bpf93 ## "make tools/bpf" sets $(OUTPUT) to ...tools/bpf/runqslower for94 ## runqslower, but the default (used for the "clean" target) is .output.95 ## Let's make sure we clean runqslower's directory properly.96 make -C tools/bpf/runqslower OUTPUT=${KDIR_ROOT_DIR}/tools/bpf/runqslower/ clean97 98 ## $OUTPUT is overwritten in kbuild Makefile, and thus cannot be passed99 ## down from toplevel Makefile to bpftool's Makefile.100 101 # make_with_tmpdir tools/bpf OUTPUT102 echo -e "skip: make tools/bpf OUTPUT=<dir> (not supported)\n"103 104 make_with_tmpdir tools/bpf O105else106 echo -e "skip: make tools/bpf (no .config found)\n"107 echo -e "skip: make tools/bpf OUTPUT=<dir> (not supported)\n"108 echo -e "skip: make tools/bpf O=<dir> (no .config found)\n"109fi110 111echo -e "... from kernel source tree\n"112 113make_and_clean -C tools/bpf/bpftool114 115make_with_tmpdir -C tools/bpf/bpftool OUTPUT116 117make_with_tmpdir -C tools/bpf/bpftool O118 119echo -e "... from tools/\n"120cd tools/121 122make_and_clean bpf123 124## In tools/bpf/Makefile, function "descend" is called and passes $(O) and125## $(OUTPUT). We would like $(OUTPUT) to have "bpf/bpftool/" appended before126## calling bpftool's Makefile, but this is not the case as the "descend"127## function focuses on $(O)/$(subdir). However, in the present case, updating128## $(O) to have $(OUTPUT) recomputed from it in bpftool's Makefile does not129## work, because $(O) is not defined from command line and $(OUTPUT) is not130## updated in tools/scripts/Makefile.include.131##132## Workarounds would require to a) edit "descend" or use an alternative way to133## call bpftool's Makefile, b) modify the conditions to update $(OUTPUT) and134## other variables in tools/scripts/Makefile.include (at the risk of breaking135## the build of other tools), or c) append manually the "bpf/bpftool" suffix to136## $(OUTPUT) in bpf's Makefile, which may break if targets for other directories137## use "descend" in the future.138 139# make_with_tmpdir bpf OUTPUT140echo -e "skip: make bpf OUTPUT=<dir> (not supported)\n"141 142make_with_tmpdir bpf O143 144echo -e "... from bpftool's dir\n"145cd bpf/bpftool146 147make_and_clean148 149make_with_tmpdir OUTPUT150 151make_with_tmpdir O152