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