Initial revision
[official-gcc.git] / gcc / fixproto
blob39849e6b2716d78c376888fce5645baedf1c7503
1 #!/bin/sh
3 # SYNOPSIS
4 # fixproto TARGET-DIR [ SOURCE-DIR ... ]
6 # COPYRIGHT
7 # Copyright (C) 1993 Free Software Foundation, Inc.
8 # This file is part of GNU CC.
10 # GNU CC is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2, or (at your option)
13 # any later version.
15 # GNU CC is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with GNU CC; see the file COPYING. If not, write to
22 # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 # DESCRIPTION
25 # Adjunct script for GNU CC to populate a directory with ANSI,
26 # Posix.1, and C++ compatible header files.
28 # Each file found under each SOURCE-DIR is analized and "fixed."
29 # The SOURCE-DIRs are searched in order; a file found
30 # under multiple SOURCE-DIRs is only handled for the first one.
32 # STRATEGY
33 # Each include file is fed through cpp, and the scan-decls program
34 # parses it, and emits any found function declarations.
35 # The patch-header program analyzes the scan-decls output,
36 # together with the original include file, and writes a "fixed"
37 # include file, if needed.
39 # The comment at the beginning of patch-header.c lists specifically
40 # what kind of changes are made.
42 # NOTE
43 # Some file space will be wasted, because the original header
44 # files are copied. An earlier version just included the original
45 # by "reference", using GNU cpp's #include_next mechanism.
46 # This is currently not done, partly because #include_next is
47 # fragile (susceptible to version incompatibilties, and depends
48 # and GCC-specific features), and partly for performance reasons.
50 # AUTHORS
51 # Ron Guilmette (rfg@netcom.com) (original idea and code)
52 # Per Bothner (bothner@cygnus.com) (major re-write)
54 progname=$0
55 progname=`basename $progname`
56 original_dir=`pwd`
57 CPP=${CPP-./cpp}
59 if [ `echo $1 | wc -w` = 0 ] ; then
60 echo $progname\: usage\: $progname target-dir \[ source-dir \.\.\. \]
61 exit 1
63 rel_target_dir=$1
64 shift
66 if [ `expr $rel_target_dir : '\(.\)'` != '/' ] ; then
67 abs_target_dir=$original_dir/$rel_target_dir
68 else
69 abs_target_dir=$rel_target_dir
72 # Determine whether this system has symbolic links.
73 if ln -s X $rel_target_dir/ShouldNotExist 2>/dev/null; then
74 rm -f $rel_target_dir/ShouldNotExist
75 LINKS=true
76 elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
77 rm -f /tmp/ShouldNotExist
78 LINKS=true
79 else
80 LINKS=false
83 if [ \! -d $abs_target_dir ] ; then
84 echo $progname\: creating directory $rel_target_dir
85 mkdir $abs_target_dir
88 echo $progname\: populating \`$rel_target_dir\'
90 include_path=""
92 if [ `echo $* | wc -w` != 0 ] ; then
93 for rel_source_dir in $*; do
94 if [ `expr $rel_source_dir : '\(.\)'` != '/' ] ; then
95 abs_source_dir=$original_dir/$rel_source_dir
96 else
97 abs_source_dir=$rel_source_dir
99 include_path="$include_path -I$abs_source_dir"
100 done
103 required_stdlib_h="abort abs atexit atof atoi atol bsearch calloc exit free getenv labs malloc qsort rand realloc srand strtod strtol strtoul system"
104 # "div ldiv", - ignored because these depend on div_t, ldiv_t
105 # ignore these: "mblen mbstowcs mbstowc wcstombs wctomb"
106 # Should perhaps also add NULL
107 required_unistd_h="_exit access alarm chdir chown close ctermid cuserid dup dup2 execl execle execlp execv execve execvp fork fpathconf getcwd getegid geteuid getgid getgroups getlogin getpgrp getpid getppid getuid isatty link lseek pathconf pause pipe read rmdir setgid setpgid setsid setuid sleep sysconf tcgetpgrp tcsetpgrp ttyname unlink write"
109 done_dirs=""
110 echo "" >fixproto.list
112 if [ `echo $* | wc -w` != 0 ] ; then
113 for rel_source_dir in $* ; do
114 if [ `expr $rel_source_dir : '\(.\)'` != '/' ] ; then
115 abs_source_dir=$original_dir/$rel_source_dir
116 else
117 abs_source_dir=$rel_source_dir
119 if [ \! -d $abs_source_dir ] ; then
120 echo $progname\: warning\: no such directory\: \`$rel_source_dir\'
121 continue
124 dirs="."
125 levels=2
126 subdirs="."
127 while $LINKS && test -n "$dirs" -a $levels -gt 0
129 levels=`expr $levels - 1`
130 newdirs=
131 for d in $dirs ; do
132 # Find all directories under $d, relative to $d, excluding $d itself.
133 subdirs="$subdirs "`cd $abs_source_dir/$d; find . -type d -print | \
134 sed -e '/^\.$/d' -e "s|^\./|${d}/|" -e 's|^\./||'`
135 links=
136 links=`cd $abs_source_dir; find $d/. -type l -print | \
137 sed -e "s|$d/./|$d/|" -e 's|^\./||'`
138 for link in $links --dummy-- ; do
139 if test -d $abs_source_dir/$link/. ; then
140 newdirs="$newdirs $link"
142 done
143 done
144 dirs="$newdirs"
145 subdirs="$subdirs $newdirs"
146 done
148 for rel_source_subdir in $subdirs; do
149 abs_target_subdir=${abs_target_dir}/${rel_source_subdir}
150 if [ \! -d $abs_target_subdir ] ; then
151 mkdir $abs_target_subdir
153 # Append "/"; remove initial "./". Hence "." -> "" and "sys" -> "sys/".
154 rel_source_prefix=`echo $rel_source_subdir | sed -e 's|$|/|' -e 's|^./||'`
156 # The 'sed' is in case the *.h matches nothing, which yields "*.h"
157 # which would then get re-globbed in the current directory. Sigh.
158 rel_source_files=`cd ${abs_source_dir}/${rel_source_subdir}; echo *.h | sed -e 's|[*].h|NONE|'`
160 for filename in $rel_source_files ; do
161 rel_source_file=${rel_source_prefix}${filename}
162 abs_source_file=$abs_source_dir/$rel_source_file
163 abs_target_file=$abs_target_dir/$rel_source_file
165 if test "$filename" = 'NONE' ; then
166 echo "(No *.h files in $abs_source_dir/$rel_source_subdir)"
167 # If target file exists, check if was written while processing one
168 # of the earlier source directories; if so ignore it.
169 elif test -f $abs_target_file -a -n "$done_dirs" \
170 && grep "$rel_source_file" fixproto.list >/dev/null
171 then true
172 # echo $abs_target_file exists, $abs_source_file is ignored
173 else
174 # echo doing $rel_source_file from $abs_source_dir
175 required_list=
176 extra_check_list=
177 case $rel_source_file in
178 ctype.h)
179 required_list="isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit tolower toupper" ;;
180 dirent.h)
181 required_list="closedir opendir readdir rewinddir" ;;
182 errno.h)
183 extra_check_list="errno" ;;
184 curses.h)
185 required_list="box delwin endwin getcurx getcury initscr mvcur mvwprintw mvwscanw newwin overlay overwrite scroll subwin touchwin waddstr wclear wclrtobot wclrtoeol waddch wdelch wdeleteln werase wgetch wgetstr winsch winsertln wmove wprintw wrefresh wscanw wstandend wstandout" ;;
186 fcntl.h)
187 required_list="creat fcntl open" ;;
188 grp.h)
189 #Maybe also "getgrent fgetgrent setgrent endgrent" */
190 required_list="getgrgid getgrnam" ;;
191 limit.h)
192 required_list= /* Lots of macros */ ;;
193 locale.h)
194 required_list="localeconv setlocale" ;;
195 math.h)
196 required_list="acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp ldexp log10 log modf pow sin sinh sqrt tan tanh"
197 extra_check_list="HUGE_VAL" ;;
198 pwd.h)
199 required_list="getpwnam getpwuid" ;;
200 setjmp.h)
201 required_list="longjmp setjmp siglongjmp sigsetjmp" ;;
202 signal.h)
203 required_list="kill raise sigaction sigaddset sigdelset sigemptyset sigfillset sigismember sigpending sigprocmask sigsuspend" ;;
204 # Left out signal() - its prototype is too complex for us!
205 stdio.h)
206 required_list="clearerr fclose feof ferror fflush fgetc fgetpos fgets fopen fprintf fputc fputs fread freopen fscanf fseek fsetpos ftell fwrite getc getchar gets perror printf putc putchar puts remove rename rewind scanf setbuf setvbuf sprintf sscanf tmpfile tmpnam ungetc vfprintf vprintf vsprintf"
207 if grep _flsbuf <$abs_source_file >/dev/null ; then
208 required_list="$required_list _flsbuf _filbuf"
210 # Should perhaps also handle NULL, EOF, ... ?
212 stdlib.h)
213 required_list="$required_stdlib_h" ;;
214 string.h)
215 required_list="memchr memcmp memcpy memmove memset strcat strchr strcmp strcoll strcpy strcspn strerror strlen strncat strncmp" ;;
216 # Should perhaps also add NULL and size_t
217 sys/stat.h)
218 required_list="chmod fstat mkdir mkfifo stat umask"
219 extra_check_list="S_ISDIR S_ISBLK S_ISCHR S_ISFIFO S_ISREG S_ISLNK S_IFDIR S_IFBLK S_IFCHR S_IFIFO S_IFREG S_IFLNK" ;;
220 sys/times.h)
221 required_list="times" ;;
222 # "sys/types.h" add types (not in old g++-include)
223 sys/resource.h)
224 required_list="getrusage getrlimit setrlimit getpriority setpriority" ;;
225 sys/utsname.h)
226 required_list="uname" ;;
227 sys/wait.h)
228 required_list="wait waitpid"
229 extra_check_list="WEXITSTATUS WIFEXITED WIFSIGNALED WIFSTOPPED WSTOPSIG WTERMSIG WNOHANG WNOTRACED" ;;
230 tar.h)
231 required_list= ;;
232 termios.h)
233 required_list="cfgetispeed cfgetospeed cfsetispeed cfsetospeed tcdrain tcflow tcflush tcgetattr tcsendbreak tcsetattr" ;;
234 time.h)
235 required_list="asctime clock ctime difftime gmtime localtime mktime strftime time tzset" ;;
236 unistd.h)
237 required_list="$required_unistd_h" ;;
238 esac
239 rm -f fixtmp.c fixtmp.i
240 echo "#include <${rel_source_file}>" >fixtmp.c
241 for macro in ${required_list} ${extra_check_list}
243 echo "#ifdef ${macro}" >>fixtmp.c
244 echo "__DEFINED_MACRO_${macro};" >>fixtmp.c
245 echo "#endif" >>fixtmp.c
246 done
247 if ${CPP} -D__STDC__ -D__cplusplus -D_POSIX_SOURCE $include_path fixtmp.c >fixtmp.i 2>/dev/null
248 then
249 cat $abs_source_file >fixtmp.c
250 # echo Doing: "$original_dir/scan-decls <fixtmp.i | $original_dir/patch-header $rel_source_file fixtmp.c $abs_target_file \"$required_list\""
252 $original_dir/scan-decls <fixtmp.i | \
253 $original_dir/patch-header $rel_source_file fixtmp.c $abs_target_file "$required_list"
254 else
255 echo "${progname}: cpp could not parse ${abs_source_file} (ignored)"
257 echo "${rel_source_file}" >>fixproto.list
259 done
260 rm -f fixtmp.c fixtmp.i
261 done
262 # check for broken assert.h that needs stdio.h
263 if test -f $abs_source_dir/assert.h -a \! -f $abs_target_dir/assert.h \
264 && grep 'stderr' $abs_source_dir/assert.h >/dev/null
265 then
266 if grep 'include.*stdio.h' $abs_source_dir/assert.h >/dev/null
267 then true
268 else
269 echo 'Fixing broken assert.h (needs stdio.h)'
270 cat $abs_source_dir/assert.h >$abs_target_dir/assert.h
271 echo '#include <stdio.h>' >>$abs_target_dir/assert.h
274 done_dirs="$done_dir $rel_source_dir"
275 done
278 # This might be more cleanly moved into the main loop, by adding
279 # a <dummy> source directory at the end. FIXME!
280 for rel_source_file in unistd.h stdlib.h
282 if grep "$rel_source_file" fixproto.list >/dev/null
283 then true
284 else
285 echo Adding missing $rel_source_file
286 rel_source_ident=`echo $rel_source_file | tr ./ __`
287 required_list=`eval echo '${required_'${rel_source_ident}'-}'`
288 cat >tmp.h <<EOF
289 #ifndef ${rel_source_ident}
290 #define ${rel_source_ident}
291 #endif
293 $original_dir/patch-header $rel_source_file tmp.h $abs_target_dir/$rel_source_file "$required_list" </dev/null
294 rm tmp.h
296 done
297 exit 0