GNU AutoTools 简介

23
GNU AutoTools In a GNU AutoTools In a Nutshell Nutshell GNU AutoTools GNU AutoTools 简简 简简 Automake, Autoconf and Libtool Automake, Autoconf and Libtool 2004-10-09 2004-10-09

description

GNU AutoTools 简介. Automake, Autoconf and Libtool 黄 颂 2004-10-09. GNU Package 的典型安装. FAQ 0039 @ smth 下载源代码包 foo-1.0.tar.gz tar xvzf foo-1.0.tar.gz cd foo-1.0 ./configure make (su) make install. 幕后英雄 —— GNU Auto Tools : autoconf, automake, libtool, autoscan, autoheader……. - PowerPoint PPT Presentation

Transcript of GNU AutoTools 简介

Page 1: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

GNU AutoToolsGNU AutoTools 简介 简介 Automake, Autoconf and LibtoolAutomake, Autoconf and Libtool

黄 颂 黄 颂 2004-10-092004-10-09

Page 2: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

GNU PackageGNU Package 的典型安装的典型安装 FAQ 0039 @ smthFAQ 0039 @ smth

下载源代码包下载源代码包 foo-1.0.tar.gzfoo-1.0.tar.gz tar xvzf foo-1.0.tar.gztar xvzf foo-1.0.tar.gz cd foo-1.0cd foo-1.0 ./configure./configure makemake (su) make install(su) make install

问题 1 :配置脚本 configure 是怎么生成的?

问题 2 : configure 脚本怎么知道该如何生成 Makefile

幕后英雄——GNU Auto Tools :

autoconf, automake, libtool, autoscan, autoheader……

Page 3: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Makefile’s way to developMakefile’s way to develop

三元组三元组 (Target, Dependency, Command)(Target, Dependency, Command) Target : Dep1 Dep2Target : Dep1 Dep2

<TAB>Cmd1 <TAB>Cmd1

<TAB>Cmd2<TAB>Cmd2

[Blank Line] [Blank Line]

.s1 .s2 :.s1 .s2 :<TAB>Cmd1 <TAB>Cmd1

$(var), $<, $@$(var), $<, $@ …………

Page 4: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Makefile’s Big Trouble?Makefile’s Big Trouble?

GNUGNU 的的 MakefileMakefile 规范相当复杂,随着项目规范相当复杂,随着项目复杂性的增加,要写出一个正确而高效的复杂性的增加,要写出一个正确而高效的MakefileMakefile 相当困难,对新手而言更是如此。相当困难,对新手而言更是如此。

越是复杂的软件越有可能有自己独特的越是复杂的软件越有可能有自己独特的 builbuildd 方法,怎么办?每次都重新学习?!方法,怎么办?每次都重新学习?!

不同的类不同的类 UnixUnix 操作系统之间存在着差异,操作系统之间存在着差异,移植性如何保证?需要大量手工编写的脚移植性如何保证?需要大量手工编写的脚本和条件编译。本和条件编译。

Page 5: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

All in One Solution?All in One Solution?

GNU Auto ToolsGNU Auto Tools 是上个世纪是上个世纪 9090 年代开始年代开始发展起来的一系列辅助开发、打安装包的发展起来的一系列辅助开发、打安装包的自动化工具。自动化工具。

各种工具分别开发,但是协同工作的很好。各种工具分别开发,但是协同工作的很好。比如比如 autoconf, automake, libtoolautoconf, automake, libtool 等等。等等。

但是,连开发者自己也承认,这套工具虽但是,连开发者自己也承认,这套工具虽然好用,但是学习曲线很陡峭。然好用,但是学习曲线很陡峭。

困难在于:一套新的思路和习惯,一套自困难在于:一套新的思路和习惯,一套自定义的宏和规则,使用者的交流定义的宏和规则,使用者的交流

Page 6: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Hello World – Step by StepHello World – Step by Step 1. hello.c1. hello.c#include <stdio.h>#include <stdio.h>int main() { printf(“Hello World!\n”); return 0; }int main() { printf(“Hello World!\n”); return 0; } 2. Makefile.am2. Makefile.ambin_PROGRAMS = hellobin_PROGRAMS = hellohello_SOURCES = hello.chello_SOURCES = hello.c 3. configure.in3. configure.inAC_INIT(hello.c)AC_INIT(hello.c)AM_INIT_AUTOMAKE(hello, 0.1)AM_INIT_AUTOMAKE(hello, 0.1)AC_PROG_CCAC_PROG_CCAC_OUTPUT(Makefile)AC_OUTPUT(Makefile) 4. aclocal 4. aclocal 5. autoconf5. autoconf 6. automake --add-missing --foreign6. automake --add-missing --foreign 7. ./configure7. ./configure 8. make (make install 8. make (make install 安装,安装, make dist make dist 产生安装包产生安装包 ))

Page 7: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Hello World! Hello World! 演示演示———— 如何杀鸡用牛刀如何杀鸡用牛刀

Page 8: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

来龙去脉来龙去脉

autoscanconfigure.scan configure.in

Makefile.am Makefile.in

Makefile

automake

editaclocal.m4

aclocal

configure

autoconf

Page 9: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

configure.in(1)configure.in(1)

是是 configureconfigure 脚本的输入文件,为了解决在不同脚本的输入文件,为了解决在不同 uunixnix 变种之间移植程序的问题:库名可能不同,应变种之间移植程序的问题:库名可能不同,应用程序名可能不同,结构和常量的定义可能不同用程序名可能不同,结构和常量的定义可能不同…………

configureconfigure 脚本完成脚本完成 autoconfautoconf 与与 automakeautomake 的初始的初始化工作,为不同的平台定义相应的宏,检测并指化工作,为不同的平台定义相应的宏,检测并指定适当的程序名、库名、结构和常量名等等,指定适当的程序名、库名、结构和常量名等等,指定要为哪些目录输出定要为哪些目录输出 MakefileMakefile 文件。总之,为编文件。总之,为编译程序做好一切准备工作。译程序做好一切准备工作。

Page 10: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

configure.in(2)configure.in(2)

configure.inconfigure.in 的八股文的八股文1.1. 基本初始化部分:包括基本初始化部分:包括 AC_INIT (AC_INIT ( 必须第必须第

一个出现一个出现 ), AM_INIT_AUTOMAKE(), AM_INIT_AUTOMAKE( 程序程序包名,版本号包名,版本号 ), AC_CONFIG_HEADER), AC_CONFIG_HEADER

2.2. 可选宏:如可选宏:如 AC_ARG_ENABLEAC_ARG_ENABLE

3.3. 检测某些程序的存在性检测某些程序的存在性4.4. 检查程序用到的库检查程序用到的库

Page 11: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

configure.in(3)configure.in(3)

5.5. 检查某些头文件是否存在。检查某些头文件是否存在。6.6. 检查检查 Typedefs and structuresTypedefs and structures 。。7.7. 检查检查 FunctionsFunctions 。。8.8. 指定在哪些目录输出指定在哪些目录输出 MakefileMakefile 。。Seems troublesome, mmm? Don’t worry, Seems troublesome, mmm? Don’t worry,

autoscan will do most of the work for autoscan will do most of the work for you.you.

Page 12: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Makefile.am(1)Makefile.am(1) 一种比一种比 MakefileMakefile 更高层次的规则。只指定要生成更高层次的规则。只指定要生成

什么目标,它由什么源文件生成,要安装到什么什么目标,它由什么源文件生成,要安装到什么目录。目录。

Just simple and stupid! Just simple and stupid! 可执行文件:可执行文件:bin_PROGRAMS = foobin_PROGRAMS = foofoo_SOURCES = foo1.c foo1.h foo2.cfoo_SOURCES = foo1.c foo1.h foo2.cfoo_LDADD = foo3.o -lm foo4.afoo_LDADD = foo3.o -lm foo4.afoo_LDFLAGS = -L<lib_path>foo_LDFLAGS = -L<lib_path>foo_DEPENDENCIES = foo_DEPENDENCIES =

Page 13: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Makefile.am(2)Makefile.am(2)

对静态库:对静态库:lib_LIBRARIES = libfoo.alib_LIBRARIES = libfoo.afoo_a_SOURCES =foo_a_SOURCES =foo_a_LDADD = foo_a_LDADD = foo_a_LIBADD =foo_a_LIBADD =foo_a_LDFLAGS = foo_a_LDFLAGS = 只在只在 makemake 时做静态连接用,不安装的库:时做静态连接用,不安装的库:noinst_LIBRARIES = libfoo.anoinst_LIBRARIES = libfoo.a………… 对头文件:对头文件: include_HEADERS = foo.hinclude_HEADERS = foo.h 对数据文件:对数据文件: data_DATA = data1 data2 data_DATA = data1 data2

Page 14: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Makefile.am(3)Makefile.am(3)

全局变量全局变量 (( 对所有目标都适用对所有目标都适用 ))

INCLUDES = -I/dir1 -I/dir2INCLUDES = -I/dir1 -I/dir2

LDFLAGS = -L/dir1 -L/dir2LDFLAGS = -L/dir1 -L/dir2

LDADD = foo.o foo.a -lfooLDADD = foo.o foo.a -lfoo

EXTRA_DIST = file1 file2 EXTRA_DIST = file1 file2 源程序和一些默认的文件自源程序和一些默认的文件自动打入动打入 .tar.gz.tar.gz 包,其它文件若要进入包,其它文件若要进入 .tar.gz.tar.gz 包可以用这种办法,比包可以用这种办法,比

如配置文件如配置文件 , , 数据文件等等数据文件等等。。SUBDIRS = dir1 dir2 SUBDIRS = dir1 dir2 在处理本目录之前要递归处理哪些子目在处理本目录之前要递归处理哪些子目

录录

Page 15: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Makefile.am(4)Makefile.am(4) 标准安装路径标准安装路径$(prefix) = /usr/local $(prefix) = /usr/local 是所有安装目录的默认前缀,是所有安装目录的默认前缀,

可以通过可以通过 ./configure --prefix=<new_prefix>./configure --prefix=<new_prefix> 的方的方法覆盖。法覆盖。

其它的预定义目录如:其它的预定义目录如: bindir = $(prefix)/bin, libdir bindir = $(prefix)/bin, libdir = $(prefix)/lib, datadir = $(prefix)/share, sysconfd= $(prefix)/lib, datadir = $(prefix)/share, sysconfdir = $(prefix)/etc, …ir = $(prefix)/etc, …

想定义一个新的安装路径?比如想定义一个新的安装路径?比如 config, config, 可定义可定义confdir = $(prefix)/config, confdir = $(prefix)/config, 然后然后 conf_DATA = file1 conf_DATA = file1

file2file2 ,则,则 file1, file2file1, file2 会作为数据文件安装到会作为数据文件安装到 $(pref$(prefix)/configix)/config 目录下。目录下。

Page 16: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Makefile.am(5)Makefile.am(5)

尽量用相对路径引用源程序的位置,以下尽量用相对路径引用源程序的位置,以下两个变量是预定义好的:两个变量是预定义好的: $(top_srcdir)$(top_srcdir) 无论在哪个目录层次,该变量定无论在哪个目录层次,该变量定

义了包含义了包含 srcsrc 目录的目录位置,用于引用源程目录的目录位置,用于引用源程序;序;

$(top_builddir)$(top_builddir) 定义了生成目标文件上最上层定义了生成目标文件上最上层目录,用于引用目录,用于引用 .o.o 等编译出来的目标文件。等编译出来的目标文件。

…………

Page 17: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

configure’s Giftsconfigure’s Gifts

configureconfigure 脚本生成的脚本生成的 MakefileMakefile中已经带了中已经带了很多常用的目标如:很多常用的目标如: check, all, install, unicheck, all, install, uninstall, clean, dist, distcheck, distclean, tagnstall, clean, dist, distcheck, distclean, tags, maintainerclean.s, maintainerclean. If configure or make did it, make distclean undIf configure or make did it, make distclean und

oes it.oes it. If make did it, make clean undoes it.If make did it, make clean undoes it. If make install did it, make uninstall undoes it.If make install did it, make uninstall undoes it. If you did it, make maintainer-clean undoes it.If you did it, make maintainer-clean undoes it.

Page 18: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

天网千帆的目录结构天网千帆的目录结构

Page 19: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

libtoollibtool 简介简介 生成各种程序库的方便工具。生成各种程序库的方便工具。 提供一个统一的接口,程序员不用关心各种烦人提供一个统一的接口,程序员不用关心各种烦人

的底层细节:不同的平台的库可能要求不同的后的底层细节:不同的平台的库可能要求不同的后缀,不同平台对库的安装方法不同,有些平台不缀,不同平台对库的安装方法不同,有些平台不支持动态库等等。支持动态库等等。

生成高层次的库,称为生成高层次的库,称为 libtool librarylibtool library ,后缀是,后缀是 .la.la 。。用它连接时,默认产生动态连接库,也可以用用它连接时,默认产生动态连接库,也可以用 -st-staticatic参数指定生成静态连接库。参数指定生成静态连接库。

既可单独使用又可与既可单独使用又可与 automakeautomake 和和 autoconfautoconf 一起一起使用更加强大、方便。使用更加强大、方便。

Page 20: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

使用使用 libtoollibtool 在在 configure.inconfigure.in 文件中加上文件中加上 AC_PROG_LIBTOOLAC_PROG_LIBTOOL 宏,如宏,如果原来有果原来有 AC_PROG_RANLIBAC_PROG_RANLIB 宏,删去它。宏,删去它。

在在 Makefile.amMakefile.am 文件中:文件中: lib_LTLIBRARIES = libshell.la lib_LTLIBRARIES = libshell.la libshell_la_SOURCES = object.c subr.c symbol.c libshell_la_SOURCES = object.c subr.c symbol.c

与原来的写法非常相似!与原来的写法非常相似! .la.la 库只能连入库只能连入 .lo(.lo( 使用使用 libtoollibtool 生成的目标文件生成的目标文件 )) libshell_la_LDADD = xmalloc.lo @LTLIBOBJS@ libshell_la_LDADD = xmalloc.lo @LTLIBOBJS@ 传入库的版本号:传入库的版本号:

libshell_la_LDFLAGS = -version-info 1:0:1 libshell_la_LDFLAGS = -version-info 1:0:1 与其它目标文件连接时用与其它目标文件连接时用 LDFLAGSLDFLAGS 指定连接的方式指定连接的方式 (( 默默

认是动态方式):认是动态方式): -static, --all-static-static, --all-static 指定静态连接。指定静态连接。

Page 21: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

小结小结 Autoconf:根据用户提供的 configure.in文件,生成一

个名为 configure的脚本。该脚本可以搜集有关移植性的平台相关信息,这些信息被用来生成Makefiles,配置头文件和其它平台相关的文件。

Automake:根据用户提供的一个高层次的生成规则Makefile.am,生成Makefile文件的模板Makefile.in。 Automake生成的Makefiles 符合 GNU的Makefile标准,用户无需再手工编写Makefile文件。

Libtool:使得生成内存位置无关的代码且让共享库在不同平台间移成为可能。它可以不依赖 autoconf和 automake 单独运行,另一方面, automake和 libtool可以无缝地集成使用。

Page 22: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

ReferencesReferences

ToolsManual:ToolsManual:

http://www.amath.washington.edu/~lf/tutorialhttp://www.amath.washington.edu/~lf/tutorials/autoconf/s/autoconf/ AutoBook:AutoBook:

http://sources.redhat.com/autobook/http://sources.redhat.com/autobook/ info & maninfo & man GNU Projects (GNU Projects ( 学习其他人的技巧学习其他人的技巧 ))

Page 23: GNU AutoTools 简介

GNU AutoTools In a NutshellGNU AutoTools In a Nutshell

Thanks.Thanks.

Any questions?Any questions?