OSDN Git Service

PR c++/9704
[pf3gnuchains/gcc-fork.git] / gcc / fixinc / fixincl.sh
1 #!/bin/sh
2 #
3 # Install modified versions of certain ANSI-incompatible system header
4 # files which are fixed to work correctly with ANSI C and placed in a
5 # directory that GNU C will search.
6 #
7 # See README-fixinc for more information.
8 #
9 #  fixincludes copyright (c) 1998, 1999, 2000, 2002
10 #  The Free Software Foundation, Inc.
11 #
12 # fixincludes is free software.
13
14 # You may redistribute it and/or modify it under the terms of the
15 # GNU General Public License, as published by the Free Software
16 # Foundation; either version 2, or (at your option) any later version.
17
18 # fixincludes is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 # See the GNU General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with fixincludes.  See the file "COPYING".  If not,
25 # write to:  The Free Software Foundation, Inc.,
26 #            59 Temple Place - Suite 330,
27 #            Boston,  MA  02111-1307, USA.
28 #
29 # # # # # # # # # # # # # # # # # # # # #
30 #
31 # Directory in which to store the results.
32 # Fail if no arg to specify a directory for the output.
33 if [ "x$1" = "x" ]
34 then
35   echo fixincludes: no output directory specified
36   exit 1
37 fi
38
39 LIB=${1}
40 shift
41
42 # Make sure it exists.
43 if [ ! -d $LIB ]; then
44   mkdir $LIB || {
45     echo fixincludes:  output dir '`'$LIB"' cannot be created"
46     exit 1
47   }
48 else
49   ( cd $LIB && touch DONE && rm DONE ) || {
50     echo fixincludes:  output dir '`'$LIB"' is an invalid directory"
51     exit 1
52   }
53 fi
54
55 if test -z "$VERBOSE"
56 then
57   VERBOSE=2
58   export VERBOSE
59 else
60   case "$VERBOSE" in
61   [0-9] ) : ;;
62   * )  VERBOSE=3 ;;
63   esac
64 fi
65
66 # Define what target system we're fixing.
67 #
68 if test -r ./Makefile; then
69   target_canonical="`sed -n -e 's,^target[      ]*=[    ]*\(.*\)$,\1,p' < Makefile`"
70 fi
71
72 # If not from the Makefile, then try config.guess
73 #
74 if test -z "${target_canonical}" ; then
75   if test -x ./config.guess ; then
76     target_canonical="`config.guess`" ; fi
77   test -z "${target_canonical}" && target_canonical=unknown
78 fi
79 export target_canonical
80
81 # # # # # # # # # # # # # # # # # # # # #
82 #
83 # Define PWDCMD as a command to use to get the working dir
84 # in the form that we want.
85 PWDCMD=${PWDCMD-pwd}
86
87 case "`$PWDCMD`" in
88 //*)
89     # On an Apollo, discard everything before `/usr'.
90     PWDCMD="eval pwd | sed -e 's,.*/usr/,/usr/,'"
91     ;;
92 esac
93
94 # Original directory.
95 ORIGDIR=`${PWDCMD}`
96 export ORIGDIR
97 FIXINCL=${ORIGDIR}/fixinc/fixincl
98 if [ ! -x $FIXINCL ] ; then
99   FIXINCL=${ORIGDIR}/fixincl
100   if [ ! -x $FIXINCL ] ; then
101     echo "Cannot find working fixincl" >&2
102     exit 1
103   fi
104 fi
105 export FIXINCL
106
107 # Make LIB absolute only if needed to avoid problems with the amd.
108 case $LIB in
109 /*)
110     ;;
111 *)
112     cd $LIB; LIB=`${PWDCMD}`
113     ;;
114 esac
115
116 if test $VERBOSE -gt 0
117 then echo Fixing headers into ${LIB} for ${target_canonical} target ; fi
118
119 # Determine whether this system has symbolic links.
120 if test -n "$DJDIR"; then
121   LINKS=false
122 elif ln -s X $LIB/ShouldNotExist 2>/dev/null; then
123   rm -f $LIB/ShouldNotExist
124   LINKS=true
125 elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
126   rm -f /tmp/ShouldNotExist
127   LINKS=true
128 else
129   LINKS=false
130 fi
131
132 # # # # # # # # # # # # # # # # # # # # #
133 #
134 #  Search each input directory for broken header files.
135 #  This loop ends near the end of the file.
136 #
137 if test $# -eq 0
138 then
139     INPUTLIST="/usr/include"
140 else
141     INPUTLIST="$@"
142 fi
143
144 for INPUT in ${INPUTLIST} ; do
145
146 cd ${ORIGDIR}
147
148 #  Make sure a directory exists before changing into it,
149 #  otherwise Solaris2 will fail-exit the script.
150 #
151 if [ ! -d ${INPUT} ]; then
152   continue
153 fi
154 cd ${INPUT}
155
156 INPUT=`${PWDCMD}`
157 export INPUT
158
159 #
160 # # # # # # # # # # # # # # # # # # # # #
161 #
162 if test $VERBOSE -gt 1
163 then echo Finding directories and links to directories ; fi
164
165 # Find all directories and all symlinks that point to directories.
166 # Put the list in $all_dirs.
167 # Each time we find a symlink, add it to newdirs
168 # so that we do another find within the dir the link points to.
169 # Note that $all_dirs may have duplicates in it;
170 # later parts of this file are supposed to ignore them.
171 dirs="."
172 levels=2
173 all_dirs=""
174 search_dirs=""
175
176 while [ -n "$dirs" ] && [ $levels -gt 0 ]
177 do
178   levels=`expr $levels - 1`
179   newdirs=
180   for d in $dirs
181   do
182     if test $VERBOSE -gt 1
183     then echo " Searching $INPUT/$d" ; fi
184
185     # Find all directories under $d, relative to $d, excluding $d itself.
186     # (The /. is needed after $d in case $d is a symlink.)
187     all_dirs="$all_dirs `find $d/. -type d -print | \
188                sed -e '/\/\.$/d' -e 's@/./@/@g'`"
189     # Find all links to directories.
190     # Using `-exec test -d' in find fails on some systems,
191     # and trying to run test via sh fails on others,
192     # so this is the simplest alternative left.
193     # First find all the links, then test each one.
194     theselinks=
195     $LINKS && \
196       theselinks=`find $d/. -type l -print | sed -e 's@/./@/@g'`
197     for d1 in $theselinks --dummy--
198     do
199       # If the link points to a directory,
200       # add that dir to $newdirs
201       if [ -d $d1 ]
202       then
203         all_dirs="$all_dirs $d1"
204         if [ "`ls -ld $d1 | sed -n 's/.*-> //p'`" != "." ]
205         then
206           newdirs="$newdirs $d1"
207           search_dirs="$search_dirs $d1"
208         fi
209       fi
210     done
211   done
212
213   dirs="$newdirs"
214 done
215
216 # # # # # # # # # # # # # # # # # # # # #
217 #
218 dirs=
219 if test $VERBOSE -gt 2
220 then echo "All directories (including links to directories):"
221      echo $all_dirs
222 fi
223
224 for file in $all_dirs; do
225   rm -rf $LIB/$file
226   if [ ! -d $LIB/$file ]
227   then mkdir $LIB/$file
228   fi
229 done
230 mkdir $LIB/root
231
232 # # # # # # # # # # # # # # # # # # # # #
233 #
234 # treetops gets an alternating list
235 # of old directories to copy
236 # and the new directories to copy to.
237 treetops=". ${LIB}"
238
239 if $LINKS; then
240   if test $VERBOSE -gt 1
241   then echo 'Making symbolic directory links' ; fi
242   cwd=`${PWDCMD}`
243
244   for sym_link in $search_dirs; do
245     cd ${INPUT}
246     dest=`ls -ld ${sym_link} | sed -n 's/.*-> //p'`
247
248     # In case $dest is relative, get to ${sym_link}'s dir first.
249     #
250     cd ./`echo ${sym_link} | sed 's;/[^/]*$;;'`
251
252     # Check that the target directory exists.
253     # Redirections changed to avoid bug in sh on Ultrix.
254     #
255     (cd $dest) > /dev/null 2>&1
256     if [ $? = 0 ]; then
257       cd $dest
258
259       # full_dest_dir gets the dir that the link actually leads to.
260       #
261       full_dest_dir=`${PWDCMD}`
262
263       # Canonicalize ${INPUT} now to minimize the time an
264       # automounter has to change the result of ${PWDCMD}.
265       #
266       cinput=`cd ${INPUT}; ${PWDCMD}`
267
268       # If a link points to ., make a similar link to .
269       #
270       if [ ${full_dest_dir} = ${cinput} ]; then
271         if test $VERBOSE -gt 2
272         then echo ${sym_link} '->' . ': Making self link' ; fi
273         rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
274         ln -s . ${LIB}/${sym_link} > /dev/null 2>&1
275
276       # If link leads back into ${INPUT},
277       # make a similar link here.
278       #
279       elif expr ${full_dest_dir} : "${cinput}/.*" > /dev/null; then
280         # Y gets the actual target dir name, relative to ${INPUT}.
281         y=`echo ${full_dest_dir} | sed -n "s&${cinput}/&&p"`
282         # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
283         dots=`echo "${sym_link}" |
284           sed -e 's@^./@@' -e 's@/./@/@g' -e 's@[^/][^/]*@..@g' -e 's@..$@@'`
285         if test $VERBOSE -gt 2
286         then echo ${sym_link} '->' $dots$y ': Making local link' ; fi
287         rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
288         ln -s $dots$y ${LIB}/${sym_link} > /dev/null 2>&1
289
290       else
291         # If the link is to a dir $target outside ${INPUT},
292         # repoint the link at ${INPUT}/root$target
293         # and process $target into ${INPUT}/root$target
294         # treat this directory as if it actually contained the files.
295         #
296         if test $VERBOSE -gt 2
297         then echo ${sym_link} '->' root${full_dest_dir} ': Making rooted link'
298         fi
299         if [ -d $LIB/root${full_dest_dir} ]
300         then true
301         else
302           dirname=root${full_dest_dir}/
303           dirmade=.
304           cd $LIB
305           while [ x$dirname != x ]; do
306             component=`echo $dirname | sed -e 's|/.*$||'`
307             mkdir $component >/dev/null 2>&1
308             cd $component
309             dirmade=$dirmade/$component
310             dirname=`echo $dirname | sed -e 's|[^/]*/||'`
311           done
312         fi
313
314         # Duplicate directory structure created in ${LIB}/${sym_link} in new
315         # root area.
316         #
317         for file2 in $all_dirs; do
318           case $file2 in
319             ${sym_link}/*)
320               dupdir=${LIB}/root${full_dest_dir}/`echo $file2 |
321                       sed -n "s|^${sym_link}/||p"`
322               if test $VERBOSE -gt 2
323               then echo "Duplicating ${sym_link}'s ${dupdir}" ; fi
324               if [ -d ${dupdir} ]
325               then true
326               else
327                 mkdir ${dupdir}
328               fi
329               ;;
330             *)
331               ;;
332           esac
333         done
334
335         # Get the path from ${LIB} to ${sym_link}, accounting for symlinks.
336         #
337         parent=`echo "${sym_link}" | sed -e 's@/[^/]*$@@'`
338         libabs=`cd ${LIB}; ${PWDCMD}`
339         file2=`cd ${LIB}; cd $parent; ${PWDCMD} | sed -e "s@^${libabs}@@"`
340
341         # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
342         #
343         dots=`echo "$file2" | sed -e 's@/[^/]*@../@g'`
344         rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
345         ln -s ${dots}root${full_dest_dir} ${LIB}/${sym_link} > /dev/null 2>&1
346         treetops="$treetops ${sym_link} ${LIB}/root${full_dest_dir}"
347       fi
348     fi
349   done
350 fi
351
352 # # # # # # # # # # # # # # # # # # # # #
353 #
354 required=
355 set x $treetops
356 shift
357 while [ $# != 0 ]; do
358   # $1 is an old directory to copy, and $2 is the new directory to copy to.
359   #
360   SRCDIR=`cd ${INPUT} ; cd $1 ; ${PWDCMD}`
361   export SRCDIR
362
363   FIND_BASE=$1
364   export FIND_BASE
365   shift
366
367   DESTDIR=`cd $1;${PWDCMD}`
368   export DESTDIR
369   shift
370
371   # The same dir can appear more than once in treetops.
372   # There's no need to scan it more than once.
373   #
374   if [ -f ${DESTDIR}/DONE ]
375   then continue ; fi
376
377   touch ${DESTDIR}/DONE
378   if test $VERBOSE -gt 1
379   then echo Fixing directory ${SRCDIR} into ${DESTDIR} ; fi
380
381   # Check files which are symlinks as well as those which are files.
382   #
383   cd ${INPUT}
384   required="$required `if $LINKS; then
385     find ${FIND_BASE}/. -name '*.h' \( -type f -o -type l \) -print
386   else
387     find ${FIND_BASE}/. -name '*.h' -type f -print
388   fi | \
389     sed -e 's;/\./;/;g' -e 's;//*;/;g' | \
390     ${FIXINCL}`"
391 done
392
393 ## Make sure that any include files referenced using double quotes
394 ## exist in the fixed directory.  This comes last since otherwise
395 ## we might end up deleting some of these files "because they don't
396 ## need any change."
397 set x `echo $required`
398 shift
399 while [ $# != 0 ]; do
400   newreq=
401   while [ $# != 0 ]; do
402     # $1 is the directory to copy from,
403     # $2 is the unfixed file,
404     # $3 is the fixed file name.
405     #
406     cd ${INPUT}
407     cd $1
408     if [ -f $2 ] ; then
409       if [ -r $2 ] && [ ! -r $3 ]; then
410         cp $2 $3 >/dev/null 2>&1 || echo "Can't copy $2" >&2
411         chmod +w $3 2>/dev/null
412         chmod a+r $3 2>/dev/null
413         if test $VERBOSE -gt 2
414         then echo Copied $2 ; fi
415         for include in `egrep '^[       ]*#[    ]*include[      ]*"[^/]' $3 |
416              sed -e 's/^[       ]*#[    ]*include[      ]*"\([^"]*\)".*$/\1/'`
417         do
418           dir=`echo $2 | sed -e s'|/[^/]*$||'`
419           dir2=`echo $3 | sed -e s'|/[^/]*$||'`
420           newreq="$newreq $1 $dir/$include $dir2/$include"
421         done
422       fi
423     fi
424     shift; shift; shift
425   done
426   set x $newreq
427   shift
428 done
429
430 if test $VERBOSE -gt 2
431 then echo 'Cleaning up DONE files.' ; fi
432 cd $LIB
433 # Look for files case-insensitively, for the benefit of
434 # DOS/Windows filesystems.
435 find . -name '[Dd][Oo][Nn][Ee]' -exec rm -f '{}' ';'
436
437 if test $VERBOSE -gt 1
438 then echo 'Cleaning up unneeded directories:' ; fi
439 cd $LIB
440 all_dirs=`find . -type d \! -name '.' -print | sort -r`
441 for file in $all_dirs; do
442   if rmdir $LIB/$file > /dev/null
443   then
444     test $VERBOSE -gt 3 && echo "  removed $file"
445   fi
446 done 2> /dev/null
447
448 # On systems which don't support symlinks, `find' may barf
449 # if called with "-type l" predicate.  So only use that if
450 # we know we should look for symlinks.
451 if $LINKS; then
452   test $VERBOSE -gt 2 && echo "Removing unused symlinks"
453
454   all_dirs=`find . -type l -print`
455   for file in $all_dirs
456   do
457     if test ! -d $file
458     then
459       rm -f $file
460       test $VERBOSE -gt 3 && echo "  removed $file"
461       rmdir `dirname $file` > /dev/null && \
462            test $VERBOSE -gt 3 && \
463            echo "  removed `dirname $file`"
464     fi
465   done 2> /dev/null
466 fi
467
468 if test $VERBOSE -gt 0
469 then echo fixincludes is done ; fi
470
471 # # # # # # # # # # # # # # # # # # # # #
472 #
473 # End of for INPUT directories
474 #
475 done
476 #
477 # # # # # # # # # # # # # # # # # # # # #