2011-01-21 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / libgo / mksysinfo.sh
blobed6a16444b2253501c0e324e738d4b08987f71f8
1 #!/bin/sh
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.
7 # Create sysinfo.go.
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.
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.
19 CC=${CC:-gcc}
20 OUT=tmp-sysinfo.go
22 set -e
24 rm -f sysinfo.go
26 rm -f sysinfo.c
27 cat > sysinfo.c <<EOF
28 #include "config.h"
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35 #include <signal.h>
36 #if defined(HAVE_SYSCALL_H)
37 #include <syscall.h>
38 #endif
39 #if defined(HAVE_SYS_EPOLL_H)
40 #include <sys/epoll.h>
41 #endif
42 #if defined(HAVE_SYS_PTRACE_H)
43 #include <sys/ptrace.h>
44 #endif
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/wait.h>
50 #include <sys/un.h>
51 #if defined(HAVE_SYS_USER_H)
52 #include <sys/user.h>
53 #endif
54 #if defined(HAVE_SYS_UTSNAME_H)
55 #include <sys/utsname.h>
56 #endif
57 #include <unistd.h>
58 EOF
60 ${CC} -D_GNU_SOURCE -fdump-go-spec=gen-sysinfo.go -S -o sysinfo.s sysinfo.c
62 echo 'package syscall' > ${OUT}
64 # Get all the consts and types, skipping ones which could not be
65 # represented in Go and ones which we need to rewrite. We also skip
66 # function declarations, as we don't need them here. All the symbols
67 # will all have a leading underscore.
68 grep -v '^// ' gen-sysinfo.go | \
69 grep -v '^func' | \
70 grep -v '^type _timeval ' | \
71 grep -v '^type _timespec ' | \
72 grep -v '^type _timestruc_t ' | \
73 grep -v '^type _epoll_' | \
74 grep -v 'in6_addr' | \
75 grep -v 'sockaddr_in6' | \
76 sed -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
77 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
78 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
79 >> ${OUT}
81 # The errno constants.
82 grep '^const _E' gen-sysinfo.go | \
83 sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
85 # The O_xxx flags.
86 grep '^const _\(O\|F\|FD\)_' gen-sysinfo.go | \
87 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
88 if ! grep '^const O_ASYNC' ${OUT} >/dev/null 2>&1; then
89 echo "const O_ASYNC = 0" >> ${OUT}
91 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
92 echo "const O_CLOEXEC = 0" >> ${OUT}
95 # The signal numbers.
96 grep '^const _SIG[^_]' gen-sysinfo.go | \
97 grep -v '^const _SIGEV_' | \
98 sed -e 's/^\(const \)_\(SIG[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
100 # The syscall numbers. We force the names to upper case.
101 grep '^const _SYS_' gen-sysinfo.go | \
102 sed -e 's/const _\(SYS_[^= ]*\).*$/\1/' | \
103 while read sys; do
104 sup=`echo $sys | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
105 echo "const $sup = _$sys" >> ${OUT}
106 done
108 # Stat constants.
109 grep '^const _S_' gen-sysinfo.go | \
110 sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
112 # Process status constants.
113 grep '^const _W' gen-sysinfo.go |
114 sed -e 's/^\(const \)_\(W[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
115 # WSTOPPED was introduced in glibc 2.3.4.
116 if ! grep '^const _WSTOPPED = ' gen-sysinfo.go >/dev/null 2>&1; then
117 if grep '^const _WUNTRACED = ' gen-sysinfo.go > /dev/null 2>&1; then
118 echo 'const WSTOPPED = _WUNTRACED' >> ${OUT}
119 else
120 echo 'const WSTOPPED = 2' >> ${OUT}
123 if grep '^const ___WALL = ' gen-sysinfo.go >/dev/null 2>&1 \
124 && ! grep '^const _WALL = ' gen-sysinfo.go >/dev/null 2>&1; then
125 echo 'const WALL = ___WALL' >> ${OUT}
128 # Networking constants.
129 grep '^const _\(AF\|SOCK\|SOL\|SO\|IPPROTO\|TCP\|IP\|IPV6\)_' gen-sysinfo.go |
130 sed -e 's/^\(const \)_\([^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
131 grep '^const _SOMAXCONN' gen-sysinfo.go |
132 sed -e 's/^\(const \)_\(SOMAXCONN[^= ]*\)\(.*\)$/\1\2 = _\2/' \
133 >> ${OUT}
134 grep '^const _SHUT_' gen-sysinfo.go |
135 sed -e 's/^\(const \)_\(SHUT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
137 # pathconf constants.
138 grep '^const __PC' gen-sysinfo.go |
139 sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
141 # The epoll constants were picked up by the errno constants, but we
142 # need to be sure the EPOLLRDHUP is defined.
143 if ! grep '^const EPOLLRDHUP' ${OUT} >/dev/null 2>&1; then
144 echo "const EPOLLRDHUP = 0x2000" >> ${OUT}
147 # Ptrace constants. We don't expose all the PTRACE flags, just the
148 # PTRACE_O_xxx and PTRACE_EVENT_xxx ones.
149 grep '^const _PTRACE_O' gen-sysinfo.go |
150 sed -e 's/^\(const \)_\(PTRACE_O[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
151 grep '^const _PTRACE_EVENT' gen-sysinfo.go |
152 sed -e 's/^\(const \)_\(PTRACE_EVENT[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
153 # We need PTRACE_SETOPTIONS and PTRACE_GETEVENTMSG, but they are not
154 # defined in older versions of glibc.
155 if ! grep '^const _PTRACE_SETOPTIONS' ${OUT} > /dev/null 2>&1; then
156 echo "const _PTRACE_SETOPTIONS = 0x4200" >> ${OUT}
158 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
159 echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
161 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
162 echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
164 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
165 echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
167 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
168 echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
170 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
171 echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
173 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
174 echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
176 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
177 echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
179 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
180 echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
182 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
183 echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
185 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
186 echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
188 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
189 echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
191 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
192 echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
194 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
195 echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
197 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
198 echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
200 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
201 echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
203 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
204 echo "const _PTRACE_TRACEME = 0" >> ${OUT}
207 # The registers returned by PTRACE_GETREGS. This is probably
208 # GNU/Linux specific; it should do no harm if there is no
209 # _user_regs_struct.
210 regs=`grep '^type _user_regs_struct struct' gen-sysinfo.go || true`
211 if test "$regs" != ""; then
212 regs=`echo $regs | sed -e 's/type _user_regs_struct struct //' -e 's/[{}]//g'`
213 regs=`echo $regs | sed -e s'/^ *//'`
214 nregs=
215 while test -n "$regs"; do
216 field=`echo $regs | sed -e 's/^\([^;]*\);.*$/\1/'`
217 regs=`echo $regs | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
218 # Capitalize the first character of the field.
219 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
220 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
221 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
222 field="$f$r"
223 nregs="$nregs $field;"
224 done
225 echo "type PtraceRegs struct {$nregs }" >> ${OUT}
228 # Some basic types.
229 echo 'type Size_t _size_t' >> ${OUT}
230 echo "type Ssize_t _ssize_t" >> ${OUT}
231 if grep '^const _HAVE_OFF64_T = ' gen-sysinfo.go > /dev/null 2>&1; then
232 echo "type Offset_t _off64_t" >> ${OUT}
233 else
234 echo "type Offset_t _off_t" >> ${OUT}
236 echo "type Mode_t _mode_t" >> ${OUT}
237 echo "type Pid_t _pid_t" >> ${OUT}
238 echo "type Uid_t _uid_t" >> ${OUT}
239 echo "type Gid_t _gid_t" >> ${OUT}
240 echo "type Socklen_t _socklen_t" >> ${OUT}
242 # The long type, needed because that is the type that ptrace returns.
243 sizeof_long=`grep '^const ___SIZEOF_LONG__ = ' gen-sysinfo.go | sed -e 's/.*= //'`
244 if test "$sizeof_long" = "4"; then
245 echo "type _C_long int32" >> ${OUT}
246 elif test "$sizeof_long" = "8"; then
247 echo "type _C_long int64" >> ${OUT}
248 else
249 echo 1>&2 "mksysinfo.sh: could not determine size of long (got $sizeof_long)"
250 exit 1
253 # Solaris 2 needs _u?pad128_t, but its default definition in terms of long
254 # double is commented by -fdump-go-spec.
255 if grep "^// type _pad128_t" gen-sysinfo.go > /dev/null 2>&1; then
256 echo "type _pad128_t struct { _l [4]int32; }" >> ${OUT}
258 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
259 echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
262 # The time structures need special handling: we need to name the
263 # types, so that we can cast integers to the right types when
264 # assigning to the structures.
265 timeval=`grep '^type _timeval ' gen-sysinfo.go`
266 timeval_sec=`echo $timeval | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
267 timeval_usec=`echo $timeval | sed -n -e 's/^.*tv_usec \([^ ]*\);.*$/\1/p'`
268 echo "type Timeval_sec_t $timeval_sec" >> ${OUT}
269 echo "type Timeval_usec_t $timeval_usec" >> ${OUT}
270 echo $timeval | \
271 sed -e 's/type _timeval /type Timeval /' \
272 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timeval_sec_t/' \
273 -e 's/tv_usec *[a-zA-Z0-9_]*/Usec Timeval_usec_t/' >> ${OUT}
274 timespec=`grep '^type _timespec ' gen-sysinfo.go`
275 timespec_sec=`echo $timespec | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
276 timespec_nsec=`echo $timespec | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
277 echo "type Timespec_sec_t $timespec_sec" >> ${OUT}
278 echo "type Timespec_nsec_t $timespec_nsec" >> ${OUT}
279 echo $timespec | \
280 sed -e 's/^type _timespec /type Timespec /' \
281 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timespec_sec_t/' \
282 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timespec_nsec_t/' >> ${OUT}
284 timestruc=`grep '^type _timestruc_t ' gen-sysinfo.go || true`
285 if test "$timestruc" != ""; then
286 timestruc_sec=`echo $timestruc | sed -n -e 's/^.*tv_sec \([^ ]*\);.*$/\1/p'`
287 timestruc_nsec=`echo $timestruc | sed -n -e 's/^.*tv_nsec \([^ ]*\);.*$/\1/p'`
288 echo "type Timestruc_sec_t $timestruc_sec" >> ${OUT}
289 echo "type Timestruc_nsec_t $timestruc_nsec" >> ${OUT}
290 echo $timestruc | \
291 sed -e 's/^type _timestruc_t /type Timestruc /' \
292 -e 's/tv_sec *[a-zA-Z0-9_]*/Sec Timestruc_sec_t/' \
293 -e 's/tv_nsec *[a-zA-Z0-9_]*/Nsec Timestruc_nsec_t/' >> ${OUT}
296 # The stat type.
297 grep 'type _stat ' gen-sysinfo.go | \
298 sed -e 's/type _stat/type Stat_t/' \
299 -e 's/st_dev/Dev/' \
300 -e 's/st_ino/Ino/' \
301 -e 's/st_nlink/Nlink/' \
302 -e 's/st_mode/Mode/' \
303 -e 's/st_uid/Uid/' \
304 -e 's/st_gid/Gid/' \
305 -e 's/st_rdev/Rdev/' \
306 -e 's/st_size/Size/' \
307 -e 's/st_blksize/Blksize/' \
308 -e 's/st_blocks/Blocks/' \
309 -e 's/st_atim/Atime/' \
310 -e 's/st_mtim/Mtime/' \
311 -e 's/st_ctim/Ctime/' \
312 -e 's/\([^a-zA-Z0-9_]\)_timeval\([^a-zA-Z0-9_]\)/\1Timeval\2/g' \
313 -e 's/\([^a-zA-Z0-9_]\)_timespec\([^a-zA-Z0-9_]\)/\1Timespec\2/g' \
314 -e 's/\([^a-zA-Z0-9_]\)_timestruc_t\([^a-zA-Z0-9_]\)/\1Timestruc\2/g' \
315 >> ${OUT}
317 # The directory searching types.
318 grep '^type _dirent ' gen-sysinfo.go | \
319 sed -e 's/type _dirent/type Dirent/' \
320 -e 's/d_name/Name/' \
321 -e 's/]int8/]byte/' \
322 -e 's/d_ino/Ino/' \
323 -e 's/d_off/Off/' \
324 -e 's/d_reclen/Reclen/' \
325 -e 's/d_type/Type/' \
326 >> ${OUT}
327 echo "type DIR _DIR" >> ${OUT}
329 # The rusage struct.
330 rusage=`grep '^type _rusage struct' gen-sysinfo.go`
331 if test "$rusage" != ""; then
332 rusage=`echo $rusage | sed -e 's/type _rusage struct //' -e 's/[{}]//g'`
333 rusage=`echo $rusage | sed -e 's/^ *//'`
334 nrusage=
335 while test -n "$rusage"; do
336 field=`echo $rusage | sed -e 's/^\([^;]*\);.*$/\1/'`
337 rusage=`echo $rusage | sed -e 's/^[^;]*; *\(.*\)$/\1/'`
338 # Drop the leading ru_, capitalize the next character.
339 field=`echo $field | sed -e 's/^ru_//'`
340 f=`echo $field | sed -e 's/^\(.\).*$/\1/'`
341 r=`echo $field | sed -e 's/^.\(.*\)$/\1/'`
342 f=`echo $f | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ`
343 # Fix _timeval _timespec, and _timestruc_t.
344 r=`echo $r | sed -e s'/ _timeval$/ Timeval/'`
345 r=`echo $r | sed -e s'/ _timespec$/ Timespec/'`
346 r=`echo $r | sed -e s'/ _timestruc_t$/ Timestruc/'`
347 field="$f$r"
348 nrusage="$nrusage $field;"
349 done
350 echo "type Rusage struct {$nrusage }" >> ${OUT}
353 # The utsname struct.
354 grep '^type _utsname ' gen-sysinfo.go | \
355 sed -e 's/_utsname/Utsname/' \
356 -e 's/sysname/Sysname/' \
357 -e 's/nodename/Nodename/' \
358 -e 's/release/Release/' \
359 -e 's/version/Version/' \
360 -e 's/machine/Machine/' \
361 -e 's/domainname/Domainname/' \
362 >> ${OUT}
364 mv -f ${OUT} sysinfo.go
365 exit $?