解剖Nginx自动脚本篇(7)类型相关脚本系列
解剖 Nginx自动脚本篇(7)类型相关脚本系列- Author: Poechant
- Blog:?blog.CSDN.net/Poechant
- Email: zhongchao.ustc#gmail.com (#->@)
- Date: March 12th, 2012
- Copyright ? 柳大Poechant
1 auto/types/sizeof
该脚本的功能,是通过测试程序获知给定的ngx_type的大小。
1.1 显示提示信息echo $ngx_n "checking for $ngx_type size ...$ngx_c"cat << END >> $NGX_AUTOCONF_ERR----------------------------------------checking for $ngx_type sizeEND
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_Hint 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_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
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_Hint 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 fifi
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_ERRelse breakfi
到此循环就结束了。然后:
if [ $ngx_found = no ]; then echo echo "$0: error: can not define $ngx_type" exit 1fi
2.6 将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
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#endifEND
4 auto/types/uintptr_t4.1 提示echo $ngx_n "checking for uintptr_t ...$ngx_c"echo >> $NGX_ERRecho "checking for uintptr_t" >> $NGX_ERR
4.2 生成测试程序cat << END > $NGX_AUTOTEST.c#include <sys/types.h>$NGX_INTTYPES_Hint main() { uintptr_t i = 0; return 0;}END
4.3 编译测试程序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
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_Hfi
-
转载请注明来自“柳大Poechant的CSDN博客”:blog.CSDN.net/Poechany
-
1 auto/types/sizeof
该脚本的功能,是通过测试程序获知给定的ngx_type的大小。
1.1 显示提示信息echo $ngx_n "checking for $ngx_type size ...$ngx_c"cat << END >> $NGX_AUTOCONF_ERR----------------------------------------checking for $ngx_type sizeEND
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_Hint main() { printf("%d", sizeof($ngx_type)); return 0;}END
1.3 编译测试程序
echo $ngx_n "checking for $ngx_type size ...$ngx_c"cat << END >> $NGX_AUTOCONF_ERR----------------------------------------checking for $ngx_type sizeENDngx_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;}END1.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"fi1.5 删除NGX_AUTOTEST测试程序可执行文件rm -f $NGX_AUTOTEST
1.6 设定整数最大值
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 1esac2 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_Hint 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 fifi
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_ERRelse breakfi
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;}ENDngx_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 fifi
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_ERRelse breakfi
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 fifirm -f $NGX_AUTOTEST2.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_ERRelse breakfi
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 1fi2.6 将typedef语句写入objs/ngx_auto_config.h
ngx_found是原类型,ngx_type是别名类型。
if [ $ngx_found != yes ]; then echo "typedef $ngx_found $ngx_type;" >> $NGX_AUTO_CONFIG_Hfi3 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#endifEND4 auto/types/uintptr_t4.1 提示echo $ngx_n "checking for uintptr_t ...$ngx_c"echo >> $NGX_ERRecho "checking for uintptr_t" >> $NGX_ERR
4.2 生成测试程序cat << END > $NGX_AUTOTEST.c#include <sys/types.h>$NGX_INTTYPES_Hint main() { uintptr_t i = 0; return 0;}END
4.3 编译测试程序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
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_Hfi
echo $ngx_n "checking for uintptr_t ...$ngx_c"echo >> $NGX_ERRecho "checking for uintptr_t" >> $NGX_ERR4.2 生成测试程序cat << END > $NGX_AUTOTEST.c#include <sys/types.h>$NGX_INTTYPES_Hint main() { uintptr_t i = 0; return 0;}END
4.3 编译测试程序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
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_Hfi
cat << END > $NGX_AUTOTEST.c#include <sys/types.h>$NGX_INTTYPES_Hint main() { uintptr_t i = 0; return 0;}ENDfound=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_cfi4.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_Hfi
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
-