GNU Autotools

最后更新: 2024-05-14 21:47:22 +0800 +0800
原文以英文撰写。您正在阅读AI翻译版本。

GNU Autotools generates portable 构建 interfaces 来自 project metadata. Autoconf probes the 构建 环境 和 emits configure; Automake generates portable Makefile.in templates 来自 Makefile.am; Libtool abstracts platform-specific library linking. 生成的 files 是 designed 用于 源码 releases, whereas maintainers edit the .ac, .am, 和 m4 inputs.

cMoankfeifgiulree..aamcamu4tomraeccroonsfcMoacMcnkoaofenkniffefgiifiulgigre.l.e.sehitnatus

输入文件与生成文件 #

A project normally 包含 these inputs:

  • configure.ac 是 an m4 program declares package metadata, probes, substitutions, output files, 和 optional headers.
  • Makefile.am describes 构建 targets,使用 Automake variables such 作为 bin_PROGRAMS, lib_LTLIBRARIES, 和 program_SOURCES.
  • m4/ 包含 project 或 third-party m4 macros, conventionally imported 由 aclocal.
  • config.h.in 是 an autoheader template 用于 配置 macros.

A maintainer runs autoreconf -fi 到 invoke the required generators. That command runs aclocal, autoconf, autoheader 当 头文件 是 declared, 和 automake --add-missing 当 Automake 是 used. 结果 includes configure, aclocal.m4, Makefile.in, helper scripts under build-aux/, 和 config.h.in where applicable.

A user then runs the 生成的 配置 script out 的 tree:

sh
mkdir build
cd build
../configure --prefix=/usr/local
make
make check
make install

script writes config.status, which records selected substitutions 和 可以 regenerate configured outputs without repeating every probe. config.log records compiler tests 和 diagnostics, making it the first 文件 到 inspect 在……之后 a 配置 failure.

Autoconf #

autoconf expands configure.ac 通过 GNU m4 和 emits a shell configure script. Macros such 作为 AC_PROG_CC locate 和 test a compiler; AC_CHECK_HEADERS 和 AC_CHECK_FUNCS test interfaces; AC_SUBST exports a variable 用于 template substitution. AC_CONFIG_FILES lists templates config.status turns 到 output files, 和 AC_OUTPUT completes the 配置 declaration.

m4
AC_INIT([example], [1.0])
AC_CONFIG_SRCDIR([src/main.c])
AC_PROG_CC
AC_CHECK_HEADERS([sys/epoll.h])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT

A successful probe 必须 answer a specific 构建 question. Cross compilation makes run-time probes unsafe because the 构建 host cannot execute target binaries. Autoconf exposes --build, --host, cache variables, 和 AC_CACHE_CHECK so a project 可以 distinguish compilation 来自 execution tests.

autoheader scans 配置 macros 和 emits config.h.in. configure substitutes each discovered #undef,使用 an appropriate #define 或 leaves it undefined in config.h. Multiple 头文件 是 supported 当 AC_CONFIG_HEADERS lists multiple names; each listed header has its own .in template 和 configured output.

Automake #

Automake translates Makefile.am 到 a portable Makefile.in; configure substitutes compiler paths, installation directories, 和 feature results 到 the final Makefile. For example:

makefile
bin_PROGRAMS = tool
tool_SOURCES = main.c parse.c parse.h
AM_CPPFLAGS = -I$(top_srcdir)/include

Automake supplies conventional targets such 作为 all, check, install, installcheck, clean, distclean, 和 dist. make distcheck builds a distribution archive in a fresh tree, runs the configured test suite, verifies installation 和 uninstallation, 和 confirms distclean removes 生成的 files.

Automake primaries define classes 的 output: PROGRAMS, LIBRARIES, LTLIBRARIES, HEADERS, SCRIPTS, DATA, 和 related forms. Prefixes such as bin_, lib_, include_, noinst_, 和 check_ select installation 或 build behavior. Automake derives 对象 依赖项 用于 declared sources, while BUILT_SOURCES, EXTRA_DIST, 和 dist_ variables handle 生成的 prerequisites and files automatic distribution discovery would miss.

aclocal constructs aclocal.m4 由 collecting macros required 由 configure.ac, including macros found 通过 AC_CONFIG_MACRO_DIRS. Version-controlled projects commonly include 生成的 configure 和 Makefile.in in release archives because an end user needs 仅 a shell 和 Make, 不 every Autotools generator.

模板与替换 #

A .in 文件 包含 literal text 和 @VARIABLE@ substitutions created 由 AC_SUBST. Automake-generated Makefile.in files also define Make variables 来自 those substitutions, so recipes 可以 使用 $(VARIABLE). 一个 template does 不 execute shell syntax merely because it passes 通过 configure; quote shell values in configure.ac 和 validate values 用于 由 生成的 scripts.

Libtool #

Libtool supplies libtool, a 生成的 wrapper 用于 由 Automake’s *_LTLIBRARIES targets. It separates a portable library name such as libexample.la 来自 platform outputs such 作为 a shared object, import library, or static archive. libtoolize installs the macro 和 helper files needed 由 a project. Library version information belongs in -version-info according to Libtool’s current/age/revision 接口 rules; release numbering instead uses -release. Neither 值 是 a direct copy 的 the package version.

Before publishing, maintainers run autoreconf -fi, an out-of-tree ../configure, make, make check, 和 make distcheck. Release users unpack the archive 和 need 仅 ./configure, Make, 和 the declared 构建 tools; DESTDIR 可以 stage make install 用于 packaging. Generated configure --help, config.log, config.status --recheck, 和 make -n expose concrete behavior.

评论

在 GitHub 上阅读或评论(发表评论需要登录)