2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / scan-types.sh
blob59d786a9fd01cb8a6662a3cd379ad1614e66d783
1 #! /bin/sh
2 # Deduce values of standard ANSI and POSIX types (e.g. size_t, pid_t).
3 # Emits macros definitions for these, and some other types.
4 # Intended to be used to massage the sys-protos.h file.
5 # Expects one arg, which is the GCC source directory.
7 CC=${CC-"./xgcc -B$1/"}
8 CPP=${CPP-`echo ${CC} -E -I"$1/"`}
9 SED=sed
11 # Generate definitions for the standard types (such as mode_t)
12 # compatible with those in the standard C header files.
13 # It works by a dummy program through the C pre-processor, and then
14 # using sed to search for typedefs in the output.
16 cat >st-dummy.c <<!EOF!
17 #include <sys/types.h>
18 #include <stddef.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <time.h>
22 #include <signal.h>
23 #ifdef size_t
24 typedef size_t Xsize_t;
25 #elif defined(__SIZE_TYPE__)
26 typedef __SIZE_TYPE__ Xsize_t;
27 #endif
28 #ifdef va_list
29 typedef va_list XXXva_list;
30 #endif
31 !EOF!
33 if ${CPP} st-dummy.c >TMP ; then true
34 else
35 echo "scan-types: could not invoke ${CPP} on st-dummy.c" 1>&2 ; exit 1
37 tr ' ' ' ' <TMP >st-dummy.out
39 for TYPE in dev_t clock_t fpos_t gid_t ino_t mode_t nlink_t off_t pid_t size_t ssize_t time_t uid_t va_list int32_t uint_32_t ; do
40 IMPORTED=`eval 'echo $'"$TYPE"`
41 if [ -n "${IMPORTED}" ] ; then
42 eval "$TYPE='$IMPORTED"
43 else
44 # Search st-dummy.out for a typedef for $TYPE, and write it out
45 # to TMP in #define syntax.
46 rm -f TMP
47 ${SED} -n -e "s|.*typedef *\(.*\) X*$TYPE *;.*|\1|w TMP" <st-dummy.out>/dev/null
48 # Now select the first definition.
49 if [ -s TMP ]; then
50 # VALUE is now the typedef'd definition of $TYPE.
51 eval "VALUE='`${SED} -e 's| *$||' -e '2,$d' <TMP`'"
52 # Unless VALUE contains a blank, look for a typedef for it
53 # in turn (this could be a loop, but that would be over-kill).
54 # Ensure $VALUE is double quoted to protect cases where it
55 # contains an asterisk and would cause filename expansion.
56 # E.g. when va_list is "char *".
57 if echo "$VALUE" | grep " " >/dev/null ; then true
58 else
59 rm -f TMP
60 ${SED} -n -e "s|.*typedef[ ][ ]*\(.*[^a-zA-Z0-9_]\)${VALUE}[ ]*;.*|\1|w TMP" <st-dummy.out>/dev/null
61 if [ -s TMP ]; then
62 eval "VALUE='`${SED} -e '2,$d' -e 's|[ ]*$||' <TMP`'"
65 eval "$TYPE='$VALUE'"
68 done
70 cat <<!EOF!
71 #define ${macro_prefix}clock_t ${clock_t-int /* default */}
72 #define ${macro_prefix}dev_t ${dev_t-int /* default */}
73 #define ${macro_prefix}fpos_t ${fpos_t-long /* default */}
74 #define ${macro_prefix}gid_t ${gid_t-int /* default */}
75 #define ${macro_prefix}ino_t ${ino_t-int /* default */}
76 #define ${macro_prefix}mode_t ${mode_t-int /* default */}
77 #define ${macro_prefix}nlink_t ${nlink_t-int /* default */}
78 #define ${macro_prefix}off_t ${off_t-long /* default */}
79 #define ${macro_prefix}pid_t ${pid_t-int /* default */}
80 #define ${macro_prefix}ptrdiff_t __PTRDIFF_TYPE__
81 #define ${macro_prefix}size_t __SIZE_TYPE__
82 #define ${macro_prefix}time_t ${time_t-int /* default */}
83 #define ${macro_prefix}uid_t ${uid_t-int /* default */}
84 #define ${macro_prefix}wchar_t __WCHAR_TYPE__
85 #define ${macro_prefix}int32_t ${int32_t-int /* default */}
86 #define ${macro_prefix}uint32_t ${uint32_t-unsigned int /* default */}
87 !EOF!
89 # (wait_arg_t*) should be (int*), according to Posix, but
90 # BSD traditionally used (union wait*). Use (void*) to allow either usage.
91 echo "#define ${macro_prefix}wait_arg_t void"
93 # ssize_t is the signed version of size_t
94 if [ -n "${ssize_t}" ] ; then
95 echo "#define ${macro_prefix}ssize_t ${ssize_t}"
96 elif [ -z "${size_t}" ] ; then
97 echo "#define ${macro_prefix}ssize_t long"
98 else
99 # Remove "unsigned" from ${size_t} to get ${ssize_t}.
100 tmp="`echo ${size_t} | ${SED} -e 's|unsigned||g' -e 's| | |g'`"
101 if [ -z "$tmp" ] ; then
102 tmp=int
103 else
104 # check $tmp doesn't conflict with <unistd.h>
105 echo "#include <unistd.h>
106 extern $tmp read();" >st-dummy.c
107 ${CC} -c st-dummy.c >/dev/null 2>&1 || tmp=int
109 echo "#define ${macro_prefix}ssize_t $tmp /* default */"
112 # va_list can cause problems (e.g. some systems have va_list as a struct).
113 # Check to see if ${va_list-char*} really is compatible with stdarg.h.
114 cat >st-dummy.c <<!EOF!
115 #define X_va_list ${va_list-char* /* default */}
116 extern long foo(X_va_list ap); /* Check that X_va_list compiles on its own */
117 #include <stdarg.h>
118 long foo(X_va_list ap) { return va_arg(ap, long); }
119 long bar(int i, ...)
120 { va_list ap; long j; va_start(ap, i); j = foo(ap); va_end(ap); return j; }
121 !EOF!
122 if ${CC} -c st-dummy.c >/dev/null 2>&1 ; then
123 # Ok: We have something that works.
124 echo "#define ${macro_prefix}va_list ${va_list-char* /* default */}"
125 else
126 # No, it breaks. Indicate that <stdarg.h> must be included.
127 echo "#define ${macro_prefix}NEED_STDARG_H
128 #define ${macro_prefix}va_list va_list"
131 # stuff needed for curses.h
133 # This isn't correct for SVR4 (for example). However, we only
134 # use this when adding a missing prototype, so it shouldn't matter.
135 echo "#define chtype int"
136 # sys-protos.h uses non-standard names (due to the CHTYPE argument problem).
137 echo "#define box32 box"
138 echo "#define initscr32 initscr"
139 echo "#define w32addch waddch"
140 echo "#define w32insch winsch"
142 rm -f st-dummy.c st-dummy.o TMP st-dummy.out