TRACE_ENTRY() / TRACE_EXIT*() statements are now filtered from the output
[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-2009 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   | if [ "${generating_upstream_patch}" = "true" ]; then
110       scripts/filter-trace-entry-exit
111     else
112       cat
113     fi
114 }
115
116 # Read a patch from stdin, specialize it for kernel version ${kernel_version}
117 # and write the output either to stdout or to the file $1 (if not empty),
118 # depending on the value of the variable ${multiple_patches}.
119 function process_patch {
120   if [ "${multiple_patches}" = "true" ]; then
121     if [ "$1" != "" ]; then
122       if [ -e "${patchdir}/$1" ]; then
123         echo "Warning: overwriting ${patchdir}/$1"
124       fi
125     (
126       echo "Signed-off-by: ${SIGNED_OFF_BY}"
127       echo ""
128       specialize_patch
129     ) >"${patchdir}/$1"
130     else
131       # echo "Discarded $(wc -l) lines."
132       true
133     fi
134   else
135     specialize_patch
136   fi
137 }
138
139 # Returns 0 (true) if SCST core file "$1" should be added in a separate patch,
140 # and 1 (false) if not.
141 function in_separate_patch {
142   echo "${source_files_in_separate_patch}" | grep -qE "^$1 | $1 | $1\$|^$1\$"
143 }
144
145
146 #########################
147 # Argument verification #
148 #########################
149
150 generating_upstream_patch="false"
151 mpt_scst="false"
152 multiple_patches="false"
153 patchdir=""
154 qla2x00t="true"
155 replace_sbug_by_bug="true"
156 specialize_patch_options="-v delete_disabled_code=1"
157 srpt="true"
158
159 if [ ! -e scst -o ! -e iscsi-scst -o ! -e srpt -o ! -e scst_local ]; then
160   echo "Please run this script from inside the SCST subversion source tree."
161   exit 1
162 fi
163
164 set -- $(/usr/bin/getopt hlmnp:u "$@")
165 while [ "$1" != "${1#-}" ]
166 do
167   case "$1" in
168     '-h') usage; exit 1;;
169     '-l') shift;;
170     '-m') mpt_scst="true"; shift;;
171     '-n') specialize_patch_options="-v blank_deleted_code=1"
172           shift
173           ;;
174     '-p') multiple_patches="true"; patchdir="$2"; shift; shift;;
175     '-u') generating_upstream_patch="true"; shift;;
176     '--') shift;;
177     *)    usage; exit 1;;
178   esac
179 done
180
181 if [ "${generating_upstream_patch}" = "true" ]; then
182   specialize_patch_options="${specialize_patch_options} -v generating_upstream_patch_defined=1 -v config_tcp_zero_copy_transfer_completion_notification_undefined=1 -v config_scst_proc_undefined=1"
183 fi
184
185 if [ $# != 1 ]; then
186   usage
187   exit 1
188 fi
189
190 if [ "${multiple_patches}" = "" ]; then
191   if [ -e "${patchdir}" ]; then
192     echo "Patch output directory ${patchdir} already exists."
193   fi
194   if [ ! -d "${patchdir}" ]; then
195     echo "Error: ${patchdir} is not a directory."
196   fi
197 fi
198
199
200 ####################
201 # Patch Generation #
202 ####################
203
204 # Strip patch level from the kernel version number.
205 if [ "${1#[0-9]*.[0-9]*.[0-9]*.[0-9]*}" != "$1" ]; then
206   kernel_version="${1%.[0-9]*}"
207   patch_level="${1%${kernel_version}}"
208 else
209   kernel_version="$1"
210 fi
211
212 if [ "${multiple_patches}" = "false" ]; then
213   echo "Signed-off-by: ${SIGNED_OFF_BY}"
214   echo ""
215 fi
216
217
218 # General kernel patches.
219
220 scsi_exec_req_fifo_defined=0
221 scst_io_context=0
222 for p in scst/kernel/*-${kernel_version}.patch \
223          iscsi-scst/kernel/patches/*-${kernel_version}.patch
224 do
225   # Exclude the put_page_callback patch when command-line option -u has been
226   # specified since the current approach is not considered acceptable for
227   # upstream kernel inclusion. See also http://lkml.org/lkml/2008/12/11/213.
228   if [ "${generating_upstream_patch}" = "false" \
229        -o "${p#iscsi-scst/kernel/patches/put_page_callback}" = "$p" ]
230   then
231     if grep -q '^\+#define SCSI_EXEC_REQ_FIFO_DEFINED$' "${p}"; then
232       scsi_exec_req_fifo_defined=1
233     fi
234     if grep -q '^\+#define SCST_IO_CONTEXT$' "${p}"; then
235       scst_io_context=1
236     fi
237     diffname="${p#scst/kernel/}"
238     diffname="${p%-${kernel_version}.patch}.diff"
239     process_patch < "$p" "${diffname}"
240   fi
241 done
242
243
244 scst_debug="scst/include/scst_debug.h scst/src/scst_debug.c"
245 scst_proc="scst/src/scst_proc.c"
246 scst_sgv="scst/include/scst_sgv.h scst/src/scst_mem.h scst/src/scst_mem.c"
247 scst_user="scst/include/scst_user.h scst/src/dev_handlers/scst_user.c"
248 scst_vdisk="scst/src/dev_handlers/scst_vdisk.c"
249 separate_patches="scst_debug scst_proc scst_sgv scst_user scst_vdisk"
250 source_files_in_separate_patch="${scst_debug} ${scst_proc} ${scst_sgv} ${scst_user} ${scst_vdisk}"
251
252
253 # Directory include/scst/
254
255 for f in $(ls scst/include/*h 2>/dev/null)
256 do
257   if ! in_separate_patch "${f}"; then
258     add_file "${f}" "include/scst/${f#scst/include/}"
259   fi
260 done \
261 | process_patch "scst_public_headers.diff"
262
263
264 # Directory drivers/
265
266 (
267 add_patch "scst/kernel/in-tree/Kconfig.drivers.Linux-${kernel_version}.patch" \
268           "drivers/Kconfig"
269
270 add_patch "scst/kernel/in-tree/Makefile.drivers.Linux-${kernel_version}.patch"\
271           "drivers/Makefile"
272 ) \
273 | process_patch "misc.diff"
274
275
276 # Directory drivers/scst/
277
278 (
279 add_file "scst/kernel/in-tree/Kconfig.scst"  "drivers/scst/Kconfig"
280
281 add_file "scst/kernel/in-tree/Makefile.scst" "drivers/scst/Makefile"
282
283 for f in $(ls scst/src/*.[ch] 2>/dev/null)
284 do
285   if [ "${generating_upstream_patch}" = "true" \
286        -a "${f}" = "scst/src/scst_proc.c" ]; then
287     continue
288   fi
289   if ! in_separate_patch "${f}"; then
290     add_file "${f}" "drivers/scst/${f#scst/src/}"
291   fi
292 done
293 ) \
294 | process_patch "scst_core.diff"
295
296 for s in ${separate_patches}
297 do
298   fileset=$s
299   for f in $(set | grep "^$s=" | sed -e "s/^$s='\(.*\)'\$/\1/" -e "s/^$s=\(.*\)\$/\1/")
300   do
301     if [ "${generating_upstream_patch}" = "true" \
302          -a "${f}" = "scst/src/scst_proc.c" ]; then
303       continue
304     fi
305     if [ "${f#scst/include}" != "${f}" ]; then
306       add_file "${f}" "include/scst/${f#scst/include/}"
307     else
308       add_file "${f}" "drivers/scst/${f#scst/src/}"
309     fi
310   done \
311   | process_patch "${s}.diff"
312 done
313
314 add_file "scst/README_in-tree" "Documentation/scst/README.scst" \
315 | process_patch "scst_core_doc.diff"
316
317
318
319 # Directory drivers/scst/dev_handlers/
320
321 (
322 add_file "scst/kernel/in-tree/Makefile.dev_handlers" \
323          "drivers/scst/dev_handlers/Makefile" \
324 | process_patch "dev_handlers_makefile.diff"
325
326 for f in $(ls scst/src/dev_handlers/*.[ch] 2>/dev/null)
327 do
328   if ! in_separate_patch "${f}"; then
329     add_file "${f}" "drivers/scst/dev_handlers/${f#scst/src/dev_handlers/}"
330   fi
331 done
332 ) \
333 | process_patch "scst_passthrough.diff"
334
335
336 # Directory drivers/scst/iscsi-scst/
337
338 # Make sure the file iscsi-scst/iscsi_scst_itf_ver.h is up to date.
339 make -s -C iscsi-scst include/iscsi_scst_itf_ver.h
340
341 (
342 for f in $(ls iscsi-scst/include/*h 2>/dev/null)
343 do
344   if [ "${f}" != "iscsi-scst/include/iscsi_scst_itf_ver.h" ]; then
345     add_file "${f}" "include/scst/${f#iscsi-scst/include/}"
346   fi
347 done
348
349 add_file "iscsi-scst/include/iscsi_scst_itf_ver.h" "include/scst/iscsi_scst_itf_ver.h" | process_patch ""
350
351 add_file "iscsi-scst/kernel/Makefile.in-kernel" \
352          "drivers/scst/iscsi-scst/Makefile"
353
354 add_file "iscsi-scst/kernel/Kconfig"  "drivers/scst/iscsi-scst/Kconfig"
355
356 for f in $(ls iscsi-scst/kernel/*.[ch] 2>/dev/null)
357 do
358   add_file "${f}" "drivers/scst/iscsi-scst/${f#iscsi-scst/kernel/}"
359 done
360 ) \
361 | process_patch "iscsi-scst.diff"
362
363 add_file "iscsi-scst/README_in-tree" "Documentation/scst/README.iscsi" \
364 | process_patch "iscsi-scst-doc.diff"
365
366
367 # Directory drivers/scst/qla2x00-target/
368
369 if [ "${qla2x00t}" = "true" ]; then
370
371   ( cd qla2x00t && ./generate-in-tree-patches "$1" )
372
373   for f in $(ls qla2x00t/in-tree-patches/"$1"/*.patch 2>/dev/null)
374   do
375     g="${f#qla2x00t/in-tree-patches/$1/}"
376     g="${g%.patch}"
377     add_patch "${f}" "drivers/scsi/qla2xxx/${g}"
378   done
379
380   add_file "qla2x00t/qla2x_tgt.h" \
381            "drivers/scsi/qla2xxx/qla2x_tgt.h"
382
383   add_file "qla2x00t/qla2x_tgt_def.h" \
384            "drivers/scsi/qla2xxx/qla2x_tgt_def.h"
385
386   add_file "qla2x00t/qla2x00-target/Makefile_in-tree" \
387            "drivers/scst/qla2xxx-target/Makefile"
388
389   add_file "qla2x00t/qla2x00-target/Kconfig" \
390            "drivers/scst/qla2xxx-target/Kconfig"
391
392   for f in $(ls qla2x00t/qla2x00-target/*.[ch] 2>/dev/null)
393   do
394     add_file "${f}" "drivers/scst/qla2xxx-target/${f#qla2x00t/qla2x00-target/}"
395   done
396
397   add_file "qla2x00t/qla2x00-target/README" \
398            "Documentation/scst/README.qla2x00t" \
399   | process_patch "qla2x00t-doc.diff"
400
401 else
402
403   add_empty_file "drivers/scst/qla2xxx-target/Makefile"
404
405   add_empty_file "drivers/scst/qla2xxx-target/Kconfig"
406
407 fi \
408 | process_patch "qla2x00t.diff"
409
410
411 # Directory drivers/scst/srpt
412
413 if [ "$srpt" = "true" ]; then
414
415   add_file  "srpt/src/Kconfig"            "drivers/scst/srpt/Kconfig"
416
417   add_file  "srpt/src/Makefile.in_kernel" "drivers/scst/srpt/Makefile"
418
419   for f in $(ls srpt/src/*.[ch] 2>/dev/null)
420   do
421     add_file "${f}" "drivers/scst/srpt/${f#srpt/src/}"
422   done
423
424 else
425
426   add_empty_file "drivers/scst/srpt/Kconfig"
427
428   add_empty_file "drivers/scst/srpt/Makefile"
429
430 fi \
431 | process_patch "srpt.diff"
432
433 add_file "srpt/README_in-tree" "Documentation/scst/README.srpt" \
434 | process_patch "srpt-doc.diff"
435
436
437 # Directory drivers/message/fusion/mpt_scst
438
439 if [ "$mpt_scst" = "true" ]; then
440
441   (
442   add_patch "mpt/in-tree/Kconfig-2.6.24.diff" "drivers/message/fusion/Kconfig"
443
444   add_patch "mpt/in-tree/Makefile.diff"       "drivers/message/fusion/Makefile"
445
446   add_file  "mpt/in-tree/Makefile" "drivers/message/fusion/mpt_scst/Makefile"
447
448   add_file  "mpt/in-tree/Kconfig"  "drivers/message/fusion/mpt_scst/Kconfig"
449
450   for f in $(ls mpt/*.[ch] 2>/dev/null)
451   do
452     add_file "${f}" "drivers/message/fusion/mpt_scst/${f#mpt/}"
453   done
454   ) \
455   | process_patch "mpt_scst.diff"
456
457 fi
458
459
460 # Directory drivers/scst/scst_local
461
462 (
463   add_file "scst_local/in-tree/Kconfig" "drivers/scst/scst_local/Kconfig"
464
465   add_file "scst_local/in-tree/Makefile" "drivers/scst/scst_local/Makefile"
466
467   add_file "scst_local/scst_local.c" "drivers/scst/scst_local/scst_local.c"
468 ) \
469 | process_patch "scst_local.diff"
470
471 add_file "scst_local/README" "Documentation/scst/README.scst_local" \
472 | process_patch "scst_local-doc.diff"