fixinc.in 13.6 KB
Newer Older
Bruce Korb committed
1 2 3 4
#!/bin/sh
#
# Install modified versions of certain ANSI-incompatible system header
# files which are fixed to work correctly with ANSI C and placed in a
5
# directory that GCC will search.
Bruce Korb committed
6 7 8
#
# See README-fixinc for more information.
#
9
#  fixincludes copyright (c) 1998, 1999, 2000, 2002, 2009
10
#  The Free Software Foundation, Inc.
Bruce Korb committed
11
#
Bruce Korb committed
12 13 14 15
# fixincludes is free software.
# 
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
16
# Foundation; either version 3, or (at your option) any later version.
Bruce Korb committed
17 18 19 20 21 22 23
# 
# fixincludes is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
24 25
# along with fixincludes; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.
Bruce Korb committed
26 27
#
# # # # # # # # # # # # # # # # # # # # #
28

29
# Usage: fixinc.sh output-dir input-dir
Bruce Korb committed
30 31 32 33
#
# Directory in which to store the results.
# Fail if no arg to specify a directory for the output.
if [ "x$1" = "x" ]
Bruce Korb committed
34 35 36
then
  echo fixincludes: no output directory specified
  exit 1
Bruce Korb committed
37 38 39 40 41 42 43 44 45 46 47 48
fi

LIB=${1}
shift

# Make sure it exists.
if [ ! -d $LIB ]; then
  mkdir $LIB || {
    echo fixincludes:  output dir '`'$LIB"' cannot be created"
    exit 1
  }
else
Bruce Korb committed
49
  ( cd $LIB && touch DONE && rm DONE ) || {
Bruce Korb committed
50 51 52 53 54
    echo fixincludes:  output dir '`'$LIB"' is an invalid directory"
    exit 1
  }
fi

Bruce Korb committed
55 56 57 58 59 60 61 62 63 64 65
if test -z "$VERBOSE"
then
  VERBOSE=2
  export VERBOSE
else
  case "$VERBOSE" in
  [0-9] ) : ;;
  * )  VERBOSE=3 ;;
  esac
fi

Bruce Korb committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
# Define what target system we're fixing.
#
if test -r ./Makefile; then
  target_canonical="`sed -n -e 's,^target[ 	]*=[ 	]*\(.*\)$,\1,p' < Makefile`"
fi

# If not from the Makefile, then try config.guess
#
if test -z "${target_canonical}" ; then
  if test -x ./config.guess ; then
    target_canonical="`config.guess`" ; fi
  test -z "${target_canonical}" && target_canonical=unknown
fi
export target_canonical

# # # # # # # # # # # # # # # # # # # # #
#
# Define PWDCMD as a command to use to get the working dir
# in the form that we want.
85
PWDCMD=${PWDCMD-pwd}
Bruce Korb committed
86 87 88 89 90 91 92 93 94 95

case "`$PWDCMD`" in
//*)
    # On an Apollo, discard everything before `/usr'.
    PWDCMD="eval pwd | sed -e 's,.*/usr/,/usr/,'"
    ;;
esac

# Original directory.
ORIGDIR=`${PWDCMD}`
96
export ORIGDIR
Geoffrey Keating committed
97
FIXINCL=`${PWDCMD}`/fixincl
98
if [ ! -x $FIXINCL ] ; then
Geoffrey Keating committed
99
  echo "Cannot find fixincl" >&2
100
  exit 1
101
fi
Bruce Korb committed
102 103 104 105 106 107 108 109 110 111 112
export FIXINCL

# Make LIB absolute only if needed to avoid problems with the amd.
case $LIB in
/*)
    ;;
*)
    cd $LIB; LIB=`${PWDCMD}`
    ;;
esac

Bruce Korb committed
113 114
if test $VERBOSE -gt 0
then echo Fixing headers into ${LIB} for ${target_canonical} target ; fi
Bruce Korb committed
115

Bruce Korb committed
116
# Determine whether this system has symbolic links.
117 118 119
if test -n "$DJDIR"; then
  LINKS=false
elif ln -s X $LIB/ShouldNotExist 2>/dev/null; then
Bruce Korb committed
120 121 122 123 124 125 126 127
  rm -f $LIB/ShouldNotExist
  LINKS=true
elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
  rm -f /tmp/ShouldNotExist
  LINKS=true
else
  LINKS=false
fi
Bruce Korb committed
128

Bruce Korb committed
129 130
# # # # # # # # # # # # # # # # # # # # #
#
Robert Mason committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#  Check to see if the machine_name fix needs to be disabled.
#
#  On some platforms, machine_name doesn't work properly and
#  breaks some of the header files.  Since everything works
#  properly without it, just wipe the macro list to
#  disable the fix.

case "${target_canonical}" in
    *-*-vxworks*)
	test -f ${MACRO_LIST} &&  echo > ${MACRO_LIST}
        ;;
esac


# # # # # # # # # # # # # # # # # # # # #
#
Geoffrey Keating committed
147
#  In the file macro_list are listed all the predefined
148
#  macros that are not in the C89 reserved namespace (the reserved
Geoffrey Keating committed
149 150 151
#  namespace is all identifiers beginnning with two underscores or one
#  underscore followed by a capital letter).  A regular expression to find
#  any of those macros in a header file is written to MN_NAME_PAT.
152 153 154 155
#
#  Note dependency on ASCII. \012 = newline.
#  tr ' ' '\n' is, alas, not portable.

Geoffrey Keating committed
156
if test -s ${MACRO_LIST}
157
then
Geoffrey Keating committed
158 159 160 161 162
  if test $VERBOSE -gt 0; then
    echo "Forbidden identifiers: `tr '\012' ' ' < ${MACRO_LIST}`"
  fi
  MN_NAME_PAT="`sed 's/^/\\\\</; s/$/\\\\>/; $!s/$/|/' \
      < ${MACRO_LIST} | tr -d '\012'`"
163 164 165 166 167 168 169 170
  export MN_NAME_PAT
else
  if test $VERBOSE -gt 0
  then echo "No forbidden identifiers defined by this target" ; fi
fi

# # # # # # # # # # # # # # # # # # # # #
#
Bruce Korb committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184
#  Search each input directory for broken header files.
#  This loop ends near the end of the file.
#
if test $# -eq 0
then
    INPUTLIST="/usr/include"
else
    INPUTLIST="$@"
fi

for INPUT in ${INPUTLIST} ; do

cd ${ORIGDIR}

185 186 187 188 189 190 191 192
#  Make sure a directory exists before changing into it,
#  otherwise Solaris2 will fail-exit the script.
#
if [ ! -d ${INPUT} ]; then
  continue
fi
cd ${INPUT}

Bruce Korb committed
193
INPUT=`${PWDCMD}`
194
export INPUT
Bruce Korb committed
195 196 197 198

#
# # # # # # # # # # # # # # # # # # # # #
#
Bruce Korb committed
199 200
if test $VERBOSE -gt 1
then echo Finding directories and links to directories ; fi
Bruce Korb committed
201 202

# Find all directories and all symlinks that point to directories.
Bruce Korb committed
203
# Put the list in $all_dirs.
Bruce Korb committed
204 205
# Each time we find a symlink, add it to newdirs
# so that we do another find within the dir the link points to.
Bruce Korb committed
206
# Note that $all_dirs may have duplicates in it;
Bruce Korb committed
207 208 209
# later parts of this file are supposed to ignore them.
dirs="."
levels=2
Bruce Korb committed
210 211 212
all_dirs=""
search_dirs=""

Bruce Korb committed
213 214
while [ -n "$dirs" ] && [ $levels -gt 0 ]
do
Bruce Korb committed
215 216 217 218
  levels=`expr $levels - 1`
  newdirs=
  for d in $dirs
  do
Bruce Korb committed
219 220
    if test $VERBOSE -gt 1
    then echo " Searching $INPUT/$d" ; fi
Bruce Korb committed
221

Bruce Korb committed
222 223
    # Find all directories under $d, relative to $d, excluding $d itself.
    # (The /. is needed after $d in case $d is a symlink.)
Bruce Korb committed
224
    all_dirs="$all_dirs `find $d/. -type d -print | \
Bruce Korb committed
225 226 227 228 229 230 231 232 233 234 235
               sed -e '/\/\.$/d' -e 's@/./@/@g'`"
    # Find all links to directories.
    # Using `-exec test -d' in find fails on some systems,
    # and trying to run test via sh fails on others,
    # so this is the simplest alternative left.
    # First find all the links, then test each one.
    theselinks=
    $LINKS && \
      theselinks=`find $d/. -type l -print | sed -e 's@/./@/@g'`
    for d1 in $theselinks --dummy--
    do
Bruce Korb committed
236 237 238 239 240
      # If the link points to a directory,
      # add that dir to $newdirs
      if [ -d $d1 ]
      then
        all_dirs="$all_dirs $d1"
Bruce Korb committed
241 242
        if [ "`ls -ld $d1 | sed -n 's/.*-> //p'`" != "." ]
        then
Bruce Korb committed
243 244
          newdirs="$newdirs $d1"
          search_dirs="$search_dirs $d1"
Bruce Korb committed
245
        fi
Bruce Korb committed
246
      fi
Bruce Korb committed
247
    done
Bruce Korb committed
248 249 250
  done

  dirs="$newdirs"
Bruce Korb committed
251
done
Bruce Korb committed
252

Bruce Korb committed
253 254 255
# # # # # # # # # # # # # # # # # # # # #
#
dirs=
Bruce Korb committed
256 257 258 259
if test $VERBOSE -gt 2
then echo "All directories (including links to directories):"
     echo $all_dirs
fi
Bruce Korb committed
260 261

for file in $all_dirs; do
Bruce Korb committed
262 263 264 265 266 267
  rm -rf $LIB/$file
  if [ ! -d $LIB/$file ]
  then mkdir $LIB/$file
  fi
done
mkdir $LIB/root
Bruce Korb committed
268

Bruce Korb committed
269 270 271 272 273
# # # # # # # # # # # # # # # # # # # # #
#
# treetops gets an alternating list
# of old directories to copy
# and the new directories to copy to.
Bruce Korb committed
274 275
treetops=". ${LIB}"

Bruce Korb committed
276
if $LINKS; then
Bruce Korb committed
277 278
  if test $VERBOSE -gt 1
  then echo 'Making symbolic directory links' ; fi
Bruce Korb committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  cwd=`${PWDCMD}`

  for sym_link in $search_dirs; do
    cd ${INPUT}
    dest=`ls -ld ${sym_link} | sed -n 's/.*-> //p'`

    # In case $dest is relative, get to ${sym_link}'s dir first.
    #
    cd ./`echo ${sym_link} | sed 's;/[^/]*$;;'`

    # Check that the target directory exists.
    # Redirections changed to avoid bug in sh on Ultrix.
    #
    (cd $dest) > /dev/null 2>&1
    if [ $? = 0 ]; then
      cd $dest

      # full_dest_dir gets the dir that the link actually leads to.
      #
      full_dest_dir=`${PWDCMD}`

      # Canonicalize ${INPUT} now to minimize the time an
      # automounter has to change the result of ${PWDCMD}.
      #
      cinput=`cd ${INPUT}; ${PWDCMD}`

      # If a link points to ., make a similar link to .
      #
      if [ ${full_dest_dir} = ${cinput} ]; then
Bruce Korb committed
308 309
        if test $VERBOSE -gt 2
        then echo ${sym_link} '->' . ': Making self link' ; fi
Bruce Korb committed
310 311 312 313 314 315 316 317 318 319 320 321
        rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
        ln -s . ${LIB}/${sym_link} > /dev/null 2>&1

      # If link leads back into ${INPUT},
      # make a similar link here.
      #
      elif expr ${full_dest_dir} : "${cinput}/.*" > /dev/null; then
        # Y gets the actual target dir name, relative to ${INPUT}.
        y=`echo ${full_dest_dir} | sed -n "s&${cinput}/&&p"`
        # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
        dots=`echo "${sym_link}" |
          sed -e 's@^./@@' -e 's@/./@/@g' -e 's@[^/][^/]*@..@g' -e 's@..$@@'`
Bruce Korb committed
322 323
        if test $VERBOSE -gt 2
        then echo ${sym_link} '->' $dots$y ': Making local link' ; fi
Bruce Korb committed
324 325 326
        rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
        ln -s $dots$y ${LIB}/${sym_link} > /dev/null 2>&1

Bruce Korb committed
327
      else
Bruce Korb committed
328 329 330 331 332
        # If the link is to a dir $target outside ${INPUT},
        # repoint the link at ${INPUT}/root$target
        # and process $target into ${INPUT}/root$target
        # treat this directory as if it actually contained the files.
        #
Bruce Korb committed
333 334 335
        if test $VERBOSE -gt 2
        then echo ${sym_link} '->' root${full_dest_dir} ': Making rooted link'
        fi
Bruce Korb committed
336
        if [ -d $LIB/root${full_dest_dir} ]
Bruce Korb committed
337 338
        then true
        else
Bruce Korb committed
339 340 341 342 343 344 345 346 347
          dirname=root${full_dest_dir}/
          dirmade=.
          cd $LIB
          while [ x$dirname != x ]; do
            component=`echo $dirname | sed -e 's|/.*$||'`
            mkdir $component >/dev/null 2>&1
            cd $component
            dirmade=$dirmade/$component
            dirname=`echo $dirname | sed -e 's|[^/]*/||'`
Bruce Korb committed
348
          done
Bruce Korb committed
349 350 351 352 353 354 355 356 357 358
        fi

        # Duplicate directory structure created in ${LIB}/${sym_link} in new
        # root area.
        #
        for file2 in $all_dirs; do
          case $file2 in
            ${sym_link}/*)
              dupdir=${LIB}/root${full_dest_dir}/`echo $file2 |
                      sed -n "s|^${sym_link}/||p"`
Bruce Korb committed
359 360
              if test $VERBOSE -gt 2
              then echo "Duplicating ${sym_link}'s ${dupdir}" ; fi
Bruce Korb committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
              if [ -d ${dupdir} ]
              then true
              else
                mkdir ${dupdir}
              fi
              ;;
            *)
              ;;
          esac
        done

        # Get the path from ${LIB} to ${sym_link}, accounting for symlinks.
        #
        parent=`echo "${sym_link}" | sed -e 's@/[^/]*$@@'`
        libabs=`cd ${LIB}; ${PWDCMD}`
        file2=`cd ${LIB}; cd $parent; ${PWDCMD} | sed -e "s@^${libabs}@@"`

        # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}.
        #
        dots=`echo "$file2" | sed -e 's@/[^/]*@../@g'`
        rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
        ln -s ${dots}root${full_dest_dir} ${LIB}/${sym_link} > /dev/null 2>&1
        treetops="$treetops ${sym_link} ${LIB}/root${full_dest_dir}"
Bruce Korb committed
384 385 386 387
      fi
    fi
  done
fi
Bruce Korb committed
388

Bruce Korb committed
389 390 391 392 393 394 395 396 397 398
# # # # # # # # # # # # # # # # # # # # #
#
required=
set x $treetops
shift
while [ $# != 0 ]; do
  # $1 is an old directory to copy, and $2 is the new directory to copy to.
  #
  SRCDIR=`cd ${INPUT} ; cd $1 ; ${PWDCMD}`
  export SRCDIR
Bruce Korb committed
399 400 401

  FIND_BASE=$1
  export FIND_BASE
Bruce Korb committed
402 403 404 405 406 407 408 409 410 411 412 413 414
  shift

  DESTDIR=`cd $1;${PWDCMD}`
  export DESTDIR
  shift

  # The same dir can appear more than once in treetops.
  # There's no need to scan it more than once.
  #
  if [ -f ${DESTDIR}/DONE ]
  then continue ; fi

  touch ${DESTDIR}/DONE
Bruce Korb committed
415 416
  if test $VERBOSE -gt 1
  then echo Fixing directory ${SRCDIR} into ${DESTDIR} ; fi
Bruce Korb committed
417

Bruce Korb committed
418
  # Check files which are symlinks as well as those which are files.
Bruce Korb committed
419
  #
Bruce Korb committed
420
  cd ${INPUT}
Bruce Korb committed
421
  required="$required `if $LINKS; then
Bruce Korb committed
422
    find ${FIND_BASE}/. -name '*.h' \( -type f -o -type l \) -print
Bruce Korb committed
423
  else
Bruce Korb committed
424
    find ${FIND_BASE}/. -name '*.h' -type f -print
Bruce Korb committed
425
  fi | \
426
    sed -e 's;/\./;/;g' -e 's;//*;/;g' | \
Bruce Korb committed
427
    ${FIXINCL}`"
Bruce Korb committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
done

## Make sure that any include files referenced using double quotes
## exist in the fixed directory.  This comes last since otherwise
## we might end up deleting some of these files "because they don't
## need any change."
set x `echo $required`
shift
while [ $# != 0 ]; do
  newreq=
  while [ $# != 0 ]; do
    # $1 is the directory to copy from,
    # $2 is the unfixed file,
    # $3 is the fixed file name.
    #
    cd ${INPUT}
    cd $1
Bruce Korb committed
445 446 447 448 449
    if [ -f $2 ] ; then
      if [ -r $2 ] && [ ! -r $3 ]; then
        cp $2 $3 >/dev/null 2>&1 || echo "Can't copy $2" >&2
        chmod +w $3 2>/dev/null
        chmod a+r $3 2>/dev/null
Bruce Korb committed
450 451
        if test $VERBOSE -gt 2
        then echo Copied $2 ; fi
Bruce Korb committed
452
        for include in `egrep '^[ 	]*#[ 	]*include[ 	]*"[^/]' $3 |
Bruce Korb committed
453
             sed -e 's/^[ 	]*#[ 	]*include[ 	]*"\([^"]*\)".*$/\1/'`
Bruce Korb committed
454 455 456 457 458 459
        do
	  dir=`echo $2 | sed -e s'|/[^/]*$||'`
	  dir2=`echo $3 | sed -e s'|/[^/]*$||'`
	  newreq="$newreq $1 $dir/$include $dir2/$include"
        done
      fi
Bruce Korb committed
460 461 462 463 464 465 466
    fi
    shift; shift; shift
  done
  set x $newreq
  shift
done

Bruce Korb committed
467 468
if test $VERBOSE -gt 2
then echo 'Cleaning up DONE files.' ; fi
Bruce Korb committed
469
cd $LIB
470 471 472
# Look for files case-insensitively, for the benefit of
# DOS/Windows filesystems.
find . -name '[Dd][Oo][Nn][Ee]' -exec rm -f '{}' ';'
Bruce Korb committed
473

Bruce Korb committed
474 475
if test $VERBOSE -gt 1
then echo 'Cleaning up unneeded directories:' ; fi
Bruce Korb committed
476
cd $LIB
477
all_dirs=`find . -type d \! -name '.' -print | sort -r`
Bruce Korb committed
478
for file in $all_dirs; do
479 480 481 482 483 484
  if rmdir $LIB/$file > /dev/null
  then
    test $VERBOSE -gt 3 && echo "  removed $file"
  fi
done 2> /dev/null

485 486 487 488 489
# On systems which don't support symlinks, `find' may barf
# if called with "-type l" predicate.  So only use that if
# we know we should look for symlinks.
if $LINKS; then
  test $VERBOSE -gt 2 && echo "Removing unused symlinks"
490

491 492 493
  all_dirs=`find . -type l -print`
  for file in $all_dirs
  do
494 495 496 497 498
    if test ! -d $file
    then
      rm -f $file
      test $VERBOSE -gt 3 && echo "  removed $file"
      rmdir `dirname $file` > /dev/null && \
499 500 501 502 503
           test $VERBOSE -gt 3 && \
           echo "  removed `dirname $file`"
    fi
  done 2> /dev/null
fi
Bruce Korb committed
504

Bruce Korb committed
505 506 507
if test $VERBOSE -gt 0
then echo fixincludes is done ; fi

Bruce Korb committed
508 509 510 511 512 513 514
# # # # # # # # # # # # # # # # # # # # #
#
# End of for INPUT directories
#
done
#
# # # # # # # # # # # # # # # # # # # # #