OSDN Git Service

2008-11-09 Thomas Schwinge <tschwinge@gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fixproto
1 #!/bin/sh
2 #
3 # SYNOPSIS
4 #       fixproto TARGET-DIR SOURCE-DIR-ALL SOURCE-DIR-STD
5 #
6 # COPYRIGHT
7 #       Copyright (C) 1993, 1994, 1997, 1998, 2002, 2003, 2007
8 #       Free Software Foundation, Inc.
9 #       This file is part of GCC.
10 #
11 #       GCC is free software; you can redistribute it and/or modify
12 #       it under the terms of the GNU General Public License as published by
13 #       the Free Software Foundation; either version 3, or (at your option)
14 #       any later version.
15 #
16 #       GCC is distributed in the hope that it will be useful,
17 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
18 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 #       GNU General Public License for more details.
20 #
21 #       You should have received a copy of the GNU General Public License
22 #       along with GCC; see the file COPYING3.  If not see
23 #       <http://www.gnu.org/licenses/>.
24 #
25 # DESCRIPTION
26 #       Adjunct script for GCC to populate a directory with ANSI,
27 #       Posix.1, and C++ compatible header files.
28 #
29 #       Each file found under SOURCE-DIR-ALL is analyzed and "fixed."
30 #       Only standard ANSI/POSIX files found under SOURCE-DIR-STD
31 #       are analyzed and "fixed."
32 #       The SOURCE-DIRs are searched in order; a file found
33 #       under multiple SOURCE-DIRs is only handled for the first one.
34 #
35 # STRATEGY
36 #       Each include file is fed through cpp, and the scan-decls program
37 #       parses it, and emits any found function declarations.
38 #       The fix-header program analyzes the scan-decls output,
39 #       together with the original include file, and writes a "fixed"
40 #       include file, if needed.
41 #
42 #       The comment at the beginning of fix-header.c lists specifically
43 #       what kind of changes are made.
44 #
45 # NOTE
46 #       Some file space will be wasted, because the original header
47 #       files are copied.  An earlier version just included the original
48 #       by "reference", using GNU cpp's #include_next mechanism.
49 #       This is currently not done, partly because #include_next is
50 #       fragile (susceptible to version incompatibilities, and depends
51 #       and GCC-specific features), and partly for performance reasons.
52 #
53 # AUTHORS
54 #       Ron Guilmette (rfg@netcom.com) (original idea and code)
55 #       Per Bothner (bothner@cygnus.com) (major re-write)
56
57 dirname=`echo "$0" | sed 's,^[^/]*$,.,;s,//*[^/]*$,,'`
58 progname=`echo "$0" | sed 's,.*/,,'`
59 original_dir=`${PWDCMD-pwd}`
60 FIX_HEADER=${FIX_HEADER-$original_dir/fix-header}
61 DEFINES="-D__STDC__=0 -D__cplusplus ${FIXPROTO_DEFINES}"
62
63 if mkdir -p . 2> /dev/null; then
64   # Great, mkdir accepts -p
65   mkinstalldirs="mkdir -p"
66 else
67   # We expect mkinstalldirs to be passed in the environment.
68   # If it is not, assume it is in the directory that contains this script.
69   mkinstalldirs=${mkinstalldirs-"/bin/sh $dirname/mkinstalldirs"}
70   if $mkinstalldirs . 2> /dev/null; then
71     :
72   else
73     # But, in case of failure, fallback to plain mkdir, and hope it works
74     mkinstalldirs=mkdir
75   fi
76 fi
77
78 if [ `echo $1 | wc -w` = 0 ] ; then
79   echo $progname\: usage\: $progname target-dir \[ source-dir \.\.\. \]
80   exit 1
81 fi
82
83 std_files="ctype.h dirent.h errno.h curses.h fcntl.h grp.h locale.h math.h pwd.h setjmp.h signal.h stdio.h stdlib.h string.h sys/socket.h sys/stat.h sys/times.h sys/resource.h sys/utsname.h sys/wait.h tar.h termios.h time.h unistd.h utime.h"
84
85 rel_target_dir=$1
86 # All files in $src_dir_all (normally same as $rel_target_dir) are
87 # processed.
88 src_dir_all=$2
89 # In $src_dir_std (normally same as /usr/include), only the
90 # "standard" ANSI/POSIX files listed in $std_files are processed.
91 src_dir_std=$3
92
93 case $rel_target_dir in
94   /* | [A-Za-z]:[\\/]*)
95      abs_target_dir=$rel_target_dir
96      ;;
97   *)
98      abs_target_dir=$original_dir/$rel_target_dir
99      ;;
100 esac
101
102 # Determine whether this system has symbolic links.
103 if ln -s X $rel_target_dir/ShouldNotExist 2>/dev/null; then
104   rm -f $rel_target_dir/ShouldNotExist
105   LINKS=true
106 elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
107   rm -f /tmp/ShouldNotExist
108   LINKS=true
109 else
110   LINKS=false
111 fi
112
113 if [ \! -d $abs_target_dir ] ; then
114   echo $progname\: creating directory $rel_target_dir
115   $mkinstalldirs $abs_target_dir
116 fi
117
118 echo $progname\: populating \`$rel_target_dir\'
119
120 include_path=""
121
122 if [ `echo $* | wc -w` != 0 ] ; then
123   for rel_source_dir in $src_dir_all $src_dir_std; do
124      case $rel_source_dir in
125        /* | [A-Za-z]:[\\/]*)
126          abs_source_dir=$rel_source_dir
127          ;;
128        *)
129          abs_source_dir=$original_dir/$rel_source_dir
130          ;;
131      esac
132     include_path="$include_path -I$abs_source_dir"
133   done
134 fi
135
136 done_dirs=""
137 subdirs_made=""
138 echo "" >fixproto.list
139
140 for code in ALL STD ; do
141
142   subdirs="."
143
144   case $code in
145     ALL)
146       rel_source_dir=$src_dir_all
147
148       dirs="."
149       levels=2
150       while $LINKS && test -n "$dirs" -a $levels -gt 0
151       do
152         levels=`expr $levels - 1`
153         newdirs=
154         for d in $dirs ; do
155           # Find all directories under $d, relative to $d, excluding $d itself.
156           # Assume directory names ending in CC or containing ++ are
157           # for C++, so skip those.
158           subdirs="$subdirs "`cd $rel_source_dir/$d; find . -type d -print | \
159                    sed -e '/^\.$/d' -e "s|^\./|${d}/|" -e 's|^\./||' \
160                      -e '/CC$/d' -e '/[+][+]/d'`
161           links=
162           links=`cd $rel_source_dir; find $d/. -type l -print | \
163                        sed -e "s|$d/./|$d/|" -e 's|^\./||'`
164           for link in $links --dummy-- ; do
165             test -d $rel_source_dir/$link/. && newdirs="$newdirs $link"
166           done
167         done
168         dirs="$newdirs"
169         subdirs="$subdirs $newdirs"
170       done
171       ;;
172     STD)
173       rel_source_dir=$src_dir_std
174       ;;
175   esac
176
177   case $rel_source_dir in
178     /* | [A-Za-z]:[\\/]*)
179        abs_source_dir=$rel_source_dir
180        ;;
181     *)
182        abs_source_dir=$original_dir/$rel_source_dir
183        ;;
184   esac
185
186   if [ \! -d $abs_source_dir ] ; then
187     echo $progname\: warning\: no such directory\: \`$rel_source_dir\'
188     continue
189   fi
190
191   for rel_source_subdir in $subdirs; do
192
193       abs_target_subdir=${abs_target_dir}/${rel_source_subdir}
194       if [ \! -d $abs_target_subdir ] ; then
195         if $mkinstalldirs $abs_target_subdir ; then
196           subdirs_made="$abs_target_subdir $subdirs_made"
197         fi
198       fi
199       # Append "/"; remove initial "./". Hence "." -> "" and "sys" -> "sys/".
200       rel_source_prefix=`echo $rel_source_subdir | sed -e 's|$|/|' -e 's|^\./||'`
201
202       case $code in
203         ALL)
204           # The 'sed' is in case the *.h matches nothing, which yields "*.h"
205           # which would then get re-globbed in the current directory.  Sigh.
206           rel_source_files=`cd ${abs_source_dir}/${rel_source_subdir}; echo *.h | sed -e 's|[*].h|NONE|'`
207           ;;
208
209         STD)
210           files_to_check="$std_files"
211           rel_source_files=""
212
213           # Also process files #included by the $std_files.
214           while [ -n "${files_to_check}" ]
215           do
216             new_files_to_check=""
217             for file in $files_to_check ; do
218               xxfile=`echo $file | sed -e 's|/\([^/\.][^/\.]*\)/\.\./|/|'`
219               # Create the dir where this file will go when fixed.
220               xxdir=`echo ./$file | sed -e 's|/[^/]*$||'`
221               if [ \! -d $abs_target_subdir/$xxdir ] ; then
222                 if $mkinstalldirs $abs_target_subdir/$xxdir ; then
223                   subdirs_made="$abs_target_subdir/$xxdir $subdirs_made"
224                 fi
225               fi
226               # Just in case we have edited out a symbolic link
227               if [ -f $src_dir_std/$file -a -f $src_dir_std/$xxfile ] ; then
228                 file=$xxfile
229               fi
230               case " $rel_source_files " in
231                 *" ${file} "*)
232                   # Already seen $file; nothing to do
233                   ;;
234                 *)
235                   if test -f $src_dir_std/$file ; then
236                     rel_dir=`echo $file | sed -n -e 's|^\(.*/\)[^/]*$|\1|p'`
237                     # For #include "foo.h", that might be either "foo.h"
238                     # or "${rel_dir}foo.h (or something bogus).
239                     new_files_to_check="$new_files_to_check "`sed -n \
240                         -e 's@  @ @g' \
241                         -e 's@^ *# *include *<\([^>]*\)>.*$@\1@p' -e \
242                         's@^ *# *include *\"\([^\"]*\)\".*$@\1 '$rel_dir'\1@p'\
243                         <$src_dir_std/$file`
244                     rel_source_files="$rel_source_files $file"
245                   fi
246                   ;;
247               esac
248             done
249             files_to_check="$new_files_to_check"
250           done
251           rel_source_files="$rel_source_files"
252           ;;
253       esac
254
255       for filename in $rel_source_files ; do
256         rel_source_file=${rel_source_prefix}${filename}
257         abs_source_file=$abs_source_dir/$rel_source_file
258         abs_target_file=$abs_target_dir/$rel_source_file
259
260         if test "$filename" = 'NONE' ; then
261           echo "(No *.h files in $abs_source_dir/$rel_source_subdir)"
262         # If target file exists, check if was written while processing one
263         # of the earlier source directories;  if so ignore it.
264         elif test -f $abs_target_file -a -n "$done_dirs" \
265           && grep "$rel_source_file" fixproto.list >/dev/null
266         then true
267         else
268           $FIX_HEADER $rel_source_file $abs_source_file $abs_target_file ${DEFINES} $include_path
269           if test $? != 0 ; then exit 1 ; fi
270           echo "${rel_source_file}" >>fixproto.list
271         fi
272       done
273     done
274     done_dirs="$done_dir $rel_source_dir"
275 done
276
277 # This might be more cleanly moved into the main loop, by adding
278 # a <dummy> source directory at the end.  FIXME!
279
280 # All the headers we create define size_t and NULL.
281 for rel_source_file in unistd.h stdlib.h string.h time.h ; do
282   if grep "$rel_source_file" fixproto.list >/dev/null ; then
283     : # It exists, we don't need to make it
284   else
285     echo Adding missing $rel_source_file
286     rel_source_ident=`echo $rel_source_file | tr ./ __`
287     cat >tmp.h <<EOF
288 /* Fake ${rel_source_file}, created by GCC.
289    The functions declared in this file do not necessarily exist in
290    your C library. */
291 #ifndef __${rel_source_ident}
292 #define __${rel_source_ident}
293
294 #define __need_NULL
295 #define __need_size_t
296 #include <stddef.h>
297 EOF
298     # Insert special stuff for particular files here.
299     case ${rel_source_file} in
300       time.h)
301         # If time.h doesn't exist, find out if sys/time.h does.
302         if test -f $src_dir_std/sys/time.h \
303             || grep "sys/time.h" fixproto.list >/dev/null ; then
304           # It does; include it and hope it has the needed declarations.
305           # Some versions require sys/types.h.
306           cat >>tmp.h <<EOF
307
308 #include <sys/types.h>
309 #include <sys/time.h>
310 EOF
311         else
312           # It doesn't.  Make up plausible definitions for time_t, clock_t.
313           # Forward-declare struct tm.  Hope nobody tries to use it.  (Odds
314           # are they won't.)
315           cat >>tmp.h <<EOF
316
317 typedef long time_t;
318 typedef long clock_t;
319 struct tm;
320 EOF
321         fi ;;
322     esac
323     cat >>tmp.h <<EOF
324
325 #endif /* __${rel_source_ident} */
326 EOF
327     ${FIX_HEADER} $rel_source_file tmp.h $abs_target_dir/$rel_source_file ${DEFINES} $include_path
328     if test $? != 0 ; then exit 1 ; fi
329     if test -f $abs_target_dir/$rel_source_file ; then
330       rm tmp.h
331     else
332       mv tmp.h $abs_target_dir/$rel_source_file
333     fi
334   fi
335 done
336
337 # Remove any directories that we made that are still empty.
338 rmdir $subdirs_made 2>/dev/null
339
340 exit 0