解剖 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 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.h
ngx_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
-
轉載請註明來自“柳大·Poechant的CSDN博客”:blog.CSDN.net/Poechant