解剖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过程,正是由这些编译器相关的自动脚本所保证的。

-

时间: 2024-08-26 16:44:52

解剖Nginx·自动脚本篇(5)编译器相关主脚本的相关文章

解剖Nginx&#183;自动脚本篇(6)编译器名称变量脚本 auto/cc/name

回顾变量 CC 最初是在auto/options脚本中初始化的: CC=${CC:-gcc} 1 C Compiler 的 feature Windows 平台的编译器叫做MSVC,其他平台的都统称为C Compiler. 1.1 获取编译器参数 该脚本并不复杂,首先通过NGX_PLATFORM变量来判断是否是win32(该变量是在auto/options中初始化的),如果是,则: ngx_feature="C compiler" ngx_feature_name= ngx_featu

解剖Nginx&#183;自动脚本篇(4)工具型脚本系列

目录 auto/have 向自动配置头文件追加可用宏定义(objs/ngx_auto_config.h) auto/nohave 向自动配置头文件追加不可用宏定义(objs/ngx_auto_config.h) auto/define 向自动配置脚本追加 K-V 宏定义(objs/ngx_auto_config.h),表示“设置了 K,其值为 V” auto/have_headers 向自动头头文件(objs/ngx_auto_header.h) auto/feature auto/types/

解剖Nginx&#183;自动脚本篇(3)源码相关变量脚本 auto/sources

在configure脚本中,运行完auto/options和auto/init脚本后,接下来就运行auto/soures脚本.这个脚本是为编译做准备的. 目录 核心模块 事件模块 OpenSSL 模块相关变量 事件驱动模块 操作系统相关项 HTTP 模块 邮件模块 Google PerfTools 模块 C++ 测试模块 1 核心模块 1.1 核心模块名称 CORE_MODULES CORE_MODULES变量记录 Nginx 的核心模块,默认包括ngx_core_module.ngx_errl

解剖Nginx&#183;自动脚本篇(7)类型相关脚本系列

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_t

解剖Nginx&#183;自动脚本篇(1)解析配置选项脚本 auto/options

在安装Nginx之前(即运行make脚本之前),首先是进行安装的配置准备,包括环境检查及生成文件.这些工作是由自动脚本完成的.和绝大多数软件一样,Nginx的自动脚本的入口,同样是名为configure的文件. 除了configure,其他的自动脚本都在auto目录下.通过分析configure脚本源码,我们可以看到,configure首先运行了auto目录下的几个自动脚本,如下: . auto/options . auto/init . auto/sources 其中通过运行auto/opti

解剖Nginx&#183;自动脚本篇(2)设置初始变量脚本 auto/init

在configure中运行完auto/options脚本后,接着运行auto/init脚本,其中所做的工作如下. 1 Makefile文件名变量 默认情况下是: objs/Makefile 代码如下: NGX_MAKEFILE=$NGX_OBJS/Makefile 2 源文件名变量 默认情况下是: objs/ngx_modules.c 代码如下: NGX_MODULES_C=$NGX_OBJS/ngx_modules.c 3 头文件名变量 默认情况下是: objs/ngx_auto_header

解剖Nginx&#183;模块开发篇(3)ngx_http_hello_world_module 模块的基本函数实现

还记得我们定义过一个结构体如下吗? typedef struct { ngx_str_t output_words; } ngx_http_hello_world_loc_conf_t; 它就是 HelloWorld 的 location 组件配置,其中有一个字符串成员 output_words. 1 create location 用于 ngx_http_hello_world_module_ctx 中的 location 创建函数: static void* ngx_http_hello_w

解剖Nginx&#183;模块开发篇(1)跑起你的 Hello World 模块!

1 学习 Nginx 模块开发需要有哪些准备? 需要的预备知识不多,有如下几点: 有过一些 C 语言的编程经历: 知道 Nginx 是干嘛的,并有过编写或改写 Nginx 的配置文件的经历. OK,就这两点就够了 :) 好了,那就开始吧~ 2 我们的 HelloWorld 的目标是什么? 我们的目标,就是你在浏览器里输入http://localhost/hello_world时,显示: hello world 当然,为了能够更加自定义一些,我们尝试在hello world后面再显示一个字符串,比

解剖Nginx&#183;模块开发篇(2)ngx_http_hello_world_module 模块基本结构定义

elloWorld 是一个典型的 location 模块.什么是 location 模块?在 Nginx 中,根据作用域,有 main 模块.server 模块.location 模块. 1 模块定义 在 HelloWorld 模块中有一个 ngx_http_hello_world_module 变量,用于定义模块.它是 ngx_module_t 类型.ngx_module_t 是 ngx_module_s 的别名,其定义如下: struct ngx_module_s { ngx_uint_t