-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
362 lines (263 loc) · 8.52 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# (c) 2014+ virtan [email protected]
# Variables
OS=$(shell uname -s | tr '[:upper:]' '[:lower:]')
BYROOT=$(shell test `id -u` -eq 0 && echo || which sudo || echo "su root -c")
GIT=$(shell which git || echo install-git-$(OS))
ERLC=$(shell which erlc || echo install-erlang-$(OS))
ERL=$(shell which erl || echo install-erlang-$(OS))
PANDOC=$(shell which pandoc || echo install-pandoc-$(OS))
REBAR=$(CURDIR)/rebar
DIALYZER_DEPS_PLT=$(CURDIR)/.deps_plt
DIALYZER_DEPS=erts kernel stdlib
ERLFLAGS= -pa $(CURDIR)/.eunit -pa $(CURDIR)/ebin -pa $(CURDIR)/deps/*/ebin
# Targets
all: deps compile
deps: $(REBAR)
$(REBAR) get-deps
$(REBAR) compile
update-deps: $(REBAR)
$(REBAR) update-deps
$(REBAR) compile
compile: $(REBAR)
$(REBAR) skip_deps=true compile
release: $(REBAR) deps compile
$(REBAR) generate
generate: release
rel: release
doc: $(REBAR)
$(REBAR) skip_deps=true doc
eunit: $(REBAR) compile
$(REBAR) skip_deps=true eunit
check: test
test: deps compile eunit
$(DIALYZER_DEPS_PLT): $(ERL)
@echo Building local plt at $(DIALYZER_DEPS_PLT)
@echo
dialyzer --output_plt $(DIALYZER_DEPS_PLT) --build_plt \
--apps $(DIALYZER_DEPS) -r deps
dialyzer: $(DIALYZER_DEPS_PLT) $(ERL)
dialyzer --fullpath --plt $(DIALYZER_DEPS_PLT) -Wrace_conditions -r ./ebin
typer: $(ERL)
typer --plt $(DIALYZER_DEPS_PLT) -r ./src
shell: $(REBAR) $(ERL) deps compile
# You often want *rebuilt* rebar tests to be available to the
# shell you have to call eunit (to get the tests
# rebuilt). However, eunit runs the tests, which probably
# fails (thats probably why You want them in the shell). This
# runs eunit but tells make to ignore the result.
- @$(REBAR) skip_deps=true eunit
@$(ERL) $(ERLFLAGS) -sname cclshell@localhost
pdf: $(PANDOC)
$(PANDOC) README.md -o README.pdf
clean:
- rm -rf $(CURDIR)/test/*.beam
- rm -rf $(CURDIR)/logs
- rm -rf $(CURDIR)/ebin
$(REBAR) skip_deps=true clean
distclean: clean
- rm -rf $(DIALYZER_DEPS_PLT)
- rm -rvf $(CURDIR)/deps
- rm -f $(CURDIR)/rebar
rebuild: distclean deps compile
export TMPL_REBAR_CONFIG
export TMPL_GITIGNORE
new-project: $(REBAR)
@echo
@read -p "Enter the name of project: " THENAME && \
echo && \
$(REBAR) create-app appid=$$THENAME && \
echo "$$TMPL_REBAR_CONFIG" | sed "s/PROJECTNAME/$$THENAME/g" > rebar.config && \
mkdir rel && \
( cd rel && $(REBAR) create-node nodeid=$$THENAME && cd .. ) && \
sed -i.tmp "s/^\(.*{app, $$THENAME, .*\)\]\}$$/\\1, {lib_dir, \"..\"}]}/" rel/reltool.config && \
rm -f rel/reltool.config.tmp && \
sed -i.tmp "s/^\(.*{lib_dirs, \[\)\(\]\},\)$$/\\1\"..\/deps\"\\2/" rel/reltool.config && \
rm -f rel/reltool.config.tmp && \
sed -i.tmp "s/^\(.*{rel, \"$$THENAME\", \"\)1\(\",\)$$/\\10.1\\2/" rel/reltool.config && \
rm -f rel/reltool.config.tmp && \
echo "$$TMPL_GITIGNORE" | sed "s/PROJECTNAME/$$THENAME/g" > .gitignore
export TMPL_GEN_SERVER
gen-server:
@echo
@read -p "Enter the name of gen_server module: " GENSERVER && \
echo && \
echo "$$TMPL_GEN_SERVER" | sed "s/GENSERVERNAME/$$GENSERVER/g" > $$GENSERVER.erl && \
mv $$GENSERVER.erl src/ 2>/dev/null
export TMPL_PORT_CONTROLLER
port-controller:
@echo
@read -p "Enter the name of port controller module: " PORTCONTROLLER && \
echo && \
echo "$$TMPL_PORT_CONTROLLER" | sed "s/PORTNAME/$$PORTCONTROLLER/g" > $$PORTCONTROLLER.erl && \
mv $$PORTCONTROLLER.erl src/ 2>/dev/null
update-makefile:
-mkdir subs
-rm -rf subs/erlang-makefile
git clone https://github.com/virtan/erlang-makefile.git subs/erlang-makefile
cp -f subs/erlang-makefile/Makefile .
rm -rf subs/erlang-makefile
# Tools
$(REBAR): $(ERLC) $(GIT)
-mkdir subs
-rm -rf subs/rebar
git clone https://github.com/basho/rebar.git subs/rebar
cd subs/rebar && ./bootstrap
cp subs/rebar/rebar $(REBAR)
rm -rf subs/rebar
install-%-darwin:
$(BYROOT) port install $* || \
$(BYROOT) brew install $* || \
( echo "Please, install $*" ; false )
install-%-linux:
$(BYROOT) apt-get install $* || \
$(BYROOT) yum install $* || \
( echo "Please, install $*" ; false )
install-%:
( echo "Please, install $*" | sed "s/-[a-z0-9]*$$//" ; false )
# System
.PHONY: all deps update-deps compile doc eunit test \
dialyzer typer shell pdf distclean rebuild rebar \
generate release rel new-project gen-server
# Templates
define TMPL_REBAR_CONFIG
{sub_dirs, ["rel"]}.
{erl_opts, [debug_info]}.
{deps, [
{vutil, ".*", {git, "git://github.com/virtan/vutil.git", ""}},
{eper, ".*", {git, "git://github.com/massemanet/eper.git", ""}}
]}.
{cover_enabled, true}.
{eunit_opts, [verbose]}.
endef
define TMPL_GITIGNORE
deps
.eunit
subs
ebin/
.dialyzer_plt
rel/PROJECTNAME
endef
define TMPL_GEN_SERVER
-module(GENSERVERNAME).
-behaviour(gen_server).
-export([
start/1,
start_link/1,
stop/0,
command/0,
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
code_change/3,
terminate/2
]).
-record(state, {
%% TODO: define internal state
}).
%% Exported
start(Options) ->
gen_server:start({local, ?MODULE}, ?MODULE, Options, []).
start_link(Options) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, Options, []).
stop() ->
gen_server:call(?MODULE, stop).
command() ->
gen_server:call(?MODULE, command).
%% Internal
init(_Options) ->
%% TODO: process Options, prepare state
{ok, #state{}}.
handle_call(command, _From, #state{} = State) ->
{reply, the_reply, State};
handle_call(stop, _From, #state{} = State) ->
{stop, normal, stopped, State};
handle_call(Unexpected, _From, #state{} = State) ->
{stop, {error_unexpected, Unexpected}, error_unexpected, State}.
handle_cast(command, #state{} = State) ->
{noreply, State};
handle_cast(Unexpected, #state{} = State) ->
{stop, {error_unexpected, Unexpected}, State}.
handle_info(command, #state{} = State) ->
{noreply, State};
handle_info(Unexpected, #state{} = State) ->
{stop, {error_unexpected, Unexpected}, State}.
code_change(_OldVsn, #state{} = State, _Extra) ->
{ok, State}.
terminate({error_unexpected, _Unexpected}, #state{} = _State) ->
%% TODO: report about unexpected
ok;
terminate(_Reason, #state{} = _State) ->
ok.
endef
define TMPL_PORT_CONTROLLER
-module(PORTNAME).
-behaviour(gen_server).
-export([
start/1,
start_link/1,
stop/0,
command/0,
init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
code_change/3,
terminate/2
]).
-record(state, {
port
}).
%% Exported
start(Options) ->
gen_server:start({local, ?MODULE}, ?MODULE, Options, []).
start_link(Options) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, Options, []).
stop() ->
gen_server:call(?MODULE, stop).
send(Data) ->
gen_server:call(?MODULE, {send, Data}).
%% Internal
init(Options) ->
process_flag(trap_exit, true),
Exe = proplists:get_value(executable, Options),
ExeOpts = proplists:get_value(executable_options, Options,
[{packet, 4}, exit_status, use_stdio,
stderr_to_stdout, binary]),
Port = open_port({spawn_executable, Exe}, ExeOpts),
{ok, #state{port = Port}}.
handle_call({send, Data}, From, #state{port = Port} = State) ->
Port ! {self(), {command, term_to_binary({From, Data})}},
{noreply, State};
handle_call(stop, _From, #state{} = State) ->
{stop, normal, stopped, State};
handle_call(Unexpected, _From, #state{} = State) ->
{stop, {error_unexpected, Unexpected}, error_unexpected, State}.
handle_cast(Unexpected, #state{} = State) ->
{stop, {error_unexpected, Unexpected}, State}.
handle_info({Port, {data, Data}}, #state{port = Port} = State) ->
case binary_to_term(Data) of
{From, Data1} ->
gen_server:reply(From, Data1);
_Error ->
do_nothing
end,
{noreply, State};
handle_info({Port, {exit_status, Status}}, #state{port = Port} = State) ->
{stop, exited, State};
handle_info({Port, closed}, #state{port = Port} = State) ->
{stop, normal, State};
handle_info({'EXIT', Port, Reason}, #state{port = Port} = State) ->
{stop, died, State};
handle_info(Unexpected, #state{} = State) ->
{stop, {error_unexpected, Unexpected}, State}.
code_change(_OldVsn, #state{} = State, _Extra) ->
{ok, State}.
terminate({error_unexpected, _Unexpected}, #state{port = Port} = _State) ->
catch port_close(Port),
%% TODO: report about unexpected
ok;
terminate(_Reason, #state{port = Port} = _State) ->
catch port_close(Port),
ok.
endef