Slides présentés lors du Meetup Aix Marseille Embedded Linux du 16 Juin.
Présentation sur les GNU/Autotools accompagné d'un exemple Hello World basé sur les autotools.
Administration et logiciel libre - Guide pour les marchés publics
Autotools
1. Autotools - 16 Juin 2015 1
www.ciose.fr
AutotoolsAutotools
C. CharreyreC. Charreyre
christian.charreyre@ciose.frchristian.charreyre@ciose.fr
http://www.ciose.frhttp://www.ciose.fr
@CIOinfoindus@CIOinfoindus
2. Autotools - 16 Juin 2015 2
www.ciose.fr
Licence
Attribution-Noncommercial-Share Alike 4.0 International
You are free:
to Share - copy and redistribute the material in any medium or format
to Adapt - remix, transform, and build upon the material
The licensor cannot revoke these freedoms as long as you follow the license terms.
Under the following conditions:
Attribution — You must give appropriate credit, provide a link to the license, and indicate if
changes were made. You may do so in any reasonable manner, but not in any way that
suggests the licensor endorses you or your use.
NonCommercial — You may not use the material for commercial purposes.
ShareAlike — If you remix, transform, or build upon the material, you must distribute your
contributions under the same license as the original.
No additional restrictions — You may not apply legal terms or
technological measures that legally restrict others from doing
anything the license permits.
License text : http://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
3. Autotools - 16 Juin 2015 3
www.ciose.fr
Crédits
Document utilisé pour bâtir cette présentation :
Using GNU Autotools – Alexandre Duret-Lutz – 16
Mai 2010
4. Autotools - 16 Juin 2015 4
www.ciose.fr
CIO en quelques mots
Société d'ingénierie en systèmes embarqués :
électronique et logiciel
Au service de nos clients depuis 1990
15 spécialistes en embarqué et temps réel
Expert en Linux embarqué depuis 2000,
OpenEmbedded et Yocto depuis 2008
CA annuel : 1 500 000 €
Siège à Saint-Etienne
Agence à Marseille
Agréé CIR
5. Autotools - 16 Juin 2015 5
www.ciose.fr
C. Charreyre en quelques
mots
Associé au sein de CIO
Responsable des technologies Linux embarqué
Formateur Linux embarqué (avec Captronic et
en direct)
30 ans dans l'embarqué et le monde Unix / Linux
Fervent promoteur du logiciel libre
Membre de Medinsoft – Commission Logiciel
Libre
6. Autotools - 16 Juin 2015 6
www.ciose.fr
Pourquoi les Autotools ?
Dans l'univers du libre, on distribue le code
source
C'est l'utilisateur qui compile le code
Mais l'utilisateur n'a pas forcément le même
environnement logiciel que le développeur
Nécessité de sources portables
Nécessité d'un système de build adaptatif
Autotools = système de génération de Makefiles
adaptés à l'environnement logiciel de l'utilisateur,
bâtis dynamiquement avant compilation
Utile dans le cas de l'embarqué : ressources
logicielles réduites, développement croisé ….
7. Autotools - 16 Juin 2015 7
www.ciose.fr
Les rôles
L'utilisateur final :
Extrait, configure, compile et installe le logiciel
tar xzvf prog.tar.gz, ./configure,
make, sudo make install
Le développeur
Fournit un système de configuration dynamique des
Makefiles (en particulier flags compilateur et linker)
Fournit des contrôles de prérequis logiciel lors de la phase
de configuration
Écrit ses sources de manière portable avec des
compilations conditionnelles dépendant des résultats de
configuration dynamique
Fonction présente ou pas
Signatures fonction variable
Fichiers d'include variables ….
Options activées ou pas
8. Autotools - 16 Juin 2015 8
www.ciose.fr
Les rôles
Le développeur est utilisateur final sur son poste
de développement lorsqu'il exécute pour lui
même
./configure, make, sudo make install
9. Autotools - 16 Juin 2015 9
www.ciose.fr
Autoconf
Couvre les utilitaires autoconf,
autoheader, autoscan, autoreconf,
autoupdate
Fonction principale :
Créer configure et config.h.in à partir de
configure.ac
configure.ac écrit par le développeur
configure généré par autoconf – shell script de
configuration lancé avant compilation. Assure la
configuration dynamique de la chaîne de compilation.
config.h.in généré par autoheader – template
de fichier d'include pour les compilations
conditionnelles liés au résultat de configuration.
Permet la portabilité des sources
10. Autotools - 16 Juin 2015 10
www.ciose.fr
Autoconf
Autres fonctions :
autoscan crée un 1er
template de fichier
configure.ac nommé configure.scan, par
analyse du répertoire de travail
autoreconf appelle les divers outils en séquence,
dans le bon ordre – à utiliser au tout début
autoupdate actualise configure.ac en cas de
changement de version
autoconf exécuté par le développeur
11. Autotools - 16 Juin 2015 11
www.ciose.fr
Automake
Couvre les utilitaires automake, aclocal
Fonction principale :
Créer le(s) fichier(s) Makefile.in à partir du(des)
template(s) Makefile.am
Autres fonctions :
aclocal crée le fichier aclocal.m4 (macros en
langage m4) à partir de configure.ac
automake exécuté par le développeur.
12. Autotools - 16 Juin 2015 12
www.ciose.fr
configure
configure crée le fichier config.h et le(s)
fichier(s) Makefile à partir de config.h.in et
Makefile.in
configure est exécuté par l'utilisateur final
13. Autotools - 16 Juin 2015 13
www.ciose.fr
Récapitulatif
Crédit : Using GNU Autotools – Alexandre Duret-Lutz
Actions développeur
14. Autotools - 16 Juin 2015 14
www.ciose.fr
Récapitulatif
Actions utilisateur final
15. Autotools - 16 Juin 2015 15
www.ciose.fr
Un exemple : autohello
Création d'un programme Hello World basé
autotools
Structure standard :
Répertoire autohello comprend configure.ac et
Makefile.am
Sources dans un sous répertoire src
16. Autotools - 16 Juin 2015 16
www.ciose.fr
Autohello : top level
directory
Fichier Makefile.am
/tmp/autohello : cat Makefile.am
SUBDIRS = src
Chaînage vers
le répertoire src
17. Autotools - 16 Juin 2015 17
www.ciose.fr
Autohello : top level
directory
Fichier configure.ac
/tmp/autohello : cat configure.ac
# * Autoconf *
# Process this file with autoconf to produce a
configure script.
# Define version major, minor and micro number
m4_define([autohello_version_major], [1])
m4_define([autohello_version_minor], [0])
m4_define([autohello_version_micro], [0])
m4_define([autohello_version],
[autohello_version_major.autohello_version_minor.
autohello_version_micro])
Définition des
éléments de
version
23. Autotools - 16 Juin 2015 23
www.ciose.fr
Autohello : top level
directory
Fichier configure.ac (continued)
AC_PREREQ([2.63])
AC_INIT([autohello], [autohello_version],
[christian.charreyre@ciose.fr])
AC_CONFIG_SRCDIR([src/autohello.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([foreign])
AUTOHELLO_VERSION_MAJOR=autohello_version_major
AUTOHELLO_VERSION_MINOR=autohello_version_minor
AUTOHELLO_VERSION_MICRO=autohello_version_micro
AUTOHELLO_VERSION=autohello_version
AC_SUBST(AUTOHELLO_VERSION_MAJOR)
AC_SUBST(AUTOHELLO_VERSION_MINOR)
AC_SUBST(AUTOHELLO_VERSION_MICRO)
AC_SUBST(AUTOHELLO_VERSION)
Macros nécessaires
pour générer Makefiles
(lien vers automake)
24. Autotools - 16 Juin 2015 24
www.ciose.fr
Autohello : top level
directory
Fichier configure.ac (continued)
AC_PREREQ([2.63])
AC_INIT([autohello], [autohello_version],
[christian.charreyre@ciose.fr])
AC_CONFIG_SRCDIR([src/autohello.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE([foreign])
AUTOHELLO_VERSION_MAJOR=autohello_version_major
AUTOHELLO_VERSION_MINOR=autohello_version_minor
AUTOHELLO_VERSION_MICRO=autohello_version_micro
AUTOHELLO_VERSION=autohello_version
AC_SUBST(AUTOHELLO_VERSION_MAJOR)
AC_SUBST(AUTOHELLO_VERSION_MINOR)
AC_SUBST(AUTOHELLO_VERSION_MICRO)
AC_SUBST(AUTOHELLO_VERSION)
Affectation des valeurs
définies par m4_define
Substitution des valeurs
dans les fichiers
générés
25. Autotools - 16 Juin 2015 25
www.ciose.fr
Autohello : top level
directory
Fichier configure.ac (continued)
# Checks for programs.
AC_PROG_CC
# Checks for header files.
AC_CHECK_HEADERS([stdio.h stdlib.h])
# Generate files
AC_CONFIG_FILES([Makefile
src/Makefile
src/autohelloversion.h])
AC_OUTPUT
26. Autotools - 16 Juin 2015 26
www.ciose.fr
Autohello : top level
directory
Fichier configure.ac (continued)
# Checks for programs.
AC_PROG_CC
# Checks for header files.
AC_CHECK_HEADERS([stdio.h stdlib.h])
# Generate files
AC_CONFIG_FILES([Makefile
src/Makefile
src/autohelloversion.h])
AC_OUTPUT
Vérification présence
compilateur C
27. Autotools - 16 Juin 2015 27
www.ciose.fr
Autohello : top level
directory
Fichier configure.ac (continued)
# Checks for programs.
AC_PROG_CC
# Checks for header files.
AC_CHECK_HEADERS([stdio.h stdlib.h])
# Generate files
AC_CONFIG_FILES([Makefile
src/Makefile
src/autohelloversion.h])
AC_OUTPUT
Vérification
présence
headers requis
28. Autotools - 16 Juin 2015 28
www.ciose.fr
Autohello : top level
directory
Fichier configure.ac (continued)
# Checks for programs.
AC_PROG_CC
# Checks for header files.
AC_CHECK_HEADERS([stdio.h stdlib.h])
# Generate files
AC_CONFIG_FILES([Makefile
src/Makefile
src/autohelloversion.h])
AC_OUTPUT
Fichiers à
créer
29. Autotools - 16 Juin 2015 29
www.ciose.fr
Autohello : src directory
Fichier autohello-version.h.in
/tmp/autohello/src : cat autohelloversion.h.in
/**
* @file autohelloversion.h
*
* This header files defines parts if the software
version.
*
* @author Christian Charreyre
<christian.charreyre@ciose.fr>
*/
#ifndef _AUTOHELLO_VERSION_H_
#define _AUTOHELLO_VERSION_H_
/** The major version, (1, if %AUTOHELLO_VERSION is
1.2.3) */
#define AUTOHELLO_VERSION_MAJOR
(@AUTOHELLO_VERSION_MAJOR@)
Remplacé par valeur définie
lors du configure
30. Autotools - 16 Juin 2015 30
www.ciose.fr
Autohello : src directory
Fichier autohello-version.h.in (continued)
/** The minor version (2, if %AUTOHELLO_VERSION is
1.2.3) */
#define AUTOHELLO_VERSION_MINOR
(@AUTOHELLO_VERSION_MINOR@)
/** The micro version (3, if %AUTOHELLO_VERSION is
1.2.3) */
#define AUTOHELLO_VERSION_MICRO
(@AUTOHELLO_VERSION_MICRO@)
/** The full version, like 1.2.3 */
#define AUTOHELLO_VERSION @AUTOHELLO_VERSION@
/** The full version, in string form (suited for
string concatenation)
*/
#define AUTOHELLO_VERSION_STRING "@AUTOHELLO_VERSION@"
#endif /* _AUTOHELLO_VERSION_H_ */
Remplacé par valeur définie
lors du configure
31. Autotools - 16 Juin 2015 31
www.ciose.fr
Autohello : src directory
Fichier autohello.c
/tmp/autohello/src : cat autohello.c
/**
* @file autohello.c
*
* This file implements simple hello world program.
*
* @author Christian Charreyre
<christian.charreyre@ciose.fr>
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "autohelloversion.h"
int main(int argc, char **argv)
{
printf("Hello world version %sn",
AUTOHELLO_VERSION_STRING);
printf("Report bugs to %sn", PACKAGE_BUGREPORT);
exit(0);
}
32. Autotools - 16 Juin 2015 32
www.ciose.fr
Autohello : src directory
Fichier autohello.c
/tmp/autohello/src : cat autohello.c
/**
* @file autohello.c
*
* This file implements simple hello world program.
*
* @author Christian Charreyre
<christian.charreyre@ciose.fr>
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "autohelloversion.h"
int main(int argc, char **argv)
{
printf("Hello world version %sn",
AUTOHELLO_VERSION_STRING);
printf("Report bugs to %sn", PACKAGE_BUGREPORT);
exit(0);
}
Présence headers
vérifiée durant
configure
33. Autotools - 16 Juin 2015 33
www.ciose.fr
Autohello : src directory
Fichier autohello.c
/tmp/autohello/src : cat autohello.c
/**
* @file autohello.c
*
* This file implements simple hello world program.
*
* @author Christian Charreyre
<christian.charreyre@ciose.fr>
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "autohelloversion.h"
int main(int argc, char **argv)
{
printf("Hello world version %sn",
AUTOHELLO_VERSION_STRING);
printf("Report bugs to %sn", PACKAGE_BUGREPORT);
exit(0);
}
config.h généré
par configure
34. Autotools - 16 Juin 2015 34
www.ciose.fr
Autohello : src directory
Fichier autohello.c
/tmp/autohello/src : cat autohello.c
/**
* @file autohello.c
*
* This file implements simple hello world program.
*
* @author Christian Charreyre
<christian.charreyre@ciose.fr>
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "autohelloversion.h"
int main(int argc, char **argv)
{
printf("Hello world version %sn",
AUTOHELLO_VERSION_STRING);
printf("Report bugs to %sn", PACKAGE_BUGREPORT);
exit(0);
}
autohello-version.h généré
par configure
35. Autotools - 16 Juin 2015 35
www.ciose.fr
Autohello : src directory
Fichier autohello.c
/tmp/autohello/src : cat autohello.c
/**
* @file autohello.c
*
* This file implements simple hello world program.
*
* @author Christian Charreyre
<christian.charreyre@ciose.fr>
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "autohelloversion.h"
int main(int argc, char **argv)
{
printf("Hello world version %sn",
AUTOHELLO_VERSION_STRING);
printf("Report bugs to %sn",
PACKAGE_BUGREPORT);
exit(0);
}
Défini dans
autohello-version.h
36. Autotools - 16 Juin 2015 36
www.ciose.fr
Autohello : src directory
Fichier autohello.c
/tmp/autohello/src : cat autohello.c
/**
* @file autohello.c
*
* This file implements simple hello world program.
*
* @author Christian Charreyre
<christian.charreyre@ciose.fr>
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "autohelloversion.h"
int main(int argc, char **argv)
{
printf("Hello world version %sn",
AUTOHELLO_VERSION_STRING);
printf("Report bugs to %sn",
PACKAGE_BUGREPORT);
exit(0);
}
Défini dans
config.h
43. Autotools - 16 Juin 2015 43
www.ciose.fr
Ajout option configuration
Ajout d'une option de configuration pour utiliser
gcc ou colorgcc
cat configure.ac
# Checks for programs.
AC_PROG_CC
# compiler option (gcc or colorgcc)
AC_ARG_ENABLE(colorgcc,
AS_HELP_STRING([enablecolorgcc],
[enable compiling with colorgcc]),
[use_colorgcc=$enableval], [use_colorgcc=no])
if test "x$use_colorgcc" = "xyes"; then
# we need colorgcc:
AC_CHECK_PROG([COLORGCC], [colorgcc], [yes], [no])
if test "$COLORGCC" == "yes"; then
AC_MSG_NOTICE([Compiling using colorgcc])
CC=colorgcc
else
AC_MSG_ERROR([colorgcc not found])
fi
fi
44. Autotools - 16 Juin 2015 44
www.ciose.fr
Utilisation option
configuration
./configure –help
Optional Features:
disableoptionchecking ignore unrecognized
enable/with options
disableFEATURE do not include FEATURE
(same as enableFEATURE=no)
enableFEATURE[=ARG] include FEATURE [ARG=yes]
enablesilentrules less verbose build output
(undo: "make V=1")
disablesilentrules verbose build output (undo:
"make V=0")
enabledependencytracking
do not reject slow
dependency extractors
disabledependencytracking
speeds up onetime build
enablecolorgcc enable compiling with
colorgcc
45. Autotools - 16 Juin 2015 45
www.ciose.fr
Utilisation option
configuration
Par défaut ./configure n'utilise pas colorgcc
Utilisation avec l'option –enablecolorgcc
./configure –enablecolorgcc
…
checking dependency style of gcc... gcc3
checking for colorgcc... yes
configure: Compiling using colorgcc
checking how to run the C preprocessor... colorgcc E
checking for grep that handles long lines and
e... /bin/grep
…
make
…
colorgcc DHAVE_CONFIG_H I. I.. D_GNU_SOURCE
Wall Werror g O2 MT autohello.o MD MP MF
.deps/autohello.Tpo c o autohello.o autohello.c
46. Autotools - 16 Juin 2015 46
www.ciose.fr
Configuration librairie
Utilisation conditionnelle de la librairie glib
cat configure.ac
# Checks for header files.
AC_CHECK_HEADERS([stdio.h stdlib.h])
# Check for libraries.
PKG_CHECK_MODULES([glib], [glib2.0 >= 2.40.0],
[AC_DEFINE([HAVE_GLIB], [1], [Use Glib])])
CPPFLAGS="$glib_CFLAGS"
LDADD="$glib_LIBS"
AC_SUBST([CPPFLAGS])
AC_SUBST([LDADD])
Recherche glib-2.0 >= 2.40.0
Définition constante HAVE_GLIB
dans config.h
Définition CPPFLAGS et LDADD
en fonction présence
Glib-2.0
47. Autotools - 16 Juin 2015 47
www.ciose.fr
Configuration librairie
cat src/autohello.c
#include "_builddate.h"
#ifdef HAVE_GLIB
#include <glib.h>
#endif
int main(int argc, char **argv)
{
#ifdef HAVE_GLIB
GString *text;
text=g_string_new("");
g_string_printf(text, "Hello world version %s built
on %sn",
AUTOHELLO_VERSION_STRING, _BUILD_DATE);
printf("%s", (char*)(text>str));
#else
Inclusion conditionnelle
Code si glib-2.0 disponible
48. Autotools - 16 Juin 2015 48
www.ciose.fr
Configuration librairie
#else
printf("Hello world version %s built on %sn",
AUTOHELLO_VERSION_STRING, _BUILD_DATE);
#endif
exit(0);
}
Code si glib-2.0 non disponible
49. Autotools - 16 Juin 2015 49
www.ciose.fr
Créer une archive
distribuable
Cibles dédiées à la distribution : distxxx, xxx
formats de compression variés
make distbzip2
…
ls *.tar.*
autohello1.0.2.tar.bz2