kselftest_deps.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # kselftest_deps.sh
  4. #
  5. # Checks for kselftest build dependencies on the build system.
  6. # Copyright (c) 2020 Shuah Khan <[email protected]>
  7. #
  8. #
  9. usage()
  10. {
  11. echo -e "Usage: $0 -[p] <compiler> [test_name]\n"
  12. echo -e "\tkselftest_deps.sh [-p] gcc"
  13. echo -e "\tkselftest_deps.sh [-p] gcc vm"
  14. echo -e "\tkselftest_deps.sh [-p] aarch64-linux-gnu-gcc"
  15. echo -e "\tkselftest_deps.sh [-p] aarch64-linux-gnu-gcc vm\n"
  16. echo "- Should be run in selftests directory in the kernel repo."
  17. echo "- Checks if Kselftests can be built/cross-built on a system."
  18. echo "- Parses all test/sub-test Makefile to find library dependencies."
  19. echo "- Runs compile test on a trivial C file with LDLIBS specified"
  20. echo " in the test Makefiles to identify missing library dependencies."
  21. echo "- Prints suggested target list for a system filtering out tests"
  22. echo " failed the build dependency check from the TARGETS in Selftests"
  23. echo " main Makefile when optional -p is specified."
  24. echo "- Prints pass/fail dependency check for each tests/sub-test."
  25. echo "- Prints pass/fail targets and libraries."
  26. echo "- Default: runs dependency checks on all tests."
  27. echo "- Optional: test name can be specified to check dependencies for it."
  28. exit 1
  29. }
  30. # Start main()
  31. main()
  32. {
  33. base_dir=`pwd`
  34. # Make sure we're in the selftests top-level directory.
  35. if [ $(basename "$base_dir") != "selftests" ]; then
  36. echo -e "\tPlease run $0 in"
  37. echo -e "\ttools/testing/selftests directory ..."
  38. exit 1
  39. fi
  40. print_targets=0
  41. while getopts "p" arg; do
  42. case $arg in
  43. p)
  44. print_targets=1
  45. shift;;
  46. esac
  47. done
  48. if [ $# -eq 0 ]
  49. then
  50. usage
  51. fi
  52. # Compiler
  53. CC=$1
  54. tmp_file=$(mktemp).c
  55. trap "rm -f $tmp_file.o $tmp_file $tmp_file.bin" EXIT
  56. #echo $tmp_file
  57. pass=$(mktemp).out
  58. trap "rm -f $pass" EXIT
  59. #echo $pass
  60. fail=$(mktemp).out
  61. trap "rm -f $fail" EXIT
  62. #echo $fail
  63. # Generate tmp source fire for compile test
  64. cat << "EOF" > $tmp_file
  65. int main()
  66. {
  67. }
  68. EOF
  69. # Save results
  70. total_cnt=0
  71. fail_trgts=()
  72. fail_libs=()
  73. fail_cnt=0
  74. pass_trgts=()
  75. pass_libs=()
  76. pass_cnt=0
  77. # Get all TARGETS from selftests Makefile
  78. targets=$(egrep "^TARGETS +|^TARGETS =" Makefile | cut -d "=" -f2)
  79. # Initially, in LDLIBS related lines, the dep checker needs
  80. # to ignore lines containing the following strings:
  81. filter="\$(VAR_LDLIBS)\|pkg-config\|PKG_CONFIG\|IOURING_EXTRA_LIBS"
  82. # Single test case
  83. if [ $# -eq 2 ]
  84. then
  85. test=$2/Makefile
  86. l1_test $test
  87. l2_test $test
  88. l3_test $test
  89. l4_test $test
  90. l5_test $test
  91. print_results $1 $2
  92. exit $?
  93. fi
  94. # Level 1: LDLIBS set static.
  95. #
  96. # Find all LDLIBS set statically for all executables built by a Makefile
  97. # and filter out VAR_LDLIBS to discard the following:
  98. # gpio/Makefile:LDLIBS += $(VAR_LDLIBS)
  99. # Append space at the end of the list to append more tests.
  100. l1_tests=$(grep -r --include=Makefile "^LDLIBS" | \
  101. grep -v "$filter" | awk -F: '{print $1}' | uniq)
  102. # Level 2: LDLIBS set dynamically.
  103. #
  104. # Level 2
  105. # Some tests have multiple valid LDLIBS lines for individual sub-tests
  106. # that need dependency checks. Find them and append them to the tests
  107. # e.g: vm/Makefile:$(OUTPUT)/userfaultfd: LDLIBS += -lpthread
  108. # Filter out VAR_LDLIBS to discard the following:
  109. # memfd/Makefile:$(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS)
  110. # Append space at the end of the list to append more tests.
  111. l2_tests=$(grep -r --include=Makefile ": LDLIBS" | \
  112. grep -v "$filter" | awk -F: '{print $1}' | uniq)
  113. # Level 3
  114. # memfd and others use pkg-config to find mount and fuse libs
  115. # respectively and save it in VAR_LDLIBS. If pkg-config doesn't find
  116. # any, VAR_LDLIBS set to default.
  117. # Use the default value and filter out pkg-config for dependency check.
  118. # e.g:
  119. # memfd/Makefile
  120. # VAR_LDLIBS := $(shell pkg-config fuse --libs 2>/dev/null)
  121. l3_tests=$(grep -r --include=Makefile "^VAR_LDLIBS" | \
  122. grep -v "pkg-config\|PKG_CONFIG" | awk -F: '{print $1}' | uniq)
  123. # Level 4
  124. # some tests may fall back to default using `|| echo -l<libname>`
  125. # if pkg-config doesn't find the libs, instead of using VAR_LDLIBS
  126. # as per level 3 checks.
  127. # e.g:
  128. # netfilter/Makefile
  129. # LDLIBS += $(shell $(HOSTPKG_CONFIG) --libs libmnl 2>/dev/null || echo -lmnl)
  130. l4_tests=$(grep -r --include=Makefile "^LDLIBS" | \
  131. grep "pkg-config\|PKG_CONFIG" | awk -F: '{print $1}' | uniq)
  132. # Level 5
  133. # some tests may use IOURING_EXTRA_LIBS to add extra libs to LDLIBS,
  134. # which in turn may be defined in a sub-Makefile
  135. # e.g.:
  136. # mm/Makefile
  137. # $(OUTPUT)/gup_longterm: LDLIBS += $(IOURING_EXTRA_LIBS)
  138. l5_tests=$(grep -r --include=Makefile "LDLIBS +=.*\$(IOURING_EXTRA_LIBS)" | \
  139. awk -F: '{print $1}' | uniq)
  140. #echo l1_tests $l1_tests
  141. #echo l2_tests $l2_tests
  142. #echo l3_tests $l3_tests
  143. #echo l4_tests $l4_tests
  144. #echo l5_tests $l5_tests
  145. all_tests
  146. print_results $1 $2
  147. exit $?
  148. }
  149. # end main()
  150. all_tests()
  151. {
  152. for test in $l1_tests; do
  153. l1_test $test
  154. done
  155. for test in $l2_tests; do
  156. l2_test $test
  157. done
  158. for test in $l3_tests; do
  159. l3_test $test
  160. done
  161. for test in $l4_tests; do
  162. l4_test $test
  163. done
  164. for test in $l5_tests; do
  165. l5_test $test
  166. done
  167. }
  168. # Use same parsing used for l1_tests and pick libraries this time.
  169. l1_test()
  170. {
  171. test_libs=$(grep --include=Makefile "^LDLIBS" $test | \
  172. grep -v "$filter" | \
  173. sed -e 's/\:/ /' | \
  174. sed -e 's/+/ /' | cut -d "=" -f 2)
  175. check_libs $test $test_libs
  176. }
  177. # Use same parsing used for l2_tests and pick libraries this time.
  178. l2_test()
  179. {
  180. test_libs=$(grep --include=Makefile ": LDLIBS" $test | \
  181. grep -v "$filter" | \
  182. sed -e 's/\:/ /' | sed -e 's/+/ /' | \
  183. cut -d "=" -f 2)
  184. check_libs $test $test_libs
  185. }
  186. l3_test()
  187. {
  188. test_libs=$(grep --include=Makefile "^VAR_LDLIBS" $test | \
  189. grep -v "pkg-config" | sed -e 's/\:/ /' |
  190. sed -e 's/+/ /' | cut -d "=" -f 2)
  191. check_libs $test $test_libs
  192. }
  193. l4_test()
  194. {
  195. test_libs=$(grep --include=Makefile "^VAR_LDLIBS\|^LDLIBS" $test | \
  196. grep "\(pkg-config\|PKG_CONFIG\).*|| echo " | \
  197. sed -e 's/.*|| echo //' | sed -e 's/)$//')
  198. check_libs $test $test_libs
  199. }
  200. l5_test()
  201. {
  202. tests=$(find $(dirname "$test") -type f -name "*.mk")
  203. test_libs=$(grep "^IOURING_EXTRA_LIBS +\?=" $tests | \
  204. cut -d "=" -f 2)
  205. check_libs $test $test_libs
  206. }
  207. check_libs()
  208. {
  209. if [[ ! -z "${test_libs// }" ]]
  210. then
  211. #echo $test_libs
  212. for lib in $test_libs; do
  213. let total_cnt+=1
  214. $CC -o $tmp_file.bin $lib $tmp_file > /dev/null 2>&1
  215. if [ $? -ne 0 ]; then
  216. echo "FAIL: $test dependency check: $lib" >> $fail
  217. let fail_cnt+=1
  218. fail_libs+="$lib "
  219. fail_target=$(echo "$test" | cut -d "/" -f1)
  220. fail_trgts+="$fail_target "
  221. targets=$(echo "$targets" | grep -v "$fail_target")
  222. else
  223. echo "PASS: $test dependency check passed $lib" >> $pass
  224. let pass_cnt+=1
  225. pass_libs+="$lib "
  226. pass_trgts+="$(echo "$test" | cut -d "/" -f1) "
  227. fi
  228. done
  229. fi
  230. }
  231. print_results()
  232. {
  233. echo -e "========================================================";
  234. echo -e "Kselftest Dependency Check for [$0 $1 $2] results..."
  235. if [ $print_targets -ne 0 ]
  236. then
  237. echo -e "Suggested Selftest Targets for your configuration:"
  238. echo -e "$targets";
  239. fi
  240. echo -e "========================================================";
  241. echo -e "Checked tests defining LDLIBS dependencies"
  242. echo -e "--------------------------------------------------------";
  243. echo -e "Total tests with Dependencies:"
  244. echo -e "$total_cnt Pass: $pass_cnt Fail: $fail_cnt";
  245. if [ $pass_cnt -ne 0 ]; then
  246. echo -e "--------------------------------------------------------";
  247. cat $pass
  248. echo -e "--------------------------------------------------------";
  249. echo -e "Targets passed build dependency check on system:"
  250. echo -e "$(echo "$pass_trgts" | xargs -n1 | sort -u | xargs)"
  251. fi
  252. if [ $fail_cnt -ne 0 ]; then
  253. echo -e "--------------------------------------------------------";
  254. cat $fail
  255. echo -e "--------------------------------------------------------";
  256. echo -e "Targets failed build dependency check on system:"
  257. echo -e "$(echo "$fail_trgts" | xargs -n1 | sort -u | xargs)"
  258. echo -e "--------------------------------------------------------";
  259. echo -e "Missing libraries system"
  260. echo -e "$(echo "$fail_libs" | xargs -n1 | sort -u | xargs)"
  261. fi
  262. echo -e "--------------------------------------------------------";
  263. echo -e "========================================================";
  264. }
  265. main "$@"