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