Sunday, 17 July 2016

Generate configure & Makefile in linux

If you are not using a installer for installing any software in linux, you will need to follow these 3 steps:
User is always provided with 'configure' & 'Makefile' scripts.
1) ./configure
2) make
3) make install

1)Configuration File
Configuration file checks whether all dependencies required for program/software installation are installed on your system. It generates Makefile using Makefile.in.
 2)make (build your project)
make use Makefile script to build project. Makefile contains all the information how the project should be compiled like compiler, paths, file dependencies etc.
3)make install
After building the project make install only copies final  executable files to destination paths like PATH. It also copies man page in corresponding man pages directory.

Autogenerate Configuration file in linux

Configuration script uses Makefile.in to generate Makefile which are further used by make  to build the project.

Writing configuration files is a tidy job. Hence, we are going to auto-generate configuration files.
For auto-generating configuration files we need to use m4 macro based package autotools which consist of autoconfig, automake etc.

Lets get started with a small project to auto-generate config files.
1) Install autotools using:
apt-get update
apt-get install autoconfig

Also install m4 macro processor:

Downloading m4:

Run the command below,
wget ftp://ftp.gnu.org/gnu/m4/m4-1.4.10.tar.gz

Extracting files from the downloaded package:

tar -xvzf m4-1.4.10.tar.gz
Now, enter the directory where the package is extracted.
cd m4-1.4.10

Configuring m4:

./configure --prefix=/usr/local/m4
Replace "/usr/local/m4" above with the directory path where you want to copy the files and folders. Note: check for any error message.

Compiling m4:

make
2) First we need to write a basic C program main.c:

#include<stdio.h>
int main()
{
    printf("Hello World!\n");
    return 0;
}

3)We need to write a script configure.ac :
AC_INIT([helloworld], [0.5], [anything@myname.com]) AM_INIT_AUTOMAKE AC_PROG_CC AC_CONFIG_FILES([Makefile]) AC_OUTPUT

Briefs:
Initializing autoconf:
AC_INIT initialize autoconf & initialize our target project "helloworld" with version ".5" & maintainer information "anything@myname.com".

Initialize automake:
AM_INIT_AUTOMAKE It is used to initialize automake which we will use to generate Makefile from Makefile.in .
Makefile.in is a file enlisting the sources to be used by configuration file for generating final Makefile.

Adding Project dependencies:
AC_PROG_CC In our proect we only need a C compiler. So, this line only checks for a C compiler in our system.
WE can also add other dependencies using AC_PATH_PROG for a given program at user PATH

Inform configure file to use Makefile 
AC_CONFIG_FILES([Makefile]) This macro tells autoconf that configure file should use Makefile.in to generate Makefile .

Display script output
AC_OUTPUT This macro displays script output to screen.

4) Creating Makefile.in
Makefile.in is a big & complex file, So, we generate Makefile.in using Makefile.am . Automake will use Makefile.am to generate Makefile.in .

Makefile.am :

AUTOMAKE_OPTIONS = foreign bin_PROGRAMS = helloworld helloworld_SOURCES = main.c

Brief:
AUTOMAKE_OPTIONS = foreign 
AUTOMAKE_OPTIONS specifies project layout. As we are not using standard GNU project, hence we are using foreign project.

bin_PROGRAMS = helloworld
This specifies target project to be helloworld.

helloworld_SOURCES = main.c
ProgramName_SOURCES specifies source files for helloworld project. We can add multiple source files.

5) Combining  all
Now we have configure.ac & Makefile.am
a)Set m4 environment to use autotools:
aclocal
b) Now we run autoconf to convert configure.ac to configure & automake to convert Makefile.am to Makefile.
autoconf
automake --add-missing

Using autotools we have finally generated 'configure' & 'Makefile' scripts. Now lets build our project:

./configure
make

Run:
./helloworld
= Hello World!


No comments:

Post a Comment