Path: blob/master/tools/testing/selftests/kselftest_deps.sh
26298 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.02# kselftest_deps.sh3#4# Checks for kselftest build dependencies on the build system.5# Copyright (c) 2020 Shuah Khan <[email protected]>6#7#89usage()10{1112echo -e "Usage: $0 -[p] <compiler> [test_name]\n"13echo -e "\tkselftest_deps.sh [-p] gcc"14echo -e "\tkselftest_deps.sh [-p] gcc mm"15echo -e "\tkselftest_deps.sh [-p] aarch64-linux-gnu-gcc"16echo -e "\tkselftest_deps.sh [-p] aarch64-linux-gnu-gcc mm\n"17echo "- Should be run in selftests directory in the kernel repo."18echo "- Checks if Kselftests can be built/cross-built on a system."19echo "- Parses all test/sub-test Makefile to find library dependencies."20echo "- Runs compile test on a trivial C file with LDLIBS specified"21echo " in the test Makefiles to identify missing library dependencies."22echo "- Prints suggested target list for a system filtering out tests"23echo " failed the build dependency check from the TARGETS in Selftests"24echo " main Makefile when optional -p is specified."25echo "- Prints pass/fail dependency check for each tests/sub-test."26echo "- Prints pass/fail targets and libraries."27echo "- Default: runs dependency checks on all tests."28echo "- Optional: test name can be specified to check dependencies for it."29exit 13031}3233# Start main()34main()35{3637base_dir=`pwd`38# Make sure we're in the selftests top-level directory.39if [ $(basename "$base_dir") != "selftests" ]; then40echo -e "\tPlease run $0 in"41echo -e "\ttools/testing/selftests directory ..."42exit 143fi4445print_targets=04647while getopts "p" arg; do48case $arg in49p)50print_targets=151shift;;52esac53done5455if [ $# -eq 0 ]56then57usage58fi5960# Compiler61CC=$16263tmp_file=$(mktemp).c64trap "rm -f $tmp_file.o $tmp_file $tmp_file.bin" EXIT65#echo $tmp_file6667pass=$(mktemp).out68trap "rm -f $pass" EXIT69#echo $pass7071fail=$(mktemp).out72trap "rm -f $fail" EXIT73#echo $fail7475# Generate tmp source fire for compile test76cat << "EOF" > $tmp_file77int main()78{79}80EOF8182# Save results83total_cnt=084fail_trgts=()85fail_libs=()86fail_cnt=087pass_trgts=()88pass_libs=()89pass_cnt=09091# Get all TARGETS from selftests Makefile92targets=$(grep -E "^TARGETS +|^TARGETS =" Makefile | cut -d "=" -f2)9394# Initially, in LDLIBS related lines, the dep checker needs95# to ignore lines containing the following strings:96filter="\$(VAR_LDLIBS)\|pkg-config\|PKG_CONFIG\|IOURING_EXTRA_LIBS"9798# Single test case99if [ $# -eq 2 ]100then101test=$2/Makefile102103l1_test $test104l2_test $test105l3_test $test106l4_test $test107l5_test $test108109print_results $1 $2110exit $?111fi112113# Level 1: LDLIBS set static.114#115# Find all LDLIBS set statically for all executables built by a Makefile116# and filter out VAR_LDLIBS to discard the following:117# gpio/Makefile:LDLIBS += $(VAR_LDLIBS)118# Append space at the end of the list to append more tests.119120l1_tests=$(grep -r --include=Makefile "^LDLIBS" | \121grep -v "$filter" | awk -F: '{print $1}' | uniq)122123# Level 2: LDLIBS set dynamically.124#125# Level 2126# Some tests have multiple valid LDLIBS lines for individual sub-tests127# that need dependency checks. Find them and append them to the tests128# e.g: mm/Makefile:$(OUTPUT)/userfaultfd: LDLIBS += -lpthread129# Filter out VAR_LDLIBS to discard the following:130# memfd/Makefile:$(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS)131# Append space at the end of the list to append more tests.132133l2_tests=$(grep -r --include=Makefile ": LDLIBS" | \134grep -v "$filter" | awk -F: '{print $1}' | uniq)135136# Level 3137# memfd and others use pkg-config to find mount and fuse libs138# respectively and save it in VAR_LDLIBS. If pkg-config doesn't find139# any, VAR_LDLIBS set to default.140# Use the default value and filter out pkg-config for dependency check.141# e.g:142# memfd/Makefile143# VAR_LDLIBS := $(shell pkg-config fuse --libs 2>/dev/null)144145l3_tests=$(grep -r --include=Makefile "^VAR_LDLIBS" | \146grep -v "pkg-config\|PKG_CONFIG" | awk -F: '{print $1}' | uniq)147148# Level 4149# some tests may fall back to default using `|| echo -l<libname>`150# if pkg-config doesn't find the libs, instead of using VAR_LDLIBS151# as per level 3 checks.152# e.g:153# netfilter/Makefile154# LDLIBS += $(shell $(HOSTPKG_CONFIG) --libs libmnl 2>/dev/null || echo -lmnl)155l4_tests=$(grep -r --include=Makefile "^LDLIBS" | \156grep "pkg-config\|PKG_CONFIG" | awk -F: '{print $1}' | uniq)157158# Level 5159# some tests may use IOURING_EXTRA_LIBS to add extra libs to LDLIBS,160# which in turn may be defined in a sub-Makefile161# e.g.:162# mm/Makefile163# $(OUTPUT)/gup_longterm: LDLIBS += $(IOURING_EXTRA_LIBS)164l5_tests=$(grep -r --include=Makefile "LDLIBS +=.*\$(IOURING_EXTRA_LIBS)" | \165awk -F: '{print $1}' | uniq)166167#echo l1_tests $l1_tests168#echo l2_tests $l2_tests169#echo l3_tests $l3_tests170#echo l4_tests $l4_tests171#echo l5_tests $l5_tests172173all_tests174print_results $1 $2175176exit $?177}178# end main()179180all_tests()181{182for test in $l1_tests; do183l1_test $test184done185186for test in $l2_tests; do187l2_test $test188done189190for test in $l3_tests; do191l3_test $test192done193194for test in $l4_tests; do195l4_test $test196done197198for test in $l5_tests; do199l5_test $test200done201}202203# Use same parsing used for l1_tests and pick libraries this time.204l1_test()205{206test_libs=$(grep --include=Makefile "^LDLIBS" $test | \207grep -v "$filter" | \208sed -e 's/\:/ /' | \209sed -e 's/+/ /' | cut -d "=" -f 2)210211check_libs $test $test_libs212}213214# Use same parsing used for l2_tests and pick libraries this time.215l2_test()216{217test_libs=$(grep --include=Makefile ": LDLIBS" $test | \218grep -v "$filter" | \219sed -e 's/\:/ /' | sed -e 's/+/ /' | \220cut -d "=" -f 2)221222check_libs $test $test_libs223}224225l3_test()226{227test_libs=$(grep --include=Makefile "^VAR_LDLIBS" $test | \228grep -v "pkg-config" | sed -e 's/\:/ /' |229sed -e 's/+/ /' | cut -d "=" -f 2)230231check_libs $test $test_libs232}233234l4_test()235{236test_libs=$(grep --include=Makefile "^VAR_LDLIBS\|^LDLIBS" $test | \237grep "\(pkg-config\|PKG_CONFIG\).*|| echo " | \238sed -e 's/.*|| echo //' | sed -e 's/)$//')239240check_libs $test $test_libs241}242243l5_test()244{245tests=$(find $(dirname "$test") -type f -name "*.mk")246[[ -z "${tests// }" ]] && return247test_libs=$(grep "^IOURING_EXTRA_LIBS +\?=" $tests | \248cut -d "=" -f 2)249250check_libs $test $test_libs251}252253check_libs()254{255256if [[ ! -z "${test_libs// }" ]]257then258259#echo $test_libs260261for lib in $test_libs; do262263let total_cnt+=1264$CC -o $tmp_file.bin $lib $tmp_file > /dev/null 2>&1265if [ $? -ne 0 ]; then266echo "FAIL: $test dependency check: $lib" >> $fail267let fail_cnt+=1268fail_libs+="$lib "269fail_target=$(echo "$test" | cut -d "/" -f1)270fail_trgts+="$fail_target "271targets=$(echo "$targets" | grep -v "$fail_target")272else273echo "PASS: $test dependency check passed $lib" >> $pass274let pass_cnt+=1275pass_libs+="$lib "276pass_trgts+="$(echo "$test" | cut -d "/" -f1) "277fi278279done280fi281}282283print_results()284{285echo -e "========================================================";286echo -e "Kselftest Dependency Check for [$0 $1 $2] results..."287288if [ $print_targets -ne 0 ]289then290echo -e "Suggested Selftest Targets for your configuration:"291echo -e "$targets";292fi293294echo -e "========================================================";295echo -e "Checked tests defining LDLIBS dependencies"296echo -e "--------------------------------------------------------";297echo -e "Total tests with Dependencies:"298echo -e "$total_cnt Pass: $pass_cnt Fail: $fail_cnt";299300if [ $pass_cnt -ne 0 ]; then301echo -e "--------------------------------------------------------";302cat $pass303echo -e "--------------------------------------------------------";304echo -e "Targets passed build dependency check on system:"305echo -e "$(echo "$pass_trgts" | xargs -n1 | sort -u | xargs)"306fi307308if [ $fail_cnt -ne 0 ]; then309echo -e "--------------------------------------------------------";310cat $fail311echo -e "--------------------------------------------------------";312echo -e "Targets failed build dependency check on system:"313echo -e "$(echo "$fail_trgts" | xargs -n1 | sort -u | xargs)"314echo -e "--------------------------------------------------------";315echo -e "Missing libraries system"316echo -e "$(echo "$fail_libs" | xargs -n1 | sort -u | xargs)"317fi318319echo -e "--------------------------------------------------------";320echo -e "========================================================";321}322323main "$@"324325326