Home > English, Erlang > How to manage your Erlang software with a simple Makefile

How to manage your Erlang software with a simple Makefile


Most of the times you write Erlang code, you write modules that are going to be used by many other people, so it is a good idea to provide makefiles for compiling the code and installing all the things (configuration files, logs) in the right places; moreover people wants things to be easy, so why don’t why create unix scripts to start/stop our modules?

MAKE the installation easy!

Why should you use Makefiles? Well, are you bored of typing and retyping the same commands (erlc, cp, rm) over and over again, most likely for testing purposes? Yes? Than use Makefiles! The GNU make utility helps you to maintain groups of programs, with it you can easily compile, recompile, delete all of your erl files, you can manage configuration files and log files, and believe me, this a really useful! How do we write a Makefile? Let’s see a stub!

ERLC=/usr/local/bin/erlc
ERLCFLAGS=-o
SRCDIR=src
LOGDIR=/var/log/mysoftware
CONFDIR=/etc/mysoftware
BEAMDIR=./ebin

all: 
 @ mkdir -p $(BEAMDIR) ;
 @ $(ERLC) $(ERLCFLAGS) $(BEAMDIR) $(SRCDIR)/*.erl ;
 @ mkdir -p $(CONFDIR) ;
 @ mkdir -p $(LOGDIR) ;
 @ cp conf/mysoftware.conf $(CONFDIR)/mysoftware.conf-example
clean: 
 @ rm -rf $(BEAMDIR) ;
 @ rm -rf erl_crush.dump

This Makefile is really simple, but still it may be helpful for you.

First of all we declare a list of variables, the first is ERLC, which points to the Erlang compiler in the machine (note that if you installed your compiler in a different path, or if you want to use a specific Erlang release, you may want to change the path). The second variable is a flag for the compiler: the “-o” tells to the compiler that the .beam files obtained by the compilation process must be placed in the directory put after the flag itself.

The final 4 directory are used to tell the compiler where to find/put the .erl/.beam files, and where to create the directory for log/configuration files (notice that in this case the LOGDIR and the CONFDIR are set so that you will need to be superuser when running the makefile.)

Ok, let’s move on!

The first directive you find for this Makefile is “all”, whenever you run in the unix shell the command make, the instructions after this directive will be executed. The “all” directive does:

  1. Create a new directory where to put the .beam files
  2. Compile all the .erl files in the source directory and put them into the previously created beam directory
  3. Create a directory for configuration file
  4. Create a directory for log file
  5. Copy an example of configuration file into the configuration directory (notice that we do not overwrite a possible configuration file previously edited by the user

After the “all” directive, we find a “clean” directive, which contains a set of instructions dedicated to the clean up.

The “clean” directive does:

  1. Remove the directory containing the .beam files
  2. Remove a possible erl_crush.dump file

And this is it!  You may change this file and design you own one!

Notice that I’m not a “guru” in Makefiles, so if you want to suggest some change you can comment this post.

Next week we will see how to design a proper script for running our code.

Categories: English, Erlang Tags: , ,
  1. M8R
    September 21, 2011 at 11:49 am

    Hi,

    Can you please describe what is following line means I usually see in Makefiles:

    cp $< $@

    Thanks

  2. M8R
    September 21, 2011 at 1:25 pm

    Thank you for the link!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: