OSDN Git Service

Add MS7619SE
[uclinux-h8/uClinux-dist.git] / tools / romfs-inst.sh
1 #!/bin/sh
2 #
3 # A tool to simplify Makefiles that need to put something
4 # into the ROMFS
5 #
6 # Copyright (C) David McCullough, 2002,2003
7 #
8 #############################################################################
9
10 # Provide a default PATH setting to avoid potential problems...
11 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:$PATH"
12
13 usage()
14 {
15 cat << !EOF >&2
16 $0: [options] [src] dst
17     -v          : output actions performed.
18     -V          : output actions not performed.
19     -e env-var  : only take action if env-var is set to "y".
20     -E env-var  : only take action if env-var is not set to "y".
21     -o option   : only take action if option is set to "y".
22     -O option   : only take action if option is not set to "y".
23     -c          : process with cpp+cflags
24     -p perms    : chmod style permissions for dst.
25     -d          : make dst directory if it doesn't exist
26     -S          : don't strip after installing
27     -a text     : append text to dst.
28     -A pattern  : only append text if pattern doesn't exist in file
29     -l link     : dst is a hard link to 'link'.
30     -s sym-link : dst is a sym-link to 'sym-link'.
31     -M          : install kernel module into dst subdir of module dir
32     -r          : root directory of install (default ROMFSDIR)
33     -R          : remove dst
34     -f          : do not follow symlinks
35
36     if "src" is not provided,  basename is run on dst to determine the
37     source in the current directory.
38
39     multiple -e and -o options are ANDed together.  To achieve an OR affect
40     use a single -e/-o with 1 or more y/n/"" chars in the condition.
41
42     if src is a directory,  everything in it is copied recursively to dst
43     with special files removed (currently CVS and Subversion dirs).
44 !EOF
45         exit 1
46 }
47
48 #############################################################################
49
50 setperm()
51 {
52         rc=0
53         # Always start with write access for the user so that files can be
54         # updated/overwritten during make romfs
55         chmod u+w ${ROMFSDIR}${dst}
56         if [ "$perm" ]
57         then
58                 [ "$v" ] && echo "chmod ${perm} ${ROMFSDIR}${dst}"
59                 chmod ${perm} ${ROMFSDIR}${dst}
60                 rc=$?
61         fi
62         return $rc
63 }
64
65 #############################################################################
66
67 file_copy()
68 {
69         rc=0
70         if [ -d "${src}" ]
71         then
72                 [ "$v" ] && echo "CopyDir ${src} ${ROMFSDIR}${dst}"
73                 (
74                         cd ${src} || return 1
75                         VV=
76                         [ "$v" ] && VV=v
77                         find . -print | grep -E -v '/CVS|/\.svn' | cpio --quiet -p${VV}dum${follow} ${ROMFSDIR}${dst}
78                         rc=$?
79                         # And make sure these files are still writable
80                         find . -print | grep -E -v '/CVS|/\.svn' | ( cd ${ROMFSDIR}${dst}; xargs chmod u+w )
81                         setperm ${ROMFSDIR}${dst}
82                         find . -type f | grep -E -v '/CVS|/\.svn|\.ko$' | while read t; do
83                                 if [ -n "$strip" ]; then
84                                         ${STRIPTOOL} ${ROMFSDIR}${dst}/$t 2>/dev/null
85                                         ${STRIPTOOL} -R .comment -R .note ${ROMFSDIR}${dst}/$t 2>/dev/null
86                                 fi
87                         done
88                 )
89         else
90                 if [ -d ${ROMFSDIR}${dst} ]; then
91                         dstfile=${ROMFSDIR}${dst}/`basename ${src}`
92                 else
93                         dstfile=${ROMFSDIR}${dst}
94                 fi
95                 rm -f ${dstfile}
96                 [ "$v" ] && echo "cp ${src} ${dstfile}"
97                 if [ ! -d "$IMAGEDIR" ]
98                 then
99                         mkdir -p $IMAGEDIR
100                 fi
101                 case "$src" in
102                         /*) echo "${src} ${dstfile}" ;;
103                         *)  echo "`pwd`/${src} ${dstfile}" ;;
104                 esac >> $IMAGEDIR/romfs-inst.log
105                 cp ${src} ${dstfile} && setperm ${dstfile}
106                 rc=$?
107                 if [ $rc -eq 0 -a -n "$strip" ]; then
108                         ${STRIPTOOL} ${dstfile} 2>/dev/null
109                         ${STRIPTOOL} -R .comment -R .note ${dstfile} 2>/dev/null
110                 fi
111         fi
112         return $rc
113 }
114
115 #############################################################################
116
117 file_append()
118 {
119         touch ${ROMFSDIR}${dst} || return 1
120         if [ -z "${pattern}" ] && grep -F "${src}" ${ROMFSDIR}${dst} > /dev/null
121         then
122                 [ "$V" ] && echo "File entry already installed."
123         elif [ "${pattern}" ] && egrep "${pattern}" ${ROMFSDIR}${dst} > /dev/null
124         then
125                 [ "$V" ] && echo "File pattern already installed."
126         else
127                 [ "$v" ] && echo "Installing entry into ${ROMFSDIR}${dst}."
128                 if [ -s ${ROMFSDIR}${dst} ] ; then
129                         # if file lacks a trailing new line, add it before appending the text
130                         if [ $(tail -n1 ${ROMFSDIR}${dst} | tr -d '\n' | wc -c) = $(tail -n1 ${ROMFSDIR}${dst} | wc -c) ] ; then
131                                 echo "" >> ${ROMFSDIR}${dst} || return 1
132                         fi
133                 fi
134                 echo "${src}" >> ${ROMFSDIR}${dst} || return 1
135         fi
136         setperm ${ROMFSDIR}${dst}
137         return $?
138 }
139
140 #############################################################################
141
142 hard_link()
143 {
144         rm -f ${ROMFSDIR}${dst}
145         [ "$v" ] && echo "ln ${src} ${ROMFSDIR}${dst}"
146         ln ${ROMFSDIR}${src} ${ROMFSDIR}${dst}
147         return $?
148 }
149
150 #############################################################################
151
152 sym_link()
153 {
154         rm -f ${ROMFSDIR}${dst}
155         [ "$v" ] && echo "ln -s ${src} ${ROMFSDIR}${dst}"
156         ln -sf ${src} ${ROMFSDIR}${dst}
157         return $?
158 }
159
160 #############################################################################
161
162 cpp_file()
163 {
164         set -x
165         if [ -d ${ROMFSDIR}${dst} ]; then
166                 dstfile=${ROMFSDIR}${dst}/`basename ${src}`
167         else
168                 dstfile=${ROMFSDIR}${dst}
169         fi
170         rm -f ${dstfile}
171         [ "$v" ] && echo "${CROSS_COMPILE}cpp ${CFLAGS} -P < ${src} > ${dstfile}"
172         ${CROSS_COMPILE}cpp ${CFLAGS} -P < ${src} > ${dstfile}
173         return $?
174 }
175
176 #############################################################################
177
178 rm_file()
179 {
180         # if ROMFSDIR is not set, play it safe
181         [ "${ROMFSDIR}" ] || return 1
182         if [ -d "${ROMFSDIR}${dst}" ]; then
183                 echo "rm -rf ${ROMFSDIR}${dst}"
184                 rm -rf "${ROMFSDIR}${dst}"
185         else
186                 echo "rm -f ${ROMFSDIR}${dst}"
187                 rm -f "${ROMFSDIR}${dst}"
188         fi
189         return $?
190 }
191
192 #############################################################################
193 #
194 # main program entry point
195 #
196
197 v=
198 V=
199 option=y
200 noption=
201 pattern=
202 perm=
203 func=file_copy
204 mdir=
205 src=
206 dst=
207 strip=1
208 kernmod=
209 r=
210 follow=L
211
212 while getopts 'VdRfSMvce:E:o:O:A:p:a:l:s:r:' opt "$@"
213 do
214         case "$opt" in
215         v) v="1";                           ;;
216         V) V="1";                           ;;
217         d) mdir="1";                        ;;
218         f) follow=;                         ;;
219         S) strip=;                                                      ;;
220         M) kernmod="1";                     ;;
221         o) option="$OPTARG";                ;;
222         O) noption="$OPTARG";               ;;
223         e) eval option=\"\$$OPTARG\";       ;;
224         E) eval noption=\"\$$OPTARG\";      ;;
225         p) perm="$OPTARG";                  ;;
226         a) src="$OPTARG"; func=file_append; ;;
227         A) pattern="$OPTARG";               ;;
228         l) src="$OPTARG"; func=hard_link;   ;;
229         s) src="$OPTARG"; func=sym_link;    ;;
230         r) ROMFSDIR="$OPTARG"; r=1;         ;;
231         c) func=cpp_file;                   ;;
232         R) func=rm_file;                    ;;
233
234         *)  break ;;
235         esac
236 #
237 #       process option here to get an ANDing effect
238 #
239         case "$option" in
240         *[mMyY]*) # this gives OR effect, ie., nYn
241                 ;;
242         *)
243                 [ "$V" ] && echo "Condition not satisfied."
244                 exit 0
245                 ;;
246         esac
247
248 #
249 #       process negative options here to get an ANDing effect
250 #
251         case "${noption:-n}" in
252         *[nN]*) # this gives OR effect, ie., yNy
253                 ;;
254         *)
255                 [ "$V" ] && echo "Condition not satisfied."
256                 exit 0
257                 ;;
258         esac
259 done
260
261 if [ -z "$ROMFSDIR" -a -z "$r" ]
262 then
263         echo "ROMFSDIR is not set" >&2
264         usage
265         exit 1
266 fi
267
268 if [ -z "$STRIPTOOL" ]
269 then
270         STRIPTOOL=strip
271 fi      
272
273 shift `expr $OPTIND - 1`
274
275 case $# in
276 1)
277         dst="$1"
278         if [ -z "$src" ]
279         then
280                 src="`basename $dst`"
281         fi
282         ;;
283 2)
284         if [ ! -z "$src" ]
285         then
286                 echo "Source file already provided" >&2
287                 exit 1
288         fi
289         src="$1"
290         dst="$2"
291         ;;
292 *)
293         usage
294         ;;
295 esac
296
297 if [ -n "$kernmod" ]; then
298         strip=
299         kerndir=${ROOTDIR}/${LINUXDIR}
300         # could prob take from UTS headers as well ...
301         kernver=$(cat ${kerndir}/include/config/kernel.release)
302         dst="/lib/modules/${kernver}/${dst}"
303 fi
304
305 if [ "$mdir" -a ! -d "`dirname ${ROMFSDIR}${dst}`/." ]
306 then
307         mkdir -p "`dirname ${ROMFSDIR}${dst}`/." || exit 1
308 fi
309
310 $func || exit 1
311
312 exit 0
313
314 #############################################################################