6.扫描configure.ac,使用aclocal

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

6.1 配置要求

唯一的要求就是在configure.ac文件中加上AM_INIT_AUTOMAKE宏。

下面是几个Automake要求的但是不会被AM_INIT_AUTOMAKE运行的宏

  • AC_CONFIG_FILES
  • AC_OUTPUT
    这两个经常在末尾被调用,指明了哪些文件会被生成。如果以.am结尾则生成.in结尾的文件。不能识别shell变量,需要在AC_SUBST中注册,并且以${xxx}形式引用才可识别。
    AC_SUBST([APIVERSION], …)
    …
    AC_CONFIG_FILES(
      [tests/aclocal-${APIVERSION}:tests/aclocal.in],
      [chmod +x tests/aclocal-${APIVERSION}],
      [APIVERSION=$APIVERSION])
    AC_CONFIG_FILES(
      [tests/automake-${APIVERSION}:tests/automake.in],
      [chmod +x tests/automake-${APIVERSION}])
  • 等等…

6.3 自动生成aclocal.m4

一些宏必须被定义在aclocal.m4中,否则autoconf不会找到他们。

aclocal可以根据configure.ac生成aclocal.m4。首先aclocal扫描所有的.m4文件搜集宏定义,然后扫描configure.ac,所有在第一步找到的宏及其依赖的宏都会被放在aclocal.m4文件中。如果本地没有找到就会在系统层面的目录中寻找。

通常宏放到aclocal.m4中是通过复制整个文件内容来完成的。但是通过命令参数-I来指定相对路径时,使用m4_include代替复制它到aclocal.m4文件中;指定绝对路径时或者是系统层面的目录时,会直接复制过来。故最好使用-I `pwd`/reldir代替-I reldir

acinclude.m4也会被指定放到aclocal.m4文件中,建议不要在新包中使用该文件。

操作aclocal.m4时,aclocal运行autom4te (see Using Autom4te in The Autoconf Manual) 追踪哪些宏被真正的使用了。autom4e也要存在于PATH环境变量中。

6.3.1 aclocal接受的一些选项。

比较重要的两个选项

-I dir

Add the directory dir to the list of directories searched for .m4 files.

–install

Install system-wide third-party macros into the first directory specified with ‘-I dir’ instead of copying them in the output file. Note that this will happen also if dir is an absolute path.

When this option is used, and only when this option is used, aclocal will also honor ‘#serial number’ lines that appear in macros: an M4 file is ignored if there exists another M4 file with the same basename and a greater serial number in the search path (see Serials).

6.3.2 宏搜索路径

默认的搜索路径及顺序:

  • acdir-APIVERSION APIVERSION取决于Automake的版本
  • acdir 此目录适用于第三方.m4文件,automake构建时被配置。通常被配置为${prefix}/share/aclocal/

修改搜素路径方式一:
-I dir1 -I dir2
把会dir加到默认的前面:dir1 -> dir2 -> 默认路径

修改搜素路径方式二:
dirlist

修改搜素路径方式三:
ACLOCAL_PATH

6.3.3 编写自己的aclocal宏

这个技术可以被用于想要支持自己的Autoconf宏被其它程序使用的库(This can be used by libraries that want to supply their own Autoconf macros for use by other programs)。比如,gettext库支持AM_GNU_GETTEXT宏被用于任何使用gettext的包。库安装之后,其宏也被安装,这样aclocal就能找到它。

A macro file’s name should end in .m4. Such files should be installed in $(datadir)/aclocal. This is as simple as writing:

aclocaldir = $(datadir)/aclocal
aclocal_DATA = mymacro.m4 myothermacro.m4

宏文件应该由一系列的quoted AC_DEFUN(see Macro Definitions in The Autoconf Manual)组成。

6.3.4 处理本地宏

Autoconf提供的功能测试不能满足所有的需求。人们需要用自己的宏或者第三方宏补充现有的测试。有以下两种方式:

  • 在acinclude.m4中列出你所有的宏
  • (推荐方法)将每个宏写到自己的文件中然后放到一个目录中(通常是m4),然后在configure.ac中添加变量:AC_CONFIG_MACRO_DIRS([m4])。第三方的宏文件最好也放到这个目录下。

aclocal提供了--install选项来复制系统层面的宏到你本地的宏目录,帮助你解决上面的问题。本地的宏比系统层面的宏有优先权。推荐在每次运行aclocal时使用--install选项。

6.3.5 编号

为了区分一个宏的版本。
位于M4文件的宏定义的前面。
#必须是此行的第一个字符,允许在version之后添加其他字符

    #serial version garbage

使用了alcohol的–install选项的话,aclocal会选择含有#serial的或者编号比较新的那个文件。此编号对应的是整个文件,而不是单一的一个宏。

6.3.6 aclocal的未来

aclocal可能会消失。因为处理M4的宏是Autoconf的任务。

永远不要自己调用aclocal,让它处于autoreconf和Automake的构建规则的控制下。

很多包含有bootstrap或者autogen.sh脚本,实际上他们只是安照正确的顺序调用命令(aclocal, libtoolize, gettextize or autopoint, autoconf, autoheader, and automake),这些工作autoreconf就可以做到。

6.4 Automake提供的Autoconf宏

• Public Macros: Macros that you can use.
• Obsolete Macros: Macros that will soon be removed.
• Private Macros: Macros that you should not use.

公共宏

AM_INIT_AUTOMAKE ([options])

使用空格分隔的automake选项。

    AM_INIT_AUTOMAKE([-Wno-portability])
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值