Autotools Tutorial Part 3 of 3

In order to illustrate template modification, we just add a target in the Makefile template to define the build date :

~/autohello/src$ cat Makefile.am

bin_PROGRAMS = autohello
 
autohello_SOURCES = autohello.c
 
CLEANFILES = *~
AM_CPPFLAGS = -D_GNU_SOURCE
AM_CFLAGS = -Wall -Werror
 
_build-date.h: force
    date +'#define _BUILD_DATE "%d/%m/%Y"' > _build-date.h
 
force:

We also add the file inclusion in source code and add _BUILD_DATE macro in printf :

~/autohello/src$ cat autohello.c

/**
 * @file autohello.c
 * 
 * This file implements simple hello world program.
 * 
 * @author Xxx Yyy 
 */
#include 
#include 
#include "config.h"
#include "autohello-version.h"
#include "_build-date.h"
 
int main(int argc, char **argv)
{
    printf("Hello world version %s built on %s\n",
            AUTOHELLO_VERSION_STRING, _BUILD_DATE);
    exit(0);
}

Now just create _build-date.h for the first time (after that it will be auto generated at each compilation, due to the recorded dependency from autohello.c  to _build_date.h), then run make again :

~/autohello/src$ make _build-date.h
cd .. && /bin/bash /home/cch/autohello/missing automake-1.14 --foreign src/Makefile
cd .. && /bin/bash ./config.status src/Makefile depfiles
config.status: creating src/Makefile
config.status: executing depfiles commands
date +'#define _BUILD_DATE "%d/%m/%Y"' > _build-date.h
~/autohello/src$ make
gcc -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
mv -f .deps/autohello.Tpo .deps/autohello.Po
gcc -Wall -Werror -g -O2   -o autohello autohello.o
~/autohello/src$ ./autohello
Hello world version 1.0.0 built on 11/06/2015
~/autohello/src$

The Makefile is automatically recreated before compilation.

Now we can add a configuration option to enable or disable color-gcc use instead of gcc (to colorize compilation results), and bump software micro version :

~/autohello/src$ cd ..
~/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], [1])
 
m4_define([autohello_version],
        [autohello_version_major.autohello_version_minor.autohello_version_micro])
 
AC_PREREQ([2.63])
AC_INIT([autohello], [autohello_version], [xxx dot yyy at company dot com])
AC_CONFIG_SRCDIR([src/autohello.c])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
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)
 
# Checks for programs.
AC_PROG_CC
 
# compiler option (gcc or colorgcc)
AC_ARG_ENABLE(colorgcc,
AS_HELP_STRING([--enable-colorgcc],
        [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
 
# Checks for header files.
AC_CHECK_HEADERS([stdio.h stdlib.h])
 
# Generate files
AC_CONFIG_FILES([Makefile
    src/Makefile
    src/autohello-version.h])
AC_OUTPUT

If we run make again, then the configure script is automatically rebuilt and executed before compilation is performed :

~/autohello$ make
cd .. && make  am--refresh
make[1]: entrant dans le répertoire « /home/cch/autohello »
CDPATH="${ZSH_VERSION+.}:" && cd . && /bin/bash /home/cch/autohello/missing aclocal-1.14
cd . && /bin/bash /home/cch/autohello/missing automake-1.14 --foreign
CDPATH="${ZSH_VERSION+.}:" && cd . && /bin/bash /home/cch/autohello/missing autoconf
/bin/bash ./config.status --recheck
running CONFIG_SHELL=/bin/bash /bin/bash ./configure --no-create --no-recursion
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
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
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking stdio.h usability... yes
checking stdio.h presence... yes
checking for stdio.h... yes
checking for stdlib.h... (cached) yes
checking that generated files are newer than configure... done
configure: creating ./config.status
/bin/bash ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating src/autohello-version.h
config.status: creating config.h
config.status: executing depfiles commands
make[1]: quittant le répertoire « /home/cch/autohello »
make[1]: entrant dans le répertoire « /home/cch/autohello »
make[1]: quittant le répertoire « /home/cch/autohello »
make[1]: entrant dans le répertoire « /home/cch/autohello »
make[1]: quittant le répertoire « /home/cch/autohello »
make[1]: entrant dans le répertoire « /home/cch/autohello »
make[1]: quittant le répertoire « /home/cch/autohello »
date +'#define _BUILD_DATE "%d/%m/%Y"' > _build-date.h
gcc -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
mv -f .deps/autohello.Tpo .deps/autohello.Po
gcc -Wall -Werror -g -O2   -o autohello autohello.o
~/autohello$

Thanks to this regeneration and execution of configure, we are sure to compile with up to date configuration template.

We can now reconfigure to use color-gcc :

~/autohello$ ./configure --enable-colorgcc
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
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
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
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
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking stdio.h usability... yes
checking stdio.h presence... yes
checking for stdio.h... yes
checking for stdlib.h... (cached) yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating src/autohello-version.h
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
~/autohello$
~/autohello$
~/autohello$ make
(CDPATH="${ZSH_VERSION+.}:" && cd . && /bin/bash /home/cch/autohello/missing autoheader)
rm -f stamp-h1
touch config.h.in
cd . && /bin/bash ./config.status config.h
config.status: creating config.h
config.status: config.h is unchanged
make  all-recursive
make[1]: entrant dans le répertoire « /home/cch/autohello »
Making all in src
make[2]: entrant dans le répertoire « /home/cch/autohello/src »
date +'#define _BUILD_DATE "%d/%m/%Y"' > _build-date.h
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
mv -f .deps/autohello.Tpo .deps/autohello.Po
colorgcc -Wall -Werror -g -O2   -o autohello autohello.o  
make[2]: quittant le répertoire « /home/cch/autohello/src »
make[2]: entrant dans le répertoire « /home/cch/autohello »
make[2]: quittant le répertoire « /home/cch/autohello »
make[1]: quittant le répertoire « /home/cch/autohello »
~/autohello$

If you want to try this tutorial, you can download the files : autohello 1.0.1 archive.

Partager cet article

Partager sur facebook
Partager sur twitter
Partager sur linkedin
Partager sur pinterest
Partager sur print
Partager sur email

UN BLOG DE

CIO Systèmes Embarqués – 1 Rue de la Presse, 42 000 Saint-Étienne – contact@ciose.fr – 04 77 93 34 32 

CIO  Systèmes Embarqués est le nom commercial de la SAS CIO Informatique Industrielle 

CIO Informatique Industrielle © 1991-2020 v3.0

Mentions Légales