f923d7893b874b2fb49557463262e31d60a80ebc
[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] [-l] [-m] [-q] [-s] <kernel version>, where: "
28   echo "        -h - show this text"
29   echo "        -l - add scst_local target driver"
30   echo "        -m - add mpt target driver"
31   echo "        -q - add qla2x00t driver"
32 }
33
34 # Convert an existing patch.
35 # $1: path of patch to be added.
36 # $2: path in kernel tree of file to be patched.
37 function add_patch {
38   if [ ! -e "$1" ]; then
39     echo "Error: could not find $1." >&2
40     exit 1
41   fi
42
43   sed -e "s:^--- [^ ]*:--- orig/linux-${kernel_version}/$2:" \
44       -e "s:^+++ [^ ]*:+++ linux-${kernel_version}/$2:"      \
45       < "$1"
46 }
47
48 # Generate a patch for a file to be added to the kernel source tree, and strip
49 # trailing whitespace from C source files while converting the file to patch
50 # format.
51 # $1: path of file to be added.
52 # $2: path in kernel tree where file should be added.
53 function add_file {
54   local a b
55
56   if [ ! -e "$1" ]; then
57     echo "Error: could not find $1." >&2
58     exit 1
59   fi
60
61   # Only include files that were not generated by the build process
62   # -- skip *.mod.c.
63   if [ "$1" = "${1%.mod.c}" -a "$1" ]; then
64     cat <<EOF
65 diff -uprN orig/linux-${kernel_version}/$2 linux-${kernel_version}/$2
66 --- orig/linux-${kernel_version}/$2
67 +++ linux-${kernel_version}/$2
68 @@ -0,0 +1,$(wc -l "$1" | { read a b; echo $a; }) @@
69 EOF
70     if [ "${2%.[ch]}" != "$2" ]; then
71       sed -e 's/sBUG(/BUG(/g' -e 's/sBUG_ON(/BUG_ON(/g' -e 's/^/+/' \
72           -e 's/^ \([^ ]*:\)$/\1/' < "$1"
73     else
74       sed -e 's/sBUG(/BUG(/g' -e 's/sBUG_ON(/BUG_ON(/g' -e 's/^/+/' < "$1"
75     fi
76   fi
77 }
78
79 function add_empty_file {
80   local a b
81
82   cat <<EOF
83 diff -uprN orig/linux-${kernel_version}/$1 linux-${kernel_version}/$1
84 --- orig/linux-${kernel_version}/$1
85 +++ linux-${kernel_version}/$1
86 @@ -0,0 +1,1 @@
87 +
88 EOF
89 }
90
91
92 #########################
93 # Argument verification #
94 #########################
95
96 qla2x00t="false"
97 srpt="true"
98 mpt_scst="false"
99 scst_local="false"
100
101 if [ ! -e scst -o ! -e iscsi-scst -o ! -e srpt -o ! -e scst_local ]; then
102   echo "Please run this script from inside the SCST subversion source tree."
103   exit 1
104 fi
105
106 set -- $(/usr/bin/getopt hlmq "$@")
107 while [ "$1" != "${1#-}" ]
108 do
109   case "$1" in
110     '-h') usage; exit 1;;
111     '-l') scst_local="true"; shift;;
112     '-m') mpt_scst="true"; shift;;
113     '-q') qla2x00t="true"; shift;;
114     '--') shift;;
115     *)    usage; exit 1;;
116   esac
117 done
118
119 if [ $# != 1 ]; then
120   usage
121   exit 1
122 fi
123
124
125 ####################
126 # Patch Generation #
127 ####################
128
129 # Strip patch level from the kernel version number.
130 if [ "${1#[0-9]*.[0-9]*.[0-9]*.[0-9]*}" != "$1" ]; then
131   kernel_version="${1%.[0-9]*}"
132 else
133   kernel_version="$1"
134 fi
135
136 echo "Signed-off-by: ... <...@...>"
137 echo ""
138
139 kpatch=(                                                                \
140   "scst/kernel/scst_exec_req_fifo-${kernel_version}.patch"              \
141   "scst/kernel/alloc_io_context-${kernel_version}.patch"                \
142   "iscsi-scst/kernel/patches/put_page_callback-${kernel_version}.patch" \
143 )
144
145 for p in "${kpatch[@]}"
146 do
147   if [ ! -e "$p" ]; then
148     echo "Error: kernel version ${kernel_version} is not supported by SCST."
149     echo "(could not find file $p)."
150     exit 1
151   fi >&2
152 done
153
154
155 # Redirect the output of all subsequent commands to the specialize-patch script
156
157 trap "rm -f ${fifo}" EXIT
158 fifo=/tmp/generate-kernel-patch-fifo.$$
159 rm -f "${fifo}"
160 mkfifo "${fifo}"
161 "$(dirname $0)/specialize-patch" -v kernel_version="${kernel_version}" \
162   < "${fifo}" &
163 exec >"${fifo}"
164
165 # General kernel patches.
166
167 cat "${kpatch[@]}"
168
169
170 # Directory include/scst/
171
172 for f in $(ls scst/include/*h)
173 do
174   add_file "${f}" "include/scst/${f#scst/include/}"
175 done
176
177
178 # Directory drivers/
179
180 add_patch "scst/kernel/in-tree/Kconfig.drivers.Linux-${kernel_version}.patch" \
181           "drivers/Kconfig"
182
183 add_patch "scst/kernel/in-tree/Makefile.drivers.Linux-${kernel_version}.patch"\
184           "drivers/Makefile"
185
186
187 # Directory drivers/scst/
188
189 add_file "scst/kernel/in-tree/Kconfig.scst"  "drivers/scst/Kconfig"
190
191 add_file "scst/kernel/in-tree/Makefile.scst" "drivers/scst/Makefile"
192
193 add_file "scst/README_in-tree"               "Documentation/scst/README.scst"
194
195 for f in $(ls scst/src/*.[ch])
196 do
197   add_file "${f}" "drivers/scst/${f#scst/src/}"
198 done
199
200
201 # Directory drivers/scst/dev_handlers/
202
203 add_file "scst/kernel/in-tree/Makefile.dev_handlers" \
204          "drivers/scst/dev_handlers/Makefile"
205
206 for f in $(ls scst/src/dev_handlers/*.[ch])
207 do
208   add_file "${f}" "drivers/scst/dev_handlers/${f#scst/src/dev_handlers/}"
209 done
210
211
212 # Directory drivers/scst/iscsi-scst/
213
214 # Make sure the file iscsi-scst/iscsi_scst_itf_ver.h is up to date.
215 make -s -C iscsi-scst include/iscsi_scst_itf_ver.h
216
217 for f in $(ls iscsi-scst/include/*h)
218 do
219   add_file "${f}" "include/scst/${f#iscsi-scst/include/}"
220 done
221
222 add_file "iscsi-scst/kernel/Makefile.in-kernel" \
223          "drivers/scst/iscsi-scst/Makefile"
224
225 add_file "iscsi-scst/kernel/Kconfig"  "drivers/scst/iscsi-scst/Kconfig"
226
227 for f in $(ls iscsi-scst/kernel/*.[ch])
228 do
229   add_file "${f}" "drivers/scst/iscsi-scst/${f#iscsi-scst/kernel/}"
230 done
231
232
233 # Directory drivers/scst/qla2x00-target/
234
235 if [ "${qla2x00t}" = "true" ]; then
236
237   add_file "qla2x00t/qla2x00-target/Makefile_in-tree" \
238            "drivers/scst/qla2xxx-target/Makefile"
239
240   add_file "qla2x00t/qla2x00-target/Kconfig" \
241            "drivers/scst/qla2xxx-target/Kconfig"
242
243   add_file "qla2x00t/qla2x_tgt_def.h" \
244            "drivers/scst/qla2x00-target/qla2x_tgt_def.h"
245
246   add_file "qla2x00t/qla2x00-target/README" \
247            "Documentation/scst/README.qla2x00t"
248
249   for f in $(ls qla2x00t/qla2x00-target/*.[ch])
250   do
251     add_file "${f}" "drivers/scst/qla2xxx-target/${f#qla2x00t/qla2x00-target/}"
252   done
253
254 else
255
256   add_empty_file "drivers/scst/qla2xxx-target/Makefile"
257
258   add_empty_file "drivers/scst/qla2xxx-target/Kconfig"
259
260 fi
261
262 # Directory drivers/scst/srpt
263
264 if [ "$srpt" = "true" ]; then
265
266   add_file  "srpt/src/Kconfig"            "drivers/scst/srpt/Kconfig"
267
268   add_file  "srpt/src/Makefile.in_kernel" "drivers/scst/srpt/Makefile"
269
270   add_file  "srpt/README"                 "Documentation/scst/README.srpt"
271
272   for f in $(ls srpt/src/*.[ch])
273   do
274     add_file "${f}" "drivers/scst/srpt/${f#srpt/src/}"
275   done
276
277 else
278
279   add_empty_file "drivers/scst/srpt/Kconfig"
280
281   add_empty_file "drivers/scst/srpt/Makefile"
282
283 fi
284
285 # Directory drivers/message/fusion/mpt_scst
286
287 if [ "$mpt_scst" = "true" ]; then
288
289   add_patch "mpt/in-tree/Kconfig-2.6.24.diff" "drivers/message/fusion/Kconfig"
290
291   add_patch "mpt/in-tree/Makefile.diff"       "drivers/message/fusion/Makefile"
292
293   add_file  "mpt/in-tree/Makefile" "drivers/message/fusion/mpt_scst/Makefile"
294
295   add_file  "mpt/in-tree/Kconfig"  "drivers/message/fusion/mpt_scst/Kconfig"
296
297   for f in $(ls mpt/*.[ch])
298   do
299     add_file "${f}" "drivers/message/fusion/mpt_scst/${f#mpt/}"
300   done
301
302 else
303
304   add_empty_file "drivers/message/fusion/Kconfig"
305
306   add_empty_file "drivers/message/fusion/Makefile"
307
308 fi
309
310 # Directory drivers/scst/scst-local
311
312 if [ "$scst_local" = "true" ]; then
313
314   add_file "scst_local/in-tree/Kconfig" "drivers/scst/scst_local/Kconfig"
315
316   add_file "scst_local/in-tree/Makefile" "drivers/scst/scst_local/Makefile"
317
318   add_file "scst_local/scst_local.c" "drivers/scst/scst_local/scst_local.c"
319
320 else
321
322   add_empty_file "drivers/scst/scst_local/Kconfig"
323
324   add_empty_file "drivers/scst/scst_local/Makefile"
325
326 fi