test_bpftool_build.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  3. case $1 in
  4. -h|--help)
  5. echo -e "$0 [-j <n>]"
  6. echo -e "\tTest the different ways of building bpftool."
  7. echo -e ""
  8. echo -e "\tOptions:"
  9. echo -e "\t\t-j <n>:\tPass -j flag to 'make'."
  10. exit 0
  11. ;;
  12. esac
  13. J=$*
  14. # Assume script is located under tools/testing/selftests/bpf/. We want to start
  15. # build attempts from the top of kernel repository.
  16. SCRIPT_REL_PATH=$(realpath --relative-to=$PWD $0)
  17. SCRIPT_REL_DIR=$(dirname $SCRIPT_REL_PATH)
  18. KDIR_ROOT_DIR=$(realpath $PWD/$SCRIPT_REL_DIR/../../../../)
  19. cd $KDIR_ROOT_DIR
  20. if [ ! -e tools/bpf/bpftool/Makefile ]; then
  21. echo -e "skip: bpftool files not found!\n"
  22. exit 4 # KSFT_SKIP=4
  23. fi
  24. ERROR=0
  25. TMPDIR=
  26. # If one build fails, continue but return non-0 on exit.
  27. return_value() {
  28. if [ -d "$TMPDIR" ] ; then
  29. rm -rf -- $TMPDIR
  30. fi
  31. exit $ERROR
  32. }
  33. trap return_value EXIT
  34. check() {
  35. local dir=$(realpath $1)
  36. echo -n "binary: "
  37. # Returns non-null if file is found (and "false" is run)
  38. find $dir -type f -executable -name bpftool -print -exec false {} + && \
  39. ERROR=1 && printf "FAILURE: Did not find bpftool\n"
  40. }
  41. make_and_clean() {
  42. echo -e "\$PWD: $PWD"
  43. echo -e "command: make -s $* >/dev/null"
  44. make $J -s $* >/dev/null
  45. if [ $? -ne 0 ] ; then
  46. ERROR=1
  47. fi
  48. if [ $# -ge 1 ] ; then
  49. check ${@: -1}
  50. else
  51. check .
  52. fi
  53. (
  54. if [ $# -ge 1 ] ; then
  55. cd ${@: -1}
  56. fi
  57. make -s clean
  58. )
  59. echo
  60. }
  61. make_with_tmpdir() {
  62. local ARGS
  63. TMPDIR=$(mktemp -d)
  64. if [ $# -ge 2 ] ; then
  65. ARGS=${@:1:(($# - 1))}
  66. fi
  67. echo -e "\$PWD: $PWD"
  68. echo -e "command: make -s $ARGS ${@: -1}=$TMPDIR/ >/dev/null"
  69. make $J -s $ARGS ${@: -1}=$TMPDIR/ >/dev/null
  70. if [ $? -ne 0 ] ; then
  71. ERROR=1
  72. fi
  73. check $TMPDIR
  74. rm -rf -- $TMPDIR
  75. echo
  76. }
  77. echo "Trying to build bpftool"
  78. echo -e "... through kbuild\n"
  79. if [ -f ".config" ] ; then
  80. make_and_clean tools/bpf
  81. ## "make tools/bpf" sets $(OUTPUT) to ...tools/bpf/runqslower for
  82. ## runqslower, but the default (used for the "clean" target) is .output.
  83. ## Let's make sure we clean runqslower's directory properly.
  84. make -C tools/bpf/runqslower OUTPUT=${KDIR_ROOT_DIR}/tools/bpf/runqslower/ clean
  85. ## $OUTPUT is overwritten in kbuild Makefile, and thus cannot be passed
  86. ## down from toplevel Makefile to bpftool's Makefile.
  87. # make_with_tmpdir tools/bpf OUTPUT
  88. echo -e "skip: make tools/bpf OUTPUT=<dir> (not supported)\n"
  89. make_with_tmpdir tools/bpf O
  90. else
  91. echo -e "skip: make tools/bpf (no .config found)\n"
  92. echo -e "skip: make tools/bpf OUTPUT=<dir> (not supported)\n"
  93. echo -e "skip: make tools/bpf O=<dir> (no .config found)\n"
  94. fi
  95. echo -e "... from kernel source tree\n"
  96. make_and_clean -C tools/bpf/bpftool
  97. make_with_tmpdir -C tools/bpf/bpftool OUTPUT
  98. make_with_tmpdir -C tools/bpf/bpftool O
  99. echo -e "... from tools/\n"
  100. cd tools/
  101. make_and_clean bpf
  102. ## In tools/bpf/Makefile, function "descend" is called and passes $(O) and
  103. ## $(OUTPUT). We would like $(OUTPUT) to have "bpf/bpftool/" appended before
  104. ## calling bpftool's Makefile, but this is not the case as the "descend"
  105. ## function focuses on $(O)/$(subdir). However, in the present case, updating
  106. ## $(O) to have $(OUTPUT) recomputed from it in bpftool's Makefile does not
  107. ## work, because $(O) is not defined from command line and $(OUTPUT) is not
  108. ## updated in tools/scripts/Makefile.include.
  109. ##
  110. ## Workarounds would require to a) edit "descend" or use an alternative way to
  111. ## call bpftool's Makefile, b) modify the conditions to update $(OUTPUT) and
  112. ## other variables in tools/scripts/Makefile.include (at the risk of breaking
  113. ## the build of other tools), or c) append manually the "bpf/bpftool" suffix to
  114. ## $(OUTPUT) in bpf's Makefile, which may break if targets for other directories
  115. ## use "descend" in the future.
  116. # make_with_tmpdir bpf OUTPUT
  117. echo -e "skip: make bpf OUTPUT=<dir> (not supported)\n"
  118. make_with_tmpdir bpf O
  119. echo -e "... from bpftool's dir\n"
  120. cd bpf/bpftool
  121. make_and_clean
  122. make_with_tmpdir OUTPUT
  123. make_with_tmpdir O