1. Introduction
In Linux, configure scripts are crucial to portable software installation using the Autotools suite. Without them, there would probably be no check for dependencies and no Makefile. Of course, without a Makefile, installing software with make install is improbable.
In this tutorial, we’ll discuss the creation of a configure script in Linux.
2. Install the Autotools Suite
Before creating a configure script, we must have the Autotools suite installed on our system. The Autotools set includes autoconf, libtool, and automake.
We can install the Autotools set on a Debian-based system using apt:
$ sudo apt install autoconf libtool automake
On Red Hat-based systems, we’ll install the set using dnf:
$ sudo dnf install autoconf libtool automake
3. Create the autoconf Script
To create a configure script, we must first create an autoconf script in the root of our project directory. An autoconf script may also be called a configure.ac file.
configure.ac files are essential to the portability of software installations using the Autotools toolset. For one, they facilitate the build system with details needed to automatically check for and detect dependencies. Then, since they specify the configuration of the software to be installed, they promote consistency even when apps are installed across various systems.
configure.ac files also check for inconsistencies or errors in the build process, particularly in the early stages of installation.
3.1. Generate a configure.ac Template File
We can create a configure.ac file manually by writing m4 instructions from scratch. But to make things easier, we’ll start with a template file (configure.scan) generated using autoscan:
$ autoscan
When we installed autoconf, we installed autoscan automatically.
Running autoscan in an empty directory will create two files:
$ ls
autoscan.log configure.scan
Of course, autoscan.log contains autoscan logs, including information about macro requests. Then, configure.scan contains some of the m4 macros we need to create our configure.ac file:
$ cat configure.scan
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
# Checks for programs.
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT
The configure.scan file would’ve had more macros if some of the other installation files already existed in the project directory. But since our project directory is pretty empty, we have only three macros in the configure.scan file.
Of course, we may edit and customize the configure.scan file as needed. For instance, t****o generate a Makefile.in file using automake, we must have initialized automake in the configure.ac file using the AM_INIT_AUTOMAKE macro.
Also, we must add the AC_CONFIG_FILES([Makefile]) macro if we want the configure script to generate a Makefile from the details of the Makefile.in file.
Once we’ve edited configure.scan to meet our needs, we can convert it to configure.ac by renaming it:
$ mv configure.scan configure.ac
4. Create the configure Script
Now that we finally have our configure.ac file, we can create our configure script. But before we do that, we’ll run aclocal to generate an aclocal.m4 file:
$ aclocal
$ ls
aclocal.m4 autom4te.cache autoscan.log configure.ac
If we don’t run aclocal, we might run into errors when creating our configure script.
As seen in the above output, besides the aclocal.m4 file, running aclocal creates a directory named autom4te.cache. As the directory name hints, the autom4te.cache directory stores cache files generated whenever an Autotools command invokes autom4te to compile configure.ac.
The cache autom4te generated from compiling configure.ac is pretty useful as it shortens compile time when we run other Autotools commands. So, if we run automake, autoconf, autoscan, or autoheader after the autom4te.cache has been created, the compile time will be shorter.
As long as there’s a valid configure.ac file, running other Autotools commands would’ve also created an autom4te.cache directory if it didn’t exist already.
To create our configure script, we’ll run autoconf in the directory containing the configure.ac file:
$ autoconf
After running autoconf, we’ll have our configure script:
$ ls
aclocal.m4 autom4te.cache autoscan.log configure configure.ac
In the command output above, the file named configure is the configure script.
5. Run the configure Script
Before we run the configure script, we need a Makefile.in file. Typically, Makefile.in files are complicated and tedious to write manually. So, instead, we’ll write a Makefile.am file, which uses high-level language. Then, we’ll convert the Makefile.am file to a Makefile.in file using automake –add-missing.
Once we have our Makefile.in file, we can run our configure script:
$ ./configure
This will create the Makefile:
$ ls
aclocal.m4 autoscan.log config.log configure configure.scan install-sh Makefile Makefile.in
...truncated...
Now that we have the Makefile, we can run make to build our application:
$ make
6. Conclusion
In this article, we talked about how to create a configure script in Linux. We also illustrated the uses of some Autotools commands, indicating their roles in the creation of configure scripts.