This post is a discussion on Erlang build tool called erlang.mk. I will discuss about basics of project generation, building and releases. Now that we know what we going to do so let’s get right to it.

Getting erlang.mk

You can download erlang.mk as follows,

$ wget https://raw.githubusercontent.com/ninenines/erlang.mk/master/erlang.mk

Or you can download from here.

Generally there are three kinds of projects people tend to build:

OTP libraries : This type of project is usually intended to be included into other applications. An OTP library is an Erlang application that has no supervision tree. In other words it is nothing but a bunch of Erlang modules.

: This type of project is usually intended to be included into other applications. An OTP library is an Erlang application that has no supervision tree. In other words it is nothing but a bunch of Erlang modules. OTP application : An OTP application is an Erlang application that has a supervision tree. In other words, it will always have processes running. We will talk about this later.

: An OTP application is an Erlang application that has a supervision tree. In other words, it will always have processes running. We will talk about this later. OTP releases: These projects are self contained OTP applications that you can deploy on your servers. In other words these are full fledged projects which are built and released like Whatsapp or you next big Erlang app.

I will discuss all of the above types but before we get to the building part we need have a project first ! Fear not you won’t have to write any code, erlang.mk allows you to bootstrap. Following is how you bootstrap a OTP library,



$ wget

$ make -f erlang.mk bootstrap-lib

$ tree

.

├── Makefile

└── src $ mkdir lib1 && cd lib1$ wget https://raw.githubusercontent.com/ninenines/erlang.mk/master/erlang.mk $ make -f erlang.mk bootstrap-lib$ tree├── Makefile└── src 1 directory, 1 file

$ cat Makefile

PROJECT = lib1

include erlang.mk

$ make

DEPEND lib1.d

APP lib1

$ ls

ebin erlang.mk lib1.d Makefile src

This is basically it and as you can see there are no files . You might feel betrayed but for an OTP library there is no boiler plate code required. You just need to start creating your modules in src directory. Lastly you can compile your library with make.

Now we will see how to create an OTP application,



$ wget

$ make -f erlang.mk bootstrap LEGACY=1

$ tree

.

├── erlang.mk

├── Makefile

└── src

├── app1_app.erl

├── app1.app.src

└── app1_sup.erl

1 directory, 5 files

$ make

DEPEND app1.d

ERLC app1_app.erl app1_sup.erl

APP app1 $ mkdir app1 && cd app1$ wget https://raw.githubusercontent.com/ninenines/erlang.mk/master/erlang.mk $ make -f erlang.mk$ tree├── erlang.mk├── Makefile└── src├── app1_app.erl├── app1.app.src└── app1_sup.erl1 directory, 5 files$ makeDEPEND app1.dERLC app1_app.erl app1_sup.erlAPP app1

Here you can see that you have your bootstrap code i.e. app1.app.src, app2_app.erl and app2_sup.erl . erlang.mk will not generate .app.src if you don’t specify the option LEGACY=1 because it’s capable of generating .app file from the Makefile itself. But there are quite a few options that cannot be specified in Makefile (eg. included_applications) so they end up not being in the .app files. So I suggest you should specify LEGACY=1 for the time being.

Next we will see how to bootstrap an OTP release,

$ mkdir app0 && cd app0

$ make -f erlang.mk bootstrap bootstrap-rel LEGACY=1

$ tree

.

├── erlang.mk

├── Makefile

├── rel

│ ├── sys.config

│ └── vm.args

├── relx.config

└── src

├── app0_app.erl

├── app0.app.src

└── app0_sup.erl

2 directories, 8 files

$ make

... compile log ...

We have seen that the bootstrap switch generates an OTP application. The bootstrap-rel switch will generate relx.config, sys.config and vm.args which are required when generating a release. When you execute make it will fetch and compile dependencies (we will discuss how to specify dependencies), then compile the project after which it will make a release. You can also execute each of these steps individually,

$ make app # compile the app

$ make deps # fetch and build all deps

$ make rel # generate release

Dependencies

While building you awesome new application you might want to use other projects so you will need to specify them as dependencies. Let’s try specify some dependencies in the Makefile for app0,

PROJECT = app0

PROJECT_DESCRIPTION = Awesome app

PROJECT_VERSION = 0.1.0 LOCAL_DEPS = crypto DEPS = cowboy eleveldb

dep_cowboy = git

eleveldb = git dep_cowboy = git https://github.com/ninenines/cowboy 2.0.0-pre.1eleveldb = git https://github.com/basho/eleveldb master include erlang.mk

_____

Makefile for app0

In the first we specify the project name using the PROJECT variable

Next we specify the project description and project version.

LOCAL_DEPS, allows specifying runtime dependencies which are part of Erlang/OTP itself, but also dependencies that are included in the repository. Since they are already on your system, there is no need to fetch them. Do note that there is no way to choose the version, the application used will be the one already on your system.

Next we specify dependencies using DEPS that will need to be fetched i.e cowboy and eleveldb. We specify exactly where to fetch them from and corresponding tag/branch to be fetched. In general we specify a dep as follows,

DEPS = some_dep

dep_some_dep = [fetch_method] [location] [tag/branch]