Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License.”
3 Automake的设计理念
本节主要讲讲Automake是如何工作的。
3.1 一般操作
Automake 读取Makefile.am 并生成文件Makefile.in。
Makefile.am定义的变量和规则会指导Automake生成相关的代码。
比如bin_PROGRAMS 变量定义了如何编译连接的规则。
Makefile.am 大部分定义的变量和规则会拷贝到生成的文件。
GNU make支持追加操作符 +=。
Automake在解析时并不是特别聪明,所以最好不要编写一些奇形怪状的语句。
特别是最好不要使用 <TAB> ,如果使用很多复杂的宏也会引起诸多问题:
% cat Makefile.am
$(FOO:=x): bar
% automake
Makefile.am:1: bad characters in variable name '$(FOO'
Makefile.am:1: ':='-style assignments are not portable
Some Makefile variables are reserved by the GNU Coding Standards for the use of the “user”—the person building the package. For instance, CFLAGS is one such variable.
Sometimes package developers are tempted to set user variables such as CFLAGS because it appears to make their job easier. However, the package itself should never set a user variable, particularly not to include switches that are required for proper compilation of the package. Since these variables are documented as being for the package builder, that person rightfully expects to be able to override any of these variables at build time.
To get around this problem, Automake introduces an automake-specific shadow variable for each user flag variable. (Shadow variables are not introduced for variables like CC, where they would make no sense.) The shadow variable is named by prepending AM_ to the user variable’s name. For instance, the shadow variable for YFLAGS is AM_YFLAGS. The package maintainer—that is, the author(s) of the Makefile.am and configure.ac files—may adjust these shadow variables however necessary.
config.guess config.sub These two programs compute the canonical triplets for the given build, host, or target architecture. These programs are updated regularly to support new architectures and fix probes broken by changes in new kernel versions. Each new release of Automake comes with up-to-date copies of these programs. If your copy of Automake is getting old, you are encouraged to fetch the latest versions of these files from https://savannah.gnu.org/git/?group=config before making a release.
depcomp This program understands how to run a compiler so that it will generate not only the desired output but also dependency information that is then used by the automatic dependency tracking feature
install-sh This is a replacement for the install program that works on platforms where install is unavailable or unusable.
mdate-sh This script is used to generate a version.texi file. It examines a file and prints some date information about it.
missing This wraps a number of programs that are typically only required by maintainers. If the program in question does not exist, or seems to old, missing will print an informative warning before failing out, to provide the user with more context and information.
mkinstalldirs This script used to be a wrapper around mkdir -p, which is not portable. Now we prefer to use install-sh -d when configure finds that mkdir -p does not work, this makes one less script to distribute. For backward compatibility mkinstalldirs is still used and distributed when automake finds it in a package. But it is no longer installed automatically, and it should be safe to remove it.
py-compile This is used to byte-compile Python scripts.
test-driver This implements the default test driver offered by the parallel testsuite harness.
texinfo.tex Not a program, this file is required for make dvi, make ps and make pdf to work when Texinfo sources are in the package. The latest version can be downloaded from https://www.gnu.org/software/texinfo/.
ylwrap This program wraps lex and yacc to rename their output files. It also ensures that, for instance, multiple yacc instances can be invoked in a single directory in parallel.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License.”
2 Autotools 简介
如果以前没有接触过Automake,你可能大概也许知道它是 Autotools 工具集的一部分。
如果你已经开始研究诸如 configure,configure.ac,Makefile.in,Makefile.am,aclocal.m等,那么你可能已经看到他们被声明为 generated by Autoconf or Automake。
[HOST1] ~ % mkdir /tmp/amh && cd /tmp/amh [HOST1] /tmp/amh % /nfs/src/amhello-1.0/configure --prefix /usr ... [HOST1] /tmp/amh % make && sudo make install ...
On the second host, however, we need only install the architecture-specific files.
1 2 3 4 5
[HOST2] ~ % mkdir /tmp/amh && cd /tmp/amh [HOST2] /tmp/amh % /nfs/src/amhello-1.0/configure --prefix /usr ... [HOST2] /tmp/amh % make && sudo make install-exec ...
In packages that have installation checks, it would make sense to run make installcheck to verify that the package works correctly despite the apparent partial installation.
~/amhello-1.0 % ./configure --build i686-pc-linux-gnu --host i586-mingw32msvc checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for gawk... gawk checking whether make sets $(MAKE)... yes checking for i586-mingw32msvc-strip... i586-mingw32msvc-strip checking for i586-mingw32msvc-gcc... i586-mingw32msvc-gcc checking for C compiler default output file name... a.exe checking whether the C compiler works... yes checking whether we are cross compiling... yes checking for suffix of executables... .exe checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether i586-mingw32msvc-gcc accepts -g... yes checking for i586-mingw32msvc-gcc option to accept ANSI C... ... ~/amhello-1.0 % make ... ~/amhello-1.0 % cd src; file hello.exe hello.exe: MS Windows PE 32-bit Intel 80386 console executable not relocatable
2.2.9 安装时重命名程序
可以使用 configure的编译选项,在安装之前加上特定的前缀或后缀来避免与系统程序冲突。
--program-prefix=PREFIX:增加前缀
--program-suffix=SUFFIX:增加后缀
--program-transform-name=PROGRAM:安装时运行sed PROGRAM
比如下面的例子将安装 hello到/usr/local/bin/test-hello。
1 2 3 4 5 6
~/amhello-1.0 % ./configure --program-prefix test- ... ~/amhello-1.0 % make ... ~/amhello-1.0 % sudo make install ...
2.2.10 使用DESTDIR编译二进制包
单纯的GNU Build System的 make install和 make uninstall接口并不能完全满足系统管理员的部署,比如我们希望在编译的时候安装指定路径,而在make的时候因为可能没有root权限暂时放在一个临时目录,然后再部署到其他机器。
比如像下面这个例子就很好的诠释了这个方法。
1 2 3 4 5 6 7 8 9 10 11
~/amhello-1.0 % ./configure --prefix /usr ... ~/amhello-1.0 % make ... ~/amhello-1.0 % make DESTDIR=$HOME/inst install ... ~/amhello-1.0 % cd ~/inst ~/inst % find . -type f -print > ../files.lst ~/inst % tar zcvf ~/amhello-1.0-i686.tar.gz `cat ../files.lst` ./usr/bin/hello ./usr/share/doc/amhello/README
make dist收集源码及必须的文件创建一个压缩包,名为 PACKAGE-VERSION.tar.gz。
虽然也能满足大部分的功能,但是还有一个更有用的命令 make distcheck,这个命令也会像 make dist一样创建压缩包 PACKAGE-VERSION.tar.gz,并会做各种各样的检查以确保压缩包没有问题:
• 将运行所有的软件包命令,比如 make, make check, make install和 make installcheck, 甚至是 make dist,来确保软件发布没有问题 • 测试只读源码树的VPATH编译 • 确保 make clean, make distclean, and make uninstall不忽略任何文件 • 检查 DESTDIR安装是否工作
所有的这些操作都在一个临时目录进行,所以并不需要root权限。
如果 make distcheck失败了,意味着其中的一个场景没有满足,可能不会影响程序的使用,不过最好根据失败的提示进一步改进得到一个完美的发布包。
~/amhello % ./configure checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for gawk... no checking for mawk... mawk checking whether make sets $(MAKE)... yes checking for gcc... gcc checking for C compiler default output file name... a.out checking whether the C compiler works... yes checking whether we are cross compiling... no checking for suffix of executables... checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking for style of include used by make... GNU checking dependency style of gcc... gcc3 configure: creating ./config.status config.status: creating Makefile config.status: creating src/Makefile config.status: creating config.h config.status: executing depfiles commands
此时我们就可以看到已经创建了 Makefile, src/Makefile和 config.h。
1 2 3 4 5 6 7 8 9 10 11
~/amhello % make ... ~/amhello % src/hello Hello World! This is amhello 1.0. ~/amhello % make distcheck ... ============================================= amhello-1.0 archives ready for distribution: amhello-1.0.tar.gz =============================================
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License.”