OSDN Git Service

e5def66820cdd5e37e1922102672289b4e6e38ce
[pf3gnuchains/gcc-fork.git] / libgo / mksysinfo.sh
1 #!/bin/sh
2
3 # Copyright 2009 The Go Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style
5 # license that can be found in the LICENSE file.
6
7 # Create sysinfo.go.
8
9 # This shell script creates the sysinfo.go file which holds types and
10 # constants extracted from the system header files.  This relies on a
11 # hook in gcc: the -fdump-go-spec option will generate debugging
12 # information in Go syntax.
13
14 # We currently #include all the files at once, which works, but leads
15 # to exposing some names which ideally should not be exposed, as they
16 # match grep patterns.  E.g., WCHAR_MIN gets exposed because it starts
17 # with W, like the wait flags.
18
19 CC=${CC:-gcc}
20 OUT=tmp-sysinfo.go
21
22 set -e
23
24 rm -f sysinfo.c
25 cat > sysinfo.c <<EOF
26 #include "config.h"
27
28 #define _GNU_SOURCE
29 #if defined(__sun__) && defined(__svr4__)
30 /* Needed by Solaris header files.  */
31 #define _XOPEN_SOURCE 600
32 #define __EXTENSIONS__
33 #endif
34
35 #include <sys/types.h>
36 #include <dirent.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
41 #include <signal.h>
42 #if defined(HAVE_SYSCALL_H)
43 #include <syscall.h>
44 #endif
45 #if defined(HAVE_SYS_EPOLL_H)
46 #include <sys/epoll.h>
47 #endif
48 #if defined(HAVE_SYS_PTRACE_H)
49 #include <sys/ptrace.h>
50 #endif
51 #include <sys/resource.h>
52 #include <sys/uio.h>
53 #include <sys/socket.h>
54 #include <sys/stat.h>
55 #include <sys/time.h>
56 #include <sys/wait.h>
57 #include <sys/un.h>
58 #if defined(HAVE_SYS_USER_H)
59 #include <sys/user.h>
60 #endif
61 #if defined(HAVE_SYS_UTSNAME_H)
62 #include <sys/utsname.h>
63 #endif
64 #include <unistd.h>
65 EOF
66
67 ${CC} -fdump-go-spec=gen-sysinfo.go -S -o sysinfo.s sysinfo.c
68
69 echo 'package syscall' > ${OUT}
70
71 # Get all the consts and types, skipping ones which could not be
72 # represented in Go and ones which we need to rewrite.  We also skip
73 # function declarations, as we don't need them here.  All the symbols
74 # will all have a leading underscore.
75 grep -v '^// ' gen-sysinfo.go | \
76   grep -v '^func' | \
77   grep -v '^type _timeval ' | \
78   grep -v '^type _timespec ' | \
79   grep -v '^type _timestruc_t ' | \
80   grep -v '^type _epoll_' | \
81   grep -v 'in6_addr' | \
82   grep -v 'sockaddr_in6' | \
83   sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
84       -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
85       -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
86     >> ${OUT}
87
88 # The errno constants.
89 grep '^const _E' gen-sysinfo.go | \
90   sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
91
92 # The O_xxx flags.
93 grep '^const _\(O\|F\|FD\)_' gen-sysinfo.go | \
94   sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
95 if ! grep '^const O_ASYNC' ${OUT} >/dev/null 2>&1; then
96   echo "const O_ASYNC = 0" >> ${OUT}
97 fi
98 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
99   echo "const O_CLOEXEC = 0" >> ${OUT}
100 fi
101
102 # The signal numbers.
103 grep '^const _SIG[^_]' gen-sysinfo.go | \
104   grep -v '^const _SIGEV_' | \
105   sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
106
107 # The syscall numbers.  We force the names to upper case.
108 grep '^const _SYS_' gen-sysinfo.go | \
109   sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
110   while read sys; do
111     sup=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
112     echo "const $sup = _$sys" >> ${OUT}
113   done
114
115 # Stat constants.
116 grep '^const _S_' gen-sysinfo.go | \
117   sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
118
119 # Process status constants.
120 grep '^const _W' gen-sysinfo.go |
121   sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
122 # WSTOPPED was introduced in glibc 2.3.4.
123 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go >/dev/null 2>&1; then
124   if grep '^const _WUNTRACED = ' gen-sysinfo.go > /dev/null 2>&1; then
125     echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
126   else
127     echo 'const WSTOPPED = 2' >> ${OUT}
128   fi
129 fi
130 if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
131    && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
132   echo 'const WALL = ___WALL' >> ${OUT}
133 fi
134
135 # Networking constants.
136 grep '^const _\(AF\|SOCK\|SOL\|SO\|IPPROTO\|TCP\|IP\|IPV6\)_' gen-sysinfo.go |
137   sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
138 grep '^const _SOMAXCONN' gen-sysinfo.go |
139   sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
140     >> ${OUT}
141 grep '^const _SHUT_' gen-sysinfo.go |
142   sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
143
144 # The net package requires a definition for IPV6ONLY.
145 if ! grep '^const IPV6_V6ONLY ' ${OUT} >/dev/null 2>&1; then
146   echo "const IPV6_V6ONLY = 0" >> ${OUT}
147 fi
148
149 # pathconf constants.
150 grep '^const __PC' gen-sysinfo.go |
151   sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
152
153 # The epoll constants were picked up by the errno constants, but we
154 # need to be sure the EPOLLRDHUP is defined.
155 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
156   echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
157 fi
158
159 # Ptrace constants.  We don't expose all the PTRACE flags, just the
160 # PTRACE_O_xxx and PTRACE_EVENT_xxx ones.
161 grep '^const _PTRACE_O' gen-sysinfo.go |
162   sed -e 's/^\(const \)_\(PTRACE_O[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
163 grep '^const _PTRACE_EVENT' gen-sysinfo.go |
164   sed -e 's/^\(const \)_\(PTRACE_EVENT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
165 # We need PTRACE_SETOPTIONS and PTRACE_GETEVENTMSG, but they are not
166 # defined in older versions of glibc.
167 if ! grep '^const _PTRACE_SETOPTIONS' ${OUT} > /dev/null 2>&1; then
168   echo "const _PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
169 fi
170 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
171   echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
172 fi
173 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
174   echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
175 fi
176 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
177   echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
178 fi
179 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
180   echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
181 fi
182 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
183   echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
184 fi
185 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
186   echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
187 fi
188 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
189   echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
190 fi
191 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
192   echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
193 fi
194 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
195   echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
196 fi
197 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
198   echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
199 fi
200 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
201   echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
202 fi
203 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
204   echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
205 fi
206 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
207   echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
208 fi
209 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
210   echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
211 fi
212 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
213   echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
214 fi
215 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
216   echo "const _PTRACE_TRACEME = 0" >> ${OUT}
217 fi
218
219 # The registers returned by PTRACE_GETREGS.  This is probably
220 # GNU/Linux specific; it should do no harm if there is no
221 # _user_regs_struct.
222 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
223 if test "$regs" != ""; then
224   regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
225   regs=`echo $regs | sed -e s'/^ *//'`
226   nregs=
227   while test -n "$regs"; do
228     field=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
229     regs=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
230     # Capitalize the first character of the field.
231     f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
232     r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
233     f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
234     field="$f$r"
235     nregs="$nregs $field;"
236   done
237   echo "type PtraceRegs struct {$nregs }" >> ${OUT}
238 fi
239
240 # Some basic types.
241 echo 'type Size_t _size_t' >> ${OUT}
242 echo "type Ssize_t _ssize_t" >> ${OUT}
243 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go > /dev/null 2>&1; then
244   echo "type Offset_t _off64_t" >> ${OUT}
245 else
246   echo "type Offset_t _off_t" >> ${OUT}
247 fi
248 echo "type Mode_t _mode_t" >> ${OUT}
249 echo "type Pid_t _pid_t" >> ${OUT}
250 echo "type Uid_t _uid_t" >> ${OUT}
251 echo "type Gid_t _gid_t" >> ${OUT}
252 echo "type Socklen_t _socklen_t" >> ${OUT}
253
254 # The long type, needed because that is the type that ptrace returns.
255 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
256 if test "$sizeof_long" = "4"; then
257   echo "type _C_long int32" >> ${OUT}
258 elif test "$sizeof_long" = "8"; then
259   echo "type _C_long int64" >> ${OUT}
260 else
261   echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
262   exit 1
263 fi
264
265 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
266 # double is commented by -fdump-go-spec.
267 if grep "^// type _pad128_t" gen-sysinfo.go > /dev/null 2>&1; then
268   echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
269 fi
270 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
271   echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
272 fi
273
274 # The time structures need special handling: we need to name the
275 # types, so that we can cast integers to the right types when
276 # assigning to the structures.
277 timeval=`grep '^type _timeval ' gen-sysinfo.go`
278 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
279 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
280 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
281 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
282 echo $timeval | \
283   sed -e 's/type _timeval /type Timeval /' \
284       -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
285       -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
286 timespec=`grep '^type _timespec ' gen-sysinfo.go`
287 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
288 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
289 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
290 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
291 echo $timespec | \
292   sed -e 's/^type _timespec /type Timespec /' \
293       -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
294       -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
295
296 timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
297 if test "$timestruc" != ""; then
298   timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
299   timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
300   echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
301   echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
302   echo $timestruc | \
303     sed -e 's/^type _timestruc_t /type Timestruc /' \
304         -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
305         -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
306 fi
307
308 # The stat type.
309 grep 'type _stat ' gen-sysinfo.go | \
310   sed -e 's/type _stat/type Stat_t/' \
311       -e 's/st_dev/Dev/' \
312       -e 's/st_ino/Ino/' \
313       -e 's/st_nlink/Nlink/' \
314       -e 's/st_mode/Mode/' \
315       -e 's/st_uid/Uid/' \
316       -e 's/st_gid/Gid/' \
317       -e 's/st_rdev/Rdev/' \
318       -e 's/st_size/Size/' \
319       -e 's/st_blksize/Blksize/' \
320       -e 's/st_blocks/Blocks/' \
321       -e 's/st_atim/Atime/' \
322       -e 's/st_mtim/Mtime/' \
323       -e 's/st_ctim/Ctime/' \
324       -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
325       -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
326       -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
327     >> ${OUT}
328
329 # The directory searching types.
330 grep '^type _dirent ' gen-sysinfo.go | \
331   sed -e 's/type _dirent/type Dirent/' \
332       -e 's/d_name/Name/' \
333       -e 's/]int8/]byte/' \
334       -e 's/d_ino/Ino/' \
335       -e 's/d_off/Off/' \
336       -e 's/d_reclen/Reclen/' \
337       -e 's/d_type/Type/' \
338     >> ${OUT}
339 echo "type DIR _DIR" >> ${OUT}
340
341 # The rusage struct.
342 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
343 if test "$rusage" != ""; then
344   rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
345   rusage=`echo $rusage | sed -e 's/^ *//'`
346   nrusage=
347   while test -n "$rusage"; do
348     field=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
349     rusage=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
350     # Drop the leading ru_, capitalize the next character.
351     field=`echo $field | sed -e 's/^ru_//'`
352     f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
353     r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
354     f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
355     # Fix _timeval _timespec, and _timestruc_t.
356     r=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
357     r=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
358     r=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
359     field="$f$r"
360     nrusage="$nrusage $field;"
361   done
362   echo "type Rusage struct {$nrusage }" >> ${OUT}
363 fi
364
365 # The utsname struct.
366 grep '^type _utsname ' gen-sysinfo.go | \
367     sed -e 's/_utsname/Utsname/' \
368       -e 's/sysname/Sysname/' \
369       -e 's/nodename/Nodename/' \
370       -e 's/release/Release/' \
371       -e 's/version/Version/' \
372       -e 's/machine/Machine/' \
373       -e 's/domainname/Domainname/' \
374     >> ${OUT}
375
376 # The iovec struct.
377 iovec=`grep '^type _iovec ' gen-sysinfo.go`
378 iovec_len=`echo $iovec | sed -n -e 's/^.*iov_len \([^ ]*\);.*$/\1/p'`
379 echo "type Iovec_len_t $iovec_len" >> ${OUT}
380 echo $iovec | \
381     sed -e 's/_iovec/Iovec/' \
382       -e 's/iov_base/Base/' \
383       -e 's/iov_len *[a-zA-Z0-9_]*/Len Iovec_len_t/' \
384     >> ${OUT}
385
386 # The msghdr struct.
387 msghdr=`grep '^type _msghdr ' gen-sysinfo.go`
388 msghdr_controllen=`echo $msghdr | sed -n -e 's/^.*msg_controllen \([^ ]*\);.*$/\1/p'`
389 echo "type Msghdr_controllen_t $msghdr_controllen" >> ${OUT}
390 echo $msghdr | \
391     sed -e 's/_msghdr/Msghdr/' \
392       -e 's/msg_name/Name/' \
393       -e 's/msg_namelen/Namelen/' \
394       -e 's/msg_iov/Iov/' \
395       -e 's/msg_iovlen/Iovlen/' \
396       -e 's/_iovec/Iovec/' \
397       -e 's/msg_control/Control/' \
398       -e 's/msg_controllen *[a-zA-Z0-9_]*/Controllen Msghdr_controllen_t/' \
399       -e 's/msg_flags/Flags/' \
400     >> ${OUT}
401
402 exit $?