blob: 669cd85a50ff5ac67e64df04f6c00c90cb9803b3 [file] [log] [blame]
Russ Coxfd1add22009-11-01 11:13:27 -08001#!/bin/bash
Russ Cox602a4462009-06-01 22:14:57 -07002# 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.
5
Russ Coxfd1add22009-11-01 11:13:27 -08006# Generate Go code listing errors and other #defined constant
7# values (ENAMETOOLONG etc.), by asking the preprocessor
8# about the definitions.
Russ Cox602a4462009-06-01 22:14:57 -07009
Kai Backmane5c884f2009-10-01 06:55:01 -070010case "$GOARCH" in
11arm)
12 GCC=arm-gcc
13 ;;
14*)
15 GCC=gcc
16 ;;
17esac
18
Russ Coxfd1add22009-11-01 11:13:27 -080019uname=$(uname)
Russ Cox602a4462009-06-01 22:14:57 -070020
Russ Coxfd1add22009-11-01 11:13:27 -080021includes_Linux='
22#define _LARGEFILE_SOURCE
23#define _LARGEFILE64_SOURCE
24#define _FILE_OFFSET_BITS 64
25#define _GNU_SOURCE
26
27#include <sys/types.h>
28#include <sys/epoll.h>
29#include <linux/ptrace.h>
30#include <linux/wait.h>
31'
32
33includes_Darwin='
34#define __DARWIN_UNIX03 0
35#define KERNEL
36#define _DARWIN_USE_64_BIT_INODE
37#include <sys/wait.h>
38#include <sys/event.h>
39'
40
41includes='
42#include <sys/types.h>
43#include <fcntl.h>
44#include <dirent.h>
45#include <sys/socket.h>
46#include <netinet/in.h>
47#include <netinet/tcp.h>
48#include <errno.h>
49#include <sys/signal.h>
50#include <signal.h>
51'
Russ Cox602a4462009-06-01 22:14:57 -070052
53# Write godefs input.
54(
Russ Coxfd1add22009-11-01 11:13:27 -080055 indirect="includes_$(uname)"
56 echo "${!indirect} $includes"
57 echo
Russ Cox602a4462009-06-01 22:14:57 -070058 echo 'enum {'
Russ Coxfd1add22009-11-01 11:13:27 -080059
60 # The gcc command line prints all the #defines
61 # it encounters while processing the input
62 echo "${!indirect} $includes" | $GCC -x c - -E -dM |
63 awk '
64 $1 != "#define" || $2 ~ /\(/ {next}
65
66 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
67
68 $2 ~ /^E[A-Z0-9_]+$/ ||
69 $2 ~ /^SIG[^_]/ ||
70 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|TCP|EVFILT|EV)_/ ||
71 $2 == "SOMAXCONN" ||
72 $2 == "NAME_MAX" ||
73 $2 ~ /^(O|F|FD|NAME|S|PTRACE)_/ ||
74 $2 ~ /^W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", $2, $2)}
75
76 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", substr($2,3), $2)}
77
78 {next}
79 ' | sort
80
Russ Cox602a4462009-06-01 22:14:57 -070081 echo '};'
Russ Coxfd1add22009-11-01 11:13:27 -080082) >_const.c
83
84# Pull out just the error names for later.
85errors=$(
86 echo '#include <errno.h>' | $GCC -x c - -E -dM |
87 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }'
88)
Russ Cox602a4462009-06-01 22:14:57 -070089
Rob Pikecd324982009-08-13 13:22:37 -070090echo '// mkerrors.sh' "$@"
Russ Cox602a4462009-06-01 22:14:57 -070091echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
92echo
Russ Coxfd1add22009-11-01 11:13:27 -080093godefs -gsyscall "$@" _const.c
Russ Cox602a4462009-06-01 22:14:57 -070094
95# Run C program to print error strings.
96(
Russ Cox9feee912009-08-24 11:03:23 -070097 /bin/echo "
Russ Cox602a4462009-06-01 22:14:57 -070098#include <stdio.h>
99#include <errno.h>
100#include <ctype.h>
101#include <string.h>
102
103#define nelem(x) (sizeof(x)/sizeof((x)[0]))
104
105enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
106
107int errors[] = {
108"
109 for i in $errors
110 do
Russ Cox9feee912009-08-24 11:03:23 -0700111 /bin/echo ' '$i,
Russ Cox602a4462009-06-01 22:14:57 -0700112 done
113
Russ Cox9feee912009-08-24 11:03:23 -0700114 # Use /bin/echo to avoid builtin echo,
115 # which interprets \n itself
116 /bin/echo '
Russ Cox602a4462009-06-01 22:14:57 -0700117};
118
119int
120main(void)
121{
122 int i, j, e;
123 char buf[1024];
124
125 printf("\n\n// Error table\n");
126 printf("var errors = [...]string {\n");
127 for(i=0; i<nelem(errors); i++) {
128 e = errors[i];
129 for(j=0; j<i; j++)
130 if(errors[j] == e) // duplicate value
131 goto next;
132 strcpy(buf, strerror(e));
133 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
134 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
135 buf[0] += a - A;
136 printf("\t%d: \"%s\",\n", e, buf);
137 next:;
138 }
139 printf("}\n\n");
Russ Coxfd1add22009-11-01 11:13:27 -0800140 return 0;
Russ Cox602a4462009-06-01 22:14:57 -0700141}
142
143'
144) >_errors.c
145
Russ Coxfd1add22009-11-01 11:13:27 -0800146gcc -o _errors _errors.c && ./_errors && rm -f _errors.c _errors _const.c