From 8c19cea5e667f325ececdc1678bfddf063af0da2 Mon Sep 17 00:00:00 2001
From: Samuel Martin <s.martin49@gmail.com>
Date: Sun, 1 Jun 2014 16:05:04 +0200
Subject: [PATCH 5/5] auto/unix: make sys_nerr guessing cross-friendly

This patch replaces the default sys_nerr runtest with a test done at
buildtime.

The idea behind this buildtime test is finding the value of the ERR_MAX
macro if defined, or the EHWPOISON (which is currently the last errno)
otherwise.

Signed-off-by: Samuel Martin <s.martin49@gmail.com>
---
 auto/os/sys_nerr | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 auto/unix        | 10 ++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 auto/os/sys_nerr

diff --git a/auto/os/sys_nerr b/auto/os/sys_nerr
new file mode 100644
index 0000000..25e7c22
--- /dev/null
+++ b/auto/os/sys_nerr
@@ -0,0 +1,78 @@
+
+# Copyright (C) Samuel Martin <s.martin49@gmail.com>
+
+
+echo $ngx_n "checking for sys_nerr value...$ngx_c"
+
+# sys_nerr guessing is done using a (very) poor (but working)
+# heuristics, by checking for the value of ERR_MAX if defined, or
+# EHWPOISON otherwise.
+
+cat << END >> $NGX_AUTOCONF_ERR
+
+----------------------------------------
+checking for sys_nerr value
+
+END
+
+ngx_sys_nerr=
+
+cat << _EOF > $NGX_AUTOTEST.c
+
+#include <stdio.h>
+#include <errno.h>
+
+static char sys_nerr_test[ERR_MAX];
+int main(void)
+{
+    return 0;
+}
+
+_EOF
+
+if $CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
+        $NGX_AUTOTEST.c -o $NGX_AUTOTEST \
+        $NGX_LD_OPT $ngx_feature_libs >/dev/null 2>&1 ; then
+    _ngx_max_err_macro=ERR_MAX
+else
+    # the +2 has been empirically found!
+    _ngx_max_err_macro="EHWPOISON + 2"
+fi
+
+cat << _EOF > $NGX_AUTOTEST.c
+
+#include <stdio.h>
+#include <errno.h>
+
+static char sys_nerr_test[(TEST_ERR_MAX == $_ngx_max_err_macro) ? 1 : -1];
+int main(void)
+{
+    return 0;
+}
+
+_EOF
+
+
+ngx_sys_nerr=`for i in $(seq 0 2000) ; do \
+    $CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
+    -DTEST_ERR_MAX="$i" \
+    $NGX_AUTOTEST.c -o $NGX_AUTOTEST \
+    $NGX_LD_OPT $ngx_feature_libs >/dev/null 2>&1 || continue ;\
+    echo $i ; break ; done`
+
+rm -rf $NGX_AUTOTEST*
+
+if test -z $ngx_sys_nerr ; then
+    ngx_size=0
+    ngx_sys_nerr=0
+fi
+
+cat << END >> $NGX_AUTO_CONFIG_H
+
+#ifndef $ngx_feature_name
+#define $ngx_feature_name $ngx_sys_nerr
+#endif
+
+END
+
+echo " $ngx_sys_nerr"
diff --git a/auto/unix b/auto/unix
index b7cc9cb..1a49ba3 100755
--- a/auto/unix
+++ b/auto/unix
@@ -550,6 +550,10 @@ ngx_feature_incs='#include <errno.h>
                   #include <stdio.h>'
 ngx_feature_path=
 ngx_feature_libs=
+
+if false ; then
+# Disabled because only valid for native build.
+
 ngx_feature_test='printf("%d", sys_nerr);'
 . auto/feature
 
@@ -598,6 +602,12 @@ if [ $ngx_found = no ]; then
     . auto/feature
 fi
 
+else
+    # Cross-compilation support
+    . auto/os/sys_nerr
+
+fi
+
 
 ngx_feature="localtime_r()"
 ngx_feature_name="NGX_HAVE_LOCALTIME_R"
-- 
1.9.2

