Blog Categories
Ad Space
Twitter Updates
    follow me on Twitter
    Currently Reading
    Powered by Squarespace
    Git Projects
    « My top 5 Favourite Erlang Functions | Main | Setting up a Ruby 1.9 Sandbox on Leopard »
    Thursday
    21Aug

    Erlang - Make, Rake and Emake

    When it comes to building your Erlang application there is no shortage of possibilities.

    Directory Setup

    The bare setup of the simplest Erlang program are 2 directories. Namely src and ebin. For beginners these are the ones you will most likely use for your first few programs. There are a few others however. Lets go over the whole list.

    • ./bin In here you will put shell applications to, for example, start your application.
    • ./doc Your Generated Documents.
    • ./ebin Compiled erlang source code for the vm.
    • ./include Used for include files.
    • ./priv Used for application specific files. For example, C executables are placed here.
    • ./src Your source code goes here.

    Make-ing

    There are 3 main methods of building Erlang programs. Make, Emake and Rake. I generally dislike makefiles so I will focus on the later 2.

    EMake

    EMake is Erlangs own make utility. It's quite simple but not very powerful. To get started you can create a file called Emakefile in your project root and list your sourcefiles.

    {'src/myerlang.erl', [{outdir, "ebin"}]}.
    {'src/myerlang_app.erl', [{outdir, "ebin"}]}.
    {'src/myerlang_supervisor.erl', [{outdir, "ebin"}]}.
    

    Now you can build your Erlang program by typing in you terminal erl -make. Often we the also use a Makefile to trigger the erlang make. The reason is that sometimes we want to add more commands to do various management tasks. Take this Makefile for example.

    all: compile
    
        compile:
            @erl -make
    
        clean:
            rm -f ebin/*.beam
            rm -f erl_crash.dump
    
        run:
            erl -sname console -pa ebin
    

    Here we have 3 available commands in our Terminal. Entering make will compile our code and make clean will remove all the compiled code. make run will start an erlang shell.

    Rake

    The method I prefer is using Rakefiles. Rake is a powerful and simple alternative to Make created in Ruby. Here is my Rakefile based on Sean's example. This goes in your project root in a file called Rakefile.

    require 'rake'
    require 'rake/clean'
    
    # Configuration
    START_MODULE = "myerlang"
    TEST_MODULE = "test_myerlang"
    MNESIA_DIR = "/tmp"
    
    
    # No Need to change
    PWD = `pwd`.strip
    INCLUDE = "include"
    ERLC_FLAGS = "-I#{INCLUDE} +warn_unused_vars +warn_unused_import"
    
    SRC = FileList['src/**/*.erl']
    OBJ = SRC.pathmap("%{src,ebin}X.beam")
    CLEAN.include(['**/*.dump'])
    CLOBBER.include(['**/*.beam'])
    
    directory 'ebin'
    
    
    rule ".beam" =>  ["%{ebin,src}X.erl"] do |t|
      sh "erlc -pa ebin -W #{ERLC_FLAGS} -o ebin #{t.source}"
    end
    
    desc "Compile all"
    task :compile => ['ebin'] + OBJ
    
    desc "Open up a shell"
    task :shell => [:compile] do
        sh("erl -sname #{START_MODULE} -pa #{PWD}/ebin")
    end
    
    desc "Open up a shell and run #{START_MODULE}:start()" 
    task :run => [:compile] do
        sh("erl -sname #{START_MODULE} -pa #{PWD}/ebin -run #{START_MODULE} start")
    end
    
    desc "Run Unit Tests" 
    task :test do
      sh("erl -noshell -s #{TEST_MODULE} test -s init stop")
    end
    
    
    desc "Generate Documentation"
    task :doc do
        sh("cd doc && erl -noshell -run edoc files ../#{SRC.join(" ../")} -run init stop")
    end
    
    
    task :default => :compile
    

    You can get a list of functions to run by typing in rake -T in the project root. As compile is the default you can just build your code by entering simply rake in you terminal.

    References (1)

    References allow you to track sources for this article, as well as articles that were written in response to this article.
    • Response
      Response: Mnesia Links
      Tagged your site as mnesia at iLinkShare!

    Reader Comments (4)

    Thanks for the info, it's very useful to an erlang rookie like me :)

    Aug 21, 2008 at 12:25 | Unregistered Commenterochronus

    Useful entry. Thanks.

    Aug 25, 2008 at 8:16 | Unregistered CommenterJavier

    If you use sake, then you make all those rake tasks available system wide, so that they work for all your erlang projects.

    Nov 2, 2008 at 20:59 | Unregistered CommenterLawrence Pit

    I've modified the rakefile that you wrote in this post in order to make it work under Windows. You can check out the details in this post of my blog . The post is in spanish, but the code is in english and if you look at the end of the post, you can see the hole rakefile.

    aurelianito

    Feb 27, 2009 at 17:24 | Unregistered Commenteraurelianito

    PostPost a New Comment

    Enter your information below to add a new comment.

    My response is on my own website »
    Author Email (optional):
    Author URL (optional):
    Post:
     
    Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
    Fork me on GitHub