GAR: Automating Entire OS Builds

Need to automate the building of software from many sources? Here's how to ``make'' your project almost build itself.
Why GAR?

One way to think of GAR is as a consistent interface for compiling a piece of software. No matter what mechanism the program uses to compile or install, a make install will install the program. The user need not care whether a program is a Python script or a C program that uses Imake.

In fact, the system will even satisfy dependencies. If the robotfindskitten program uses libncurses, the GAR system will pause the robotfindskitten build process right before the configure stage and then go install libncurses.

The GAR system is also centrally configurable, allowing the user to specify installation directories, CFLAGS and other useful environment variables. It is possible to configure default download locations, so that you can grab the code from a local cache or a nearby mirror of all the files.

GAR was designed with the notion that the build system and the target system may be different machines. It is also possible that the build and target system are different architectures. The following changes to show a possible way to configure GAR for a cross-compiling environment:

prefix = /usr/local/build/mips-build
LDFLAGS += -L$(libdir) -L/usr/local/lib-mips
CC = /usr/local/bin/gcc-mipsel
LD_LIBRARY_PATH += :$(libdir):/usr/local/lib-mips

All of these settings will propagate down into each individual package.

Building a GAR Package

The best way to understand the process of making a new package is to look at an example. The following is the relevant parts of the grep package:

GARNAME = grep
include ../../

The first few lines set some basic bookkeeping information. The GARNAME and GARVERSION refer to the package name and version string as the GAR system will manipulate them. They are also used to create the convenience variable DISTNAME, which is $(GARNAME)-$(GARVERSION) by default, since most GNU or automake-using packages name their tarballs in that fashion.

The actual build of the system depends on the CONFIGURE_SCRIPTS, BUILD_SCRIPTS and INSTALL_SCRIPTS variables. These point to a space-separated list of files that are essential to the configure, build and install steps (respectively).

The GAR system will know what to do with most types of scripts. It knows to run CONFIGURE_SCRIPTs named ``configure'', and to run make or make install in the directory where a Makefile lives. It also knows that Imakefiles use xmkmf, and so forth. For most packages, no extra work needs to be done here.

We usually need to specify the CONFIGURE_ARGS to include the directory settings that we define in This currently requires setting it to include $(DIRPATHS).

Finally, we include the library. This takes all of the variable setting we've done and puts it to good use. This allows your short Makefile to provide the seven basic targets described above. It must come last (or, at least, after all of the variables have been set) in order to function properly.


The automatic satisfaction of dependencies is deceptively simple. In order to specify a library dependency, for example, simply set the LIBDEPS variable to be a space-separated list of paths from the base GAR directory to the package you depend on. For example, the GNU parted program has the following:

LIBDEPS = lib/e2fsprogs-libs

Before the configure step is run, GAR will cd into ../../lib/e2fsprogs-libs/ and run make install.

Sometimes Defaults Aren't Enough

Quite often, the default behavior isn't what one would hope. You can pass parameters to a package's configure script with CONFIGURE_ARGS, but what if the GAR system doesn't know about your configuration script type? What if the configuration steps aren't enough? What if they're flat-out wrong? What if the same is true for the fetch, extract or install rules?

Fortunately, the system provides for this in a number of ways. What follows are a few of the mechanisms that a package maintainer can use to override or enhance the default behaviors.

For each of the seven basic targets, there exists slots for per-package pre- and post- rules. That means that the package maintainer can specify work to be done immediately before or after a rule.

To define a rule in make, simply place it at the beginning of a line, followed by a colon. The shell commands it should execute follow, preceded by a tab on each line.

As an example, let's consider the util-linux package. It doesn't use a standard autoconf-style configure script, but it can be configured by setting a variable at the top of the MCONFIG file. Thus, the end of our utils/util-linux/Makefile looks like the following:

    echo "DESTDIR=$(prefix)" > $(WORKSRC)/MCONFIG.NEW
include ../../

So before the configure script is run, the package-defined preconfigure rule adds code setting DESTDIR to the $(prefix) variable in MCONFIG.

The $(MAKECOOKIE) variable is a macro that creates a cookie file signifying the completion of the preconfigure step; $(MAKECOOKIE) performs some housekeeping that ensures rules are run only once, so it's important to end each rule with $(MAKECOOKIE).

As another example, the Bourne Again SHell (bash) can be linked to with the name ``sh'' in order to cause it to behave (somewhat) like a POSIX Bourne Shell. To do this, the end of our shells/bash/Makefile looks like:

    (cd $(bindir); ln -sf bash sh)
include ../../

Thus creating the symbolic link to sh.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Minor typo

Dagobert Michelsen's picture

I/O redirection should go in the other direction:


Re: GAR: Automating Entire OS Builds

dmarti's picture

In accordance with Linux Journal's "Artistic Immortality Policy", a revised version of this article, which includes information on new features of GAR, is available from the GAR web site.