Made sure that patch generation also works for other kernel versions than 2.6.26.8.
[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>] <kernel version>, where: "
28   echo "        -h - show this text"
29   echo "        -m - add mpt target driver"
30   echo "        -n - do not delete code disabled via preprocessor statements"
31   echo "        -p - generate multiple patches instead of one big patch into"\
32        "the specified directory."
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     # Insert a '+'-sign at the start of each line.
72     sed -e 's/^/+/' < "$1" | \
73     if [ "${replace_sbug_by_bug}" = "true" ]; then
74       sed -e 's/sBUG(\([^)]\)/BUG(\1/g' -e 's/sBUG_ON(\([^)]\)/BUG_ON(\1/g'
75     else
76       cat
77     fi \
78     |  \
79     if [ "${2%.[ch]}" != "$2" ]; then
80       # Make sure that labels (goto-targets) are left-aligned.
81       sed -e 's/^ \([^ ]*:\)$/\1/'
82     else
83       cat
84     fi
85   fi
86 }
87
88 function add_empty_file {
89   local a b
90
91   cat <<EOF
92 diff -uprN orig/linux-${kernel_version}/$1 linux-${kernel_version}/$1
93 --- orig/linux-${kernel_version}/$1
94 +++ linux-${kernel_version}/$1
95 @@ -0,0 +1,1 @@
96 +
97 EOF
98 }
99
100 # Read a patch from stdin, specialize it for kernel version ${kernel_version}
101 # and write the output either to stdout or to the file $1 (if not empty),
102 # depending on the value of the variable ${multiple_patches}.
103 function process_patch {
104   if [ "${multiple_patches}" = "true" ]; then
105     if [ "$1" != "" ]; then
106       if [ -e "${patchdir}/$1" ]; then
107         echo "Warning: overwriting ${patchdir}/$1"
108       fi
109     (
110       echo "Signed-off-by: ${SIGNED_OFF_BY}"
111       echo ""
112       "$(dirname $0)/specialize-patch" \
113           ${specialize_patch_options} \
114           -v kernel_version="${kernel_version}" \
115           -v SCSI_EXEC_REQ_FIFO_DEFINED="${scsi_exec_req_fifo_defined}" \
116           -v SCST_IO_CONTEXT="${scst_io_context}"
117     ) >"${patchdir}/$1"
118     else
119       # echo "Discarded $(wc -l) lines."
120       true
121     fi
122   else
123     "$(dirname $0)/specialize-patch" \
124         ${specialize_patch_options} \
125         -v kernel_version="${kernel_version}" \
126         -v SCSI_EXEC_REQ_FIFO_DEFINED="${scsi_exec_req_fifo_defined}" \
127         -v SCST_IO_CONTEXT="${scst_io_context}"
128   fi
129 }
130
131 # Returns 0 (true) if SCST core file "$1" should be added in a separate patch,
132 # and 1 (false) if not.
133 function in_separate_patch {
134   echo "${source_files_in_separate_patch}" | grep -qE "^$1 | $1 | $1\$|^$1\$"
135 }
136
137
138 #########################
139 # Argument verification #
140 #########################
141
142 qla2x00t="true"
143 srpt="true"
144 mpt_scst="false"
145 multiple_patches="false"
146 patchdir=""
147 specialize_patch_options="-v delete_disabled_code=1"
148 replace_sbug_by_bug="true"
149
150 if [ ! -e scst -o ! -e iscsi-scst -o ! -e srpt -o ! -e scst_local ]; then
151   echo "Please run this script from inside the SCST subversion source tree."
152   exit 1
153 fi
154
155 set -- $(/usr/bin/getopt hlmnp: "$@")
156 while [ "$1" != "${1#-}" ]
157 do
158   case "$1" in
159     '-h') usage; exit 1;;
160     '-l') shift;;
161     '-m') mpt_scst="true"; shift;;
162     '-n') specialize_patch_options="-v delete_disabled_code=0"
163           replace_sbug_by_bug="false"
164           shift
165           ;;
166     '-p') multiple_patches="true"; patchdir="$2"; shift; shift;;
167     '--') shift;;
168     *)    usage; exit 1;;
169   esac
170 done
171
172 if [ $# != 1 ]; then
173   usage
174   exit 1
175 fi
176
177 if [ "${multiple_patches}" = "" ]; then
178   if [ -e "${patchdir}" ]; then
179     echo "Patch output directory ${patchdir} already exists."
180   fi
181   if [ ! -d "${patchdir}" ]; then
182     echo "Error: ${patchdir} is not a directory."
183   fi
184 fi
185
186
187 ####################
188 # Patch Generation #
189 ####################
190
191 # Strip patch level from the kernel version number.
192 if [ "${1#[0-9]*.[0-9]*.[0-9]*.[0-9]*}" != "$1" ]; then
193   kernel_version="${1%.[0-9]*}"
194   patch_level="${1%${kernel_version}}"
195 else
196   kernel_version="$1"
197 fi
198
199 if [ "${multiple_patches}" = "false" ]; then
200   echo "Signed-off-by: ${SIGNED_OFF_BY}"
201   echo ""
202 fi
203
204
205 # General kernel patches.
206
207 scsi_exec_req_fifo_defined=0
208 scst_io_context=0
209 for p in scst/kernel/*-${kernel_version}.patch \
210          iscsi-scst/kernel/patches/*-${kernel_version}.patch
211 do
212   if grep -q '^\+#define SCSI_EXEC_REQ_FIFO_DEFINED$' "${p}"; then
213     scsi_exec_req_fifo_defined=1
214   fi
215   if grep -q '^\+#define SCST_IO_CONTEXT$' "${p}"; then
216     scst_io_context=1
217   fi
218   diffname="${p#scst/kernel/}"
219   diffname="${p%-${kernel_version}.patch}.diff"
220   process_patch < "$p" "${diffname}"
221 done
222
223
224 scst_debug="scst/include/scst_debug.h scst/src/scst_debug.c"
225 scst_proc="scst/src/scst_proc.c"
226 scst_sgv="scst/include/scst_sgv.h scst/src/scst_mem.h scst/src/scst_mem.c"
227 scst_user="scst/include/scst_user.h scst/src/dev_handlers/scst_user.c"
228 scst_vdisk="scst/src/dev_handlers/scst_vdisk.c"
229 separate_patches="scst_debug scst_proc scst_sgv scst_user scst_vdisk"
230 source_files_in_separate_patch="${scst_debug} ${scst_proc} ${scst_sgv} ${scst_user} ${scst_vdisk}"
231
232
233 # Directory include/scst/
234
235 for f in $(ls scst/include/*h 2>/dev/null)
236 do
237   if ! in_separate_patch "${f}"; then
238     add_file "${f}" "include/scst/${f#scst/include/}"
239   fi
240 done \
241 | process_patch "scst_public_headers.diff"
242
243
244 # Directory drivers/
245
246 (
247 add_patch "scst/kernel/in-tree/Kconfig.drivers.Linux-${kernel_version}.patch" \
248           "drivers/Kconfig"
249
250 add_patch "scst/kernel/in-tree/Makefile.drivers.Linux-${kernel_version}.patch"\
251           "drivers/Makefile"
252 ) \
253 | process_patch "misc.diff"
254
255
256 # Directory drivers/scst/
257
258 (
259 add_file "scst/kernel/in-tree/Kconfig.scst"  "drivers/scst/Kconfig"
260
261 add_file "scst/kernel/in-tree/Makefile.scst" "drivers/scst/Makefile"
262
263 for f in $(ls scst/src/*.[ch] 2>/dev/null)
264 do
265   if ! in_separate_patch "${f}"; then
266     add_file "${f}" "drivers/scst/${f#scst/src/}"
267   fi
268 done
269 ) \
270 | process_patch "scst_core.diff"
271
272 for s in ${separate_patches}
273 do
274   fileset=$s
275   for f in $(set | grep "^$s=" | sed -e "s/^$s='\(.*\)'\$/\1/" -e "s/^$s=\(.*\)\$/\1/")
276   do
277     if [ "${f#scst/include}" != "${f}" ]; then
278       add_file "${f}" "include/scst/${f#scst/include/}"
279     else
280       add_file "${f}" "drivers/scst/${f#scst/src/}"
281     fi
282   done \
283   | process_patch "${s}.diff"
284 done
285
286 add_file "scst/README_in-tree" "Documentation/scst/README.scst" \
287 | process_patch "scst_core_doc.diff"
288
289
290
291 # Directory drivers/scst/dev_handlers/
292
293 (
294 add_file "scst/kernel/in-tree/Makefile.dev_handlers" \
295          "drivers/scst/dev_handlers/Makefile" \
296 | process_patch "dev_handlers_makefile.diff"
297
298 for f in $(ls scst/src/dev_handlers/*.[ch] 2>/dev/null)
299 do
300   if ! in_separate_patch "${f}"; then
301     add_file "${f}" "drivers/scst/dev_handlers/${f#scst/src/dev_handlers/}"
302   fi
303 done
304 ) \
305 | process_patch "scst_passthrough.diff"
306
307
308 # Directory drivers/scst/iscsi-scst/
309
310 # Make sure the file iscsi-scst/iscsi_scst_itf_ver.h is up to date.
311 make -s -C iscsi-scst include/iscsi_scst_itf_ver.h
312
313 (
314 for f in $(ls iscsi-scst/include/*h 2>/dev/null)
315 do
316   if [ "${f}" != "iscsi-scst/include/iscsi_scst_itf_ver.h" ]; then
317     add_file "${f}" "include/scst/${f#iscsi-scst/include/}"
318   fi
319 done
320
321 add_file "iscsi-scst/include/iscsi_scst_itf_ver.h" "include/scst/iscsi_scst_itf_ver.h" | process_patch ""
322
323 add_file "iscsi-scst/kernel/Makefile.in-kernel" \
324          "drivers/scst/iscsi-scst/Makefile"
325
326 add_file "iscsi-scst/kernel/Kconfig"  "drivers/scst/iscsi-scst/Kconfig"
327
328 for f in $(ls iscsi-scst/kernel/*.[ch] 2>/dev/null)
329 do
330   add_file "${f}" "drivers/scst/iscsi-scst/${f#iscsi-scst/kernel/}"
331 done
332 ) \
333 | process_patch "iscsi-scst.diff"
334
335 add_file "iscsi-scst/README_in-tree" "Documentation/scst/README.iscsi" \
336 | process_patch "iscsi-scst-doc.diff"
337
338
339 # Directory drivers/scst/qla2x00-target/
340
341 if [ "${qla2x00t}" = "true" ]; then
342
343   if [ ! -e qla2x00t/qla2xxx-orig/$1 ]; then
344     ( cd qla2x00t && ./extract-qla2xxx-orig "$1" )
345   fi
346   ( cd qla2x00t && ./generate-in-tree-patches "$1" )
347
348   for f in $(ls qla2x00t/in-tree-patches/"$1"/*.patch 2>/dev/null)
349   do
350     g="${f#qla2x00t/in-tree-patches/$1/}"
351     g="${g%.patch}"
352     add_patch "${f}" "drivers/scsi/qla2xxx/${g}"
353   done
354
355   add_file "qla2x00t/qla2x_tgt.h" \
356            "drivers/scsi/qla2xxx/qla2x_tgt.h"
357
358   add_file "qla2x00t/qla2x_tgt_def.h" \
359            "drivers/scsi/qla2xxx/qla2x_tgt_def.h"
360
361   add_file "qla2x00t/qla2x00-target/Makefile_in-tree" \
362            "drivers/scst/qla2xxx-target/Makefile"
363
364   add_file "qla2x00t/qla2x00-target/Kconfig" \
365            "drivers/scst/qla2xxx-target/Kconfig"
366
367   for f in $(ls qla2x00t/qla2x00-target/*.[ch] 2>/dev/null)
368   do
369     add_file "${f}" "drivers/scst/qla2xxx-target/${f#qla2x00t/qla2x00-target/}"
370   done
371
372   add_file "qla2x00t/qla2x00-target/README" \
373            "Documentation/scst/README.qla2x00t" \
374   | process_patch "qla2x00t-doc.diff"
375
376 else
377
378   add_empty_file "drivers/scst/qla2xxx-target/Makefile"
379
380   add_empty_file "drivers/scst/qla2xxx-target/Kconfig"
381
382 fi \
383 | process_patch "qla2x00t.diff"
384
385
386 # Directory drivers/scst/srpt
387
388 if [ "$srpt" = "true" ]; then
389
390   add_file  "srpt/src/Kconfig"            "drivers/scst/srpt/Kconfig"
391
392   add_file  "srpt/src/Makefile.in_kernel" "drivers/scst/srpt/Makefile"
393
394   for f in $(ls srpt/src/*.[ch] 2>/dev/null)
395   do
396     add_file "${f}" "drivers/scst/srpt/${f#srpt/src/}"
397   done
398
399 else
400
401   add_empty_file "drivers/scst/srpt/Kconfig"
402
403   add_empty_file "drivers/scst/srpt/Makefile"
404
405 fi \
406 | process_patch "srpt.diff"
407
408 add_file "srpt/README_in-tree" "Documentation/scst/README.srpt" \
409 | process_patch "srpt-doc.diff"
410
411
412 # Directory drivers/message/fusion/mpt_scst
413
414 if [ "$mpt_scst" = "true" ]; then
415
416   (
417   add_patch "mpt/in-tree/Kconfig-2.6.24.diff" "drivers/message/fusion/Kconfig"
418
419   add_patch "mpt/in-tree/Makefile.diff"       "drivers/message/fusion/Makefile"
420
421   add_file  "mpt/in-tree/Makefile" "drivers/message/fusion/mpt_scst/Makefile"
422
423   add_file  "mpt/in-tree/Kconfig"  "drivers/message/fusion/mpt_scst/Kconfig"
424
425   for f in $(ls mpt/*.[ch] 2>/dev/null)
426   do
427     add_file "${f}" "drivers/message/fusion/mpt_scst/${f#mpt/}"
428   done
429   ) \
430   | process_patch "mpt_scst.diff"
431
432 fi
433
434
435 # Directory drivers/scst/scst_local
436
437 (
438   add_file "scst_local/in-tree/Kconfig" "drivers/scst/scst_local/Kconfig"
439
440   add_file "scst_local/in-tree/Makefile" "drivers/scst/scst_local/Makefile"
441
442   add_file "scst_local/scst_local.c" "drivers/scst/scst_local/scst_local.c"
443 ) \
444 | process_patch "scst_local.diff"
445
446 add_file "scst_local/README" "Documentation/scst/README.scst_local" \
447 | process_patch "scst_local-doc.diff"