久しぶりにErlangのプロジェクトを始めるのでメモ。
この記事では、OS X El Capitanにて以下を行います。
- Erlang 18.xのインストール
- Rebar3のインストール
- Erlangプロジェクトを作成して実行
Erlangの導入
- GCCのバージョンとOpenSSLを確認
- kerl(Erlangのバージョン管理の仕組み)をインストール
$ gcc --version
$ brew install openssl
$ mkdir kerl
$ cd kerl/
$ curl -O https://raw.githubusercontent.com/spawngrid/kerl/master/kerl
$ chmod a+x ./kerl
kerlにパス通すなりなんなりする
$ kerl list releases
$ vi ~/.kerlrc
~/.kerlrc に以下を記述
KERL_CONFIGURE_OPTIONS="--enable-kernel-poll --enable-threads --enable-dynamic-ssl-lib --enable-smp-support --enable-darwin-64bit --disable-hipe --disable-native-libs --disable-sctp --without-javac --without-odbc --without-wx --without-debugger -without-observer --without-et --with-ssl=/usr/local/opt/openssl"
autoconfとautomakeをインストール
$ brew install autoconf
$ brew install automake
Erlang18.3.4.4をビルドしてインストール
$ kerl build git https://github.com/erlang/otp/ OTP-18.3.4.4 18.3.4.4
$ cat ~/.kerl/builds/18.3.4.4/otp_build_git.log
$ kerl list builds
$ mkdir path/to/erlang
$ kerl install 18.3.4.4 path/to/erlang/18.3.4.4
Erlangを確認
$ erl
rebar3を導入
Erlangのビルドツールのrebar3を導入します。
$ git clone https://github.com/erlang/rebar3.git
$ cd rebar3/
$ ./bootstrap
$ cd ../
プロジェクト(release)を作成
$ ./rebar3/rebar3 new release myproject
$ cd myproject
## 便利なのでrebar3をプロジェクト内にコピーします。
$ cp ../rebar3/rebar3 ./
## Buildします
$ ./rebar3 compile
ちょっとハマったのが、最初release名に"-"を入れていて、compile時にinvalid term
というエラーが出ました。 release名やアプリ名に"-"を使うと、そのまま設定ファイルが出力されます。設定ファイルはErlangのterm型なので、atomに"-"を使ってしまうことになるからですね。
apps/myproject/src/myproject_app.erl
%%%-------------------------------------------------------------------
%% @doc myproject public API
%% @end
%%%-------------------------------------------------------------------
-module(myproject_app).
-behaviour(application).
%% Application callbacks
-export([start/2, stop/1]).
%%====================================================================
%% API
%%====================================================================
start(_StartType, _StartArgs) ->
io:format("Hello World~n"),
myproject_sup:start_link().
%%--------------------------------------------------------------------
stop(_State) ->
ok.
%%====================================================================
%% Internal functions
%%====================================================================
実行してみる
$./rebar3 release
##consoleで実行
$./_build/default/rel/myproject/bin/myproject console
~~なんやかんや~~
Erlang/OTP 18 [erts-7.3.1.2] [source] [64-bit] [smp:4:4] [async-threads:30] [kernel-poll:true]
Hello
buildしてできた実行ファイルでは、start, stop, consoleなど幾つかのコマンドが使えます。
##実行
$./_build/default/rel/myproject/bin/myproject start
##停止
$./_build/default/rel/myproject/bin/myproject stop
##ログを確認
$cat ./_build/default/rel/myproject/log/erlang.log.1