0%

Linux Automake 3 General ideas

GNU Automake 版本(version 1.16.1, 26 February 2018)

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

Automake 也支持下面的书写方法:

 xs = a.c b.c
 foo_SOURCES = c.c $(xs)

它将使用 a.c, b.cc.c 来代替foo_SOURCES.

Automake 也支持#开头的注释,如果以 ##开头,那么将被Automake忽略,不显示在输出中。

比如一般Makefile.am的第一行可以写成:

1
## Process this file with automake to produce Makefile.in

3.2 限定级别


Automake用于 GNU packages的维护,所以可能根据不同的需求有了一些标准,有些标准可能并不适合所有的开发人员,这里说一下三种限定级别:

foreign
Automake只检查一些必须的文件,比如标准的GNU是需要 NEWS 文件的,而这个选项就不强制,建议新手使用。

gnu
​ Automake的默认级别,会尽可能多地检查,建议极大程度上匹配GNU标准的高手使用。

gnits
​ Automake检查是否与Gnits标准一致,虽然该标准还没有定下来,所以这个暂时没有必要。

3.3 统一的命名方案


Automake变量遵循一个统一的命名方案是的很容易决定程序是如何编译和安装的。

决定 automake 什么需要编译的通常称为primary,比如称为 PROGRAMSprimary决定了一系列将编译和链接的程序。

primary 还有一个前缀,用于决定安装到什么路径。这个可以参考GNU的编码标准,比如bindir简写为bin,其中的dir省略掉。

所有应该写成bin_PROGRAMS,而不是bindir_PROGRAMS

标准目录并不一定满足我们的需求,这时就可以通过自定义参数来解决,比如:我们将把 file.xml 安装到
$(datadir)/xml.

 xmldir = $(datadir)/xml
 xml_DATA = file.xml

前缀 noinst_ 将编译但是不安装,这个参数多用于编译其他包需要目标文件的时候,比如编译创建静态库或者帮助脚本。

前缀 check_ 只有在 make check 的时候才编译,但是不安装。

目前的 primary 由: PROGRAMS, LIBRARIES, LTLIBRARIES,LISP, PYTHON, JAVA, SCRIPTS, DATA, HEADERS, MANSTEXINFOS

一些primaries 还有一些附加前缀用于控制 automake 的行为,比如:dist_, nodist_, nobase_notrans_

3.4 命令行长度不要过长


大部分的类unix系统对命令行参数和环境变量都有长度限制,详细的可以参考http://www.in-ulm.de/~mascheck/various/argmax/ ,这个在 make的时候也存在,所以我们可以采用下述策略:

 data_DATA = file1 ... fileN fileN+1 ... file2N

改写为:

 data_DATA = file1 ... fileN
 data2dir = $(datadir)
 data2_DATA = fileN+1 ... file2N

3.5 变量是如何命名的


命名中除了字母、数组和@符号,其他的都会转换为下划线。比如程序命名为 sniff-glue,最后的变量名为 sniff_glue_PROGRAMS,而不是 sniff-glue_PROGRAMS,再比如一个库 libmumble++.a将被更改为 libmumble___a_SOURCES

其中@符号的保留主要是为了防止后续Autoconf在变量替换时出现问题。

3.6 Variables reserved for the user


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.

3.7 automake可能需要的一些软件包


Automake在生成Makefile 的时候并非单兵作战,它在为了支持多个操作系统的路上需要一帮好朋友软件来支持。如下:

ar-lib:提供给微软的一个lib归档包装器

compile 方便不能同时接受参数选项 -c 和​ -o 的编辑器,

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.

处无为之事,行不言之教;作而弗始,生而弗有,为而弗恃,功成不居!

欢迎关注我的其它发布渠道