OSDN Git Service

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