-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen_compat_def
executable file
·101 lines (88 loc) · 2.32 KB
/
gen_compat_def
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash -efu
# SPDX-License-Identifier: GPL-2.0-only
#
# Generate defines based on kernel having
# some symbols declared
#
# Copyright (C) 2019-2020 <abc@openwall.com>
#
export LANG=C
fatal() {
echo "Error: $*" >&2
exit 1
}
eval $(grep ^KDIR Makefile | tr -d ' ')
[ "$KDIR" ] || fatal "KDIR is not found"
WD=cc-test-build
mkdir -p $WD
cd ./$WD || fatal "cannot cd to $WD"
# args: HAVE_SUMBOL symbol include
kbuild_test_compile() {
local cmd
cat > test.c
echo obj-m = test.o > Makefile
cmd="make -s -C $KDIR M=$PWD modules"
echo "$cmd" > log
if $cmd >> log 2>&1; then
[ "$2" ] && echo "// $2 is declared ${3:+in <$3>}"
echo "#define HAVE_$1"
echo
else
echo "#undef HAVE_$1"
echo "// ${2:-symbol} is undeclared${3:+ in <$3>}. Compile:"
sed "s/^/\/\/ /" test.c
echo "// Output:"
sed "s/^/\/\/ /" log
echo
if ! egrep -q 'has no member named|undeclared|storage size of .* isn.t known|No such file or directory' log; then
echo "Error: unexpected error from compiler" >&2
cat log >&2
echo >&2
exit 3
fi
fi
}
# args: global_symbol include_header
# Test that symbol is defined.
kbuild_test_symbol() {
kbuild_test_compile ${1^^} $1 ${2-} <<-EOF
#include <linux/module.h>
${2:+#include <$2>}
MODULE_LICENSE("GPL");
void *test = $1;
EOF
}
# args: struct_name struct_field include_header
kbuild_test_field() {
kbuild_test_compile ${1^^}_${2^^} "struct $1.$2" ${3-} <<-EOF
#include <linux/module.h>
${3:+#include <$3>}
MODULE_LICENSE("GPL");
size_t test = sizeof(((struct $1 *)0)->$2);
EOF
}
# Test that struct is defined.
kbuild_test_struct() {
kbuild_test_compile ${1^^} "struct $1" ${2-} <<-EOF
#include <linux/module.h>
${2:+#include <$2>}
MODULE_LICENSE("GPL");
struct $1 test;
EOF
}
echo "// Autogenerated for $KDIR"
echo
# helpers introduced in 613dbd95723aee7abd16860745691b6c7bda20dc
kbuild_test_symbol xt_family linux/netfilter_ipv4/ip_tables.h
kbuild_test_field sk_buff skb_iif linux/skbuff.h
kbuild_test_struct timeval linux/ktime.h
# 97a32539b9568 proc: convert everything to "struct proc_ops"
# d56c0d45f0e27 proc: decouple proc from VFS with "struct proc_ops"
kbuild_test_struct proc_ops linux/proc_fs.h
echo "// End of compat_def.h"
cd $OLDPWD
rm -rf $WD
# debug output for Travis
if [ -z "${PWD/*travis*}" ]; then
cat compat_def.h >&2
fi