6eb222a30f53306e575983abcfa93031e6606240
[mirror/scst/.git] / scripts / generate-kernel-patch
1 #!/bin/bash
2
3 ############################################################################
4 #
5 # Script for converting the SCST source tree as it exists in the Subversion
6 # repository to a Linux kernel patch.
7 #
8 # Copyright (C) 2008 Bart Van Assche <bart.vanassche@gmail.com>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation, version 2
13 # of the License.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 ############################################################################
21
22 ########################
23 # Function definitions #
24 ########################
25
26 function usage {
27   echo "Usage: $0 [-q] [-s] [-m] [-l] <kernel version>, where: "
28   echo "        -q - add qla2x00t driver"
29   echo "        -s - exclude srpt driver"
30   echo "        -m - add mpt target driver"
31   echo "        -l - add scst_local target driver"
32 }
33
34 # Convert an existing patch.
35 # $1: path of patch to be added.
36 # $2: path in kernel tree of file to be patched.
37 function add_patch {
38   if [ ! -e "$1" ]; then
39     echo "Error: could not find $1." >&2
40     exit 1
41   fi
42
43   sed -e "s:^--- [^ ]*:--- orig/linux-${kernel_version}/$2:" \
44       -e "s:^+++ [^ ]*:+++ linux-${kernel_version}/$2:"      \
45       < "$1"
46 }
47
48 # Generate a patch for a file to be added to the kernel source tree, and strip
49 # trailing whitespace from C source files while converting the file to patch
50 # format.
51 # $1: path of file to be added.
52 # $2: path in kernel tree where file should be added.
53 function add_file {
54   local a b
55
56   if [ ! -e "$1" ]; then
57     echo "Error: could not find $1." >&2
58     exit 1
59   fi
60
61   # Only include files that were not generated by the build process
62   # -- skip *.mod.c.
63   if [ "$1" = "${1%.mod.c}" -a "$1" ]; then
64     cat <<EOF
65 diff -uprN orig/linux-${kernel_version}/$2 linux-${kernel_version}/$2
66 --- orig/linux-${kernel_version}/$2
67 +++ linux-${kernel_version}/$2
68 @@ -0,0 +1,$(wc -l "$1" | { read a b; echo $a; }) @@
69 EOF
70     if [ "${2%.[ch]}" != "$2" ]; then
71       sed -e 's/sBUG(/BUG(/g' -e 's/sBUG_ON(/BUG_ON(/g' -e 's/^/+/' \
72           -e 's/^ \([^ ]*:\)$/\1/' < "$1"
73     else
74       sed -e 's/sBUG(/BUG(/g' -e 's/sBUG_ON(/BUG_ON(/g' -e 's/^/+/' < "$1"
75     fi
76   fi
77 }
78
79
80 #########################
81 # Argument verification #
82 #########################
83
84 qla2x00t="false"
85 srpt="true"
86 mpt_scst="false"
87 scst_local="false"
88
89 if [ ! -e scst -o ! -e iscsi-scst -o ! -e srpt -o ! -e scst_local ]; then
90   echo "Please run this script from inside the SCST subversion source tree."
91   exit 1
92 fi
93
94 set -- $(/usr/bin/getopt hlmqs "$@")
95 while [ "$1" != "${1#-}" ]
96 do
97   case "$1" in
98     '-h') usage; exit 1;;
99     '-l') scst_local="true"; shift;;
100     '-m') mpt_scst="true"; shift;;
101     '-q') qla2x00t="true"; shift;;
102     '-s') srpt="false"; shift;;
103     '--') shift;;
104     *)    usage; exit 1;;
105   esac
106 done
107
108 if [ $# != 1 ]; then
109   usage
110   exit 1
111 fi
112
113
114 ####################
115 # Patch Generation #
116 ####################
117
118 kernel_version="$1"
119 kpatch=(                                                                \
120   "scst/kernel/scst_exec_req_fifo-${kernel_version}.patch"              \
121   "iscsi-scst/kernel/patches/put_page_callback-${kernel_version}.patch" \
122 )
123
124 for p in "${kpatch[@]}"
125 do
126   if [ ! -e "$p" ]; then
127     echo "Error: kernel version ${kernel_version} is not supported by SCST."
128     echo "(could not find file $p)."
129     exit 1
130   fi >&2
131 done
132
133
134 # Redirect the output of all subsequent commands to the specialize-patch script
135
136 trap "rm -f ${fifo}" EXIT
137 fifo=/tmp/generate-kernel-patch-fifo.$$
138 rm -f "${fifo}"
139 mkfifo "${fifo}"
140 "$(dirname $0)/specialize-patch" -v kernel_version="${kernel_version}" \
141   < "${fifo}" &
142 exec >"${fifo}"
143
144 # General kernel patches.
145
146 cat "${kpatch[@]}"
147
148
149 # Directory include/scst/
150
151 for f in scst/include/*h
152 do
153   add_file "${f}" "include/scst/${f#scst/include/}"
154 done
155
156
157 # Directory drivers/
158
159 add_patch "scst/kernel/in-tree/Kconfig.drivers.Linux-${kernel_version}.patch" \
160           "drivers/Kconfig"
161
162 add_patch "scst/kernel/in-tree/Makefile.drivers.Linux-${kernel_version}.patch"\
163           "drivers/Makefile"
164
165
166 # Directory drivers/scst/
167
168 add_file "scst/kernel/in-tree/Kconfig.scst"  "drivers/scst/Kconfig"
169
170 add_file "scst/kernel/in-tree/Makefile.scst" "drivers/scst/Makefile"
171
172 add_file "scst/README_in-tree"               "Documentation/scst/README.scst"
173
174 for f in scst/src/*.[ch]
175 do
176   add_file "${f}" "drivers/scst/${f#scst/src/}"
177 done
178
179
180 # Directory drivers/scst/dev_handlers/
181
182 add_file "scst/kernel/in-tree/Makefile.dev_handlers" \
183          "drivers/scst/dev_handlers/Makefile"
184
185 for f in scst/src/dev_handlers/*.[ch]
186 do
187   add_file "${f}" "drivers/scst/dev_handlers/${f#scst/src/dev_handlers/}"
188 done
189
190
191 # Directory drivers/scst/iscsi-scst/
192
193 # Make sure the file iscsi-scst/iscsi_scst_itf_ver.h is up to date.
194 make -s -C iscsi-scst include/iscsi_scst_itf_ver.h
195
196 for f in iscsi-scst/include/*h
197 do
198   add_file "${f}" "include/scst/${f#iscsi-scst/include/}"
199 done
200
201 add_file "iscsi-scst/kernel/Makefile.in-kernel" \
202          "drivers/scst/iscsi-scst/Makefile"
203
204 add_file "iscsi-scst/kernel/Kconfig"  "drivers/scst/iscsi-scst/Kconfig"
205
206 for f in iscsi-scst/kernel/*.[ch]
207 do
208   add_file "${f}" "drivers/scst/iscsi-scst/${f#iscsi-scst/kernel/}"
209 done
210
211
212 # Directory drivers/scst/qla2x00-target/
213
214 if [ "${qla2x00t}" = "true" ]; then
215
216   add_file "qla2x00t/qla2x00-target/Makefile_in-tree" \
217            "drivers/scst/qla2xxx-target/Makefile"
218
219   add_file "qla2x00t/qla2x00-target/Kconfig" \
220            "drivers/scst/qla2xxx-target/Kconfig"
221
222   #add_file "qla2x00t/qla2x_tgt_def.h" \
223   #         "drivers/scst/qla2x00-target/qla2x_tgt_def.h"
224
225 #  add_file "qla2x00t/qla2x00-target/README" \
226 #           "Documentation/scst/README.qla2x00t"
227
228   for f in qla2x00t/qla2x00-target/*.[ch]
229   do
230     add_file "${f}" "drivers/scst/qla2xxx-target/${f#qla2x00t/qla2x00-target/}"
231   done
232
233 fi
234
235 # Directory drivers/infiniband/ulp/srpt/
236
237 if [ "$srpt" = "true" ]; then
238
239   add_file  "srpt/src/Kconfig"            "drivers/scst/srpt/Kconfig"
240
241   add_file  "srpt/src/Makefile.in_kernel" "drivers/scst/srpt/Makefile"
242
243   add_file  "srpt/README"                 "Documentation/scst/README.srpt"
244
245   for f in srpt/src/*.[ch]
246   do
247     add_file "${f}" "drivers/scst/srpt/${f#srpt/src/}"
248   done
249
250 fi
251
252 # Directory drivers/message/fusion/mpt_scst
253
254 if [ "$mpt_scst" = "true" ]; then
255
256   add_patch "mpt/in-tree/Kconfig-2.6.24.diff" "drivers/message/fusion/Kconfig"
257
258   add_patch "mpt/in-tree/Makefile.diff"       "drivers/message/fusion/Makefile"
259
260   add_file  "mpt/Makefile" "drivers/message/fusion/mpt_scst/Makefile"
261
262   add_file  "mpt/Kconfig"  "drivers/message/fusion/mpt_scst/Kconfig"
263
264   for f in mpt/*.[ch]
265   do
266     add_file "${f}" "drivers/message/fusion/mpt_scst/${f#mpt/}"
267   done
268
269 fi
270
271 # Directory drivers/scst/scst-local
272
273 if [ "$scst_local" = "true" ]; then
274
275   add_file "scst_local/in-tree/Kconfig" "drivers/scst/scst_local/Kconfig"
276
277   add_file "scst_local/in-tree/Makefile" "drivers/scst/scst_local/Makefile"
278
279   add_file "scst_local/scst_local.c" "drivers/scst/scst_local/scst_local.c"
280
281 fi