[PATCH 1 of 2] auto/type/sizeof: rework autotest to be cross-compilation friendly

Samuel Martin s.martin49 at gmail.com
Sun May 4 06:08:29 UTC 2014


# HG changeset patch
# User Samuel Martin <s.martin49 at gmail.com>
# Date 1398374852 -7200
#      Thu Apr 24 23:27:32 2014 +0200
# Node ID f0f3f5208b12c6afbbdfb55b32c968ebd7206ad5
# Parent  b9553b4b8e670a0231afc0484f23953c0d8b5f22
auto/type/sizeof: rework autotest to be cross-compilation friendly

Rework the sizeof test to do the checks at compile time instead of at
runtime. This way, it does not break when cross-compiling for a
different CPU architecture.

diff -r b9553b4b8e67 -r f0f3f5208b12 auto/types/sizeof
--- a/auto/types/sizeof	Tue Apr 29 22:22:38 2014 +0200
+++ b/auto/types/sizeof	Thu Apr 24 23:27:32 2014 +0200
@@ -14,7 +14,7 @@
 
 ngx_size=
 
-cat << END > $NGX_AUTOTEST.c
+cat << _EOF > $NGX_AUTOTEST.c
 
 #include <sys/types.h>
 #include <sys/time.h>
@@ -25,25 +25,52 @@
 $NGX_INCLUDE_INTTYPES_H
 $NGX_INCLUDE_AUTO_CONFIG_H
 
-int main() {
-    printf("%d", (int) sizeof($ngx_type));
+#if !defined( PASTE)
+#define PASTE2( x, y) x##y
+#define PASTE( x, y)  PASTE2( x, y)
+#endif /* PASTE */
+
+#define SAY_IF_SIZEOF( typename, type, size)   \\
+  static char PASTE( PASTE( PASTE( sizeof_, typename), _is_), size) \\
+    [(sizeof(type) == (size)) ? -1 : 1]
+#define SAY_SIZEOF_END( typename) \\
+  static char PASTE( end_search_for_sizeof_, typename)[-1]
+
+#define SAY_SIZEOF( typename, type) \\
+    SAY_IF_SIZEOF( typename, type, 1); \\
+    SAY_IF_SIZEOF( typename, type, 2); \\
+    SAY_IF_SIZEOF( typename, type, 3); \\
+    SAY_IF_SIZEOF( typename, type, 4); \\
+    SAY_IF_SIZEOF( typename, type, 5); \\
+    SAY_IF_SIZEOF( typename, type, 6); \\
+    SAY_IF_SIZEOF( typename, type, 7); \\
+    SAY_IF_SIZEOF( typename, type, 8); \\
+    SAY_IF_SIZEOF( typename, type, 9); \\
+    SAY_IF_SIZEOF( typename, type, 10); \\
+    SAY_IF_SIZEOF( typename, type, 11); \\
+    SAY_IF_SIZEOF( typename, type, 12); \\
+    SAY_IF_SIZEOF( typename, type, 13); \\
+    SAY_IF_SIZEOF( typename, type, 14); \\
+    SAY_IF_SIZEOF( typename, type, 15); \\
+    SAY_IF_SIZEOF( typename, type, 16); \\
+    SAY_SIZEOF_END( typename)
+
+SAY_SIZEOF(TEST_TYPENAME, TEST_TYPE)
+
+int main(void)
+{
     return 0;
 }
 
-END
+_EOF
 
-
-ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
-          -o $NGX_AUTOTEST $NGX_AUTOTEST.c $NGX_LD_OPT $ngx_feature_libs"
-
-eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
-
-
-if [ -x $NGX_AUTOTEST ]; then
-    ngx_size=`$NGX_AUTOTEST`
-    echo " $ngx_size bytes"
-fi
-
+_ngx_typename=`echo "$ngx_type" | sed 's/ /_/g;s/\*/p/'`
+ngx_size=`$CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
+    -DTEST_TYPENAME="$_ngx_typename" -DTEST_TYPE="$ngx_type" \
+    $NGX_AUTOTEST.c \
+    $NGX_LD_OPT $ngx_feature_libs 2>&1 | \
+    sed -nr "/.*sizeof_${_ngx_typename}_is_([0-9]+).*/ s//\1/p" || \
+    true`
 
 rm -rf $NGX_AUTOTEST*
 



More information about the nginx-devel mailing list