「GNU-BUILD-SYSTEM」- GNU 构建系统、工具链

问题描述

1)使软件程序具有可移植性可能很困难:C 编译器因系统而异;某些系统上缺少某些库函数;头文件可能有不同的名称;共享库可以以不同的方式编译和安装。
2)处理平台差异的一种方法是编写条件代码,通过预处理器指令(#ifdef)选择代码块;但由于构建环境多种多样,这种方法很快就会变得难以管理。

解决方案

GNU Build System 旨在更易于管理地解决此问题。

早期,在编译程序时,我们按照 INSTALL 或 README.md 文件要求,使用 ./configure; make && make install 来编译并安装软件。有次,我们遇到源码未包含 configure 或 Makefile 文件,而是诸如 aclocal.m4 autogen.sh config.sub install-sh 等等文件。该类文件属于 GNU Build System(GNU 构建系统)。如果想要了解相关的技术,需要学习 GNU Build System 相关的知识。

发展历史及体系结构 The History and The System

Make -> Configure -> Autotools

原理简述

GNU 构建系统,也称为 GNU 构建工具链,是组开源的编程工具,它们是 GNU 项目的一部分,用于创建可移植的软件包。

关键的组件

它主要包括以下几个核心工具:
1)Autoconf:Autoconf 是个工具,用于创建可以在多种类 Unix 系统上运行的 shell 脚本,这些脚本可以自动配置软件源代码包以适应各种环境。
2)Automake:Automake 是个工具,用于生成能够遵循 GNU 编码标准的 Makefile.in 文件的模板。它看起来很复杂,但它的主要目标是简化软件包的可移植性。
3)Libtool:Libtool 是个 shell 脚本,用于隐藏各种 Unix 系统在创建共享库时的差异。它提供了一种一致性的机制,用于管理编译和链接的过程。
4)Gettext:Gettext 是个国际化(i18n)和本地化(l10n)支持库,用于让程序更容易地适应不同的语言和地区。
5)GNU Make:自动化工具,用于编译、构建
6)GNU Compiler Collection (GCC): 编译器套件,适用于若干种语言

Autotools 是个合称(有时也指 GNU 构建系统,并无严格区分),Autotools consists of the GNU utility programs Autoconf, Automake, and Libtool. Other related tools frequently used alongside it include GNU’s make program, GNU gettext, pkg-config, and the GNU Compiler Collection, also called GCC.

使用的方法

开发者:

创建源码文件;

执行 autoscan,生成 configure.scan 文件;

将 configure.scan 复制为 configure.in 文件,并进行相应的修改;

执行 aclocal,生成 aclocal.m4 文件;

执行 autoconf,生成 autom4te.cache 和 configure 文件;

创建 Makefile.am 文件;

运行 automake,会使用 Makefile.am 生成 Makefile.in 文件;

用户:

执行 configure,生成 Makefile 文件;

执行 make 及 make install 以完成程序的安装;

注 1,在用户的实际安装过程中,源码包可能并不包含 configure 文件,需要用户自己执行 autoreconf 来生成。

特性特征

使用 Autotools 的优点:
1)其按照统一的规范生成并安装文件,保证一致性;
2)简化打包过程;

应用场景

该部分工具通常一起使用,以便在各种不同的平台和操作系统上构建和安装软件。

参考

GNU Autotools – Wikipedia
Wikipedia/GNU Toolchain: https://en.wikipedia.org/wiki/GNU_toolchain
Automake, Autoconf 使用详解: http://www.laruence.com/2009/11/18/1154.html
autotools – What’s the point of aclocal? – Stack Overflow
autogen.sh 的使用_autogen 部署文档并调取其他大模型-CSDN 博客

针对 GNU Build System 学习,相关文献有:

制定学习路线 2 Create a Learning Roadmap

大致了解:https://opensource.com/article/19/7/introduction-gnu-autotools

详细使用:https://www.lrde.epita.fr/~adl/dl/autotools.pdf

Locate the reference manuals in your preferred format.

Autoconf, Automake, Libtool, and Gettext all install reference manuals in the Info format. (Try ‘info Autoconf’, ‘info Automake’, etc.)

The web pages of these tools also have .html or .pdf versions.

These manuals may not be easy introductions to the tools, but they make good and up-to-date references.

Subscribe to these tools’ mailing lists, to see other people’s uses of the tools.

Pick a package that uses these tools and dissect its setup.

Try picking something written by somebody who isn’t just another neophyte!

I recommend looking at GNU Coreutils.

完整学习:Autotools: a practitioner’s guide to Autoconf, Automake and Libtool