make-config-h.sh: add IPV6 config tests
[girocco.git] / src / make-config-h.sh
blob2bc33c2e1f3b38b0e52ae7deac12f94f8d2ff642
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 IPV6 (see RFC 3493)
85 emitresult IPV6 testcomp <<-IPV6
86 #include <sys/types.h>
87 #include <sys/socket.h>
88 #include <netinet/in.h>
89 #if defined(IN6ADDR_ANY_INIT) && defined(AF_INET6)
90 extern void itworks(void);
91 #else
92 #error missing IPV6
93 #endif
94 IPV6
96 # Check for inet_ntop (see RFC 3493)
97 # this is separate in case IPV6 is present but inet_ntop is not
98 emitresult INET_NTOP testlink <<-INET_NTOP
99 #include <sys/types.h>
100 #include <sys/socket.h>
101 #include <netinet/in.h>
102 #include <arpa/inet.h>
103 #include <stdio.h>
104 static void check(void)
106 const char *s;
107 struct in6_addr addr;
108 char str[256];
109 s = inet_ntop(AF_INET6, &addr, str, (socklen_t)sizeof(str));
110 if (s) puts(s);
112 int main() {
113 check();
114 return 0;
116 INET_NTOP