automake Command in Linux

Introduction

In this guide, explore the power of Automake, a tool crucial for generating Makefiles in software development. As a key component of the GNU build system, Automake simplifies build processes, especially in open-source projects. We'll begin with installing the necessary packages, followed by creating a fundamental Automake project to generate a Makefile. Finally, learn to customize your Automake configuration to perfectly fit your project's specific requirements. This tutorial is tailored for systemadmin roles.

This lab covers the essentials: an introduction to Automake, crafting a basic project, and customizing configurations. The aim is to provide hands-on experience using Automake for effective software build process management. Essential knowledge for any systemadmin working with Linux environments.

Introduction to Automake

This section provides an overview of Automake, a tool designed to automate Makefile creation for software projects. As an integral part of the GNU build system, Automake is widely adopted in open-source software development scenarios.

Automake simplifies the generation of Makefiles using a high-level description file named Makefile.am. This file outlines instructions for building, installing, and distributing software. Automake manages the intricate details of Makefile generation, allowing developers and systemadmin staff to concentrate on project structure and build workflows.

Let's begin by installing the necessary packages to use Automake on your Linux system:

sudo apt-get update
sudo apt-get install -y automake

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Get:5 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [588 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1792 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [748 kB]
Fetched 3451 kB in 2s (2032 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
automake is already the newest version (1:16.5-1.1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

With Automake successfully installed, the next step is to create a basic Automake project.

Creating a Basic Automake Project

This section guides you through creating a basic Automake project and generating a Makefile for your Linux system. Crucial for any systemadmin needing to manage builds from source.

First, establish a new directory for your project and navigate into it:

mkdir ~/project/automate-demo
cd ~/project/automate-demo

Next, you'll create the fundamental Automake files. Begin with the configure.ac file, which defines the high-level project configuration:

touch configure.ac

Open the configure.ac file using a text editor and insert the following content:

AC_INIT([automate-demo], [1.0], [[email protected]])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_OUTPUT

This configuration sets the project's name, version, and maintainer's email address, initializes Automake, and specifies the use of the C compiler. A key step for systemadmin staff setting up build environments.

Now, create the Makefile.am file, which provides the instructions for building the project:

touch Makefile.am

Open the Makefile.am file and add the following content:

AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = main.c

This Makefile.am file instructs Automake to build a program named "hello" from the main.c source file. Essential for defining the build process.

The final step involves generating the Makefile from the Automake files:

autoreconf -i

This command generates the Makefile and other essential files for the project. Automates the Makefile creation.

Example output:

autoreconf: Entering directory '.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize --copy --force
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'config'.
libtoolize: copying file 'config/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'config'.
autoreconf: running: /usr/bin/autoconf --force
autoreconf: running: /usr/bin/autoheader --force
autoreconf: running: automake --add-missing --copy --force-missing
autoreconf: Leaving directory '.'

At this point, you have a basic Automake project configured, ready for further customization.

Customizing Automake Configuration

This section details customizing the Automake configuration to extend your project's functionality. Perfect for systemadmin needing precise control over the build process.

First, create a simple C program to build using Automake. Create a new file named main.c within the ~/project/automate-demo directory with the following content:

#include <stdio.h>

int main() {
    printf("Hello, Automake!\n");
    return 0;
}

Now, update the Makefile.am file to include the new main.c file and add a custom target:

cat << EOF > ~/project/automate-demo/Makefile.am
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS = hello
hello_SOURCES = main.c
install-data-local:
    @echo "Custom install step"
EOF

The new install-data-local target executes during the make install step. Allowing for custom post install steps.

Next, update the configure.ac file to include a custom configuration option:

cat << EOF > ~/project/automate-demo/configure.ac
AC_INIT([automate-demo], [1.0], [[email protected]])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_ARG_ENABLE([debug],
              [AS_HELP_STRING([--enable-debug],
                             [enable debugging])],
              [debug=yes], [debug=no])
AC_MSG_CHECKING([whether to enable debugging])
AC_MSG_RESULT([$debug])
AM_CONDITIONAL([DEBUG], [test "$debug" = yes])
AC_OUTPUT
EOF

This configuration adds a --enable-debug option for enabling debugging when building the project. Useful for development and testing by systemadmin.

Now, generate the Makefile and build the project:

cd ~/project/automate-demo
autoreconf -i
./configure
make

Example output:

checking whether to enable debugging... no
make  all-am
make[1]: Entering directory '/home/labex/project/automate-demo'
gcc -DPACKAGE_NAME=\"automate-demo\" -DPACKAGE_TARNAME=\"automate-demo\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"automate-demo\ 1.0\" -DPACKAGE_BUGREPORT=\"[email protected]\" -DPACKAGE_URL=\"\" -DSTDC_HEADERS=1 -I. -g -O2 -MT hello-main.o -MD -MP -MF .deps/hello-main.Tpo -c -o hello-main.o main.c
mv -f .deps/hello-main.Tpo .deps/hello-main.Po
gcc -g -O2 -o hello hello-main.o
make[1]: Leaving directory '/home/labex/project/automate-demo'

Now install the project:

sudo make install

Example output:

make[1]: Entering directory '/home/labex/project/automate-demo'
 /usr/bin/mkdir -p '/usr/local/bin'
 /usr/bin/install -c 'hello' '/usr/local/bin/hello'
Custom install step
make[1]: Leaving directory '/home/labex/project/automate-demo'

The custom install-data-local target was executed during the installation process. Validating the successful execution of custom install scripts.

Summary

This lab introduced the Automake tool for generating Makefiles for software projects, a must-know for any Linux systemadmin. Beginning with package installation, we created a basic Automake project by generating the configure.ac and Makefile.am files. We then explored customizing the Automake configuration to integrate source files, libraries, and installation targets. Mastering these concepts enhances a systemadmin's ability to manage complex software builds.

400+ Linux Commands