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