Fix PR47707
[official-gcc.git] / libgo / mksysinfo.sh
blobc326e3f3e4be16d9e03709d9dc03ec464617f517
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.c
25 cat > sysinfo.c <<EOF
26 #include "config.h"
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
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
67 ${CC} -fdump-go-spec=gen-sysinfo.go -std=gnu99 -S -o sysinfo.s sysinfo.c
69 echo 'package syscall' > ${OUT}
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}
88 # The errno constants.
89 grep '^const _E' gen-sysinfo.go | \
90 sed -e 's/^\(const \)_\(E[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
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}
98 if ! grep '^const O_CLOEXEC' ${OUT} >/dev/null 2>&1; then
99 echo "const O_CLOEXEC = 0" >> ${OUT}
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}
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
115 # Stat constants.
116 grep '^const _S_' gen-sysinfo.go | \
117 sed -e 's/^\(const \)_\(S_[^= ]*\)\(.*\)$/\1\2 = _\2/' >> ${OUT}
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}
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}
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}
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}
149 # pathconf constants.
150 grep '^const __PC' gen-sysinfo.go |
151 sed -e 's/^\(const \)__\(PC[^= ]*\)\(.*\)$/\1\2 = __\2/' >> ${OUT}
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}
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}
170 if ! grep '^const PTRACE_O_TRACESYSGOOD' ${OUT} > /dev/null 2>&1; then
171 echo "const PTRACE_O_TRACESYSGOOD = 0x1" >> ${OUT}
173 if ! grep '^const PTRACE_O_TRACEFORK' ${OUT} > /dev/null 2>&1; then
174 echo "const PTRACE_O_TRACEFORK = 0x2" >> ${OUT}
176 if ! grep '^const PTRACE_O_TRACEVFORK' ${OUT} > /dev/null 2>&1; then
177 echo "const PTRACE_O_TRACEVFORK = 0x4" >> ${OUT}
179 if ! grep '^const PTRACE_O_TRACECLONE' ${OUT} > /dev/null 2>&1; then
180 echo "const PTRACE_O_TRACECLONE = 0x8" >> ${OUT}
182 if ! grep '^const PTRACE_O_TRACEEXEC' ${OUT} > /dev/null 2>&1; then
183 echo "const PTRACE_O_TRACEEXEC = 0x10" >> ${OUT}
185 if ! grep '^const PTRACE_O_TRACEVFORKDONE' ${OUT} > /dev/null 2>&1; then
186 echo "const PTRACE_O_TRACEVFORKDONE = 0x20" >> ${OUT}
188 if ! grep '^const PTRACE_O_TRACEEXIT' ${OUT} > /dev/null 2>&1; then
189 echo "const PTRACE_O_TRACEEXIT = 0x40" >> ${OUT}
191 if ! grep '^const PTRACE_O_MASK' ${OUT} > /dev/null 2>&1; then
192 echo "const PTRACE_O_MASK = 0x7f" >> ${OUT}
194 if ! grep '^const _PTRACE_GETEVENTMSG' ${OUT} > /dev/null 2>&1; then
195 echo "const _PTRACE_GETEVENTMSG = 0x4201" >> ${OUT}
197 if ! grep '^const PTRACE_EVENT_FORK' ${OUT} > /dev/null 2>&1; then
198 echo "const PTRACE_EVENT_FORK = 1" >> ${OUT}
200 if ! grep '^const PTRACE_EVENT_VFORK' ${OUT} > /dev/null 2>&1; then
201 echo "const PTRACE_EVENT_VFORK = 2" >> ${OUT}
203 if ! grep '^const PTRACE_EVENT_CLONE' ${OUT} > /dev/null 2>&1; then
204 echo "const PTRACE_EVENT_CLONE = 3" >> ${OUT}
206 if ! grep '^const PTRACE_EVENT_EXEC' ${OUT} > /dev/null 2>&1; then
207 echo "const PTRACE_EVENT_EXEC = 4" >> ${OUT}
209 if ! grep '^const PTRACE_EVENT_VFORK_DONE' ${OUT} > /dev/null 2>&1; then
210 echo "const PTRACE_EVENT_VFORK_DONE = 5" >> ${OUT}
212 if ! grep '^const PTRACE_EVENT_EXIT' ${OUT} > /dev/null 2>&1; then
213 echo "const PTRACE_EVENT_EXIT = 6" >> ${OUT}
215 if ! grep '^const _PTRACE_TRACEME' ${OUT} > /dev/null 2>&1; then
216 echo "const _PTRACE_TRACEME = 0" >> ${OUT}
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}
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}
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}
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
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}
270 if grep "^// type _upad128_t" gen-sysinfo.go > /dev/null 2>&1; then
271 echo "type _upad128_t struct { _l [4]uint32; }" >> ${OUT}
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}
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}
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}
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}
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}
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}
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}
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}
402 exit $?