forked from bgmerrell/driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenmake
executable file
·174 lines (136 loc) · 5 KB
/
genmake
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# Automatic kernel makefile generation
# Jerry Cooperstein, [email protected], 2/2003 - 1/2011
# License: GPLv2
OBJS="" # list of kernel modules (.o files)
K_S="" # list of kernel modules (.c files)
U_S="" # list of userland programs (.c files)
U_X="" # list of userland programs (executables)
T_S="" # list of userland programs (.c files) that use pthreads
T_X="" # list of userland programs (executables) that use pthreads
ALL="" # list of all targets
CFLAGS_U_X="-O2 -Wall -pedantic" # compile flags for user programs
CFLAGS_T_X=$CFLAGS_U_X" -pthread" # compile flags for threaded user programs
# set the default kernel source to the running one; otherwise take from
# first command line argument
if [ "$KROOT" == "" ] ; then
KROOT=/lib/modules/$(uname -r)/build
[ ! -d "$KROOT" ] && KROOT=/usr/src/linux-$(uname -r)
fi
# abort if the source is not present
KMF=$KROOT/Makefile
KERNELSRC=$(grep "^KERNELSRC" $KMF | awk ' {print $3;}')
if [ "$KERNELSRC" != "" ] ; then
echo Primary Makefile is not in $KROOT, using $KERNELSRC
KMF=$KERNELSRC/Makefile
fi
if [ ! -d "$KROOT" ] || [ ! -f "$KMF" ] ; then
echo kernel source directory $KROOT does not exist or has no Makefile
exit 1
fi
# additional flags?
if [ "$KINCS" != "" ] ; then
CFLAGS_U_X="$CFLAGS_U_X -I$KROOT/include"
CFLAGS_T_X="$CFLAGS_T_X -I$KROOT/include"
fi
# extract the VERSION info from the Makefile
KV=$(grep "^VERSION =" $KMF | awk ' {print $3;}')
KP=$(grep "^PATCHLEVEL =" $KMF | awk ' {print $3;}')
KS=$(grep "^SUBLEVEL =" $KMF | awk ' {print $3;}')
KE=$(grep "^EXTRAVERSION =" $KMF | awk ' {print $3;}')
KERNEL=$KV.$KP.$KS$KE
echo KERNEL=$KERNEL, KV=$KV KP=$KP KS=$KS KE=$KE
# construct lists of kernel and user sources and targets
# skip empty directories
if [ "$(find . -maxdepth 1 -name "*.c")" == "" ] ; then
echo No need to make Makefile: no source code
exit
fi
for names in *.c ; do
# exclude files with NOMAKE or .mod.c files
if [ "$(grep NOMAKE $names)" ] || [ "$(grep vermagic $names)" ] ; then
echo "$names is being skipped, it is not a module or program"
else
if [ "$(grep \<linux\/module.h\> $names )" ] ; then
FILENAME_DOTO=$(basename $names .c).o
OBJS=$OBJS" $FILENAME_DOTO"
K_S=$K_S" $names"
else
# is it a pthread'ed program?
if [ "$(grep '\<pthread.h\>' $names)" ] ; then
FILENAME_EXE=$(basename $names .c)
T_X=$T_X" $FILENAME_EXE"
T_S=$T_S" $names"
else
U_X=$U_X" $(basename $names .c)"
U_S=$U_S" $names"
fi
fi
fi
done
CLEANSTUFF="$U_X $T_X"
# maybe there are no kernel modules
if [ "$OBJS" != "" ] ; then
CLEANSTUFF="$CLEANSTUFF"" Module.symvers modules.order"
fi
# get ALL the targets
if [ "$U_X" != "" ] ; then ALL=$ALL" userprogs"; fi
if [ "$T_X" != "" ] ; then ALL=$ALL" threadprogs"; fi
if [ "$OBJS" != "" ] ; then ALL=$ALL" modules" ; fi
# echo if you are curious :>
echo K_S=$K_S OBJS=$OBJS U_S=$U_S U_X=$U_X T_S=$T_S T_X=$T_X
#####################################################################################
# We're done preparing, lets build the makefile finally!
# get rid of the old Makefile, build a new one
rm -f Makefile
echo "### Automatic Makefile generation by 'genmake' script ####" >>Makefile
echo "### Copyright, Jerry Cooperstein, [email protected] 2/2003 - 1/2011 ####" >>Makefile
echo "### License: GPLv2 ###" >>Makefile
if [ "$K_S" != "" ] ; then
echo -e "\nobj-m += $OBJS" >> Makefile
echo -e "\nexport KROOT=$KROOT" >> Makefile
if [ "$ARCH" != "" ] ; then
echo -e "\nexport ARCH=$ARCH" >> Makefile
fi
fi
echo -e "\nallofit: $ALL" >> Makefile
if [ "$K_S" != "" ] ; then
echo "modules:" >> Makefile
echo -e "\t@\$(MAKE) -C \$(KROOT) M=\$(PWD) modules" >> Makefile
echo "modules_install:" >> Makefile
echo -e "\t@\$(MAKE) -C \$(KROOT) M=\$(PWD) modules_install" >> Makefile
echo "kernel_clean:" >> Makefile
echo -e "\t@\$(MAKE) -C \$(KROOT) M=\$(PWD) clean" >> Makefile
fi
if [ "$U_X" != "" ] ; then
echo -e "\nuserprogs:" >> Makefile
echo -e "\t@\$(MAKE) \\" >> Makefile
echo -e "\t\tCFLAGS=\"$CFLAGS_U_X\" \\" >> Makefile
if [ "$LDLIBS" != "" ] ; then
echo -e "\t\tLDLIBS=\"$LDLIBS\" \\" >> Makefile
fi
if [ "$CPPFLAGS" != '' ] ; then
echo -e "\t\tCPPFLAGS=\"$CPPFLAGS\" \\" >> Makefile
fi
echo -e "\t$U_X" >> Makefile
fi
if [ "$T_X" != "" ] ; then
echo -e "\nthreadprogs:" >> Makefile
echo -e "\t@\$(MAKE) \\" >> Makefile
echo -e "\t\tCFLAGS=\"$CFLAGS_T_X\" \\" >> Makefile
if [ "$LDLIBS" != "" ] ; then
echo -e "\t\tLDLIBS=\"$LDLIBS\" \\" >> Makefile
fi
if [ "$CPPFLAGS" != '' ] ; then
echo -e "\t\tCPPFLAGS=\"$CPPFLAGS\" \\" >> Makefile
fi
echo -e "\t$T_X" >> Makefile
fi
if [ "$K_S" != "" ] ; then
echo -e "\nclean: kernel_clean" >> Makefile
else
echo -e "\nclean:" >> Makefile
fi
echo -e "\trm -rf $CLEANSTUFF" >> Makefile
exit
######################################################################################