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:
- Create a new directory where to put the .beam files
- Compile all the .erl files in the source directory and put them into the previously created beam directory
- Create a directory for configuration file
- Create a directory for log file
- 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:
- Remove the directory containing the .beam files
- 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.