Autotools Tutorial for Embedded Linux
Autotools Tutorial for Embedded Linux
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 1/99
Thomas Petazzoni
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 2/99
When talking about autotools, most people think:
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 3/99
When talking about autotools, most people think:
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 3/99
When talking about autotools, most people think:
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 4/99
Autotools: benefits
I Standardized build procedure and behavior: users know how to build things that
use the autotools
I Good for human users, but also for build systems
I Proper handling for diverted installation
I I.e. build with prefix=/usr, but divert the installation to another directory. Needed
for cross-compilation.
I Built-in support for out-of-tree build
I Built-in handling of dependencies on header files
I Support for cross-compilation aspects
I Somewhat esoteric, but standardized languages used
I Learn once, use for many projects
I New contributors are more likely to know the autotools than your own custom thing
I Of course, there are alternatives, CMake being the most interesting and widely
used.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 5/99
Disclaimer
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 6/99
Autotools tutorial: agenda
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 7/99
User point of view
Free Electrons
Thomas Petazzoni
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 8/99
Using autotools based packages
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 9/99
What is configure doing?
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 10/99
Standard Makefile targets
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 11/99
Standard filesystem hierarchy
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 12/99
Standard configuration variables
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 13/99
System types: build, host, target
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 14/99
System type: native compilation example
Demo
(based on the kmod source code)
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 15/99
Cross-compilation
I By default, autotools will guess the host machine as being the current machine
I To cross-compile, it must be overridden by passing the --host option with the
appropriate configuration name
I By default, autotools will try to use the cross-compilation tools that use the
configuration name as their prefix.
I If not, the variables CC, CXX, LD, AR, etc. can be used to point to the
cross-compilation tools.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 16/99
Out of tree build
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 17/99
Out of tree build: example
Demo
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 18/99
Diverted installation with DESTDIR
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 19/99
--prefix or DESTDIR ?
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 20/99
--prefix or DESTDIR use cases
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 21/99
Analyzing issues
I autoconf keeps a log of all the tests it runs in a file called [Link]
I Very useful for analysis of autoconf issues
I It contains several sections: Platform, Core tests, Running [Link], Cache
variables, Output variables, confdefs.h
I The end of the Core tests section is usually the most interesting part
I This is where you would get more details about the reason of the configure script
failure
I At the beginning of [Link] you can also see the ./configure line that was
used, with all options and environment variables.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 22/99
autotools: autoconf and automake
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 23/99
Overall organization
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 24/99
Cache variables
I If the autodetected value is not correct for some reason, you can override any of
these variables in the environment:
$ ac_cv_path_SED=/path/to/sed ./configure
I This is sometimes useful when cross-compiling, since some tests are not always
cross-compilation friendly.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 25/99
Distribution
I In general:
I When a software is published as a tarball, the configure script and [Link]
files are already generated and part of the tarball.
I When a software is published through version control system, only the real sources
[Link] and [Link] are available.
I There are some exceptions (like tarballs not having pre-generated
configure/[Link])
I Do not version control generated files!
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 26/99
Regenerating autotools files: autoreconf
I To generate all the files used by autotools, you could call automake, autoconf,
aclocal, autoheader, etc. manually.
I But it is not very easy and efficient.
I A tool called autoreconf automates this process
I Useful option: -i or --install, to ask autoreconf to copy missing auxiliary files
I Always use autoreconf!
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 27/99
autoconf basics
Free Electrons
Thomas Petazzoni
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 28/99
[Link] language
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 29/99
Minimal [Link]
[Link]
AC_INIT([hello], [1.0])
AC_OUTPUT
I AC_INIT
I Every configure script must call AC_INIT before doing anything else that produces
output.
I Process any command-line arguments and perform initialization and verification.
I Prototype:
AC_INIT (package, version, [bug-report], [tarname], [url])
I AC_OUTPUT
I Every [Link], should finish by calling AC_OUTPUT.
I Generates and runs [Link], which in turn creates the makefiles and any
other files resulting from configuration.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 30/99
Minimal [Link] example
Demo 01
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 31/99
Additional basic macros
I AC_PREREQ
I Verifies that a recent enough version of autoconf is used
I AC_PREREQ([2.68])
I AC_CONFIG_SRCDIR
I Gives the path to one source file in your project
I Allows autoconf to check that it is really where it should be
I AC_CONFIG_SRCDIR([hello.c])
I AC_CONFIG_AUX_DIR
I Tells autoconf to put the auxiliary build tools it requires in a different directory,
rather than the one of [Link]
I Useful to keep cleaner build directory
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 32/99
Additional basic macros
Demo 02
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 33/99
Checking for basic programs
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 34/99
Checking for basic programs: example
Demo 03
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 35/99
AC CONFIG FILES
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 36/99
Output variables
AC_SUBST([FOO], [42])
FOO=42
AC_SUBST([FOO])
AC_SUBST([FOO])
FOO=42
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 37/99
AC CONFIG FILES example
Demo 04
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 38/99
[Link]: a shell script
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 39/99
Writing [Link]?
I At this point, we have seen the very basics of autoconf to perform the
configuration side of our software
I We could use AC_CONFIG_FILES to generate Makefile from [Link]
I However, writing a [Link] properly is not easy, especially if you want to:
I be portable
I automatically handle dependencies
I support conditional compilation, out-of-tree build, diverted installation,
cross-compilation, etc.
I For these reasons, [Link] are typically not written manually, but generated
by automake from a [Link] file
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 40/99
automake basics
Free Electrons
Thomas Petazzoni
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 41/99
[Link] language
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 42/99
[Link] minimal example
I The minimal example of [Link] to build just one C file into a program is
only two lines:
[Link]
bin_PROGRAMS = hello
hello_SOURCES = main.c
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 43/99
Enabling automake in [Link]
[Link]
AC_INIT([hello], [1.0])
AM_INIT_AUTOMAKE([foreign 1.13])
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 44/99
First automake demo
Demo 06
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 45/99
AM INIT AUTOMAKE
I AM_INIT_AUTOMAKE([OPTIONS])
I Interesting options:
I foreign, tells automake to not require all the GNU Coding Style files such as NEWS,
README, AUTHORS, etc.
I dist-bzip2, dist-xz, etc. tell automake which tarball format should be generated
by make dist
I subdir-objects tells automake that the objects are placed into the subdirectory of
the build directory corresponding to the subdirectory of the source file
I version, e.g 1.14.1, tells the minimal automake version that is expected
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 46/99
[Link] syntax
bin_PROGRAMS = hello
I And product source variables:
hello_SOURCES = main.c
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 47/99
Product list variables
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 48/99
Product source variables
I The product is the normalized name of the product, as listed in a product list
variable
I The normalization consists in replacing special characters such as . or + by _. For
example, libfoo+.a in a product list variable gives the libfoo__a_SOURCES
product source variable.
I _SOURCES is always used, it’s not like a configurable primary.
I Contains the list of files containing the source code for the product to be built.
I Both source files and header files should be listed.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 49/99
More complicated automake example
Demo 07
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 50/99
autoconf advanced
Free Electrons
Thomas Petazzoni
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 51/99
Configuration header
I Very often, C/C++ code needs to know the result of certain tests done by the
configure script.
I A template C header file can be automatically generated by autoheader,
generally named [Link]
I The final header file is generated by configure, generally named config.h
I Declared using AC_CONFIG_HEADERS
[Link] extract
AC_CONFIG_HEADERS([config.h])
Example config.h
/* Define if the complete vga libraries (vga, vgagl) are installed */
/* #undef HAVE_LIBVGA */
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 52/99
AC DEFINE
[Link]
AC_DEFINE([FOOBAR], [42], [This is the foobar value])
Demo 08
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 53/99
Checking for functions
I You may need to check if certain functions are available and/or meet certain
characteristics
I Family of AC_FUNC_* macros
I AC_FUNC_FORK, AC_FUNC_GETLOADAVG, AC_FUNC_MALLOC, etc.
I See autoconf manual for details
I AC_CHECK_FUNC[S] to check for generic functions
I AC_CHECK_FUNC (function, [action-if-found], [action-if-not-found])
I AC_CHECK_FUNCS (function..., [action-if-found], [action-if-not-
found])
I Results available
I ac_cv_func_<function> variable in [Link]
I HAVE_<FUNCTION> defines in configuration headers
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 54/99
AC CHECK FUNCS() example
Demo 09
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 55/99
Checking for headers
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 56/99
AC CHECK HEADERS example
[Link]
[...]
AC_CHECK_HEADERS([spawn.h],
[echo "Header spawn.h was found"; has_spawn=yes],
[echo "Header spawn.h was not found"])
echo ${has_spawn}
[...]
Execution of ./configure
$ ./configure
[...]
checking for spawn.h... yes
Header spawn.h was found
yes
[...]
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 57/99
Checking for libraries
Demo 10
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 59/99
Other checks
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 60/99
Writing new tests
I You can create your own tests by pre-processing, compiling or linking small test
programs:
I Pre-processing test
AC_PREPROC_IFELSE (input, [action-if-true], [action-if-false])
I Compiling test
AC_COMPILE_IFELSE (input, [action-if-true], [action-if-false])
I Link test
AC_LINK_IFELSE (input, [action-if-true], [action-if-false])
I Input should be formatted with AC_LANG_SOURCE or AC_LANG_PROGRAM
I Runtime tests can also be created
I Beware, by nature, they cannot work for cross-compilation!
I AC_RUN_IFELSE
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 61/99
Writing new tests: AC LINK IFELSE
Demo 11
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 62/99
Printing messages
I When creating new tests, you may want to show messages, warnings, errors, etc.
I AC_MSG_CHECKING (feature-description)
I Notify the user that configure is checking for a particular feature.
I AC_MSG_RESULT (result-description)
I Notify the user of the results of a check
I AC_MSG_NOTICE (message)
I Deliver the message to the user.
I AC_MSG_ERROR (error-description, [exit-status = $?/1])
I Notify the user of an error that prevents configure from completing.
I AC_MSG_WARN (problem-description)
I Notify the configure user of a possible problem.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 63/99
Printing messages: example
Demo 11 continued
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 64/99
Cache variables
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 65/99
Cache variables demo
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 66/99
Using external software
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 67/99
Package options
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 68/99
Formatting the help string
I To help formatting the help string, autoconf provides the AS_HELP_STRING macro
I Allows to properly align the different options in the ./configure --help output
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 69/99
AC ARG ENABLE example
Demo 12
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 70/99
Using pkg-config with autoconf
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 71/99
The PKG CHECK MODULES macro
I Syntax:
PKG_CHECK_MODULES(prefix, list-of-modules,
action-if-found, action-if-not-found)
I prefix will be used to create the <prefix>_CFLAGS and <prefix>_LIBS
variables
I Contain the pre-processor and linker flags to use the libraries listed in
list-of-modules
I Are already AC_SUBSTed, so can be used directly in [Link]
I list-of-modules is one or several pkg-config libraries
I Can contain version specifiers, such as foo >= 3 bar baz <= 4
I Will exit with a failure if one of the dependencies is missing.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 72/99
PKG CHECK MODULES example
Demo 13
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 73/99
autoscan
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 74/99
Additional m4 macros
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 75/99
autoconf-archive example
Demo 14
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 76/99
automake advanced
Free Electrons
Thomas Petazzoni
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 77/99
Subdirectories
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 78/99
Recursive make
[Link]
AC_CONFIG_FILES([Makefile src/Makefile])
[Link]
SUBDIRS = src
src/[Link]
bin_PROGRAMS = hello
hello_SOURCES = main.c
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 79/99
Non-recursive make
[Link]
AM_INIT_AUTOMAKE([subdir-objects])
AC_CONFIG_FILES([Makefile])
[Link]
bin_PROGRAMS = hello
hello_SOURCES = src/main.c
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 80/99
automake subdirectories: demo
Demo 15 and 16
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 81/99
automake conditionals
[Link]
AM_CONDITIONAL([DEBUG], [test "${debug}" = "true"])
[Link]
if DEBUG
...
else
...
endif
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 82/99
Usage of automake conditionals
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 83/99
Conditional example
Demo 17
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 84/99
Building shared libraries
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 85/99
Libtool library example
[Link]
[...]
LT_PREREQ([2.4])
LT_INIT
[...]
[Link]
bin_PROGRAMS = hello
hello_SOURCES = src/main.c
lib_LTLIBRARIES = [Link]
libmyhello_la_SOURCES = lib/core.c
include_HEADERS = lib/myhello.h
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 86/99
Libtool versioning
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 87/99
Libtool: demo
Demo 18
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 88/99
Global automake variables
Example
LDADD = $(top_builddir)/glib/[Link]
AM_CPPFLAGS = $(gmodule_INCLUDES) $(GLIB_DEBUG_FLAGS)
AM_CFLAGS = -g
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 89/99
Per product variables
Example
LDADD = $(top_builddir)/glib/[Link]
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 90/99
Useful variables
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 91/99
Demo 19
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 92/99
Silent rules
$ make
CC lib/[Link]
CCLD [Link]
CC src/main.o
CCLD hello
$ make V=1
[...]
libtool: link: (cd ".libs" && rm -f "[Link].0" && ln -s "[Link].0.0.0" ...
libtool: link: (cd ".libs" && rm -f "[Link]" && ln -s "[Link].0.0.0" ...
libtool: link: ar cru .libs/libmyhello.a lib/core.o
libtool: link: ranlib .libs/libmyhello.a
[...]
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 93/99
Demo 20
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 94/99
make dist
I Distribution can also be controlled using the dist and nodist automake product
modifiers:
[Link]
nodist_include_HEADERS += pcrecpparg.h
dist_doc_DATA = doc/[Link]
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 95/99
Macro directory
I By default, all the third-party autoconf macros get copied into the (very large)
aclocal.m4 file.
I It is possible to get some of the third-party macros copied to individiual files in a
separate directory, which is nicer.
I Directory declared using AC_CONFIG_MACRO_DIR, generally named m4 by
convention:
[Link]
AC_CONFIG_MACRO_DIR([m4])
I The ACLOCAL_AMFLAGS in [Link] should also be adjusted:
[Link]
ACLOCAL_AMFLAGS = -I m4
I For now, mainly used by libtool for its own m4 macros.
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 96/99
Auxiliary directory
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 97/99
Macro/auxiliary directory: demo
Demo 21
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 98/99
Questions?
Thomas Petazzoni
[Link]@[Link]
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. [Link] 99/99