projtool.pl: do not attempt to check unset error codes
[girocco.git] / src / make-config-h.sh
blobde2f1f4e184c6b4c424a869f4b4ed20cad515007
1 #!/bin/sh
2 set -e
3 die() { >&2 printf '%s: fatal: %s\n' "${0##*/}" "$*"; exit 1; }
4 warn() { >&2 printf '%s: %s\n' "${0##*/}" "$*"; }
6 CC="$1"
7 : "${CC:=cc}"
9 [ "${V#*[!0-9]}" = "$V" ] || V=1
10 V="$(( 0 + ${V:-0} ))"
11 NOERR='2>&1'
12 [ $V -le 1 ] || NOERR=
14 TMPD=
15 cleanup() {
16 [ $V -le 1 ] || [ -z "$TMPD" ] || warn "retaining temp dir $TMPD"
17 [ $V -ge 2 ] || [ -z "$TMPD" ] || rm -rf "$TMPD"
19 trap cleanup EXIT
20 trap 'exit 129' HUP
21 trap 'exit 130' INT
22 trap 'exit 131' QUIT
23 trap 'exit 134' ABRT
24 trap 'exit 141' PIPE
25 trap 'exit 142' ALRM
26 trap 'exit 143' TERM
28 # safe printf-based version of echo
29 echol() { printf '%s\n' "$*"; }
31 # set variable with name $1 to name of new writable temp file with .$2 extension
32 # if $2 omitted default is "c"
33 v_get_temp_ext() {
34 if [ -z "$TMPD" ]; then
35 TMPD="$(mktemp -d /tmp/make-config-h.XXXXXX)" && [ -n "$TMPD" ] && [ -w "$TMPD" ] ||
36 die "could not make temp file directory"
38 _tmpf="$(mktemp "$TMPD/source-XXXXXX")" && [ -n "$_tmpf" ] && [ -w "$_tmpf" ] ||
39 die "could not make temp file"
40 mv "$_tmpf" "$_tmpf.${2:-c}" || die "could not rename temp file to have .${2:-c} extension"
41 eval "$1"'="$_tmpf.${2:-c}"'
44 # attempt to compile "C" source file
45 # on standard input with status as only result
46 # $1 contains eval'd extra compile args
47 testcomp() {
48 v_get_temp_ext _src
49 v_get_temp_ext _obj o
50 cat >"$_src"
51 eval '"$CC" -c -o "$_obj" $1 "$_src" >/dev/null '"$NOERR"
54 # attempt to compile and link "C" source file
55 # on standard input with status as only result
56 # $1 contains eval'd extra compile args
57 # $2 contains eval'd extra link args
58 testlink() {
59 v_get_temp_ext _src
60 v_get_temp_ext _obj o
61 v_get_temp_ext _bin bin
62 cat >"$_src"
63 eval '"$CC" -c -o "$_obj" $1 "$_src" >/dev/null '"$NOERR" &&
64 eval '"$CC" -o "$_bin" $2 "$_obj" >/dev/null '"$NOERR"
67 # $1 is base name of macro to use (i.e. HAVE_$1)
68 # $2 is routine to call (either testcomp or testlink)
69 # $3... are optional options to $2
70 # standard input is source to compile or compile+link
71 emitresult() {
72 _macro="$1"
73 shift
74 [ $V -eq 0 ] || warn "checking for $_macro..."
75 if "$@"; then
76 [ $V -eq 0 ] || warn " ...HAVE_$_macro => yes"
77 echol "#define HAVE_$_macro 1"
78 else
79 [ $V -eq 0 ] || warn " ...HAVE_$_macro => no"
80 echol "#undef HAVE_$_macro"
84 # Check for stdint.h
85 emitresult STDINT_H testcomp <<-IPV6
86 #include <stdint.h>
87 extern uint32_t itworks(uint32_t);
88 IPV6
90 # Check for IPV6 (see RFC 3493)
91 emitresult IPV6 testcomp <<-IPV6
92 #include <sys/types.h>
93 #include <sys/socket.h>
94 #include <netinet/in.h>
95 #if defined(IN6ADDR_ANY_INIT) && defined(AF_INET6)
96 extern void itworks(void);
97 #else
98 #error missing IPV6
99 #endif
100 IPV6
102 # Check for inet_ntop (see RFC 3493)
103 # this is separate in case IPV6 is present but inet_ntop is not
104 emitresult INET_NTOP testlink <<-INET_NTOP
105 #include <sys/types.h>
106 #include <sys/socket.h>
107 #include <netinet/in.h>
108 #include <arpa/inet.h>
109 #include <stdio.h>
110 static void check(void)
112 const char *s;
113 struct in6_addr addr;
114 char str[256];
115 s = inet_ntop(AF_INET6, &addr, str, (socklen_t)sizeof(str));
116 if (s) puts(s);
118 int main() {
119 check();
120 return 0;
122 INET_NTOP
124 # Check for snprintf
125 emitresult SNPRINTF testlink <<-SNPRINTF
126 #include <stdio.h>
127 static void check(void)
129 char str[256];
130 int n = snprintf(str, sizeof(str), "%d", (int)42);
131 printf("%n %s", n, str);
133 int main() {
134 check();
135 return 0;
137 SNPRINTF
139 emitresult RLIMIT testlink <<-RLIMIT
140 #include <limits.h>
141 #include <sys/types.h>
142 #include <sys/time.h>
143 #include <sys/resource.h>
144 static void check(void)
146 int rc;
147 struct rlimit rl;
148 rc = getrlimit(RLIMIT_FSIZE, &rl);
149 rl.rlim_cur = (rlim_t)LONG_MAX;
150 rl.rlim_max = RLIM_INFINITY;
151 rc = setrlimit(RLIMIT_FSIZE, &rl);
153 int main() {
154 check();
155 return 0;
157 RLIMIT