Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
0003 
0004 case $1 in
0005         -h|--help)
0006                 echo -e "$0 [-j <n>]"
0007                 echo -e "\tTest the different ways of building bpftool."
0008                 echo -e ""
0009                 echo -e "\tOptions:"
0010                 echo -e "\t\t-j <n>:\tPass -j flag to 'make'."
0011                 exit 0
0012                 ;;
0013 esac
0014 
0015 J=$*
0016 
0017 # Assume script is located under tools/testing/selftests/bpf/. We want to start
0018 # build attempts from the top of kernel repository.
0019 SCRIPT_REL_PATH=$(realpath --relative-to=$PWD $0)
0020 SCRIPT_REL_DIR=$(dirname $SCRIPT_REL_PATH)
0021 KDIR_ROOT_DIR=$(realpath $PWD/$SCRIPT_REL_DIR/../../../../)
0022 cd $KDIR_ROOT_DIR
0023 if [ ! -e tools/bpf/bpftool/Makefile ]; then
0024         echo -e "skip:    bpftool files not found!\n"
0025         exit 4 # KSFT_SKIP=4
0026 fi
0027 
0028 ERROR=0
0029 TMPDIR=
0030 
0031 # If one build fails, continue but return non-0 on exit.
0032 return_value() {
0033         if [ -d "$TMPDIR" ] ; then
0034                 rm -rf -- $TMPDIR
0035         fi
0036         exit $ERROR
0037 }
0038 trap return_value EXIT
0039 
0040 check() {
0041         local dir=$(realpath $1)
0042 
0043         echo -n "binary:  "
0044         # Returns non-null if file is found (and "false" is run)
0045         find $dir -type f -executable -name bpftool -print -exec false {} + && \
0046                 ERROR=1 && printf "FAILURE: Did not find bpftool\n"
0047 }
0048 
0049 make_and_clean() {
0050         echo -e "\$PWD:    $PWD"
0051         echo -e "command: make -s $* >/dev/null"
0052         make $J -s $* >/dev/null
0053         if [ $? -ne 0 ] ; then
0054                 ERROR=1
0055         fi
0056         if [ $# -ge 1 ] ; then
0057                 check ${@: -1}
0058         else
0059                 check .
0060         fi
0061         (
0062                 if [ $# -ge 1 ] ; then
0063                         cd ${@: -1}
0064                 fi
0065                 make -s clean
0066         )
0067         echo
0068 }
0069 
0070 make_with_tmpdir() {
0071         local ARGS
0072 
0073         TMPDIR=$(mktemp -d)
0074         if [ $# -ge 2 ] ; then
0075                 ARGS=${@:1:(($# - 1))}
0076         fi
0077         echo -e "\$PWD:    $PWD"
0078         echo -e "command: make -s $ARGS ${@: -1}=$TMPDIR/ >/dev/null"
0079         make $J -s $ARGS ${@: -1}=$TMPDIR/ >/dev/null
0080         if [ $? -ne 0 ] ; then
0081                 ERROR=1
0082         fi
0083         check $TMPDIR
0084         rm -rf -- $TMPDIR
0085         echo
0086 }
0087 
0088 echo "Trying to build bpftool"
0089 echo -e "... through kbuild\n"
0090 
0091 if [ -f ".config" ] ; then
0092         make_and_clean tools/bpf
0093         ## "make tools/bpf" sets $(OUTPUT) to ...tools/bpf/runqslower for
0094         ## runqslower, but the default (used for the "clean" target) is .output.
0095         ## Let's make sure we clean runqslower's directory properly.
0096         make -C tools/bpf/runqslower OUTPUT=${KDIR_ROOT_DIR}/tools/bpf/runqslower/ clean
0097 
0098         ## $OUTPUT is overwritten in kbuild Makefile, and thus cannot be passed
0099         ## down from toplevel Makefile to bpftool's Makefile.
0100 
0101         # make_with_tmpdir tools/bpf OUTPUT
0102         echo -e "skip:    make tools/bpf OUTPUT=<dir> (not supported)\n"
0103 
0104         make_with_tmpdir tools/bpf O
0105 else
0106         echo -e "skip:    make tools/bpf (no .config found)\n"
0107         echo -e "skip:    make tools/bpf OUTPUT=<dir> (not supported)\n"
0108         echo -e "skip:    make tools/bpf O=<dir> (no .config found)\n"
0109 fi
0110 
0111 echo -e "... from kernel source tree\n"
0112 
0113 make_and_clean -C tools/bpf/bpftool
0114 
0115 make_with_tmpdir -C tools/bpf/bpftool OUTPUT
0116 
0117 make_with_tmpdir -C tools/bpf/bpftool O
0118 
0119 echo -e "... from tools/\n"
0120 cd tools/
0121 
0122 make_and_clean bpf
0123 
0124 ## In tools/bpf/Makefile, function "descend" is called and passes $(O) and
0125 ## $(OUTPUT). We would like $(OUTPUT) to have "bpf/bpftool/" appended before
0126 ## calling bpftool's Makefile, but this is not the case as the "descend"
0127 ## function focuses on $(O)/$(subdir). However, in the present case, updating
0128 ## $(O) to have $(OUTPUT) recomputed from it in bpftool's Makefile does not
0129 ## work, because $(O) is not defined from command line and $(OUTPUT) is not
0130 ## updated in tools/scripts/Makefile.include.
0131 ##
0132 ## Workarounds would require to a) edit "descend" or use an alternative way to
0133 ## call bpftool's Makefile, b) modify the conditions to update $(OUTPUT) and
0134 ## other variables in tools/scripts/Makefile.include (at the risk of breaking
0135 ## the build of other tools), or c) append manually the "bpf/bpftool" suffix to
0136 ## $(OUTPUT) in bpf's Makefile, which may break if targets for other directories
0137 ## use "descend" in the future.
0138 
0139 # make_with_tmpdir bpf OUTPUT
0140 echo -e "skip:    make bpf OUTPUT=<dir> (not supported)\n"
0141 
0142 make_with_tmpdir bpf O
0143 
0144 echo -e "... from bpftool's dir\n"
0145 cd bpf/bpftool
0146 
0147 make_and_clean
0148 
0149 make_with_tmpdir OUTPUT
0150 
0151 make_with_tmpdir O