Finished renaming of export_alloc_io_context patch into io_context performed in revis...
[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 [-h] [-m] [-p <dir>] [-q] <kernel version>, where: "
28   echo "        -h - show this text"
29   echo "        -m - add mpt target driver"
30   echo "        -p - generate multiple patches instead of one big patch into"\
31        "the specified directory."
32   echo "        -q - add qla2x00t driver"
33 }
34
35 # Convert an existing patch.
36 # $1: path of patch to be added.
37 # $2: path in kernel tree of file to be patched.
38 function add_patch {
39   if [ ! -e "$1" ]; then
40     echo "Error: could not find $1." >&2
41     exit 1
42   fi
43
44   sed -e "s:^--- [^ ]*:--- orig/linux-${kernel_version}/$2:" \
45       -e "s:^+++ [^ ]*:+++ linux-${kernel_version}/$2:"      \
46       < "$1"
47 }
48
49 # Generate a patch for a file to be added to the kernel source tree, and strip
50 # trailing whitespace from C source files while converting the file to patch
51 # format.
52 # $1: path of file to be added.
53 # $2: path in kernel tree where file should be added.
54 function add_file {
55   local a b
56
57   if [ ! -e "$1" ]; then
58     echo "Error: could not find $1." >&2
59     exit 1
60   fi
61
62   # Only include files that were not generated by the build process
63   # -- skip *.mod.c.
64   if [ "$1" = "${1%.mod.c}" -a "$1" ]; then
65     cat <<EOF
66 diff -uprN orig/linux-${kernel_version}/$2 linux-${kernel_version}/$2
67 --- orig/linux-${kernel_version}/$2
68 +++ linux-${kernel_version}/$2
69 @@ -0,0 +1,$(wc -l "$1" | { read a b; echo $a; }) @@
70 EOF
71     if [ "${2%.[ch]}" != "$2" ]; then
72       sed -e 's/sBUG(/BUG(/g' -e 's/sBUG_ON(/BUG_ON(/g' -e 's/^/+/' \
73           -e 's/^ \([^ ]*:\)$/\1/' < "$1"
74     else
75       sed -e 's/sBUG(/BUG(/g' -e 's/sBUG_ON(/BUG_ON(/g' -e 's/^/+/' < "$1"
76     fi
77   fi
78 }
79
80 function add_empty_file {
81   local a b
82
83   cat <<EOF
84 diff -uprN orig/linux-${kernel_version}/$1 linux-${kernel_version}/$1
85 --- orig/linux-${kernel_version}/$1
86 +++ linux-${kernel_version}/$1
87 @@ -0,0 +1,1 @@
88 +
89 EOF
90 }
91
92 # Read a patch from stdin, specialize it for kernel version ${kernel_version}
93 # and write the output either to stdout or to the file $1 (if not empty),
94 # depending on the value of the variable ${multiple_patches}.
95 function process_patch {
96   if [ "${multiple_patches}" = "true" ]; then
97     if [ "$1" != "" ]; then
98       if [ -e "${patchdir}/$1" ]; then
99         echo "Warning: overwriting ${patchdir}/$1"
100       fi
101     (
102       echo "Signed-off-by: ${SIGNED_OFF_BY}"
103       echo ""
104       "$(dirname $0)/specialize-patch" -v kernel_version="${kernel_version}"
105     ) >"${patchdir}/$1"
106     else
107       # echo "Discarded $(wc -l) lines."
108       true
109     fi
110   else
111     "$(dirname $0)/specialize-patch" -v kernel_version="${kernel_version}" 
112   fi
113 }
114
115 # Returns 0 (true) if SCST core file "$1" should be added in a separate patch,
116 # and 1 (false) if not.
117 function in_separate_patch {
118   echo "${source_files_in_separate_patch}" | grep -qE "^$1 | $1 | $1\$|^$1\$"
119 }
120
121
122 #########################
123 # Argument verification #
124 #########################
125
126 qla2x00t="false"
127 srpt="true"
128 mpt_scst="false"
129 multiple_patches="false"
130 patchdir=""
131
132 if [ ! -e scst -o ! -e iscsi-scst -o ! -e srpt -o ! -e scst_local ]; then
133   echo "Please run this script from inside the SCST subversion source tree."
134   exit 1
135 fi
136
137 set -- $(/usr/bin/getopt hlmp:q "$@")
138 while [ "$1" != "${1#-}" ]
139 do
140   case "$1" in
141     '-h') usage; exit 1;;
142     '-l') shift;;
143     '-m') mpt_scst="true"; shift;;
144     '-p') multiple_patches="true"; patchdir="$2"; shift; shift;;
145     '-q') qla2x00t="true"; shift;;
146     '--') shift;;
147     *)    usage; exit 1;;
148   esac
149 done
150
151 if [ $# != 1 ]; then
152   usage
153   exit 1
154 fi
155
156 if [ "${multiple_patches}" = "" ]; then
157   if [ -e "${patchdir}" ]; then
158     echo "Patch output directory ${patchdir} already exists."
159   fi
160   if [ ! -d "${patchdir}" ]; then
161     echo "Error: ${patchdir} is not a directory."
162   fi
163 fi
164
165
166 ####################
167 # Patch Generation #
168 ####################
169
170 # Strip patch level from the kernel version number.
171 if [ "${1#[0-9]*.[0-9]*.[0-9]*.[0-9]*}" != "$1" ]; then
172   kernel_version="${1%.[0-9]*}"
173 else
174   kernel_version="$1"
175 fi
176
177 if [ "${multiple_patches}" = "false" ]; then
178   echo "Signed-off-by: ${SIGNED_OFF_BY}"
179   echo ""
180 fi
181
182
183 # General kernel patches.
184
185 process_patch < "scst/kernel/scst_exec_req_fifo-${kernel_version}.patch" \
186     "scst_exec_req_fifo.diff"
187 process_patch < "scst/kernel/io_context-${kernel_version}.patch" \
188     "export_alloc_io_context.diff"
189 process_patch < "iscsi-scst/kernel/patches/put_page_callback-${kernel_version}.patch" \
190     "put_page_callback.diff"
191
192
193 scst_debug="scst/include/scst_debug.h scst/src/scst_debug.c"
194 scst_proc="scst/src/scst_proc.c"
195 scst_sgv="scst/include/scst_sgv.h scst/src/scst_mem.h scst/src/scst_mem.c"
196 scst_user="scst/include/scst_user.h scst/src/dev_handlers/scst_user.c"
197 scst_vdisk="scst/src/dev_handlers/scst_vdisk.c"
198 separate_patches="scst_debug scst_proc scst_sgv scst_user scst_vdisk"
199 source_files_in_separate_patch="${scst_debug} ${scst_proc} ${scst_sgv} ${scst_user} ${scst_vdisk}"
200
201
202 # Directory include/scst/
203
204 for f in $(ls scst/include/*h)
205 do
206   if ! in_separate_patch "${f}"; then
207     add_file "${f}" "include/scst/${f#scst/include/}"
208   fi
209 done \
210 | process_patch "scst_public_headers.diff"
211
212
213 # Directory drivers/
214
215 (
216 add_patch "scst/kernel/in-tree/Kconfig.drivers.Linux-${kernel_version}.patch" \
217           "drivers/Kconfig"
218
219 add_patch "scst/kernel/in-tree/Makefile.drivers.Linux-${kernel_version}.patch"\
220           "drivers/Makefile"
221 ) \
222 | process_patch "misc.diff"
223
224
225 # Directory drivers/scst/
226
227 (
228 add_file "scst/kernel/in-tree/Kconfig.scst"  "drivers/scst/Kconfig"
229
230 add_file "scst/kernel/in-tree/Makefile.scst" "drivers/scst/Makefile"
231
232 for f in $(ls scst/src/*.[ch])
233 do
234   if ! in_separate_patch "${f}"; then
235     add_file "${f}" "drivers/scst/${f#scst/src/}"
236   fi
237 done
238 ) \
239 | process_patch "scst_core.diff"
240
241 for s in ${separate_patches}
242 do
243   fileset=$s
244   for f in $(set | grep "^$s=" | sed -e "s/^$s='\(.*\)'\$/\1/" -e "s/^$s=\(.*\)\$/\1/")
245   do
246     if [ "${f#scst/include}" != "${f}" ]; then
247       add_file "${f}" "include/scst/${f#scst/include/}"
248     else
249       add_file "${f}" "drivers/scst/${f#scst/src/}"
250     fi
251   done \
252   | process_patch "${s}.diff"
253 done
254
255 add_file "scst/README_in-tree" "Documentation/scst/README.scst" \
256 | process_patch "scst_core_doc.diff"
257
258
259
260 # Directory drivers/scst/dev_handlers/
261
262 (
263 add_file "scst/kernel/in-tree/Makefile.dev_handlers" \
264          "drivers/scst/dev_handlers/Makefile" \
265 | process_patch "dev_handlers_makefile.diff"
266
267 for f in $(ls scst/src/dev_handlers/*.[ch])
268 do
269   if ! in_separate_patch "${f}"; then
270     add_file "${f}" "drivers/scst/dev_handlers/${f#scst/src/dev_handlers/}"
271   fi
272 done
273 ) \
274 | process_patch "scst_passthrough.diff"
275
276
277 # Directory drivers/scst/iscsi-scst/
278
279 # Make sure the file iscsi-scst/iscsi_scst_itf_ver.h is up to date.
280 make -s -C iscsi-scst include/iscsi_scst_itf_ver.h
281
282 (
283 for f in $(ls iscsi-scst/include/*h)
284 do
285   if [ "${f}" != "iscsi-scst/include/iscsi_scst_itf_ver.h" ]; then
286     add_file "${f}" "include/scst/${f#iscsi-scst/include/}"
287   fi
288 done
289
290 add_file "iscsi-scst/include/iscsi_scst_itf_ver.h" "include/scst/iscsi_scst_itf_ver.h" | process_patch ""
291
292 add_file "iscsi-scst/kernel/Makefile.in-kernel" \
293          "drivers/scst/iscsi-scst/Makefile"
294
295 add_file "iscsi-scst/kernel/Kconfig"  "drivers/scst/iscsi-scst/Kconfig"
296
297 for f in $(ls iscsi-scst/kernel/*.[ch])
298 do
299   add_file "${f}" "drivers/scst/iscsi-scst/${f#iscsi-scst/kernel/}"
300 done
301 ) \
302 | process_patch "iscsi-scst.diff"
303
304 add_file "iscsi-scst/README_in-tree" "Documentation/scst/README.iscsi" \
305 | process_patch "iscsi-scst-doc.diff"
306
307
308 # Directory drivers/scst/qla2x00-target/
309
310 if [ "${qla2x00t}" = "true" ]; then
311
312   add_file "qla2x00t/qla2x00-target/Makefile_in-tree" \
313            "drivers/scst/qla2xxx-target/Makefile"
314
315   add_file "qla2x00t/qla2x00-target/Kconfig" \
316            "drivers/scst/qla2xxx-target/Kconfig"
317
318 #  add_file "qla2x00t/qla2x_tgt_def.h" \
319 #           "drivers/scst/qla2x00-target/qla2x_tgt_def.h"
320
321   for f in $(ls qla2x00t/qla2x00-target/*.[ch])
322   do
323     add_file "${f}" "drivers/scst/qla2xxx-target/${f#qla2x00t/qla2x00-target/}"
324   done
325
326   add_file "qla2x00t/qla2x00-target/README" \
327            "Documentation/scst/README.qla2x00t" \
328   | process_patch "qla2x00t-doc.diff"
329
330 else
331
332   add_empty_file "drivers/scst/qla2xxx-target/Makefile"
333
334   add_empty_file "drivers/scst/qla2xxx-target/Kconfig"
335
336 fi \
337 | process_patch "qla2x00t.diff"
338
339
340 # Directory drivers/scst/srpt
341
342 if [ "$srpt" = "true" ]; then
343
344   add_file  "srpt/src/Kconfig"            "drivers/scst/srpt/Kconfig"
345
346   add_file  "srpt/src/Makefile.in_kernel" "drivers/scst/srpt/Makefile"
347
348   for f in $(ls srpt/src/*.[ch])
349   do
350     add_file "${f}" "drivers/scst/srpt/${f#srpt/src/}"
351   done
352
353 else
354
355   add_empty_file "drivers/scst/srpt/Kconfig"
356
357   add_empty_file "drivers/scst/srpt/Makefile"
358
359 fi \
360 | process_patch "srpt.diff"
361
362 add_file "srpt/README_in-tree" "Documentation/scst/README.srpt" \
363 | process_patch "srpt-doc.diff"
364
365
366 # Directory drivers/message/fusion/mpt_scst
367
368 if [ "$mpt_scst" = "true" ]; then
369
370   (
371   add_patch "mpt/in-tree/Kconfig-2.6.24.diff" "drivers/message/fusion/Kconfig"
372
373   add_patch "mpt/in-tree/Makefile.diff"       "drivers/message/fusion/Makefile"
374
375   add_file  "mpt/in-tree/Makefile" "drivers/message/fusion/mpt_scst/Makefile"
376
377   add_file  "mpt/in-tree/Kconfig"  "drivers/message/fusion/mpt_scst/Kconfig"
378
379   for f in $(ls mpt/*.[ch])
380   do
381     add_file "${f}" "drivers/message/fusion/mpt_scst/${f#mpt/}"
382   done
383   ) \
384   | process_patch "mpt_scst.diff"
385
386 fi
387
388
389 # Directory drivers/scst/scst_local
390
391 (
392   add_file "scst_local/in-tree/Kconfig" "drivers/scst/scst_local/Kconfig"
393
394   add_file "scst_local/in-tree/Makefile" "drivers/scst/scst_local/Makefile"
395
396   add_file "scst_local/scst_local.c" "drivers/scst/scst_local/scst_local.c"
397 ) \
398 | process_patch "scst_local.diff"
399
400 add_file "scst_local/README" "Documentation/scst/README.scst_local" \
401 | process_patch "scst_local-doc.diff"