aboutsummaryrefslogtreecommitdiff
path: root/gcc/fixinc/inclhack.tpl
blob: 06b49aeabe95d87c7d1f100e5720ca2c8f6a6344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
[= autogen template -*- Mode: ksh -*-
sh
#
#
=]
#!/bin/sh
#
[= _EVAL "## " _DNE =]
#
# Install modified versions of certain ANSI-incompatible system header
# files which are fixed to work correctly with ANSI C and placed in a
# directory that GNU C will search.
#
# This script contains [=_eval fix _count =] fixup scripts.
#
# See README-fixinc for more information.
#
[=_eval fixincludes "## " _gpl=]
#
# # # # # # # # # # # # # # # # # # # # #
#
# Directory in which to store the results.
# Fail if no arg to specify a directory for the output.
if [ "x$1" = "x" ]
then echo fixincludes: no output directory specified
exit 1
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
  ( \cd $LIB && touch DONE && rm DONE ) || {
    echo fixincludes:  output dir '`'$LIB"' is an invalid directory"
    exit 1
  }
fi

# 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.
PWDCMD=pwd

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

# Original directory.
ORIGDIR=`${PWDCMD}`[=
_IF PROGRAM _env =]
FIXINCL=${ORIGDIR}/fixincl
export FIXINCL[=
_ENDIF=]

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

echo Fixing headers into ${LIB} for ${target_canonical} target
 
# Determine whether this system has symbolic links.
if ln -s X $LIB/ShouldNotExist 2>/dev/null; then
  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
 
# # # # # # # # # # # # # # # # # # # # #
#
#  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}

cd ${INPUT} || continue

#
# # # # # # # # # # # # # # # # # # # # #
#
echo Finding directories and links to directories

# Find all directories and all symlinks that point to directories.
# Put the list in $files.
# Each time we find a symlink, add it to newdirs
# so that we do another find within the dir the link points to.
# Note that $files may have duplicates in it;
# later parts of this file are supposed to ignore them.
dirs="."
levels=2
while [ -n "$dirs" ] && [ $levels -gt 0 ]
do
    levels=`expr $levels - 1`
    newdirs=
    for d in $dirs
    do
    echo " Searching $INPUT/$d"
 
    # Find all directories under $d, relative to $d, excluding $d itself.
    # (The /. is needed after $d in case $d is a symlink.)
    files="$files `find $d/. -type d -print | \
               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
        # If the link points to a directory,
        # add that dir to $newdirs
        if [ -d $d1 ]
        then
        files="$files $d1"
        if [ "`ls -ld $d1 | sed -n 's/.*-> //p'`" != "." ]
        then
            newdirs="$newdirs $d1"
        fi
        fi
    done
    done
 
    dirs="$newdirs"
done
 
# # # # # # # # # # # # # # # # # # # # #
#
dirs=
echo "All directories (including links to directories):"
echo $files
 
for file in $files; do
  rm -rf $LIB/$file
  if [ ! -d $LIB/$file ]
  then mkdir $LIB/$file
  fi
done
mkdir $LIB/root
 
# # # # # # # # # # # # # # # # # # # # #
#
# treetops gets an alternating list
# of old directories to copy
# and the new directories to copy to.
treetops="${INPUT} ${LIB}"
 
if $LINKS; then
  echo 'Making symbolic directory links'
  for file in $files; do
    dest=`ls -ld $file | sed -n 's/.*-> //p'`
    if [ "$dest" ]; then
      cwd=`${PWDCMD}`
      # In case $dest is relative, get to $file's dir first.
      cd ${INPUT}
      cd `echo ./$file | sed -n 's&[^/]*$&&p'`
      # 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
    # X gets the dir that the link actually leads to.
    x=`${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 [ $x = ${cinput} ]; then
      echo $file '->' . ': Making link'
      rm -fr ${LIB}/$file > /dev/null 2>&1
      ln -s . ${LIB}/$file > /dev/null 2>&1
    # If link leads back into ${INPUT},
    # make a similar link here.
    elif expr $x : "${cinput}/.*" > /dev/null; then
      # Y gets the actual target dir name, relative to ${INPUT}.
      y=`echo $x | sed -n "s&${cinput}/&&p"`
      # DOTS is the relative path from ${LIB}/$file's dir back to ${LIB}.
      dots=`echo "$file" |
        sed -e 's@^./@@' -e 's@/./@/@g' -e 's@[^/][^/]*@..@g' -e 's@..$@@'`
      echo $file '->' $dots$y ': Making link'
      rm -fr ${LIB}/$file > /dev/null 2>&1
      ln -s $dots$y ${LIB}/$file > /dev/null 2>&1
    else
      # 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.
      echo $file '->' root$x ': Making link'
      if [ -d $LIB/root$x ]
      then true
      else
        dirname=root$x/
        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|[^/]*/||'`
        done
      fi
      # Duplicate directory structure created in ${LIB}/$file in new
      # root area.
      for file2 in $files; do
        case $file2 in
          $file/*)
        dupdir=${LIB}/root$x/`echo $file2 | sed -n "s|^${file}/||p"`
        echo "Duplicating ${file}'s ${dupdir}"
        if [ -d ${dupdir} ]
        then true
        else
          mkdir ${dupdir}
        fi
        ;;
          *)
        ;;
        esac
          done
      # Get the path from ${LIB} to $file, accounting for symlinks.
      parent=`echo "$file" | sed -e 's@/[^/]*$@@'`
      libabs=`cd ${LIB}; ${PWDCMD}`
      file2=`cd ${LIB}; cd $parent; ${PWDCMD} | sed -e "s@^${libabs}@@"`
      # DOTS is the relative path from ${LIB}/$file's dir back to ${LIB}.
      dots=`echo "$file2" | sed -e 's@/[^/]*@../@g'`
      rm -fr ${LIB}/$file > /dev/null 2>&1
      ln -s ${dots}root$x ${LIB}/$file > /dev/null 2>&1
      treetops="$treetops $x ${LIB}/root$x"
    fi
      fi
      cd $cwd
    fi
  done
fi
 
# # # # # # # # # # # # # # # # # # # # #
#
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
  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
  echo Fixing directory ${SRCDIR} into ${DESTDIR}

  # Check .h files which are symlinks as well as those which are files.
  # A link to a header file will not be processed by anything but this.
  #
  cd ${SRCDIR}
[=_IF PROGRAM _env ! =][=

    _include hackshell =][=

  _ELSE

=]
  required="$required `if $LINKS; then
    find . -name '*.h' \( -type f -o -type l \) -print
  else
    find . -name '*.h' -type f -print
  fi | ${FIXINCL}`"[=


  _ENDIF =]
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
    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
        echo Copied $2
        for include in `egrep '^[ 	]*#[ 	]*include[ 	]*"[^/]' $3 |
             sed -e 's/^[ 	]*#[ 	]*include[ 	]*"\([^"]*\)".*$/\1/'`
        do
	  dir=`echo $2 | sed -e s'|/[^/]*$||'`
	  dir2=`echo $3 | sed -e s'|/[^/]*$||'`
	  newreq="$newreq $1 $dir/$include $dir2/$include"
        done
      fi
    fi
    shift; shift; shift
  done
  set x $newreq
  shift
done

echo 'Cleaning up DONE files.'
cd $LIB
find . -name DONE -exec rm -f '{}' ';'

echo 'Removing unneeded directories:'
cd $LIB
files=`find . -type d -print | sort -r`
for file in $files; do
  rmdir $LIB/$file > /dev/null 2>&1 | :
done

# # # # # # # # # # # # # # # # # # # # #
#
# End of for INPUT directories
#
done
#
# # # # # # # # # # # # # # # # # # # # #

cd $ORIGDIR
rm -f include/assert.h
cp ${srcdir}/assert.h include/assert.h || exit 1
chmod a+r include/assert.h
[=

#  Make the output file executable
# =][=
_eval _outfile "chmod +x %s" _printf _shell=]