-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkernelcfg2mk
executable file
·224 lines (181 loc) · 6.94 KB
/
kernelcfg2mk
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
if [ $# -lt 1 ] ; then
echo "kernelcfg2mk: requires linux kernel directory as the first argument"
exit 2
fi
LINUX_DIR="$1"
if [ $# -gt 1 ] ; then
KERN_MODULES_DIR="$2"
fi
if [ ! -e $LINUX_DIR/.config ] ; then
echo "kernelcfg2mk: the provided locations doesnot point to configured kernel sources"
echo " check directory $LINUX_DIR"
exit 2
fi
if [ ! -e $LINUX_DIR/.config ] ; then
echo "kernelcfg2mk: the provided locations doesnot point to configured kernel sources"
echo " check directory $LINUX_DIR"
exit 2
fi
if [ -z "$KERN_BUILD_DIR" ] ; then
KERN_BUILD_DIR=`pwd`
fi
if [ -z "$KERN_MODULES_DIR" ] ; then
KERN_MODULES_DIR=`pwd`
fi
if [ -z "$MAKE" ] ; then
MAKE="make"
fi
KERN_TEST_DIR="$KERN_BUILD_DIR/kern-test-mk"
mkdir -p $KERN_TEST_DIR
#echo MAKERULES_DIR $MAKERULES_DIR
#echo LINUX_DIR $LINUX_DIR
#echo KERN_BUILD_DIR $KERN_BUILD_DIR
#echo KERN_MODULES_DIR $KERN_MODULES_DIR
RETVAL="0"
LINUX_VERSION=$(grep UTS_RELEASE ${LINUX_DIR}/include/linux/version.h | \
sed 's/[^"]*"\(.*\)\{1\}"/\1/')
if [ -z "$LINUX_VERSION" ] ; then
LINUX_VERSION=$(grep UTS_RELEASE ${LINUX_DIR}/include/linux/utsrelease.h | \
sed 's/[^"]*"\(.*\)\{1\}"/\1/')
fi
if [ -z "$LINUX_VERSION" ] ; then
LINUX_VERSION=$(grep UTS_RELEASE ${LINUX_DIR}/include/generated/utsrelease.h | \
sed 's/[^"]*"\(.*\)\{1\}"/\1/')
fi
LINUX_VERSION_MAJOR=$(echo ${LINUX_VERSION} | cut -d. -f1)
LINUX_VERSION_MINOR=$(echo ${LINUX_VERSION} | cut -d. -f2)
#echo $LINUX_VERSION
#echo $LINUX_VERSION_MAJOR
#echo $LINUX_VERSION_MINOR
case $LINUX_VERSION_MAJOR.$LINUX_VERSION_MINOR in
#=====================================================================
# test for 2.4.x kernels
2.[01234])
rm -f ${KERN_BUILD_DIR}/kernel.mk
rm -rf ${KERN_TEST_DIR}
mkdir ${KERN_TEST_DIR}
if [ $? -ne 0 ] ; then
echo "Cannot create Linux kernel test build directory \"${KERN_TEST_DIR}\""
exit 3
fi
cd ${KERN_TEST_DIR}
cat >${KERN_TEST_DIR}/Makefile <<EOF
.PHONY: modules
modules:
@echo LINUX_ARCH="\$(ARCH)" >>flags
@echo LINUX_LDFLAGS="" >>flags
@echo LINUX_ARFLAGS="\$(ARFLAGS)" >>flags
@echo LINUX_CROSS_COMPILE="\$(CROSS_COMPILE)" >>flags
@echo LINUX_KERNELRELEASE="\$(KERNELRELEASE)" >>flags
@echo LINUX_AFLAGS="\$(AFLAGS)" | sed 's#Iinclude#I"\$(LINUXDIR)/include"#g'>>flags
@echo LINUX_CFLAGS="\$(CFLAGS)" | sed 's#Iinclude#I"\$(LINUXDIR)/include"#g'>>flags
@echo LINUX_MODFLAGS="\$(MODFLAGS)"
@echo LINUX_CC="\$(CC)" >>flags
@echo LINUX_LD="\$(LD) \$(LDFLAGS)" >>flags
@echo LINUX_AS="\$(AS)" >>flags
@echo LINUX_AR="\$(AR)" >>flags
@echo LINUX_MODULE_EXT=".o" >>flags
EOF
#echo ${MAKE} -C ${LINUX_DIR} SUBDIRS=${KERN_TEST_DIR} modules
${MAKE} -C ${LINUX_DIR} SUBDIRS=${KERN_TEST_DIR} modules
if [ $? == 0 ] ; then
cp -v flags ${KERN_MODULES_DIR}/kernel.mk
if [ ! $? == 0 ] ; then
RETVAL="2"
fi
else
RETVAL="1"
fi
cd ${KERN_BUILD_DIR}
rm -rf ${KERN_TEST_DIR}
;;
#=====================================================================
# test for 2.6.x and 3.x kernels
2.6 | 3.* )
rm -f ${KERN_BUILD_DIR}/kernel.mk
rm -rf ${KERN_TEST_DIR}
mkdir ${KERN_TEST_DIR}
if [ $? -ne 0 ] ; then
echo "Cannot create Linux kernel test build directory \"${KERN_TEST_DIR}\""
exit 3
fi
cd ${KERN_TEST_DIR}
cat >${KERN_TEST_DIR}/Makefile <<EOF
obj-m += fake.o
\$(obj)/fake.c: flags
touch \$(obj)/fake.c
ifeq (\$(KBUILD_SRC),)
OMK_LINUX_SRC = \$(LINUXDIR)
else
OMK_LINUX_SRC = \$(KBUILD_SRC)
endif
#\$(warning Test \$OMK_LINUX_SRC)
.PHONY: flags
flags:
@echo LINUX_SRC="\$(OMK_LINUX_SRC)" >>\$(obj)/flags
@echo LINUX_ARCH="\$(ARCH)" >>\$(obj)/flags
@echo LINUX_BUILDHOST="\$(KBUILD_BUILDHOST)" >>\$(obj)/flags
@echo LINUX_LDFLAGS="" >>\$(obj)/flags
@echo LINUX_ARFLAGS="\$(ARFLAGS)" >>\$(obj)/flags
@echo LINUX_CROSS_COMPILE="\$(CROSS_COMPILE)" >>\$(obj)/flags
@echo LINUX_KERNELRELEASE="\$(KERNELRELEASE)" >>\$(obj)/flags
ifdef KBUILD_CFLAGS
@echo LINUX_CPPFLAGS="\$(KBUILD_CPPFLAGS)\$(if \$(filter -I,\$(KBUILD_CPPFLAGS)),, \$(LINUXINCLUDE))" | sed 's#-I\(include\|arch\)#-I"\$(LINUXDIR)/\1"#g'>>\$(obj)/flags
@echo LINUX_AFLAGS="\$(KBUILD_AFLAGS)" | sed 's#-I\(include\|arch\)#-I"\$(OMK_LINUX_SRC)/\1"#g'>>\$(obj)/flags
@echo LINUX_CFLAGS="\$(KBUILD_CFLAGS)" | sed 's#-I\(include\|arch\)#-I"\$(OMK_LINUX_SRC)/\1"#g'>>\$(obj)/flags
else
@echo LINUX_CPPFLAGS="\$(CPPFLAGS)" | sed 's#Iinclude#I"\$(LINUXDIR)/include"#g'>>\$(obj)/flags
@echo LINUX_AFLAGS="\$(AFLAGS)" | sed 's#Iinclude#I"\$(OMK_LINUX_SRC)/include"#g'>>\$(obj)/flags
@echo LINUX_CFLAGS="\$(CFLAGS)" | sed 's#Iinclude#I"\$(OMK_LINUX_SRC)/include"#g'>>\$(obj)/flags
endif
ifdef KBUILD_CFLAGS_MODULE
@echo LINUX_CFLAGS_MODULE="\$(KBUILD_CFLAGS_MODULE)" >>\$(obj)/flags
@echo LINUX_AFLAGS_MODULE="\$(KBUILD_AFLAGS_MODULE)" >>\$(obj)/flags
@echo LINUX_LDFLAGS_MODULE="\$(KBUILD_LDFLAGS_MODULE)" >>\$(obj)/flags
else
@echo LINUX_CFLAGS_MODULE="\$(CFLAGS_MODULE)" >>\$(obj)/flags
@echo LINUX_AFLAGS_MODULE="\$(CFLAGS_MODULE)" >>\$(obj)/flags
endif
@echo LINUX_CC="\$(CC)" >>\$(obj)/flags
@echo LINUX_LD="\$(LD) \$(LDFLAGS) \$(LDFLAGS_MODULE)" | sed 's#\(\<arch/[^ ]*\.o\>\)#"\$(LINUXDIR)/\1"#g' >>\$(obj)/flags
@echo LINUX_AS="\$(AS)" >>\$(obj)/flags
@echo LINUX_AR="\$(AR)" >>\$(obj)/flags
@echo LINUX_MODULE_EXT=".ko" >>\$(obj)/flags
@echo LINUX_QUOTE_MODNAME=\$(if \$(findstring KBUILD_STR,\$(basename_flags)),y,) >>\$(obj)/flags
@echo LINUX_CONFIG_MODVERSIONS=\$(CONFIG_MODVERSIONS) >>\$(obj)/flags
@echo LINUX_CONFIG_MODULE_SRCVERSION_ALL=\$(CONFIG_MODULE_SRCVERSION_ALL) >>\$(obj)/flags
@echo LINUX_CONFIG_DEBUG_SECTION_MISMATCH=\$(CONFIG_DEBUG_SECTION_MISMATCH) >>\$(obj)/flags
@echo LINUX_CONFIG_MARKERS=\$(CONFIG_MARKERS) >>\$(obj)/flags
@echo LINUX_KBUILD_EXTMOD=\$(KBUILD_EXTMOD) >>\$(obj)/flags
@echo LINUX_KBUILD_EXTRA_SYMBOLS=\$(KBUILD_EXTRA_SYMBOLS) >>\$(obj)/flags
@echo LINUX_KBUILD_MODPOST_WARN=\$(KBUILD_MODPOST_WARN) >>\$(obj)/flags
@echo LINUX_CROSS_BUILD=\$(cross_build) >>\$(obj)/flags
EOF
# modkern_cflags := $(CFLAGS_KERNEL) / $(CFLAGS_MODULE)
# _c_flags = $(KBUILD_CFLAGS) $(ccflags-y) $(CFLAGS_$(basetarget).o)
# _a_flags = $(KBUILD_AFLAGS) $(asflags-y) $(AFLAGS_$(basetarget).o)
# _cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(@F))
# export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
# export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
# export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
# -p V=1
#echo ${MAKE} -C ${LINUX_DIR} SUBDIRS=${KERN_TEST_DIR} LINUXDIR=${LINUX_DIR} MODVERDIR=${KERN_TEST_DIR} modules
${MAKE} -C ${LINUX_DIR} M=${KERN_TEST_DIR} LINUXDIR=${LINUX_DIR} MODVERDIR=${KERN_TEST_DIR}/modver modules
#if [ $? == 0 ] ; then
cp -v flags ${KERN_MODULES_DIR}/kernel.mk
if [ ! $? == 0 ] ; then
RETVAL="2"
fi
#else
# RETVAL="1"
#fi
cd ${KERN_BUILD_DIR}
rm -rf ${KERN_TEST_DIR}
;;
#=====================================================================
*)
echo "Unsupported Linux version $LINUX_VERSION_MAJOR.$LINUX_VERSION_MINOR"
;;
esac
exit ${RETVAL}