autotools-slides
autotools-slides
Autotools training
Corrections, suggestions, contributions and translations are welcome! embedded Linux and kernel engineering
Send them to [email protected]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/124
Autotools training
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 2/124
About Bootlin
About Bootlin
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 3/124
Bootlin introduction
▶ Engineering company
• In business since 2004
• Before 2018: Free Electrons
▶ Team based in France and Italy
▶ Serving customers worldwide
▶ Highly focused and recognized expertise
• Embedded Linux
• Linux kernel
• Embedded Linux build systems
▶ Strong open-source contributor
▶ Activities
• Engineering services
• Training courses
▶ https://bootlin.com
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 4/124
Bootlin engineering services
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 5/124
Bootlin training courses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 6/124
Bootlin, an open-source contributor
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 7/124
Bootlin on-line resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 8/124
Generic course information
Generic course
information
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 9/124
Training quiz and certificate
▶ You have been given a quiz to test your knowledge on the topics covered by the
course. That’s not too late to take it if you haven’t done it yet!
▶ At the end of the course, we will submit this quiz to you again. That time, you
will see the correct answers.
▶ It allows Bootlin to assess your progress thanks to the course. That’s also a kind
of challenge, to look for clues throughout the lectures and labs / demos, as all the
answers are in the course!
▶ Another reason is that we only give training certificates to people who achieve at
least a 50% score in the final quiz and who attended all the sessions.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 10/124
Participate!
During the lectures...
▶ Don’t hesitate to ask questions. Other people in the audience may have similar
questions too.
▶ Don’t hesitate to share your experience too, for example to compare Linux with
other operating systems you know.
▶ Your point of view is most valuable, because it can be similar to your colleagues’
and different from the trainer’s.
▶ In on-line sessions
• Please always keep your camera on!
• Also make sure your name is properly filled.
• You can also use the ”Raise your hand” button when you wish to ask a question but
don’t want to interrupt.
▶ All this helps the trainer to engage with participants, see when something needs
clarifying and make the session more interactive, enjoyable and useful for everyone.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 11/124
Collaborate!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 12/124
Practical lab - Training Setup
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 13/124
Autotools usage
Autotools usage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 14/124
Why do we need autotools?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 15/124
Alternatives to autotools
▶ Regular Makefiles
• Not very portable
• No configuration tests, or options
• Hard to take into account all dependencies (e.g. dependencies on header files)
• No standardized behavior
▶ CMake
• A more modern build system
• One language, instead of several for autotools
• More straightforward to use and understand
• Much less widely used than autotools, but growing in popularity
• Also generates Makefiles, like autotools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 16/124
Using autotools based packages
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 17/124
What is configure doing?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 18/124
Standard Makefile targets
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 19/124
Standard filesystem hierarchy
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 20/124
Standard configuration variables
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 21/124
System types: build, host, target
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 22/124
System type: configuration names
▶ A string identifying a combination of architecture, operating system, ABI and C
library
▶ General format: <arch>-<vendor>-<kernel>-<operating_system>
• <arch> is the type of processor, i.e. arm, i686, etc.
• <vendor> is a free form string, which can be omitted
• <kernel> is always linux when working with Linux systems, or none for bare metal
systems
• <operating_system> generally identifies the C library and ABI, i.e. gnu, gnueabi,
eabi, gnueabihf, uclibcgnueabihf
▶ Also often used as the prefix for cross-compilation tools.
▶ Examples
• x86_64-amd-linux-gnu
• powerpc-mentor-linux-gnu
• armeb-linux-gnueabihf
• i486-linux-musl
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 23/124
System type: native compilation example
$ ./configure
[...]
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for gcc... gcc
[...]
checking how to run the C preprocessor... gcc -E
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 24/124
Cross-compilation
▶ By default, autotools will guess the host machine as being the current machine
▶ To cross-compile, it must be overridden by passing the --host option with the
appropriate configuration name
▶ By default, autotools will try to use the cross-compilation tools that use the
configuration name as their prefix.
▶ If not, the variables CC, CXX, LD, AR, etc. can be used to point to the
cross-compilation tools.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 25/124
System type: cross compilation example
$ which arm-linux-gnueabihf-gcc
/usr/bin/arm-linux-gnueabihf-gcc
$ ./configure --host=arm-linux-gnueabihf
[...]
checking build system type... x86_64-unknown-linux-gnu
checking host system type... arm-unknown-linux-gnueabihf
checking for arm-linux-gnueabihf-gcc... arm-linux-gnueabihf-gcc
[...]
checking how to run the C preprocessor... arm-linux-gnueabihf-gcc -E
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 26/124
Out of tree build
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 27/124
Out of tree build: example
strace-4.9 $ ls
configure configure.ac Makefile.am system.c NEWS
AUTHORS COPYING file.c ioprio.c config.h
strace-4.9 $ mkdir ../strace-build-x86 ../strace-build-arm
strace-4.9 $ cd ../strace-build-x86
strace-build-x86 $ ../strace-4.9/configure
[...]
strace-build-x86 $ make
[...]
strace-build-x86 $ cd ../strace-build-arm
strace-build-arm $ ../strace-4.9/configure --host=arm-linux-gnueabihf
[...]
strace-build-arm $ make
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 28/124
Diverted installation with DESTDIR
▶ By default, make install installs to the directories given in --prefix and related
options.
▶ In some situations, it is useful to divert the installation to another directory
• Cross-compilation, where the build machine is not the machine where applications
will be executed.
• Packaging, where the installation needs to be done in a temporary directory.
▶ Achieved using the DESTDIR variable.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 29/124
--prefix or DESTDIR?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 30/124
--prefix or DESTDIR use cases
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 31/124
Analyzing issues
▶ autoconf keeps a log of all the tests it runs in a file called config.log
▶ Very useful for analysis of autoconf issues
▶ It contains several sections: Platform, Core tests, Running config.status, Cache
variables, Output variables, confdefs.h
▶ The end of the Core tests section is usually the most interesting part
• This is where you would get more details about the reason of the configure script
failure
▶ At the beginning of config.log you can also see the ./configure line that was
used, with all options and environment variables.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 32/124
config.log example
$ ./configure ...
[...]
checking for TIFFFlushData in -ltiff34... no
configure: WARNING: *** TIFF loader will not be built (TIFF library not found) ***
configure: error:
*** Checks for TIFF loader failed. You can build without it by passing
*** --without-libtiff to configure but some programs using GTK+ may
*** not work properly
$ cat config.log
[...]
configure:18177: .../usr/bin/x86_64-linux-gcc -std=gnu99 -o conftest -D_LARGEFILE_SOURCE
-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -static -Wall -D_LARGEFILE_SOURCE
-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -DG_DISABLE_SINGLE_INCLUDES -static
conftest.c -ltiff34 -ljpeg -lz -lm >&5
.../host/opt/ext-toolchain/bin/../lib/gcc/x86_64-buildroot-linux-uclibc/4.8.4/../../../../
x86_64-buildroot-linux-uclibc/bin/ld: cannot find -ltiff34
.../host/opt/ext-toolchain/bin/../lib/gcc/x86_64-buildroot-linux-uclibc/4.8.4/../../../../
x86_64-buildroot-linux-uclibc/bin/ld: cannot find -ljpeg
collect2: error: ld returned 1 exit status
configure:18177: $? = 1
configure: failed program was:
[...]
configure:18186: result: no
configure:18199: WARNING: *** TIFF loader will not be built (TIFF library not found) ***
configure:18210: error:
*** Checks for TIFF loader failed. You can build without it by passing
*** --without-libtiff to configure but some programs using GTK+ may
*** not work properly
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 33/124
autotools: autoconf and automake
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 34/124
Cache variables
▶ 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
▶ This is sometimes useful when cross-compiling, since some tests are not always
cross-compilation friendly.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 35/124
Distribution
▶ In general:
• When a software is published as a tarball, the configure script and Makefile.in
files are already generated and part of the tarball.
• When a software is published through version control system, only the real sources
configure.ac and Makefile.am are available.
▶ There are some exceptions (like tarballs not having pre-generated
configure/Makefile.in)
▶ Do not version control generated files!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 36/124
Regenerating autotools files: autoreconf
▶ To generate all the files used by autotools, you could call automake, autoconf,
aclocal, autoheader, etc. manually.
• But it is not very easy and efficient.
▶ A tool called autoreconf automates this process
• Useful option: -i or --install, to ask autoreconf to copy missing auxiliary files
▶ Always use autoreconf!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 37/124
autoreconf example
$ find . -type f
./src/main.c
./Makefile.am
./configure.ac
$ autoreconf -i
configure.ac:4: installing './compile'
configure.ac:3: installing './install-sh'
configure.ac:3: installing './missing'
Makefile.am: installing './depcomp'
$ find . -type f
./install-sh
./src/main.c
./config.h.in
./configure
./missing
./depcomp
./aclocal.m4
./Makefile.am
./autom4te.cache/traces.0
./autom4te.cache/output.1
./autom4te.cache/output.0
./autom4te.cache/requests
./autom4te.cache/traces.1
./compile
./Makefile.in
./configure.ac
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 38/124
Overall organization
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 39/124
Practical lab - Usage of existing autotools projects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 40/124
Autotools basics
Autotools basics
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 41/124
configure.ac language
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 42/124
Minimal configure.ac
configure.ac
AC_INIT([hello], [1.0])
AC_OUTPUT
▶ AC_INIT
• Every configure script must call AC_INIT before doing anything else that produces
output.
• Process any command-line arguments and perform initialization and verification.
• Prototype:
AC_INIT (package, version, [bug-report], [tarname], [url])
▶ AC_OUTPUT
• Every configure.ac, should finish by calling AC_OUTPUT.
• Generates and runs config.status, which in turn creates the makefiles and any other
files resulting from configuration.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 43/124
Minimal configure.ac example
$ cat configure.ac
AC_INIT([hello], [1.0])
AC_OUTPUT
$ ls
configure.ac
$ autoreconf -i
$ ls
autom4te.cache configure configure.ac
$ ./configure
configure: creating ./config.status
$ ls
autom4te.cache config.log config.status
configure configure.ac
$ wc -l configure
2390 configure
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 44/124
Additional basic macros
▶ AC_PREREQ
• Verifies that a recent enough version of autoconf is used
• AC_PREREQ([2.68])
▶ AC_CONFIG_SRCDIR
• Gives the path to one source file in your project
• Allows autoconf to check that it is really where it should be
• AC_CONFIG_SRCDIR([hello.c])
▶ AC_CONFIG_AUX_DIR
• Tells autoconf to put the auxiliary build tools it requires in a different directory,
rather than the one of configure.ac
• Useful to keep cleaner build directory
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 45/124
Checking for basic programs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 46/124
Checking for basic programs: example
configure.ac
AC_INIT([hello], [1.0])
AC_PROG_CC
AC_OUTPUT
$ ./configure
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
configure: creating ./config.status
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 47/124
AC_CONFIG_FILES
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 48/124
Output variables
▶ autoconf will replace @variable@ constructs by the appropriate values in files
listed in AC_CONFIG_FILES
▶ Long list of standard variables replaced by autoconf
▶ Additional shell variables declared in configure.ac can be replaced using
AC_SUBST
▶ The following three examples are equivalent:
AC_SUBST([FOO], [42])
FOO=42
AC_SUBST([FOO])
AC_SUBST([FOO])
FOO=42
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 49/124
AC_CONFIG_FILES example (1/2)
configure.ac
AC_INIT([hello], [1.0])
AC_PROG_CC
FOO=42
AC_SUBST([FOO])
AC_CONFIG_FILES([testfile])
AC_OUTPUT
testfile.in
abs_builddir = @abs_builddir@
CC = @CC@
FOO = @FOO@
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 50/124
AC_CONFIG_FILES example (2/2)
Executing ./configure
/tmp/foo$ ./configure
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
configure: creating ./config.status
config.status: creating testfile
Generated testfile
abs_builddir = /tmp/foo
CC = gcc
FOO = 42
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 51/124
configure.ac: a shell script
Running ./configure
configure.ac The value of CC is
checking for gcc... gcc
AC_INIT([hello], [1.0])
checking whether the C compiler works... yes
echo "The value of CC is $CC"
checking for C compiler default output file name... a.out
AC_PROG_CC
checking for suffix of executables...
echo "The value of CC is now $CC"
checking whether we are cross compiling... no
FOO=42
checking for suffix of object files... o
AC_SUBST([FOO])
checking whether we are using the GNU C compiler... yes
if test $FOO -eq 42 ; then
checking whether gcc accepts -g... yes
echo "The value of FOO is correct!"
checking for gcc option to accept ISO C89... none needed
fi
The value of CC is now gcc
AC_CONFIG_FILES([testfile])
The value of FOO is correct!
AC_OUTPUT
configure: creating ./config.status
config.status: creating testfile
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 52/124
Writing Makefile.in?
▶ At this point, we have seen the very basics of autoconf to perform the
configuration side of our software
▶ We could use AC_CONFIG_FILES to generate Makefile from Makefile.in
▶ However, writing a Makefile.in properly is not easy, especially if you want to:
• be portable
• automatically handle dependencies
• support conditional compilation
▶ For these reasons, Makefile.in are typically not written manually, but generated
by automake from a Makefile.am file
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 53/124
Makefile.am language
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 54/124
Makefile.am minimal example
▶ The minimal example of Makefile.am to build just one C file into a program is
only two lines:
Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = main.c
▶ Will compile main.c to main.o
▶ And link hello.o into the hello executable
▶ Which will be installed in $prefix/bin
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 55/124
Enabling automake in configure.ac
configure.ac
AC_INIT([hello], [1.0])
AM_INIT_AUTOMAKE([foreign 1.13])
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 56/124
AM_INIT_AUTOMAKE
▶ AM_INIT_AUTOMAKE([OPTIONS])
▶ Interesting options:
• foreign, tells automake to not require all the GNU Coding Style files such as NEWS,
README, AUTHORS, etc.
• dist-bzip2, dist-xz, etc. tell automake which tarball format should be generated
by make dist
• subdir-objects tells automake that the objects are placed into the subdirectory of
the build directory corresponding to the subdirectory of the source file
• version, e.g 1.14.1, tells the minimal automake version that is expected
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 57/124
Makefile.am syntax
bin_PROGRAMS = hello
▶ And product source variables:
hello_SOURCES = main.c
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 58/124
Product list variables
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 59/124
Product source variables
▶ The product is the normalized name of the product, as listed in a product list
variable
• 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.
▶ _SOURCES is always used, it’s not like a configurable primary.
• Contains the list of files containing the source code for the product to be built.
• Both source files and header files should be listed.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 60/124
Example: building multiple programs
Makefile.am
bin_PROGRAMS = hello test
hello_SOURCES = main.c common.c common.h
test_SOURCES = test.c common.c common.h
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 61/124
Practical lab - Your first autotools project
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 62/124
Autoconf advanced
Autoconf advanced
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 63/124
Autoconf advanced
Configuration header
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 64/124
Configuration header
▶ Very often, C/C++ code needs to know the result of certain tests done by the
configure script.
▶ A template C header file can be automatically generated by autoheader, generally
named config.h.in
▶ The final header file is generated by configure, generally named config.h
▶ Declared using AC_CONFIG_HEADERS
configure.ac extract
AC_CONFIG_HEADERS([config.h])
Example config.h
/* Define if the complete vga libraries (vga, vgagl) are installed */
/* #undef HAVE_LIBVGA */
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 65/124
AC_DEFINE
configure.ac
AC_DEFINE([FOOBAR], [42], [This is the foobar value])
Generated config.h
/* This is the foobar value */
#define FOOBAR 42
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 66/124
Autoconf advanced
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 67/124
Checking for functions
▶ You may need to check if certain functions are available and/or meet certain
characteristics
▶ Family of AC_FUNC_* macros
• AC_FUNC_FORK, AC_FUNC_GETLOADAVG, AC_FUNC_MALLOC, etc.
• See autoconf manual for details
▶ AC_CHECK_FUNC[S] to check for generic functions
• AC_CHECK_FUNC (function, [action-if-found], [action-if-not-found])
• AC_CHECK_FUNCS (function..., [action-if-found], [action-if-not-found])
• Results available
ac_cv_func_<function> variable in configure.ac
HAVE_<FUNCTION> defines in configuration headers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 68/124
AC_CHECK_FUNCS() example
configure.ac
AC_CHECK_FUNCS([printf foobar])
echo "ac_cv_func_printf: ${ac_cv_func_printf}"
echo "ac_cv_func_foobar: ${ac_cv_func_foobar}"
AC_CONFIG_HEADER([config.h])
Execution of ./configure
$ ./configure
[...]
checking for printf... yes
checking for foobar... no
ac_cv_func_printf: yes
ac_cv_func_foobar: no
[...]
config.status: creating config.h
Generated config.h
[...]
/* Define to 1 if you have the `foobar' function. */
/* #undef HAVE_FOOBAR */
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 69/124
Checking for headers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 70/124
AC_CHECK_HEADERS example
configure.ac
[...]
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
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 71/124
Checking for libraries
Execution of ./configure
$ ./configure
[...]
checking for library containing mvwaddstr... -lncurses
[...]
$ grep ac_cv_search_mvwaddstr config.log
ac_cv_search_mvwaddstr=-lncurses
Compilation
$ make
[...]
gcc -g -O2 -o hello main.o common.o -lncurses
[...]
gcc -g -O2 -o test test.o common.o -lncurses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 73/124
Other checks
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 74/124
Autoconf advanced
Custom tests
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 75/124
Writing new tests
▶ You can create your own tests by pre-processing, compiling or linking small test
programs:
• Pre-processing test
AC_PREPROC_IFELSE (input, [action-if-true], [action-if-false])
• Compiling test
AC_COMPILE_IFELSE (input, [action-if-true], [action-if-false])
• Link test
AC_LINK_IFELSE (input, [action-if-true], [action-if-false])
▶ Input should be formatted with AC_LANG_SOURCE or AC_LANG_PROGRAM
▶ Runtime tests can also be created
• Beware, by nature, they cannot work for cross-compilation!
• AC_RUN_IFELSE
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 76/124
Writing new tests: AC_LINK_IFELSE
configure.ac
AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <langinfo.h>],
[char *codeset = nl_langinfo (CODESET);])],
[glib_cv_langinfo_codeset=yes],
[glib_cv_langinfo_codeset=no])
Variable in config.log
$ grep glib_cv_langinfo_codeset config.log
glib_cv_langinfo_codeset=yes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 77/124
Printing messages
▶ When creating new tests, you may want to show messages, warnings, errors, etc.
▶ AC_MSG_CHECKING (feature-description)
• Notify the user that configure is checking for a particular feature.
▶ AC_MSG_RESULT (result-description)
• Notify the user of the results of a check
▶ AC_MSG_NOTICE (message)
• Deliver the message to the user.
▶ AC_MSG_ERROR (error-description, [exit-status = ‘$?/1’])
• Notify the user of an error that prevents configure from completing.
▶ AC_MSG_WARN (problem-description)
• Notify the configure user of a possible problem.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 78/124
Printing messages: example
configure.ac
AC_MSG_CHECKING([for nl_langinfo])
AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <langinfo.h>],
[char *codeset = nl_langinfo (CODESET);])],
[glib_cv_langinfo_codeset=yes],
[glib_cv_langinfo_codeset=no])
AC_MSG_RESULT([$glib_cv_langinfo_codeset])
Execution of ./configure
$ ./configure
[...]
checking for nl_langinfo... yes
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 79/124
Autoconf advanced
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 80/124
Using external software
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 81/124
Package options
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 82/124
Formatting the help string
▶ To help formatting the help string, autoconf provides the AS_HELP_STRING macro
▶ Allows to properly align the different options in the ./configure --help output
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 83/124
AC_ARG_ENABLE example
configure.ac
AC_ARG_ENABLE([test], AS_HELP_STRING([--enable-test], [Enable tests]),
[echo "Action if given, val = ${enableval}"],
[echo "Action if not given"])
echo "enable_test = ${enable_test}"
./configure tests
$ ./configure --help
[...]
Optional Features:
[...]
--enable-test Enable tests
$ ./configure
[...]
Action if not given
enable_test =
[...]
$ ./configure --enable-test
[...]
Action if given, val = yes
enable_test = yes
[...]
$ ./configure --disable-test
[...]
Action if given, val = no
enable_test = no
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 84/124
Autoconf advanced
pkg-config
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 85/124
Using pkg-config with autoconf
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 86/124
The PKG_CHECK_MODULES macro
▶ Syntax:
PKG_CHECK_MODULES(prefix, list-of-modules,
action-if-found, action-if-not-found)
▶ prefix will be used to create the <prefix>_CFLAGS and <prefix>_LIBS variables
• Contain the pre-processor and linker flags to use the libraries listed in
list-of-modules
• Are already AC_SUBSTed, so can be used directly in Makefile.am
▶ list-of-modules is one or several pkg-config libraries
• Can contain version specifiers, such as foo >= 3 bar baz <= 4
▶ Will exit with a failure if one of the dependencies is missing.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 87/124
PKG_CHECK_MODULES example
configure.ac
PKG_CHECK_MODULES(DBUS1,
dbus-1 >= 1.2.14,
[AC_DEFINE(HAVE_DBUS1, 1, [Define if dbus-1 is available]) have_dbus1=yes],
have_dbus1=no)
Makefile.am
gdbus_serialization_CFLAGS = $(AM_CFLAGS) $(DBUS1_CFLAGS)
gdbus_serialization_LDADD = $(LDADD) $(DBUS1_LIBS)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 88/124
Autoconf advanced
Misc
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 89/124
autoscan
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 90/124
Additional m4 macros
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 91/124
Automake advanced
Automake advanced
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 92/124
Automake advanced
Subdirectories
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 93/124
Subdirectories
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 94/124
Recursive make
Makefile.am
SUBDIRS = src
src/Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = main.c
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 95/124
Non-recursive make
configure.ac
AM_INIT_AUTOMAKE([subdir-objects])
AC_CONFIG_FILES([Makefile])
Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = src/main.c
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 96/124
Automake advanced
Conditionals
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 97/124
automake conditionals
configure.ac
AM_CONDITIONAL([DEBUG], [test x$debug = xtrue])
Makefile.am
if DEBUG
...
else
...
endif
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 98/124
Usage of automake conditionals
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 99/124
Conditional example
configure.ac
AM_CONDITIONAL(THREADS_POSIX, [test "$g_threads_impl" = "POSIX"])
AM_CONDITIONAL(THREADS_WIN32, [test "$g_threads_impl" = "WIN32"])
AM_CONDITIONAL(THREADS_NONE, [test "$g_threads_impl" = "NONE"])
Makefile.am
libglib_2_0_la_SOURCES = \
$(deprecated_sources) \
glib_probes.d \
garray.c \
[...]
if THREADS_WIN32
libglib_2_0_la_SOURCES += gthread-win32.c
else
if THREADS_POSIX
libglib_2_0_la_SOURCES += gthread-posix.c
endif
endif
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 100/124
Automake advanced
Shared libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 101/124
Building shared libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 102/124
Libtool library example
configure.ac
[...]
LT_PREREQ([2.4])
LT_INIT
[...]
Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = src/main.c
lib_LTLIBRARIES = libmyhello.la
libmyhello_la_SOURCES = lib/core.c
include_HEADERS = lib/myhello.h
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 103/124
Libtool library example (2/2)
$ ./configure
[...]
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
[...]
$ make
[...]
$ make DESTDIR=/tmp/test install
[...]
$ find /tmp/test
/tmp/test/
/tmp/test/usr
/tmp/test/usr/local
/tmp/test/usr/local/include
/tmp/test/usr/local/include/myhello.h
/tmp/test/usr/local/bin
/tmp/test/usr/local/bin/hello
/tmp/test/usr/local/lib
/tmp/test/usr/local/lib/libmyhello.a
/tmp/test/usr/local/lib/libmyhello.la
/tmp/test/usr/local/lib/libmyhello.so
/tmp/test/usr/local/lib/libmyhello.so.0
/tmp/test/usr/local/lib/libmyhello.so.0.0.0
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 104/124
Libtool versioning
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 105/124
Libtool versioning: example
Makefile.am
lib_LTLIBRARIES = libmyhello.la
libmyhello_la_SOURCES = lib/core.c
libmyhello_la_LDFLAGS = -version-info 3:4:2
Installation
$ make DESTDIR=/tmp/p install
[...]
$ ls -l /tmp/p/usr/local/lib
-rw-r--r-- 1 thomas thomas 6224 mai 20 15:28 libmyhello.a
-rwxr-xr-x 1 thomas thomas 963 mai 20 15:28 libmyhello.la
lrwxrwxrwx 1 thomas thomas 19 mai 20 15:28 libmyhello.so -> libmyhello.so.1.2.4
lrwxrwxrwx 1 thomas thomas 19 mai 20 15:28 libmyhello.so.1 -> libmyhello.so.1.2.4
-rwxr-xr-x 1 thomas thomas 10608 mai 20 15:28 libmyhello.so.1.2.4
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 106/124
Automake advanced
Misc
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 107/124
Global automake variables
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 108/124
Per product variables
Example
LDADD = $(top_builddir)/glib/libglib-2.0.la
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 109/124
Useful variables
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 110/124
Silent rules
▶ By default, automake generate Makefiles that displays the full compilation
commands
▶ Using the AM_SILENT_RULES, you can get a slimmer build output
▶ By default, the output remains verbose, but can be silenced by passing the V=0
variable.
▶ If AM_SILENT_RULES([yes]) is used, the output is quiet by default, and verbose if
V=1 is passed.
$ make
CC lib/core.lo
CCLD libmyhello.la
CC src/main.o
CCLD hello
$ make V=1
[...]
libtool: link: (cd ".libs" && rm -f "libmyhello.so.0" && ln -s "libmyhello.so.0.0.0" ...
libtool: link: (cd ".libs" && rm -f "libmyhello.so" && ln -s "libmyhello.so.0.0.0" ...
libtool: link: ar cru .libs/libmyhello.a lib/core.o
libtool: link: ranlib .libs/libmyhello.a
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 111/124
make dist
▶ make dist generates a tarball to release the software
▶ All files listed in _SOURCES variables are automatically included, as well as the
necessary autotools files
▶ Additional files can be added to the distribution using the EXTRA_DIST variable in
Makefile.am:
Makefile.am
# These files are used in the preparation of a release
EXTRA_DIST += \
PrepareRelease \
CheckMan \
CleanTxt \
[...]
▶ Distribution can also be controlled using the dist and nodist automake product
modifiers:
Makefile.am
nodist_include_HEADERS += pcrecpparg.h
dist_doc_DATA = doc/pcre.txt
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 112/124
Macro directory
▶ By default, all the third-party autoconf macros get copied into the (very large)
aclocal.m4 file.
▶ It is possible to get some of the third-party macros copied to individiual files in a
separate directory, which is nicer.
▶ Directory declared using AC_CONFIG_MACRO_DIR, generally named m4 by
convention:
configure.ac
AC_CONFIG_MACRO_DIR([m4])
▶ The ACLOCAL_AMFLAGS in Makefile.am should also be adjusted:
Makefile.am
ACLOCAL_AMFLAGS = -I m4
▶ For now, mainly used by libtool for its own m4 macros.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 113/124
Auxiliary directory
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 114/124
Practical lab - More advanced autotools usage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 115/124
Autotools references
Autotools references
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 116/124
Existing code
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 117/124
Book: Autotools, a practitioner’s guide
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 118/124
Official documentation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 119/124
Tutorials
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 120/124
Use up to date materials
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 121/124
Last slides
Last slides
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 122/124
Last slide
Thank you!
And may the Source be with you
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 123/124
Rights to copy
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 124/124