2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
6 # Generate Go code listing errors and other #defined constant
7 # values (ENAMETOOLONG etc.), by asking the preprocessor
8 # about the definitions.
14 if test -z "$GOARCH" -o -z "$GOOS"; then
15 echo 1>&2 "GOARCH or GOOS not defined in environment"
19 # Check that we are using the new build system if we should
20 if [[ "$GOOS" -eq "linux" ]] && [[ "$GOARCH" != "sparc64" ]]; then
21 if [[ "$GOLANG_SYS_BUILD" -ne "docker" ]]; then
22 echo 1>&2 "In the new build system, mkerrors should not be called directly."
23 echo 1>&2 "See README.md"
30 if [[ "$GOOS" -eq "solaris" ]]; then
31 # Assumes GNU versions of utilities in PATH.
32 export PATH=/usr/gnu/bin:$PATH
38 #define _DARWIN_C_SOURCE
40 #define _DARWIN_USE_64_BIT_INODE
41 #include <sys/types.h>
42 #include <sys/event.h>
43 #include <sys/ptrace.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
48 #include <sys/mount.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/ip.h>
60 #include <sys/types.h>
61 #include <sys/event.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/sysctl.h>
67 #include <sys/ioctl.h>
70 #include <net/if_types.h>
71 #include <net/route.h>
72 #include <netinet/in.h>
74 #include <netinet/ip.h>
75 #include <net/ip_mroute/ip_mroute.h>
79 #include <sys/capability.h>
80 #include <sys/param.h>
81 #include <sys/types.h>
82 #include <sys/event.h>
83 #include <sys/socket.h>
84 #include <sys/sockio.h>
85 #include <sys/sysctl.h>
88 #include <sys/ioctl.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <netinet/in.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip_mroute.h>
97 #include <sys/extattr.h>
100 #define IFT_CARP 0xf8 // IFT_CARP is deprecated in FreeBSD 10
102 #define SIOCAIFADDR _IOW(105, 26, struct oifaliasreq) // ifaliasreq contains if_data
103 #undef SIOCSIFPHYADDR
104 #define SIOCSIFPHYADDR _IOW(105, 70, struct oifaliasreq) // ifaliasreq contains if_data
109 #define _LARGEFILE_SOURCE
110 #define _LARGEFILE64_SOURCE
112 #define _FILE_OFFSET_BITS 64
116 // <sys/ioctl.h> is broken on powerpc64, as it fails to include definitions of
117 // these structures. We just include them copied from <bits/termios.h>.
118 #if defined(__powerpc__)
146 #include <bits/sockaddr.h>
147 #include <sys/epoll.h>
148 #include <sys/eventfd.h>
149 #include <sys/inotify.h>
150 #include <sys/ioctl.h>
151 #include <sys/mman.h>
152 #include <sys/mount.h>
153 #include <sys/prctl.h>
154 #include <sys/stat.h>
155 #include <sys/types.h>
156 #include <sys/time.h>
157 #include <sys/socket.h>
158 #include <sys/xattr.h>
159 #include <linux/if.h>
160 #include <linux/if_alg.h>
161 #include <linux/if_arp.h>
162 #include <linux/if_ether.h>
163 #include <linux/if_tun.h>
164 #include <linux/if_packet.h>
165 #include <linux/if_addr.h>
166 #include <linux/falloc.h>
167 #include <linux/filter.h>
168 #include <linux/fs.h>
169 #include <linux/keyctl.h>
170 #include <linux/netlink.h>
171 #include <linux/perf_event.h>
172 #include <linux/random.h>
173 #include <linux/reboot.h>
174 #include <linux/rtnetlink.h>
175 #include <linux/ptrace.h>
176 #include <linux/sched.h>
177 #include <linux/seccomp.h>
178 #include <linux/sockios.h>
179 #include <linux/wait.h>
180 #include <linux/icmpv6.h>
181 #include <linux/serial.h>
182 #include <linux/can.h>
183 #include <linux/vm_sockets.h>
184 #include <linux/taskstats.h>
185 #include <linux/genetlink.h>
186 #include <net/route.h>
187 #include <asm/termbits.h>
190 #define MSG_FASTOPEN 0x20000000
193 #ifndef PTRACE_GETREGS
194 #define PTRACE_GETREGS 0xc
197 #ifndef PTRACE_SETREGS
198 #define PTRACE_SETREGS 0xd
202 #define SOL_NETLINK 270
206 // SPARC includes this in /usr/include/sparc64-linux-gnu/bits/socket.h
207 // but it is already in bluetooth_linux.go
211 // Certain constants are missing from the fs/crypto UAPI
212 #define FS_KEY_DESC_PREFIX "fscrypt:"
213 #define FS_KEY_DESC_PREFIX_SIZE 8
214 #define FS_MAX_KEY_SIZE 64
218 #include <sys/types.h>
219 #include <sys/param.h>
220 #include <sys/event.h>
221 #include <sys/mman.h>
222 #include <sys/socket.h>
223 #include <sys/sockio.h>
224 #include <sys/sysctl.h>
225 #include <sys/termios.h>
226 #include <sys/ttycom.h>
227 #include <sys/wait.h>
230 #include <net/if_types.h>
231 #include <net/route.h>
232 #include <netinet/in.h>
233 #include <netinet/in_systm.h>
234 #include <netinet/ip.h>
235 #include <netinet/ip_mroute.h>
236 #include <netinet/if_ether.h>
238 // Needed since <sys/param.h> refers to it...
243 #include <sys/types.h>
244 #include <sys/param.h>
245 #include <sys/event.h>
246 #include <sys/mman.h>
247 #include <sys/socket.h>
248 #include <sys/sockio.h>
249 #include <sys/sysctl.h>
250 #include <sys/termios.h>
251 #include <sys/ttycom.h>
252 #include <sys/wait.h>
255 #include <net/if_types.h>
256 #include <net/if_var.h>
257 #include <net/route.h>
258 #include <netinet/in.h>
259 #include <netinet/in_systm.h>
260 #include <netinet/ip.h>
261 #include <netinet/ip_mroute.h>
262 #include <netinet/if_ether.h>
263 #include <net/if_bridge.h>
265 // We keep some constants not supported in OpenBSD 5.5 and beyond for
266 // the promise of compatibility.
267 #define EMUL_ENABLED 0x1
268 #define EMUL_NATIVE 0x2
269 #define IPV6_FAITH 0x1d
270 #define IPV6_OPTIONS 0x1
271 #define IPV6_RTHDR_STRICT 0x1
272 #define IPV6_SOCKOPT_RESERVED1 0x3
273 #define SIOCGIFGENERIC 0xc020693a
274 #define SIOCSIFGENERIC 0x80206939
280 #include <sys/types.h>
281 #include <sys/socket.h>
282 #include <sys/sockio.h>
283 #include <sys/mman.h>
284 #include <sys/wait.h>
285 #include <sys/ioctl.h>
288 #include <net/if_arp.h>
289 #include <net/if_types.h>
290 #include <net/route.h>
291 #include <netinet/in.h>
293 #include <netinet/ip.h>
294 #include <netinet/ip_mroute.h>
299 #include <sys/types.h>
300 #include <sys/file.h>
303 #include <sys/socket.h>
304 #include <netinet/in.h>
305 #include <netinet/ip.h>
306 #include <netinet/ip6.h>
307 #include <netinet/tcp.h>
309 #include <sys/signal.h>
311 #include <sys/resource.h>
316 # Write go tool cgo -godefs input.
321 indirect="includes_$(uname)"
322 echo "${!indirect} $includes"
325 echo 'import "syscall"'
329 # The gcc command line prints all the #defines
330 # it encounters while processing the input
331 echo "${!indirect} $includes" | $CC -x c - -E -dM $ccflags |
333 $1 != "#define" || $2 ~ /\(/ || $3 == "" {next}
335 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
336 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
337 $2 ~ /^(SCM_SRCRT)$/ {next}
338 $2 ~ /^(MAP_FAILED)$/ {next}
339 $2 ~ /^ELF_.*$/ {next}# <asm/elf.h> contains ELF_ARCH, etc.
341 $2 ~ /^EXTATTR_NAMESPACE_NAMES/ ||
342 $2 ~ /^EXTATTR_NAMESPACE_[A-Z]+_STRING/ {next}
348 $2 ~ /^E[A-Z0-9_]+$/ ||
351 $2 ~ /^CI?BAUD(EX)?$/ ||
353 $2 ~ /^V[A-Z0-9]+$/ ||
354 $2 ~ /^CS[A-Z0-9]/ ||
355 $2 ~ /^I(SIG|CANON|CRNL|UCLC|EXTEN|MAXBEL|STRIP|UTF8)$/ ||
357 $2 ~ /^IX(ON|ANY|OFF)$/ ||
358 $2 ~ /^IN(LCR|PCK)$/ ||
359 $2 ~ /(^FLU?SH)|(FLU?SH$)/ ||
360 $2 ~ /^C(LOCAL|READ|MSPAR|RTSCTS)$/ ||
367 $2 == "NOKERNINFO" ||
370 $2 ~ /^O[CNPFPL][A-Z]+[^_][A-Z]+$/ ||
371 $2 ~ /^(NL|CR|TAB|BS|VT|FF)DLY$/ ||
372 $2 ~ /^(NL|CR|T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~ /^KEY_(SPEC|REQKEY_DEFL)_/ ||
414 $2 ~ /^PERF_EVENT_IOC_/ ||
415 $2 ~ /^SECCOMP_MODE_/ ||
417 $2 ~ /^(VM|VMADDR)_/ ||
418 $2 ~ /^(TASKSTATS|TS)_/ ||
420 $2 ~ /^XATTR_(CREATE|REPLACE)/ ||
422 $2 ~ /^W[A-Z0-9]+$/ ||
423 $2 ~ /^BLK[A-Z]*(GET$|SET$|BUF$|PART$|SIZE)/ {printf("\t%s = C.%s\n", $2, $2)}
424 $2 ~ /^__WCOREFLAG$/ {next}
425 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t%s = C.%s\n", substr($2,3), $2)}
433 # Pull out the error names for later.
435 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
436 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
440 # Pull out the signal names for later.
442 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
443 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print $2 }' |
444 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
448 # Again, writing regexps to a file.
449 echo '#include <errno.h>' | $CC -x c - -E -dM $ccflags |
450 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print "^\t" $2 "[ \t]*=" }' |
452 echo '#include <signal.h>' | $CC -x c - -E -dM $ccflags |
453 awk '$1=="#define" && $2 ~ /^SIG[A-Z0-9]+$/ { print "^\t" $2 "[ \t]*=" }' |
454 egrep -v '(SIGSTKSIZE|SIGSTKSZ|SIGRT)' |
457 echo '// mkerrors.sh' "$@"
458 echo '// Code generated by the command above; see README.md. DO NOT EDIT.'
460 echo "// +build ${GOARCH},${GOOS}"
462 go tool cgo -godefs -- "$@" _const.go >_error.out
463 cat _error.out | grep -vf _error.grep | grep -vf _signal.grep
467 cat _error.out | grep -f _error.grep | sed 's/=\(.*\)/= syscall.Errno(\1)/'
473 cat _error.out | grep -f _signal.grep | sed 's/=\(.*\)/= syscall.Signal(\1)/'
476 # Run C program to print error and syscall strings.
486 #define nelem(x) (sizeof(x)/sizeof((x)[0]))
488 enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
507 # Use -E because on some systems bash builtin interprets \n itself.
512 intcmp(const void *a, const void *b)
514 return *(int*)a - *(int*)b;
523 printf("\n\n// Error table\n");
524 printf("var errors = [...]string {\n");
525 qsort(errors, nelem(errors), sizeof errors[0], intcmp);
526 for(i=0; i<nelem(errors); i++) {
528 if(i > 0 && errors[i-1] == e)
530 strcpy(buf, strerror(e));
531 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
532 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
534 printf("\t%d: \"%s\",\n", e, buf);
538 printf("\n\n// Signal table\n");
539 printf("var signals = [...]string {\n");
540 qsort(signals, nelem(signals), sizeof signals[0], intcmp);
541 for(i=0; i<nelem(signals); i++) {
543 if(i > 0 && signals[i-1] == e)
545 strcpy(buf, strsignal(e));
546 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
547 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
549 // cut trailing : number.
550 p = strrchr(buf, ":"[0]);
553 printf("\t%d: \"%s\",\n", e, buf);
563 $CC $ccflags -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.go _error.grep _signal.grep _error.out