该脚本的功能,是通过测试程序获知给定的ngx_type的大小。
1.1 显示提示信息echo $ngx_n "checking for $ngx_type size ...$ngx_c" cat << END >> $NGX_AUTOCONF_ERR ---------------------------------------- checking for $ngx_type size END 1.2 生成计算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_H int main() { printf("%d", sizeof($ngx_type)); return 0; } END 1.3 编译测试程序首先生成编译的命令。
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" 1.4 运行测试程序,得到ngx_size如果NGX_AUTOTEST文件存在且可执行,则设定ngx_size大小。
ngx_size= if [ -x $NGX_AUTOTEST ]; then ngx_size=`$NGX_AUTOTEST` echo " $ngx_size bytes" fi 1.5 删除NGX_AUTOTEST测试程序可执行文件rm -f $NGX_AUTOTEST 1.6 设定整数最大值分别处理 32 位系统的数据大小和 64 位系统的数据大小,设定整数最大值。其他情况作为错误处理。
case $ngx_size in 4) if [ "$ngx_type"="long" ]; then ngx_max_value=2147483647L else ngx_max_value=2147483647 fi ngx_max_len='(sizeof("-2147483648") - 1)' ;; 8) if [ "$ngx_type"="long long" ]; then ngx_max_value=9223372036854775807LL else ngx_max_value=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 1 esac 2 auto/types/typedef通过两个循环,分别处理ngx_type和ngx_types。向objs/ngx_auto_config.h文件写入typedef定义。
2.1 生成测试程序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_H int main() { $ngx_try i = 0; return 0; } END 2.2 编译测试程序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" 2.3 设置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 fi fi 2.4 删除测试程序可执行文件rm -f $NGX_AUTOTEST 2.5 异常情况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_ERR else break fi到此循环就结束了。然后:
if [ $ngx_found = no ]; then echo echo "$0: error: can not define $ngx_type" exit 1 fi 2.6 将typedef语句写入objs/ngx_auto_config.hngx_found是原类型,ngx_type是别名类型。
if [ $ngx_found != yes ]; then echo "typedef $ngx_found $ngx_type;" >> $NGX_AUTO_CONFIG_H fi 3 auto/types/value这与auto/define有些类似,但auto/define表示“设置了 K 值,其值为 V”,与“没有设置 K 值”相对。而auto/types/value是设置任意参数和其值。
cat << END >> $NGX_AUTO_CONFIG_H #ifndef $ngx_param #define $ngx_param $ngx_value #endif END 4 auto/types/uintptr_t 4.1 提示echo $ngx_n "checking for uintptr_t ...$ngx_c" echo >> $NGX_ERR echo "checking for uintptr_t" >> $NGX_ERR 4.2 生成测试程序cat << END > $NGX_AUTOTEST.c #include <sys/types.h> $NGX_INTTYPES_H int main() { uintptr_t i = 0; return 0; } END 4.3 编译测试程序found=no eval "$CC -o $NGX_AUTOTEST $NGX_AUTOTEST.c >> $NGX_ERR 2>&1" if [ -x $NGX_AUTOTEST ]; then echo " uintptr_t found" found=yes else echo $ngx_n " uintptr_t not found" $ngx_c fi 4.4 删除测试程序可执行文件rm $NGX_AUTOTEST* 4.5 无可执行文件时的处理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_H fi-