OSDN Git Service

* attribs.c (strip_attrs): Remove.
[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 GCC 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 # Usage: fixincl.sh output-dir input-dir
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=${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 if [ ! -x $FIXINCL ] ; then
101   FIXINCL=${ORIGDIR}/fixincl
102   if [ ! -x $FIXINCL ] ; then
103     echo "Cannot find working fixincl" >&2
104     exit 1
105   fi
106 fi
107 export FIXINCL
108
109 # Make LIB absolute only if needed to avoid problems with the amd.
110 case $LIB in
111 /*)
112     ;;
113 *)
114     cd $LIB; LIB=`${PWDCMD}`
115     ;;
116 esac
117
118 if test $VERBOSE -gt 0
119 then echo Fixing headers into ${LIB} for ${target_canonical} target ; fi
120
121 # Determine whether this system has symbolic links.
122 if test -n "$DJDIR"; then
123   LINKS=false
124 elif ln -s X $LIB/ShouldNotExist 2>/dev/null; then
125   rm -f $LIB/ShouldNotExist
126   LINKS=true
127 elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
128   rm -f /tmp/ShouldNotExist
129   LINKS=true
130 else
131   LINKS=false
132 fi
133
134 # # # # # # # # # # # # # # # # # # # # #
135 #
136 #  Extract from the gcc -dM and from the specs file all the predefined
137 #  macros that are not in the C89 reserved namespace (the reserved
138 #  namespace is all  identifiers beginnning with two underscores or one
139 #  underscore followed by a capital letter).  The specs file is in
140 #  ${ORIGDIR}, as is cc1.  A regular expression to find any of those
141 #  macros in a header file is written to MN_NAME_PAT.
142 #
143 #  Note dependency on ASCII. \012 = newline.
144 #  tr ' ' '\n' is, alas, not portable.
145
146 echo | ${ORIGDIR}/cc1 -quiet -dM -E - |
147   sed -n 's/^#define \([a-zA-Z][a-zA-Z0-9_]*\).*/\1/p' > mn.T
148 tr -s '\040\011' '\012\012' < ${ORIGDIR}/specs |
149   sed -n 's/^.*-D\([a-zA-Z_][a-zA-Z0-9_]*\).*/\1/p' >> mn.T
150
151 if sort -u mn.T | grep -v '^_[_A-Z]' > mn.U
152 then
153   if test $VERBOSE -gt 0
154   then echo "Forbidden identifiers: `tr '\012' ' ' <mn.U`" ; fi
155   sed 's/^/\\\\</; s/$/\\\\>/; $!s/$/|/' < mn.U | tr -d '\012' > mn.V
156   MN_NAME_PAT="`cat mn.V`"
157   export MN_NAME_PAT
158 else
159   if test $VERBOSE -gt 0
160   then echo "No forbidden identifiers defined by this target" ; fi
161 fi
162 rm -f mn.[TUV]
163
164 # # # # # # # # # # # # # # # # # # # # #
165 #
166 #  Search each input directory for broken header files.
167 #  This loop ends near the end of the file.
168 #
169 if test $# -eq 0
170 then
171     INPUTLIST="/usr/include"
172 else
173     INPUTLIST="$@"
174 fi
175
176 for INPUT in ${INPUTLIST} ; do
177
178 cd ${ORIGDIR}
179
180 #  Make sure a directory exists before changing into it,
181 #  otherwise Solaris2 will fail-exit the script.
182 #
183 if [ ! -d ${INPUT} ]; then
184   continue
185 fi
186 cd ${INPUT}
187
188 INPUT=`${PWDCMD}`
189 export INPUT
190
191 #
192 # # # # # # # # # # # # # # # # # # # # #
193 #
194 if test $VERBOSE -gt 1
195 then echo Finding directories and links to directories ; fi
196
197 # Find all directories and all symlinks that point to directories.
198 # Put the list in $all_dirs.
199 # Each time we find a symlink, add it to newdirs
200 # so that we do another find within the dir the link points to.
201 # Note that $all_dirs may have duplicates in it;
202 # later parts of this file are supposed to ignore them.
203 dirs="."
204 levels=2
205 all_dirs=""
206 search_dirs=""
207
208 while [ -n "$dirs" ] && [ $levels -gt 0 ]
209 do
210   levels=`expr $levels - 1`
211   newdirs=
212   for d in $dirs
213   do
214     if test $VERBOSE -gt 1
215     then echo " Searching $INPUT/$d" ; fi
216
217     # Find all directories under $d, relative to $d, excluding $d itself.
218     # (The /. is needed after $d in case $d is a symlink.)
219     all_dirs="$all_dirs `find $d/. -type d -print | \
220                sed -e '/\/\.$/d' -e 's@/./@/@g'`"
221     # Find all links to directories.
222     # Using `-exec test -d' in find fails on some systems,
223     # and trying to run test via sh fails on others,
224     # so this is the simplest alternative left.
225     # First find all the links, then test each one.
226     theselinks=
227     $LINKS && \
228       theselinks=`find $d/. -type l -print | sed -e 's@/./@/@g'`
229     for d1 in $theselinks --dummy--
230     do
231       # If the link points to a directory,
232       # add that dir to $newdirs
233       if [ -d $d1 ]
234       then
235         all_dirs="$all_dirs $d1"
236         if [ "`ls -ld $d1 | sed -n 's/.*-> //p'`" != "." ]
237         then
238           newdirs="$newdirs $d1"
239           search_dirs="$search_dirs $d1"
240         fi
241       fi
242     done
243   done
244
245   dirs="$newdirs"
246 done
247
248 # # # # # # # # # # # # # # # # # # # # #
249 #
250 dirs=
251 if test $VERBOSE -gt 2
252 then echo "All directories (including links to directories):"
253      echo $all_dirs
254 fi
255
256 for file in $all_dirs; do
257   rm -rf $LIB/$file
258   if [ ! -d $LIB/$file ]
259   then mkdir $LIB/$file
260   fi
261 done
262 mkdir $LIB/root
263
264 # # # # # # # # # # # # # # # # # # # # #
265 #
266 # treetops gets an alternating list
267 # of old directories to copy
268 # and the new directories to copy to.
269 treetops=". ${LIB}"
270
271 if $LINKS; then
272   if test $VERBOSE -gt 1
273   then echo 'Making symbolic directory links' ; fi
274   cwd=`${PWDCMD}`
275
276   for sym_link in $search_dirs; do
277     cd ${INPUT}
278     dest=`ls -ld ${sym_link} | sed -n 's/.*-> //p'`
279
280     # In case $dest is relative, get to ${sym_link}'s dir first.
281     #
282     cd ./`echo ${sym_link} | sed 's;/[^/]*$;;'`
283
284     # Check that the target directory exists.
285     # Redirections changed to avoid bug in sh on Ultrix.
286     #
287     (cd $dest) > /dev/null 2>&1
288     if [ $? = 0 ]; then
289       cd $dest
290
291       # full_dest_dir gets the dir that the link actually leads to.
292       #
293       full_dest_dir=`${PWDCMD}`
294
295       # Canonicalize ${INPUT} now to minimize the time an
296       # automounter has to change the result of ${PWDCMD}.
297       #
298       cinput=`cd ${INPUT}; ${PWDCMD}`
299
300       # If a link points to ., make a similar link to .
301       #
302       if [ ${full_dest_dir} = ${cinput} ]; then
303         if test $VERBOSE -gt 2
304         then echo ${sym_link} '->' . ': Making self link' ; fi
305         rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
306         ln -s . ${LIB}/${sym_link} > /dev/null 2>&1
307
308       # If link leads back into ${INPUT},
309       # make a similar link here.
310       #
311       elif expr ${full_dest_dir} : "${cinput}/.*" > /dev/null; then
312         # Y gets the actual target dir name, relative to ${INPUT}.
313         y=`echo ${full_dest_dir} | sed -n "s&${cinput}/&&p"`
314         # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
315         dots=`echo "${sym_link}" |
316           sed -e 's@^./@@' -e 's@/./@/@g' -e 's@[^/][^/]*@..@g' -e 's@..$@@'`
317         if test $VERBOSE -gt 2
318         then echo ${sym_link} '->' $dots$y ': Making local link' ; fi
319         rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
320         ln -s $dots$y ${LIB}/${sym_link} > /dev/null 2>&1
321
322       else
323         # If the link is to a dir $target outside ${INPUT},
324         # repoint the link at ${INPUT}/root$target
325         # and process $target into ${INPUT}/root$target
326         # treat this directory as if it actually contained the files.
327         #
328         if test $VERBOSE -gt 2
329         then echo ${sym_link} '->' root${full_dest_dir} ': Making rooted link'
330         fi
331         if [ -d $LIB/root${full_dest_dir} ]
332         then true
333         else
334           dirname=root${full_dest_dir}/
335           dirmade=.
336           cd $LIB
337           while [ x$dirname != x ]; do
338             component=`echo $dirname | sed -e 's|/.*$||'`
339             mkdir $component >/dev/null 2>&1
340             cd $component
341             dirmade=$dirmade/$component
342             dirname=`echo $dirname | sed -e 's|[^/]*/||'`
343           done
344         fi
345
346         # Duplicate directory structure created in ${LIB}/${sym_link} in new
347         # root area.
348         #
349         for file2 in $all_dirs; do
350           case $file2 in
351             ${sym_link}/*)
352               dupdir=${LIB}/root${full_dest_dir}/`echo $file2 |
353                       sed -n "s|^${sym_link}/||p"`
354               if test $VERBOSE -gt 2
355               then echo "Duplicating ${sym_link}'s ${dupdir}" ; fi
356               if [ -d ${dupdir} ]
357               then true
358               else
359                 mkdir ${dupdir}
360               fi
361               ;;
362             *)
363               ;;
364           esac
365         done
366
367         # Get the path from ${LIB} to ${sym_link}, accounting for symlinks.
368         #
369         parent=`echo "${sym_link}" | sed -e 's@/[^/]*$@@'`
370         libabs=`cd ${LIB}; ${PWDCMD}`
371         file2=`cd ${LIB}; cd $parent; ${PWDCMD} | sed -e "s@^${libabs}@@"`
372
373         # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
374         #
375         dots=`echo "$file2" | sed -e 's@/[^/]*@../@g'`
376         rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
377         ln -s ${dots}root${full_dest_dir} ${LIB}/${sym_link} > /dev/null 2>&1
378         treetops="$treetops ${sym_link} ${LIB}/root${full_dest_dir}"
379       fi
380     fi
381   done
382 fi
383
384 # # # # # # # # # # # # # # # # # # # # #
385 #
386 required=
387 set x $treetops
388 shift
389 while [ $# != 0 ]; do
390   # $1 is an old directory to copy, and $2 is the new directory to copy to.
391   #
392   SRCDIR=`cd ${INPUT} ; cd $1 ; ${PWDCMD}`
393   export SRCDIR
394
395   FIND_BASE=$1
396   export FIND_BASE
397   shift
398
399   DESTDIR=`cd $1;${PWDCMD}`
400   export DESTDIR
401   shift
402
403   # The same dir can appear more than once in treetops.
404   # There's no need to scan it more than once.
405   #
406   if [ -f ${DESTDIR}/DONE ]
407   then continue ; fi
408
409   touch ${DESTDIR}/DONE
410   if test $VERBOSE -gt 1
411   then echo Fixing directory ${SRCDIR} into ${DESTDIR} ; fi
412
413   # Check files which are symlinks as well as those which are files.
414   #
415   cd ${INPUT}
416   required="$required `if $LINKS; then
417     find ${FIND_BASE}/. -name '*.h' \( -type f -o -type l \) -print
418   else
419     find ${FIND_BASE}/. -name '*.h' -type f -print
420   fi | \
421     sed -e 's;/\./;/;g' -e 's;//*;/;g' | \
422     ${FIXINCL}`"
423 done
424
425 ## Make sure that any include files referenced using double quotes
426 ## exist in the fixed directory.  This comes last since otherwise
427 ## we might end up deleting some of these files "because they don't
428 ## need any change."
429 set x `echo $required`
430 shift
431 while [ $# != 0 ]; do
432   newreq=
433   while [ $# != 0 ]; do
434     # $1 is the directory to copy from,
435     # $2 is the unfixed file,
436     # $3 is the fixed file name.
437     #
438     cd ${INPUT}
439     cd $1
440     if [ -f $2 ] ; then
441       if [ -r $2 ] && [ ! -r $3 ]; then
442         cp $2 $3 >/dev/null 2>&1 || echo "Can't copy $2" >&2
443         chmod +w $3 2>/dev/null
444         chmod a+r $3 2>/dev/null
445         if test $VERBOSE -gt 2
446         then echo Copied $2 ; fi
447         for include in `egrep '^[       ]*#[    ]*include[      ]*"[^/]' $3 |
448              sed -e 's/^[       ]*#[    ]*include[      ]*"\([^"]*\)".*$/\1/'`
449         do
450           dir=`echo $2 | sed -e s'|/[^/]*$||'`
451           dir2=`echo $3 | sed -e s'|/[^/]*$||'`
452           newreq="$newreq $1 $dir/$include $dir2/$include"
453         done
454       fi
455     fi
456     shift; shift; shift
457   done
458   set x $newreq
459   shift
460 done
461
462 if test $VERBOSE -gt 2
463 then echo 'Cleaning up DONE files.' ; fi
464 cd $LIB
465 # Look for files case-insensitively, for the benefit of
466 # DOS/Windows filesystems.
467 find . -name '[Dd][Oo][Nn][Ee]' -exec rm -f '{}' ';'
468
469 if test $VERBOSE -gt 1
470 then echo 'Cleaning up unneeded directories:' ; fi
471 cd $LIB
472 all_dirs=`find . -type d \! -name '.' -print | sort -r`
473 for file in $all_dirs; do
474   if rmdir $LIB/$file > /dev/null
475   then
476     test $VERBOSE -gt 3 && echo "  removed $file"
477   fi
478 done 2> /dev/null
479
480 # On systems which don't support symlinks, `find' may barf
481 # if called with "-type l" predicate.  So only use that if
482 # we know we should look for symlinks.
483 if $LINKS; then
484   test $VERBOSE -gt 2 && echo "Removing unused symlinks"
485
486   all_dirs=`find . -type l -print`
487   for file in $all_dirs
488   do
489     if test ! -d $file
490     then
491       rm -f $file
492       test $VERBOSE -gt 3 && echo "  removed $file"
493       rmdir `dirname $file` > /dev/null && \
494            test $VERBOSE -gt 3 && \
495            echo "  removed `dirname $file`"
496     fi
497   done 2> /dev/null
498 fi
499
500 if test $VERBOSE -gt 0
501 then echo fixincludes is done ; fi
502
503 # # # # # # # # # # # # # # # # # # # # #
504 #
505 # End of for INPUT directories
506 #
507 done
508 #
509 # # # # # # # # # # # # # # # # # # # # #