6.编写测试

本系列文章均翻译自Autoconf官方文档:Autoconf Manual,github同步项目:question

这些宏检查当前语言的编译系统的输出。他们不会缓存检查结果,也不会打印任何信息。对于特定功能的检查会调用这些宏,然后缓存结果并打印信息。

6.1 语言选择

configure默认为C语言检查编译器及其特性。

AC_LANG (language)
‘C’
    Do compilation tests using CC and CPP and use extension .c for test programs. Use compilation flags: CPPFLAGS with CPP, and both CPPFLAGS and CFLAGS with CC.
‘C++’
    Do compilation tests using CXX and CXXCPP and use extension .C for test programs. Use compilation flags: CPPFLAGS with CXXCPP, and both CPPFLAGS and CXXFLAGS with CXX.
‘Fortran 77’
    Do compilation tests using F77 and use extension .f for test programs. Use compilation flags: FFLAGS.
‘Fortran’
    Do compilation tests using FC and use extension .f (or whatever has been set by AC_FC_SRCEXT) for test programs. Use compilation flags: FCFLAGS.
‘Erlang’
    Compile and execute tests using ERLC and ERL and use extension .erl for test Erlang modules. Use compilation flags: ERLCFLAGS.
‘Objective C’
    Do compilation tests using OBJC and OBJCPP and use extension .m for test programs. Use compilation flags: CPPFLAGS with OBJCPP, and both CPPFLAGS and OBJCFLAGS with OBJC.
‘Objective C++’
    Do compilation tests using OBJCXX and OBJCXXCPP and use extension .mm for test programs. Use compilation flags: CPPFLAGS with OBJCXXCPP, and both CPPFLAGS and OBJCXXFLAGS with OBJCXX.
‘Go’
    Do compilation tests using GOC and use extension .go for test programs. Use compilation flags GOFLAGS. 

6.2 编写测试程序

feed some program with some input, and most of the time, feed a compiler with some source file.

6.2.1 测试程序指导

测试程序必须和真正的程序一样严格。

测试程序不应该写到标准输出。建议:测试成功退出状态为0,否则状态为1,段错误和其它类似错误退出状态为非零值。

如果测试程序需要使用或创建数据文件,让它的名字以conftest开头,比如conftest.data。测试程序运行完毕后或者中断了,configure会清理这些数据文件。

6.2.3 生成源

AC_LANG_CONFTEST (source)

保存source文本到当前测试的源文件:conftest.extensionextension取决于当前语言。源文件也包括所有到此为止的AC_DEFINE的执行结果。source也可以包括引用。

AC_LANG_DEFINES_PROVIDED

为当前语言适配完成的conftest.extension的见证宏,包括所有先前的AC_DEFINE的执行结果。

AC_LANG_SOURCE (source)

将目前为止所有执行到AC_DEFINE的定义扩展到source。此宏包含了AC_LANG_DEFINES_PROVIDED的扩展。

configure.ac:
     AC_INIT([Hello], [1.0], [bug-hello@example.org], [],
             [http://www.example.org/])
     AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
       [Greetings string.])
     AC_LANG([C])
     AC_LANG_CONFTEST(
        [AC_LANG_SOURCE([[const char hw[] = "Hello, World\n";]])])
     gcc -E -dD conftest.c

on a system with gcc installed, results in:
     # 1 "conftest.c"
     
     #define PACKAGE_NAME "Hello"
     #define PACKAGE_TARNAME "hello"
     #define PACKAGE_VERSION "1.0"
     #define PACKAGE_STRING "Hello 1.0"
     #define PACKAGE_BUGREPORT "bug-hello@example.org"
     #define PACKAGE_URL "http://www.example.org/"
     #define HELLO_WORLD "Hello, World\n"
     
     const char hw[] = "Hello, World\n";
AC_LANG_PROGRAM (prologue, body)

扩展为包含prologue的源文件,body作为main函数的方法体。以为使用到了AC_LANG_SOURCE,所有后者的特性也会体现出来。

configure.ac:
     AC_INIT([Hello], [1.0], [bug-hello@example.org], [],
             [http://www.example.org/])
     AC_DEFINE([HELLO_WORLD], ["Hello, World\n"],
       [Greetings string.])
     AC_LANG_CONFTEST(
     [AC_LANG_PROGRAM([[const char hw[] = "Hello, World\n";]],
                      [[fputs (hw, stdout);]])])
     gcc -E -dD conftest.c

on a system with gcc installed, results in:
     # 1 "conftest.c"
     
     #define PACKAGE_NAME "Hello"
     #define PACKAGE_TARNAME "hello"
     #define PACKAGE_VERSION "1.0"
     #define PACKAGE_STRING "Hello 1.0"
     #define PACKAGE_BUGREPORT "bug-hello@example.org"
     #define PACKAGE_URL "http://www.example.org/"
     #define HELLO_WORLD "Hello, World\n"
     
     const char hw[] = "Hello, World\n";
     int
     main ()
     {
     fputs (hw, stdout);
       ;
       return 0;
     }

6.5 运行链接

为了检查库、函数、全局变量,configure会尝试编译和链接使用到的程序。

AC_LINK_IFELSE (input, [action-if-true], [action-if-false])

input中运行编译器及其flags和的当前语言的连接器,成功的话执行shell命令action-if-true,否则执行action-if-false

AC_MSG_CHECKING([whether $CC works])
AC_LINK_IFELSE([AC_LANG_PROGRAM()],AC_MSG_RESULT([yes]),
             [AC_MSG_RESULT([no])
              AC_MSG_ERROR([Cannot build a 32-bit program, you need to install 32-bit development libraries.])])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值