OSDN Git Service

98b8495fe042e2a26d474fd9cc5b16a0bf372697
[pf3gnuchains/gcc-fork.git] / gcc / fixproto
1 #!/bin/sh
2 #
3 # SYNOPSIS
4 #       fixproto TARGET-DIR [ SOURCE-DIR ... ]
5 #
6 # COPYRIGHT
7 #       Copyright (C) 1993 Free Software Foundation, Inc.
8 #       This file is part of GNU CC.
9 #
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.
14 #
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.
19 #
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.
23 #
24 # DESCRIPTION
25 #       Adjunct script for GNU CC to populate a directory with ANSI,
26 #       Posix.1, and C++ compatible header files.
27 #
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.
31 #
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.
38 #
39 #       The comment at the beginning of patch-header.c lists specifically
40 #       what kind of changes are made.
41 #
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.
49 #
50 # AUTHORS
51 #       Ron Guilmette (rfg@netcom.com) (original idea and code)
52 #       Per Bothner (bothner@cygnus.com) (major re-write)
53
54 progname=$0
55 progname=`basename $progname`
56 original_dir=`pwd`
57 CC=gcc
58 CPP=${CPP-./cpp}
59 #CPP="${CC} -E"
60 if [ `echo $1 | wc -w` = 0 ] ; then
61   echo $progname\: usage\: $progname target-dir \[ source-dir \.\.\. \]
62   exit 1
63 fi
64 rel_target_dir=$1
65 shift
66
67 if [ `expr $rel_target_dir : '\(.\)'` != '/' ] ; then
68   abs_target_dir=$original_dir/$rel_target_dir
69 else
70   abs_target_dir=$rel_target_dir
71 fi
72
73 if [ \! -d $abs_target_dir ] ; then
74   echo $progname\: creating directory $rel_target_dir
75   mkdir $abs_target_dir
76 fi
77
78 echo $progname\: populating \`$rel_target_dir\'
79
80 required_ctype_h="isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit tolower toupper"
81 required_dirent_h="closedir opendir readdir rewinddir"
82 extra_check_errno_h="errno"
83 required_curses_h="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"
84 required_fcntl_h="creat fcntl open"
85 # required_float_h=...
86 required_grp_h="getgrgid getgrnam"
87 # Maybe also "getgrent fgetgrent setgrent endgrent" */
88 # required_limit_h= /* Lots of macros */
89 required_locale_h="localeconv setlocale"
90 required_math_h="acos asin atan atan2 ceil cos cosh exp fabs floor fmod frexp ldexp log10 log modf pow sin sinh sqrt tan tanh"
91 extra_check_math_h="HUGE_VAL"
92
93 required_pwd_h="getpwnam getpwuid"
94 required_setjmp_h="longjmp setjmp siglongjmp sigsetjmp"
95 required_signal_h="kill raise sigaction sigaddset sigdelset sigemptyset sigfillset sigismember signal sigpending sigprocmask sigsuspend"
96 # *stdarg_h *stddef.h  - supplied by gcc
97 required_stdio_h="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"
98 # Should perhaps also handle NULL, EOF, ... ?
99 required_stdlib_h="abort abs atexit atof atoi atol bsearch calloc exit free getenv labs malloc qsort rand realloc srand strtod strtol strtoul system"
100 # "div ldiv", - ignored because these depend on div_t, ldiv_t
101 # ignore these: "mblen mbstowcs mbstowc wcstombs wctomb"
102 # Should perhaps also add NULL
103 required_string_h="memchr memcmp memcpy memmove memset strcat strchr strcmp strcoll strcpy strcspn strerror strlen strncat strncmp"
104 # Should perhaps also add NULL and size_t
105 required_sys_stat_h="chmod fstat mkdir mkfifo stat umask"
106 extra_check_sys_stat_h="S_ISDIR S_ISBLK S_ISCHR S_ISFIFO S_ISREG S_ISLNK S_IFDIR S_IFBLK S_IFCHR S_IFIFO S_IFREG S_IFLNK"
107 required_sys_times_h="times"
108 # "sys_types.h" add types (not in old g++-include)
109 required_sys_resource_h="getrusage getrlimit setrlimit getpriority setpriority"
110 required_sys_utsname_h="uname"
111 required_sys_wait_h="wait waitpid"
112 extra_check_sys_wait_h="WEXITSTATUS WIFEXITED WIFSIGNALED WIFSTOPPED WSTOPSIG WTERMSIG WNOHANG WNOTRACED"
113 # required_tar.h= 
114 required_termios_h="cfgetispeed cfgetospeed cfsetispeed cfsetospeed tcdrain tcflow tcflush tcgetattr tcsendbreak tcsetattr"
115 required_time_h="asctime clock ctime difftime gmtime localtime mktime strftime time tzset"
116 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"
117
118 include_path=""
119
120 if [ `echo $* | wc -w` != 0 ] ; then
121   for rel_source_dir in $*; do
122     if [ `expr $rel_source_dir : '\(.\)'` != '/' ] ; then
123       abs_source_dir=$original_dir/$rel_source_dir
124     else
125       abs_source_dir=$rel_source_dir
126     fi
127     include_path="$include_path -I$abs_source_dir"
128   done
129 fi
130
131 done_dirs=""
132 done_files=""
133
134 if [ `echo $* | wc -w` != 0 ] ; then
135   for rel_source_dir in $* ; do
136     if [ `expr $rel_source_dir : '\(.\)'` != '/' ] ; then
137       abs_source_dir=$original_dir/$rel_source_dir
138     else
139       abs_source_dir=$rel_source_dir
140     fi
141     if [ \! -d $abs_source_dir ] ; then
142       echo $progname\: warning\: no such directory\: \`$rel_source_dir\'
143       continue
144     fi
145     rel_source_subdirs=`cd $abs_source_dir; find . -type d -print | sed -e 's%^\./%%' -e 's/\.//'`
146     if [ `echo $rel_source_subdirs | wc -w` != 0 ] ; then
147       for rel_source_subdir in $rel_source_subdirs; do
148         abs_target_subdir=$abs_target_dir/$rel_source_subdir
149         if [ \! -d $abs_target_subdir ] ; then
150           mkdir $abs_target_subdir
151         fi
152       done
153     fi
154
155     rel_source_files=`cd $abs_source_dir; find . '!' -type d -name \*.h -print | sed 's%^\./%%'`
156
157     if [ `echo $rel_source_files | wc -w` != 0 ] ; then
158       for rel_source_file in $rel_source_files; do
159         abs_source_file=$abs_source_dir/$rel_source_file
160         abs_target_file=$abs_target_dir/$rel_source_file
161
162         # If target file exists, check if was written while processing one
163         # of the earlier source directories;  if so ignore it.
164         if test -f $abs_target_file -a -n "$done_dirs" \
165           && echo " $done_files " |grep " $rel_source_file " >/dev/null
166         then
167           echo $abs_target_file exists, $abs_source_file is ignored
168         else
169           # echo doing $rel_source_file from $abs_source_dir
170           rel_source_ident=`echo $rel_source_file | tr ./--- ___`
171           required_list=`eval echo '${required_'${rel_source_ident}'-}'`
172           extra_check_list=`eval echo '${extra_check_'${rel_source_ident}'-}'`
173           rm -f tmp.c tmp.i
174           echo "#include <${rel_source_file}>" >tmp.c
175           for macro in ${required_list} ${extra_check_list}
176           do
177             echo "#ifdef ${macro}" >>tmp.c
178             echo "__DEFINED_MACRO_${macro};" >>tmp.c
179             echo "#endif" >>tmp.c
180           done
181           if ${CPP} -D__STDC__ -D__cplusplus -D_POSIX_SOURCE $include_path tmp.c >tmp.i
182           then
183             case $rel_source_file in
184               stdio.h)
185                 if grep _flsbuf <$abs_source_file >/dev/null ; then
186                   required_list="$required_list _flsbuf _filbuf"
187                 fi ;;
188             esac
189             cat $abs_source_file >tmp.c
190 #           echo Doing: "$original_dir/scan-decls <tmp.i | $original_dir/patch-header $rel_source_file tmp.c $abs_target_file \"$required_list\""
191
192             $original_dir/scan-decls <tmp.i | \
193              $original_dir/patch-header $rel_source_file tmp.c $abs_target_file "$required_list"
194           else
195             echo "${progname}: ${CPP} couldn't grok ${abs_source_file}"
196           fi
197           done_files="${done_files} ${rel_source_file}"
198         fi
199       done
200       rm -f tmp.c tmp.i
201     fi
202     # check for broken assert.h that needs stdio.h
203     if test -f $abs_source_dir/assert.h -a \! -f $abs_target_dir/assert.h \
204       && grep 'stderr' $abs_source_dir/assert.h >/dev/null
205     then
206       if grep 'include.*stdio.h' $abs_source_dir/assert.h >/dev/null
207       then true
208       else
209         echo 'Fixing broken assert.h (needs stdio.h)'
210         cat $abs_source_dir/assert.h >$abs_target_dir/assert.h
211         echo '#include <stdio.h>' >>$abs_target_dir/assert.h
212       fi
213     fi
214     done_dirs="$done_dir $rel_source_dir"
215   done
216 fi
217
218 for rel_source_file in unistd.h stdlib.h
219 do
220   if echo " $done_files " | grep " $rel_source_file " >/dev/null
221   then true
222   else
223     echo Adding missing $rel_source_file
224     rel_source_ident=`echo $rel_source_file | tr ./--- ___`
225     required_list=`eval echo '${required_'${rel_source_ident}'-}'`
226     cat >tmp.h <<EOF
227 #ifndef ${rel_source_ident}
228 #define ${rel_source_ident}
229 #endif
230 EOF
231     $original_dir/patch-header $rel_source_file tmp.h $abs_target_dir/$rel_source_file "$required_list" </dev/null
232     rm tmp.h
233   fi
234 done
235 exit 0