blob: 05c8e8da34a40696d3af3876702518ac7b566596 [file] [log] [blame]
Devon H. O'Dell553be842009-11-14 15:29:09 -08001#!/usr/bin/env 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
Russ Cox0b126c12009-12-01 16:53:43 -080010unset LANG
11export LC_ALL=C
12export LC_CTYPE=C
13
Kai Backman534dbc72010-08-23 13:25:14 +030014GCC=gcc
Kai Backmane5c884f2009-10-01 06:55:01 -070015
Russ Coxfd1add22009-11-01 11:13:27 -080016uname=$(uname)
Russ Cox602a4462009-06-01 22:14:57 -070017
Russ Coxfd1add22009-11-01 11:13:27 -080018includes_Linux='
19#define _LARGEFILE_SOURCE
20#define _LARGEFILE64_SOURCE
21#define _FILE_OFFSET_BITS 64
22#define _GNU_SOURCE
23
24#include <sys/types.h>
25#include <sys/epoll.h>
26#include <linux/ptrace.h>
27#include <linux/wait.h>
28'
29
30includes_Darwin='
31#define __DARWIN_UNIX03 0
32#define KERNEL
33#define _DARWIN_USE_64_BIT_INODE
34#include <sys/wait.h>
35#include <sys/event.h>
36'
37
Devon H. O'Dell553be842009-11-14 15:29:09 -080038includes_FreeBSD='
39#include <sys/wait.h>
40#include <sys/event.h>
41'
42
Russ Coxfd1add22009-11-01 11:13:27 -080043includes='
44#include <sys/types.h>
45#include <fcntl.h>
46#include <dirent.h>
47#include <sys/socket.h>
48#include <netinet/in.h>
Russ Cox8fbe8be2010-03-30 14:32:59 -070049#include <netinet/ip.h>
50#include <netinet/ip6.h>
Russ Coxfd1add22009-11-01 11:13:27 -080051#include <netinet/tcp.h>
52#include <errno.h>
53#include <sys/signal.h>
54#include <signal.h>
55'
Russ Cox602a4462009-06-01 22:14:57 -070056
Russ Cox7906e312010-04-29 23:34:06 -070057ccflags=""
58next=false
59for i
60do
61 if $next; then
62 ccflags="$ccflags $i"
63 next=false
64 elif [ "$i" = "-f" ]; then
65 next=true
66 fi
67done
68
Russ Cox602a4462009-06-01 22:14:57 -070069# Write godefs input.
70(
Russ Coxfd1add22009-11-01 11:13:27 -080071 indirect="includes_$(uname)"
72 echo "${!indirect} $includes"
73 echo
Russ Cox602a4462009-06-01 22:14:57 -070074 echo 'enum {'
Russ Coxfd1add22009-11-01 11:13:27 -080075
76 # The gcc command line prints all the #defines
77 # it encounters while processing the input
Russ Cox7906e312010-04-29 23:34:06 -070078 echo "${!indirect} $includes" | $GCC -x c - -E -dM $ccflags |
Russ Coxfd1add22009-11-01 11:13:27 -080079 awk '
80 $1 != "#define" || $2 ~ /\(/ {next}
81
Russ Cox23bf4082010-05-03 11:11:01 -070082 $2 ~ /^E([ABCD]X|[BIS]P|[SD]I|S|FL)$/ {next} # 386 registers
Russ Coxfd1add22009-11-01 11:13:27 -080083 $2 ~ /^(SIGEV_|SIGSTKSZ|SIGRT(MIN|MAX))/ {next}
84
85 $2 ~ /^E[A-Z0-9_]+$/ ||
86 $2 ~ /^SIG[^_]/ ||
Russ Cox8fbe8be2010-03-30 14:32:59 -070087 $2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|EVFILT|EV|SHUT|PROT|MAP)_/ ||
Russ Coxfd1add22009-11-01 11:13:27 -080088 $2 == "SOMAXCONN" ||
89 $2 == "NAME_MAX" ||
90 $2 ~ /^(O|F|FD|NAME|S|PTRACE)_/ ||
91 $2 ~ /^W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", $2, $2)}
Russ Cox23bf4082010-05-03 11:11:01 -070092 $2 ~ /^__WCOREFLAG$/ {next}
Russ Coxfd1add22009-11-01 11:13:27 -080093 $2 ~ /^__W[A-Z0-9]+$/ {printf("\t$%s = %s,\n", substr($2,3), $2)}
94
95 {next}
96 ' | sort
97
Russ Cox602a4462009-06-01 22:14:57 -070098 echo '};'
Russ Coxfd1add22009-11-01 11:13:27 -080099) >_const.c
100
101# Pull out just the error names for later.
102errors=$(
Russ Cox7906e312010-04-29 23:34:06 -0700103 echo '#include <errno.h>' | $GCC -x c - -E -dM $ccflags |
Russ Cox0b126c12009-12-01 16:53:43 -0800104 awk '$1=="#define" && $2 ~ /^E[A-Z0-9_]+$/ { print $2 }' |
105 sort
Russ Coxfd1add22009-11-01 11:13:27 -0800106)
Russ Cox602a4462009-06-01 22:14:57 -0700107
Rob Pikecd324982009-08-13 13:22:37 -0700108echo '// mkerrors.sh' "$@"
Russ Cox602a4462009-06-01 22:14:57 -0700109echo '// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT'
110echo
Russ Cox23bf4082010-05-03 11:11:01 -0700111godefs -c $GCC "$@" -gsyscall "$@" _const.c
Russ Cox602a4462009-06-01 22:14:57 -0700112
113# Run C program to print error strings.
114(
Russ Cox9feee912009-08-24 11:03:23 -0700115 /bin/echo "
Russ Cox602a4462009-06-01 22:14:57 -0700116#include <stdio.h>
117#include <errno.h>
118#include <ctype.h>
119#include <string.h>
120
121#define nelem(x) (sizeof(x)/sizeof((x)[0]))
122
123enum { A = 'A', Z = 'Z', a = 'a', z = 'z' }; // avoid need for single quotes below
124
125int errors[] = {
126"
127 for i in $errors
128 do
Russ Cox9feee912009-08-24 11:03:23 -0700129 /bin/echo ' '$i,
Russ Cox602a4462009-06-01 22:14:57 -0700130 done
131
Russ Cox9feee912009-08-24 11:03:23 -0700132 # Use /bin/echo to avoid builtin echo,
133 # which interprets \n itself
134 /bin/echo '
Russ Cox602a4462009-06-01 22:14:57 -0700135};
136
137int
138main(void)
139{
140 int i, j, e;
141 char buf[1024];
142
143 printf("\n\n// Error table\n");
144 printf("var errors = [...]string {\n");
145 for(i=0; i<nelem(errors); i++) {
146 e = errors[i];
147 for(j=0; j<i; j++)
148 if(errors[j] == e) // duplicate value
149 goto next;
150 strcpy(buf, strerror(e));
151 // lowercase first letter: Bad -> bad, but STREAM -> STREAM.
152 if(A <= buf[0] && buf[0] <= Z && a <= buf[1] && buf[1] <= z)
153 buf[0] += a - A;
154 printf("\t%d: \"%s\",\n", e, buf);
155 next:;
156 }
157 printf("}\n\n");
Russ Coxfd1add22009-11-01 11:13:27 -0800158 return 0;
Russ Cox602a4462009-06-01 22:14:57 -0700159}
160
161'
162) >_errors.c
163
Russ Cox23bf4082010-05-03 11:11:01 -0700164$GCC $ccflags -static -o _errors _errors.c && $GORUN ./_errors && rm -f _errors.c _errors _const.c