test_bpftool_build.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. ## $OUTPUT is overwritten in kbuild Makefile, and thus cannot be passed
  82. ## down from toplevel Makefile to bpftool's Makefile.
  83. # make_with_tmpdir tools/bpf OUTPUT
  84. echo -e "skip: make tools/bpf OUTPUT=<dir> (not supported)\n"
  85. make_with_tmpdir tools/bpf O
  86. else
  87. echo -e "skip: make tools/bpf (no .config found)\n"
  88. echo -e "skip: make tools/bpf OUTPUT=<dir> (not supported)\n"
  89. echo -e "skip: make tools/bpf O=<dir> (no .config found)\n"
  90. fi
  91. echo -e "... from kernel source tree\n"
  92. make_and_clean -C tools/bpf/bpftool
  93. make_with_tmpdir -C tools/bpf/bpftool OUTPUT
  94. make_with_tmpdir -C tools/bpf/bpftool O
  95. echo -e "... from tools/\n"
  96. cd tools/
  97. make_and_clean bpf
  98. ## In tools/bpf/Makefile, function "descend" is called and passes $(O) and
  99. ## $(OUTPUT). We would like $(OUTPUT) to have "bpf/bpftool/" appended before
  100. ## calling bpftool's Makefile, but this is not the case as the "descend"
  101. ## function focuses on $(O)/$(subdir). However, in the present case, updating
  102. ## $(O) to have $(OUTPUT) recomputed from it in bpftool's Makefile does not
  103. ## work, because $(O) is not defined from command line and $(OUTPUT) is not
  104. ## updated in tools/scripts/Makefile.include.
  105. ##
  106. ## Workarounds would require to a) edit "descend" or use an alternative way to
  107. ## call bpftool's Makefile, b) modify the conditions to update $(OUTPUT) and
  108. ## other variables in tools/scripts/Makefile.include (at the risk of breaking
  109. ## the build of other tools), or c) append manually the "bpf/bpftool" suffix to
  110. ## $(OUTPUT) in bpf's Makefile, which may break if targets for other directories
  111. ## use "descend" in the future.
  112. # make_with_tmpdir bpf OUTPUT
  113. echo -e "skip: make bpf OUTPUT=<dir> (not supported)\n"
  114. make_with_tmpdir bpf O
  115. echo -e "... from bpftool's dir\n"
  116. cd bpf/bpftool
  117. make_and_clean
  118. make_with_tmpdir OUTPUT
  119. make_with_tmpdir O