112cbf43ecfbc5ba3e6b507dfcf8e38b3a3c27bd
[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     fi
109   else
110     "$(dirname $0)/specialize-patch" -v kernel_version="${kernel_version}" 
111   fi
112 }
113
114 #########################
115 # Argument verification #
116 #########################
117
118 qla2x00t="false"
119 srpt="true"
120 mpt_scst="false"
121 multiple_patches="false"
122 patchdir=""
123
124 if [ ! -e scst -o ! -e iscsi-scst -o ! -e srpt -o ! -e scst_local ]; then
125   echo "Please run this script from inside the SCST subversion source tree."
126   exit 1
127 fi
128
129 set -- $(/usr/bin/getopt hlmp:q "$@")
130 while [ "$1" != "${1#-}" ]
131 do
132   case "$1" in
133     '-h') usage; exit 1;;
134     '-l') shift;;
135     '-m') mpt_scst="true"; shift;;
136     '-p') multiple_patches="true"; patchdir="$2"; shift; shift;;
137     '-q') qla2x00t="true"; shift;;
138     '--') shift;;
139     *)    usage; exit 1;;
140   esac
141 done
142
143 if [ $# != 1 ]; then
144   usage
145   exit 1
146 fi
147
148 if [ "${multiple_patches}" = "" ]; then
149   if [ -e "${patchdir}" ]; then
150     echo "Patch output directory ${patchdir} already exists."
151   fi
152   if [ ! -d "${patchdir}" ]; then
153     echo "Error: ${patchdir} is not a directory."
154   fi
155 fi
156
157
158 ####################
159 # Patch Generation #
160 ####################
161
162 # Strip patch level from the kernel version number.
163 if [ "${1#[0-9]*.[0-9]*.[0-9]*.[0-9]*}" != "$1" ]; then
164   kernel_version="${1%.[0-9]*}"
165 else
166   kernel_version="$1"
167 fi
168
169 if [ "${multiple_patches}" = "false" ]; then
170   echo "Signed-off-by: ${SIGNED_OFF_BY}"
171   echo ""
172 fi
173
174
175 # General kernel patches.
176
177 process_patch < "scst/kernel/scst_exec_req_fifo-${kernel_version}.patch" \
178     "scst_exec_req_fifo.diff"
179 process_patch < "scst/kernel/export_alloc_io_context-${kernel_version}.patch" \
180     "export_alloc_io_context.diff"
181 process_patch < "iscsi-scst/kernel/patches/put_page_callback-${kernel_version}.patch" \
182     "put_page_callback.diff"
183
184
185 # Directory include/scst/
186
187 for f in $(ls scst/include/*h)
188 do
189   add_file "${f}" "include/scst/${f#scst/include/}"
190 done \
191 | process_patch "scst_public_headers.diff"
192
193
194 # Directory drivers/
195
196 (
197 add_patch "scst/kernel/in-tree/Kconfig.drivers.Linux-${kernel_version}.patch" \
198           "drivers/Kconfig"
199
200 add_patch "scst/kernel/in-tree/Makefile.drivers.Linux-${kernel_version}.patch"\
201           "drivers/Makefile"
202 ) \
203 | process_patch "misc.diff"
204
205
206 # Directory drivers/scst/
207
208 (
209 add_file "scst/kernel/in-tree/Kconfig.scst"  "drivers/scst/Kconfig"
210
211 add_file "scst/kernel/in-tree/Makefile.scst" "drivers/scst/Makefile"
212
213 for f in $(ls scst/src/*.[ch])
214 do
215   add_file "${f}" "drivers/scst/${f#scst/src/}"
216 done
217 ) \
218 | process_patch "scst_core.diff"
219
220 add_file "scst/README_in-tree" "Documentation/scst/README.scst" \
221 | process_patch "scst_core_doc.diff"
222
223
224
225 # Directory drivers/scst/dev_handlers/
226
227 (
228 add_file "scst/kernel/in-tree/Makefile.dev_handlers" \
229          "drivers/scst/dev_handlers/Makefile" \
230 | process_patch "dev_handlers_makefile.diff"
231
232 for f in $(ls scst/src/dev_handlers/*.[ch])
233 do
234   add_file "${f}" "drivers/scst/dev_handlers/${f#scst/src/dev_handlers/}"
235 done
236 ) \
237 | process_patch "scst_dev_handlers.diff"
238
239
240 # Directory drivers/scst/iscsi-scst/
241
242 # Make sure the file iscsi-scst/iscsi_scst_itf_ver.h is up to date.
243 make -s -C iscsi-scst include/iscsi_scst_itf_ver.h
244
245 (
246 for f in $(ls iscsi-scst/include/*h)
247 do
248   add_file "${f}" "include/scst/${f#iscsi-scst/include/}"
249 done
250
251 add_file "iscsi-scst/kernel/Makefile.in-kernel" \
252          "drivers/scst/iscsi-scst/Makefile"
253
254 add_file "iscsi-scst/kernel/Kconfig"  "drivers/scst/iscsi-scst/Kconfig"
255
256 for f in $(ls iscsi-scst/kernel/*.[ch])
257 do
258   add_file "${f}" "drivers/scst/iscsi-scst/${f#iscsi-scst/kernel/}"
259 done
260 ) \
261 | process_patch "iscsi-scst.diff"
262
263 add_file "iscsi-scst/README_in-tree" "Documentation/scst/README.iscsi" \
264 | process_patch "iscsi-scst-doc.diff"
265
266
267 # Directory drivers/scst/qla2x00-target/
268
269 if [ "${qla2x00t}" = "true" ]; then
270
271   add_file "qla2x00t/qla2x00-target/Makefile_in-tree" \
272            "drivers/scst/qla2xxx-target/Makefile"
273
274   add_file "qla2x00t/qla2x00-target/Kconfig" \
275            "drivers/scst/qla2xxx-target/Kconfig"
276
277 #  add_file "qla2x00t/qla2x_tgt_def.h" \
278 #           "drivers/scst/qla2x00-target/qla2x_tgt_def.h"
279
280   add_file "qla2x00t/qla2x00-target/README" \
281            "Documentation/scst/README.qla2x00t"
282
283   for f in $(ls qla2x00t/qla2x00-target/*.[ch])
284   do
285     add_file "${f}" "drivers/scst/qla2xxx-target/${f#qla2x00t/qla2x00-target/}"
286   done
287
288 else
289
290   add_empty_file "drivers/scst/qla2xxx-target/Makefile"
291
292   add_empty_file "drivers/scst/qla2xxx-target/Kconfig"
293
294 fi \
295 | process_patch "qla2x00t.diff"
296
297
298 # Directory drivers/scst/srpt
299
300 if [ "$srpt" = "true" ]; then
301
302   add_file  "srpt/src/Kconfig"            "drivers/scst/srpt/Kconfig"
303
304   add_file  "srpt/src/Makefile.in_kernel" "drivers/scst/srpt/Makefile"
305
306   for f in $(ls srpt/src/*.[ch])
307   do
308     add_file "${f}" "drivers/scst/srpt/${f#srpt/src/}"
309   done
310
311 else
312
313   add_empty_file "drivers/scst/srpt/Kconfig"
314
315   add_empty_file "drivers/scst/srpt/Makefile"
316
317 fi \
318 | process_patch "srpt.diff"
319
320 add_file "srpt/README" "Documentation/scst/README.srpt" \
321 | process_patch "srpt-doc.diff"
322
323
324 # Directory drivers/message/fusion/mpt_scst
325
326 if [ "$mpt_scst" = "true" ]; then
327
328   (
329   add_patch "mpt/in-tree/Kconfig-2.6.24.diff" "drivers/message/fusion/Kconfig"
330
331   add_patch "mpt/in-tree/Makefile.diff"       "drivers/message/fusion/Makefile"
332
333   add_file  "mpt/in-tree/Makefile" "drivers/message/fusion/mpt_scst/Makefile"
334
335   add_file  "mpt/in-tree/Kconfig"  "drivers/message/fusion/mpt_scst/Kconfig"
336
337   for f in $(ls mpt/*.[ch])
338   do
339     add_file "${f}" "drivers/message/fusion/mpt_scst/${f#mpt/}"
340   done
341   ) \
342   | process_patch "mpt_scst.diff"
343
344 else
345
346   (
347     add_empty_file "drivers/message/fusion/Kconfig"
348
349     add_empty_file "drivers/message/fusion/Makefile"
350   ) \
351   | process_patch ""
352
353 fi
354
355
356 # Directory drivers/scst/scst_local
357
358 (
359   add_file "scst_local/in-tree/Kconfig" "drivers/scst/scst_local/Kconfig"
360
361   add_file "scst_local/in-tree/Makefile" "drivers/scst/scst_local/Makefile"
362
363   add_file "scst_local/scst_local.c" "drivers/scst/scst_local/scst_local.c"
364 ) \
365 | process_patch "scst_local.diff"
366
367 add_file "scst_local/README" "Documentation/scst/README.scst_local" \
368 | process_patch "scst_local-doc.diff"