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