automake

摘自:http://www.gnu.org/software/automake/manual/automake.html

1,标准Makefile目标

make all 构建程序、库、文档等等(同make)
make install 安装程序到目录
make install-strip 同make install,但strip debug infos
make uninstall make install的反操作。
make clean 删除BUILD树
make distclean 在clean的基础上再删除configure生成的文件
make check 运行testsuite,如果有的话
make installcheck 检查安装的程序或者库,如果有的话
make dist 创建package-version.tar.gz包。(configure.ac AC_INIT定义)

2,平行的构建树(A.K.A VPATH Builds)

source tree的根目录是包含configure文件的目录。包含整个源代码文件。

build tree的根目录是运行configure的目录,他包含所有生成的目标文件,程序和库等等其他衍生出来的文件。如果configure程序在其存在的目录下运行,build tree和source tree是一样的,但是我们很快就会发现生成的文件,尤其是目标文件会使得目录树看起来比较杂乱。因此,我们build tree和source tree能够分开。其实这只需要创建一个build目录,然后运行执行configure就行了:

~ % tar zxf ~/amhello-1.0.tar.gz
~ % cd amhello-1.0
~/amhello-1.0 % mkdir build && cd build
~/amhello-1.0/build % ../configure
…
~/amhello-1.0/build % make
…

VPATH Build还有一些有用的特性,比如可以对不同的配置用不同的VPATH, 举个例子,对于需要调试的构建可以创建一个debug目录:

~ % tar zxf ~/amhello-1.0.tar.gz
~ % cd amhello-1.0
~/amhello-1.0 % mkdir debug optim && cd debug
~/amhello-1.0/debug % ../configure CFLAGS='-g -O0'
…
~/amhello-1.0/debug % make

而对于优化后的配置可以创建一个optim目录:

~/amhello-1.0/debug % cd ../optim
~/amhello-1.0/optim % ../configure CFLAGS='-O3 -fomit-frame-pointer'
…
~/amhello-1.0/optim % make
…

  

3,使用Autotools

3.1 amhello目录结构

amhello --|-- src --|--------------|---main.c

     --|-- README               |--Makefile.am

     --|-- Makefile.am

     --|--configure.ac (这里先不介绍configure.ac可以有autoscan生成的过程)

3.2 文件内容

~/amhello % cat src/main.c
#include <config.h>
#include <stdio.h>

int
main (void)
{
  puts ("Hello World!");
  puts ("This is " PACKAGE_STRING ".");
  return 0;
}
~/amhello % cat README
This is a demonstration package for GNU Automake.
Type 'info Automake' to read the Automake manual.
~/amhello % cat src/Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = main.c
~/amhello % cat Makefile.am
SUBDIRS = src
dist_doc_DATA = README
~/amhello % cat configure.ac
AC_INIT([amhello], [1.0], [bug-automake@gnu.org])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([
 Makefile
 src/Makefile
])
AC_OUTPUT

运行autoreconf --install, 会生成configure文件。运行configure之后可以运行make, make distcheck等命令。

3.3 configure.ac文件注释

configure.ac文件会被autconf和automake用到。前者用来生成configure,后者用来生成Makefile.in。configure.ac包含了一系列的M4宏。这些宏会被最终展开到configure脚本中。

具体怎么写configure.ac文件可以查考configure.ac.

在configure.ac文件中,AC_打头的是Autoconf的宏。AM_打头的是Automake的宏

第一二行分别初始化Autoconf和Automake. AC_INIT以程序的包名,版本和bug-report地址为参数。AM_INIT_AUTOMAKE是一个列表,包含automake的选项

AC_PROG_CC使得configure去查找C编译器,并设置CC。Automake生成的src/Makefile.in文件中会用CC取构建hello。

AC_CONFIG_HEADERS([config.h])使得configure脚本创建config.h,这个文件收集"#define"宏。这些"#define"宏由configure.ac中的其他宏定义。比如在3.2的例子中AC_INIT定义了一些宏:

…
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "bug-automake@gnu.org"

/* Define to the full name and version of this package. */
#define PACKAGE_STRING "amhello 1.0"
…

这个可以在config.h中看到。

AC_CONFIG_FILES宏申明了一组要输出的文件,这些文件由configure脚本使用*.in模板生成。Makefile需要列在这个表中。

AC_OUTPUT是结束指令。

注意:当开始一个新的项目是,建议从这样一个简单的configure.ac开始,然后逐渐添加其他项。  

3.4 Makefile.am文件注释

Makefile.am的语法规则和Makefile一样。automake会把Makefile.am整个复制到Makefile.in中。有一些特殊的标示符有特殊的含义,在automake中这些称为primary。

_PROGRAMS

_SCRIPTS

_DATA

_LIBRARIES

primary之前的前缀也有特殊的含义,比如bin告诉automake生成的程序需要安装到bindir目录下去。PROGRAMS后的每一个目标文件都需要一个prog_SOURCES项,用来定义需要生成的PROGRAMS的文件列表。同时这些文件会被打包到程序的发布包中。

首先顶层的Makefile.am:

顶层的Makefile.am主要包含SUBDIRS项。

底下的Makefile.am,包含多层目录则需要每一层目录包含SUBDIR项。当需要编程生成程序时需要在文件中定义primary.

4, strictness

foreign: 只检查必要项。

gnu: 完整的GNU标准项。默认设置

gnits: Gnits标准。

 

 

 

 

  

  

  

  

 

  

 

posted on 2016-08-29 23:53  cudac  阅读(713)  评论(0编辑  收藏  举报