1- #! /bin/bash -efu
1+ #! /bin/bash -fu
22# SPDX-License-Identifier: GPL-2.0-only
33#
44# Generate defines based on kernel having some symbols declared.
@@ -21,7 +21,7 @@ WD=cc-test-build
2121mkdir -p $WD
2222cd ./$WD || fatal " cannot cd to $WD "
2323
24- # args: HAVE_SUMBOL symbol include
24+ # args: HAVE_SYMBOL symbol [ include] [success] [failure]
2525kbuild_test_compile () {
2626 local cmd
2727
@@ -30,14 +30,15 @@ kbuild_test_compile() {
3030 cmd=" make -s -B -C $KDIR M=$PWD modules"
3131 echo " $cmd " > log
3232 if $cmd >> log 2>&1 ; then
33- echo " declared" >&2
34- [ " $2 " ] && echo " // $2 is declared ${3: +in <$3 >} "
33+ echo " ${4- declared} " >&2
34+ [ " $2 " ] && echo " // $2 ${4- is declared} ${3: + in <$3 >}"
3535 echo " #define HAVE_$1 "
3636 echo
37+ return 0
3738 else
38- echo " undeclared" >&2
39+ echo " ${5- undeclared} " >&2
3940 echo " #undef HAVE_$1 "
40- echo " // ${2:- symbol} is undeclared${3: + in <$3 >} . Compile:"
41+ echo " // ${2:- symbol} ${5- is undeclared} ${3: + in <$3 >} . Compile:"
4142 sed " s/^/\/\/ /" test.c
4243 echo " // Output:"
4344 sed " s/^/\/\/ /" log
@@ -56,6 +57,7 @@ kbuild_test_compile() {
5657 echo >&2
5758 exit 3
5859 fi
60+ return 1
5961 fi
6062}
6163
@@ -105,6 +107,22 @@ kbuild_test_member() {
105107 typeof(((struct $structname *)0)->$member ) test;
106108 EOF
107109}
110+
111+ # Test that a header is available/exist
112+ kbuild_test_header () {
113+ echo -n " Test header $* " >&2
114+ structname=${1% .* }
115+ member=${1#* .}
116+ def=${1^^}
117+ def=${def##*/ }
118+ def=${def// ./ _}
119+ kbuild_test_compile $def " header $1 " " " " exists" " doesn't exist" << -EOF
120+ #include <linux/module.h>
121+ #include <$1 >
122+ MODULE_LICENSE("GPL");
123+ EOF
124+ }
125+
108126echo " // Autogenerated for $KDIR "
109127echo
110128
@@ -136,6 +154,20 @@ kbuild_test_member nf_ct_event_notifier.ct_event net/netfilter/nf_conntrack_ecac
136154kbuild_test_symbol register_sysctl_paths linux/sysctl.h
137155# If we have strscpy, we can use that (more optimal compared to strlcpy).
138156kbuild_test_symbol sized_strscpy linux/string.h
157+ # Do we have get_random_u32_below
158+ kbuild_test_symbol get_random_u32_below linux/random.h
159+ # Do we have get_random_u32
160+ kbuild_test_symbol get_random_u32 linux/random.h
161+
162+ # prandom functions moved from random.h to prandom.h recentish.
163+ # We use these for fallback for the above only.
164+ if kbuild_test_header linux/prandom.h; then
165+ prand_h=linux/prandom.h
166+ else
167+ prand_h=linux/random.h
168+ fi
169+ kbuild_test_symbol prandom_u32 $prand_h
170+ kbuild_test_symbol prandom_u32_max $prand_h
139171
140172echo " // End of compat_def.h"
141173
0 commit comments