OSDN Git Service

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