解剖Nginx·自动脚本篇(7)类型相关脚本系列
该脚本的功能,是通过测试程序获知给定的ngx_type
的大小。
echo $ngx_n "checking for $ngx_type size ...$ngx_c"cat << END >> $NGX_AUTOCONF_ERR----------------------------------------checking for $ngx_type sizeEND
ngx_type
的测试程序cat << END > $NGX_AUTOTEST.c#include <sys/types.h>#include <sys/time.h>$NGX_INCLUDE_UNISTD_H#include <signal.h>#include <sys/resource.h>$NGX_INCLUDE_INTTYPES_H$NGX_INCLUDE_AUTO_CONFIG_Hint main() { printf("%d", sizeof($ngx_type)); return 0;}END
首先生成编译的命令。
ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS \ -o $NGX_AUTOTEST $NGX_AUTOTEST.c $NGX_LD_OPT $ngx_feature_libs"
注意-o $NGX_AUTOTEST
是生成可执行文件,然后执行编译。
eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
ngx_size
如果NGX_AUTOTEST
文件存在且可执行,则设定ngx_size
大小。
ngx_size=if [ -x $NGX_AUTOTEST ]; then ngx_size=`$NGX_AUTOTEST` echo " $ngx_size bytes"fi
NGX_AUTOTEST
测试程序可执行文件rm -f $NGX_AUTOTEST
分别处理 32 位系统的数据大小和 64 位系统的数据大小,设定整数最大值。其他情况作为错误处理。
case $ngx_size in 4) if [ "$ngx_type"="long" ]; then ngx_maxvalue=2147483647L else ngx_maxvalue=2147483647 fi ngx_max_len='(sizeof("-2147483648") - 1)' ;; 8) if [ "$ngx_type"="long long" ]; then ngx_maxvalue=9223372036854775807LL else ngx_maxvalue=9223372036854775807L fi ngx_max_len='(sizeof("-9223372036854775808") - 1)' ;; *) echo echo "$0: error: can not detect $ngx_type size" echo "----------" >> $NGX_AUTOCONF_ERR cat $NGX_AUTOTEST.c >> $NGX_AUTOCONF_ERR echo "----------" >> $NGX_AUTOCONF_ERR echo $ngx_test >> $NGX_AUTOCONF_ERR echo "----------" >> $NGX_AUTOCONF_ERR exit 1esac
通过两个循环,分别处理ngx_type
和ngx_types
。向objs/ngx_auto_config.h
文件写入typedef
定义。
cat << END > $NGX_AUTOTEST.c#include <sys/types.h>#include <signal.h>#include <sys/socket.h>#include <sys/time.h>#include <sys/resource.h>#include <netinet/in.h>$NGX_INCLUDE_INTTYPES_Hint main() { $ngx_try i = 0; return 0;}END
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"
ngx_found
值if [ -x $NGX_AUTOTEST ]; then if [ $ngx_try = $ngx_type ]; then echo " found" ngx_found=yes else echo ", $ngx_try used" ngx_found=$ngx_try fifi
rm -f $NGX_AUTOTEST
if [ $ngx_found = no ]; then echo $ngx_n " $ngx_try not found$ngx_c" echo "----------" >> $NGX_AUTOCONF_ERR cat $NGX_AUTOTEST.c >> $NGX_AUTOCONF_ERR echo "----------" >> $NGX_AUTOCONF_ERR echo $ngx_test >> $NGX_AUTOCONF_ERR echo "----------" >> $NGX_AUTOCONF_ERRelse breakfi
到此循环就结束了。然后:
if [ $ngx_found = no ]; then echo echo "$0: error: can not define $ngx_type" exit 1fi
typedef
语句写入objs/ngx_auto_config.h
ngx_found
是原类型,ngx_type
是别名类型。
if [ $ngx_found != yes ]; then echo "typedef $ngx_found $ngx_type;" >> $NGX_AUTO_CONFIG_Hfi
这与auto/define
有些类似,但auto/define
表示“设置了 K 值,其值为 V”,与“没有设置 K 值”相对。而auto/types/value
是设置任意参数和其值。
cat << END >> $NGX_AUTO_CONFIG_H#ifndef $ngx_param#define $ngx_param $ngx_value#endifEND
echo $ngx_n "checking for uintptr_t ...$ngx_c"echo >> $NGX_ERRecho "checking for uintptr_t" >> $NGX_ERR
cat << END > $NGX_AUTOTEST.c#include <sys/types.h>$NGX_INTTYPES_Hint main() { uintptr_t i = 0; return 0;}END
found=noeval "$CC -o $NGX_AUTOTEST $NGX_AUTOTEST.c >> $NGX_ERR 2>&1"if [ -x $NGX_AUTOTEST ]; then echo " uintptr_t found" found=yeselse echo $ngx_n " uintptr_t not found" $ngx_cfi
rm $NGX_AUTOTEST*
if [ $found = no ]; then found="uint`expr 8 \* $ngx_ptr_size`_t" echo ", $found used" echo "typedef $found uintptr_t;" >> $NGX_AUTO_CONFIG_H echo "typedef $found intptr_t;" | sed -e 's/u//g' >> $NGX_AUTO_CONFIG_Hfi
-
转载请注明来自“柳大·Poechant的CSDN博客”:blog.CSDN.net/Poechany
-