2 # SPDX-License-Identifier: GPL-2.0
5 # Checks for kselftest build dependencies on the build system.
6 # Copyright (c) 2020 Shuah Khan <skhan@linuxfoundation.org>
13 echo -e "Usage: $0 -[p] <compiler> [test_name]\n"
14 echo -e "\tkselftest_deps.sh [-p] gcc"
15 echo -e "\tkselftest_deps.sh [-p] gcc vm"
16 echo -e "\tkselftest_deps.sh [-p] aarch64-linux-gnu-gcc"
17 echo -e "\tkselftest_deps.sh [-p] aarch64-linux-gnu-gcc vm\n"
18 echo "- Should be run in selftests directory in the kernel repo."
19 echo "- Checks if Kselftests can be built/cross-built on a system."
20 echo "- Parses all test/sub-test Makefile to find library dependencies."
21 echo "- Runs compile test on a trivial C file with LDLIBS specified"
22 echo " in the test Makefiles to identify missing library dependencies."
23 echo "- Prints suggested target list for a system filtering out tests"
24 echo " failed the build dependency check from the TARGETS in Selftests"
25 echo " main Makefile when optional -p is specified."
26 echo "- Prints pass/fail dependency check for each tests/sub-test."
27 echo "- Prints pass/fail targets and libraries."
28 echo "- Default: runs dependency checks on all tests."
29 echo "- Optional test name can be specified to check dependencies for it."
39 # Make sure we're in the selftests top-level directory.
40 if [ $(basename "$base_dir") != "selftests" ]; then
41 echo -e "\tPlease run $0 in"
42 echo -e "\ttools/testing/selftests directory ..."
48 while getopts "p" arg; do
65 trap "rm -f $tmp_file.o $tmp_file $tmp_file.bin" EXIT
69 trap "rm -f $pass" EXIT
73 trap "rm -f $fail" EXIT
76 # Generate tmp source fire for compile test
77 cat << "EOF" > $tmp_file
92 # Get all TARGETS from selftests Makefile
93 targets=$(egrep "^TARGETS +|^TARGETS =" Makefile | cut -d "=" -f2)
95 # Initially, in LDLIBS related lines, the dep checker needs
96 # to ignore lines containing the following strings:
97 filter="\$(VAR_LDLIBS)\|pkg-config\|PKG_CONFIG\|IOURING_EXTRA_LIBS"
114 # Level 1: LDLIBS set static.
116 # Find all LDLIBS set statically for all executables built by a Makefile
117 # and filter out VAR_LDLIBS to discard the following:
118 # gpio/Makefile:LDLIBS += $(VAR_LDLIBS)
119 # Append space at the end of the list to append more tests.
121 l1_tests=$(grep -r --include=Makefile "^LDLIBS" | \
122 grep -v "$filter" | awk -F: '{print $1}' | uniq)
124 # Level 2: LDLIBS set dynamically.
127 # Some tests have multiple valid LDLIBS lines for individual sub-tests
128 # that need dependency checks. Find them and append them to the tests
129 # e.g: vm/Makefile:$(OUTPUT)/userfaultfd: LDLIBS += -lpthread
130 # Filter out VAR_LDLIBS to discard the following:
131 # memfd/Makefile:$(OUTPUT)/fuse_mnt: LDLIBS += $(VAR_LDLIBS)
132 # Append space at the end of the list to append more tests.
134 l2_tests=$(grep -r --include=Makefile ": LDLIBS" | \
135 grep -v "$filter" | awk -F: '{print $1}' | uniq)
138 # gpio, memfd and others use pkg-config to find mount and fuse libs
139 # respectively and save it in VAR_LDLIBS. If pkg-config doesn't find
140 # any, VAR_LDLIBS set to default.
141 # Use the default value and filter out pkg-config for dependency check.
144 # VAR_LDLIBS := $(shell pkg-config --libs mount) 2>/dev/null)
146 # VAR_LDLIBS := $(shell pkg-config fuse --libs 2>/dev/null)
148 l3_tests=$(grep -r --include=Makefile "^VAR_LDLIBS" | \
149 grep -v "pkg-config\|PKG_CONFIG" | awk -F: '{print $1}' | uniq)
152 # some tests may fall back to default using `|| echo -l<libname>`
153 # if pkg-config doesn't find the libs, instead of using VAR_LDLIBS
154 # as per level 3 checks.
157 # LDLIBS += $(shell $(HOSTPKG_CONFIG) --libs libmnl 2>/dev/null || echo -lmnl)
158 l4_tests=$(grep -r --include=Makefile "^LDLIBS" | \
159 grep "pkg-config\|PKG_CONFIG" | awk -F: '{print $1}' | uniq)
162 # some tests may use IOURING_EXTRA_LIBS to add extra libs to LDLIBS,
163 # which in turn may be defined in a sub-Makefile
166 # $(OUTPUT)/gup_longterm: LDLIBS += $(IOURING_EXTRA_LIBS)
167 l5_tests=$(grep -r --include=Makefile "LDLIBS +=.*\$(IOURING_EXTRA_LIBS)" | \
168 awk -F: '{print $1}' | uniq)
170 #echo l1_tests $l1_tests
171 #echo l2_tests $l2_tests
172 #echo l3_tests $l3_tests
173 #echo l4_tests $l4_tests
174 #echo l5_tests $l5_tests
185 for test in $l1_tests; do
189 for test in $l2_tests; do
193 for test in $l3_tests; do
197 for test in $l4_tests; do
201 for test in $l5_tests; do
206 # Use same parsing used for l1_tests and pick libraries this time.
209 test_libs=$(grep --include=Makefile "^LDLIBS" $test | \
210 grep -v "$filter" | \
212 sed -e 's/+/ /' | cut -d "=" -f 2)
214 check_libs $test $test_libs
217 # Use same parsing used for l2_tests and pick libraries this time.
220 test_libs=$(grep --include=Makefile ": LDLIBS" $test | \
221 grep -v "$filter" | \
222 sed -e 's/\:/ /' | sed -e 's/+/ /' | \
225 check_libs $test $test_libs
230 test_libs=$(grep --include=Makefile "^VAR_LDLIBS" $test | \
231 grep -v "pkg-config" | sed -e 's/\:/ /' |
232 sed -e 's/+/ /' | cut -d "=" -f 2)
234 check_libs $test $test_libs
239 test_libs=$(grep --include=Makefile "^VAR_LDLIBS\|^LDLIBS" $test | \
240 grep "\(pkg-config\|PKG_CONFIG\).*|| echo " | \
241 sed -e 's/.*|| echo //' | sed -e 's/)$//')
243 check_libs $test $test_libs
248 tests=$(find $(dirname "$test") -type f -name "*.mk")
249 test_libs=$(grep "^IOURING_EXTRA_LIBS +\?=" $tests | \
252 check_libs $test $test_libs
258 if [[ ! -z "${test_libs// }" ]]
263 for lib in $test_libs; do
266 $CC -o $tmp_file.bin $lib $tmp_file > /dev/null 2>&1
267 if [ $? -ne 0 ]; then
268 echo "FAIL: $test dependency check: $lib" >> $fail
271 fail_target=$(echo "$test" | cut -d "/" -f1)
272 fail_trgts+="$fail_target "
273 targets=$(echo "$targets" | grep -v "$fail_target")
275 echo "PASS: $test dependency check passed $lib" >> $pass
278 pass_trgts+="$(echo "$test" | cut -d "/" -f1) "
287 echo -e "========================================================";
288 echo -e "Kselftest Dependency Check for [$0 $1 $2] results..."
290 if [ $print_targets -ne 0 ]
292 echo -e "Suggested Selftest Targets for your configuration:"
296 echo -e "========================================================";
297 echo -e "Checked tests defining LDLIBS dependencies"
298 echo -e "--------------------------------------------------------";
299 echo -e "Total tests with Dependencies:"
300 echo -e "$total_cnt Pass: $pass_cnt Fail: $fail_cnt";
302 if [ $pass_cnt -ne 0 ]; then
303 echo -e "--------------------------------------------------------";
305 echo -e "--------------------------------------------------------";
306 echo -e "Targets passed build dependency check on system:"
307 echo -e "$(echo "$pass_trgts" | xargs -n1 | sort -u | xargs)"
310 if [ $fail_cnt -ne 0 ]; then
311 echo -e "--------------------------------------------------------";
313 echo -e "--------------------------------------------------------";
314 echo -e "Targets failed build dependency check on system:"
315 echo -e "$(echo "$fail_trgts" | xargs -n1 | sort -u | xargs)"
316 echo -e "--------------------------------------------------------";
317 echo -e "Missing libraries system"
318 echo -e "$(echo "$fail_libs" | xargs -n1 | sort -u | xargs)"
321 echo -e "--------------------------------------------------------";
322 echo -e "========================================================";