解剖Nginx·自动脚本篇,5编译器相关主脚本

在 Nginx 的自动脚本中,auto/cc目录下的所有脚本都是用于编译器相关配置使用的。Nginx的出色跨平台性(Linux、Darwin、Solaris、Win32 等)就有这些脚本的贡献。该目录下包含如下脚本:

目录

  • conf:主脚本,配置编译器的基本属性,并根据系统的编译器环境引用不同的脚本。
  • name:与编译器名称相关的处理逻辑在该脚本中。
  • gcc:GNU C 编译器的 Specified 配置。
  • sunc:Sun C 编译器的 Specified 配置。
  • acc:HP ANSI C++ 编译器的 Specified 配置。
  • bcc:Borland C++ 编译器的 Specified 配置。
  • ccc:Compaq C 编译器的 Specified 配置。
  • icc:Intel C++ 编译器的 Specified 配置。
  • msvc:Microsoft Visual C++ 编译器的 Specified 配置。
  • owc:Open Watcom C 编译器的 Specified 配置。

4.1 LINK 变量

LINK变量为:

LINK="\$(CC)"

4.2 编译选项变量

有 include、编译、输出目标文件、输出可执行文件。

ngx_include_opt="-I "
ngx_compile_opt="-c"
ngx_objout="-o "
ngx_binout="-o "

opt表示optionobj表示objectbin表示binary

4.3 文件扩展名变量

目标文件扩展名、可执行文件扩展名。

ngx_objext="o"
ngx_binext=

ext表示extension

4.4 ngx_long_start 和 ngx_long_end

相关变量为:

ngx_long_start=
ngx_long_end=

这两个变量是在编译选项中使用的,与平台相关。在这里做初始化。

4.4.1 ngx_long_start

  • 在 bcc 中,设置为'@&&|
  • 在 msvc 中,设置为@<<
  • 在 owc 中,设置为''

4.4.2 ngx_long_end

  • 在 bcc 中,设置为|
  • 在 msvc 中,设置为<<
  • 在 owc 中,设置为''

4.5 一些符号的配置

相关变量为:

ngx_regex_dirsep="\/"
ngx_dirsep='/'
  • ngx_regex_dirsep:正则表达式中的目录分隔符
  • ngx_dirsep:目录分隔符

dir表示directorysep表示seperatorregex表示regular expression

ngx_regex_cont=' \\\
    '
ngx_cont=' \
    '
ngx_tab=' \
        '
ngx_spacer=

ngx_long_regex_cont=$ngx_regex_cont
ngx_long_cont=$ngx_cont

4.6 引用 auto/cc/name 脚本

. auto/cc/name

4.7 平台相关性配置

if test -n "$CFLAGS"; then
    CC_TEST_FLAGS="$CFLAGS $NGX_CC_OPT"
    case $NGX_CC_NAME in
        ccc)
            # Compaq C V6.5-207
            ngx_include_opt="-I"
        ;;
    esac
else
    case $NGX_CC_NAME in
        gcc)
            # gcc 2.7.2.3, 2.8.1, 2.95.4, egcs-1.1.2
            #     3.0.4, 3.1.1, 3.2.3, 3.3.2, 3.3.3, 3.3.4, 3.4.0, 3.4.2
            #     4.0.0, 4.0.1, 4.1.0
            . auto/cc/gcc
        ;;
        icc)
            # Intel C++ compiler 7.1, 8.0, 8.1
            . auto/cc/icc
        ;;
        sunc)
            # Sun C 5.7 Patch 117837-04 2005/05/11
            . auto/cc/sunc
        ;;
        ccc)
            # Compaq C V6.5-207
            . auto/cc/ccc
        ;;
        acc)
            # aCC: HP ANSI C++ B3910B A.03.55.02
            . auto/cc/acc
        ;;
        msvc*)
            # MSVC++ 6.0 SP2, MSVC++ Toolkit 2003
            . auto/cc/msvc
        ;;
        owc)
            # Open Watcom C 1.0, 1.2
            . auto/cc/owc
        ;;
        bcc)
            # Borland C++ 5.5
            . auto/cc/bcc
        ;;
    esac
    CC_TEST_FLAGS="$CC_TEST_FLAGS $NGX_CC_OPT"
fi

4.8 feature

auto/feature脚本,已经在《精读 Nginx·自动脚本篇(4)工具型脚本系列》中介绍了。所以feature相关的代码很容易理解。

if test -n "$NGX_LD_OPT"; then
    ngx_feature=--with-ld-opt=\"$NGX_LD_OPT\"
    ngx_feature_name=
    ngx_feature_run=no
    ngx_feature_incs=
    ngx_feature_path=
    ngx_feature_libs=
    ngx_feature_test=
    . auto/feature

    if [ $ngx_found = no ]; then
        echo $0: error: the invalid value in --with-ld-opt=\"$NGX_LD_OPT\"
        echo
        exit 1
    fi
fi

在运行configure的时候,--with-ld-opt指定了NGX_LD_OPT,然后设置feature相关变量。其他一些feature设置如下。

4.8.1 gcc builtin atomic operations 相关 feature

ngx_feature="gcc builtin atomic operations"
ngx_feature_name=NGX_HAVE_GCC_ATOMIC
ngx_feature_run=yes
ngx_feature_incs=
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test="long  n = 0;
                  if (!__sync_bool_compare_and_swap(&n, 0, 1))
                      return 1;
                  if (__sync_fetch_and_add(&n, 1) != 1)
                      return 1;
                  if (n != 2)
                      return 1;
                  __sync_synchronize();"
. auto/feature

4.8.2 C99 variadic macros 相关 feature

if [ "$NGX_CC_NAME" = "ccc" ]; then
    echo "checking for C99 variadic macros ... disabled"
else
    ngx_feature="C99 variadic macros"
    ngx_feature_name="NGX_HAVE_C99_VARIADIC_MACROS"
    ngx_feature_run=yes
    ngx_feature_incs="#include <stdio.h>
#define var(dummy, ...)  sprintf(__VA_ARGS__)"
    ngx_feature_path=
    ngx_feature_libs=
    ngx_feature_test="char  buf[30]; buf[0] = '0';
                      var(0, buf, \"%d\", 1);
                      if (buf[0] != '1') return 1"
    . auto/feature
 fi

4.8.3 gcc variadic macros 相关 feature

ngx_feature="gcc variadic macros"
ngx_feature_name="NGX_HAVE_GCC_VARIADIC_MACROS"
ngx_feature_run=yes
ngx_feature_incs="#include <stdio.h>
#define var(dummy, args...)  sprintf(args)"
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test="char  buf[30]; buf[0] = '0';
                  var(0, buf, \"%d\", 1);
                  if (buf[0] != '1') return 1"
. auto/feature

4.9 结语

编译器相关配置的其他脚本,就不细致分析了,这对 Nginx 的整体自动脚本体系的学习理解并无多大裨益。不过如果你想了解这些内容,会有一些好处,对编写跨平台的软件的自动脚本很有帮助。能让我们在不同系统的机器上享受美妙的configure过程,正是由这些编译器相关的自动脚本所保证的。

-