2006-02-13

Note: I've expanded this entry in article Configuration.

In a non-trivial project, several programming languages will often be used. Keeping a common configuration file can become something of a nuisance. While it's easy enough to decide on one format, things like variables aren't easily used. For instance, you want to be able to have stuff like this:

  user=testuser_1
  bindir=/home/$USER/bin

Which will work fine for a file that you include in a Makefile. But for a PHP script, this is a pain. This is where m4 comes into play.

Just create a file called Config.m4 and put all your variables there in the following format:

  define(_USER_,testuser_1)dnl
  define(_GROUP_,testgroup)dnl

Then create a basic configuration in a file called Config.template as follows:

  user=_USER_
  group=_GROUP_
  bindir=/home/_USER_/bin

As part of your Makefile or Ant's build.xml, run m4 as follows:

  m4 Config.m4 Config.template > Config.h

VoilĂ , problems solved!