bootstrap.mk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. # Copyright (c) 2014-2016, Loïc Hoguin <essen@ninenines.eu>
  2. # This file is part of erlang.mk and subject to the terms of the ISC License.
  3. .PHONY: bootstrap bootstrap-lib bootstrap-rel new list-templates
  4. # Core targets.
  5. help::
  6. $(verbose) printf "%s\n" "" \
  7. "Bootstrap targets:" \
  8. " bootstrap Generate a skeleton of an OTP application" \
  9. " bootstrap-lib Generate a skeleton of an OTP library" \
  10. " bootstrap-rel Generate the files needed to build a release" \
  11. " new-app in=NAME Create a new local OTP application NAME" \
  12. " new-lib in=NAME Create a new local OTP library NAME" \
  13. " new t=TPL n=NAME Generate a module NAME based on the template TPL" \
  14. " new t=T n=N in=APP Generate a module NAME based on the template TPL in APP" \
  15. " list-templates List available templates"
  16. # Bootstrap templates.
  17. define bs_appsrc
  18. {application, $p, [
  19. {description, ""},
  20. {vsn, "0.1.0"},
  21. {modules, []},
  22. {registered, []},
  23. {applications, [
  24. kernel,
  25. stdlib
  26. ]},
  27. {mod, {$p_app, []}},
  28. {env, []}
  29. ]}.
  30. endef
  31. define bs_appsrc_lib
  32. {application, $p, [
  33. {description, ""},
  34. {vsn, "0.1.0"},
  35. {modules, []},
  36. {registered, []},
  37. {applications, [
  38. kernel,
  39. stdlib
  40. ]}
  41. ]}.
  42. endef
  43. # To prevent autocompletion issues with ZSH, we add "include erlang.mk"
  44. # separately during the actual bootstrap.
  45. ifdef SP
  46. define bs_Makefile
  47. PROJECT = $p
  48. PROJECT_DESCRIPTION = New project
  49. PROJECT_VERSION = 0.1.0
  50. # Whitespace to be used when creating files from templates.
  51. SP = $(SP)
  52. endef
  53. else
  54. define bs_Makefile
  55. PROJECT = $p
  56. PROJECT_DESCRIPTION = New project
  57. PROJECT_VERSION = 0.1.0
  58. endef
  59. endif
  60. define bs_apps_Makefile
  61. PROJECT = $p
  62. PROJECT_DESCRIPTION = New project
  63. PROJECT_VERSION = 0.1.0
  64. include $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(APPS_DIR)/app)/erlang.mk
  65. endef
  66. define bs_app
  67. -module($p_app).
  68. -behaviour(application).
  69. -export([
  70. start/2,
  71. stop/1
  72. ]).
  73. start(_Type, _Args) ->
  74. $p_sup:start_link().
  75. stop(_State) ->
  76. ok.
  77. endef
  78. define bs_relx_config
  79. {release, {$p_release, "1"}, [$p, sasl, runtime_tools]}.
  80. {extended_start_script, true}.
  81. {sys_config, "rel/sys.config"}.
  82. {vm_args, "rel/vm.args"}.
  83. endef
  84. define bs_sys_config
  85. [
  86. ].
  87. endef
  88. define bs_vm_args
  89. -name $p@127.0.0.1
  90. -setcookie $p
  91. -heart
  92. +pc unicode
  93. +K true
  94. +A 5
  95. -env ERL_MAX_PORTS 4096
  96. -env ERL_FULLSWEEP_AFTER 10
  97. endef
  98. # Normal templates.
  99. define tpl_supervisor
  100. -module($(n)).
  101. -behaviour(supervisor).
  102. -export([
  103. start_link/0,
  104. init/1
  105. ]).
  106. start_link() ->
  107. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  108. init([]) ->
  109. Procs = [],
  110. {ok, {{one_for_one, 1, 5}, Procs}}.
  111. endef
  112. define tpl_gen_server
  113. -module($(n)).
  114. -behaviour(gen_server).
  115. %% API.
  116. -export([
  117. start_link/0
  118. ]).
  119. %% gen_server.
  120. -export([
  121. init/1,
  122. handle_call/3,
  123. handle_cast/2,
  124. handle_info/2,
  125. terminate/2,
  126. code_change/3
  127. ]).
  128. -record(state, {
  129. }).
  130. %% API.
  131. -spec start_link() -> {ok, pid()}.
  132. start_link() ->
  133. gen_server:start_link(?MODULE, [], []).
  134. %% gen_server.
  135. init([]) ->
  136. {ok, #state{}}.
  137. handle_call(_Request, _From, State) ->
  138. {reply, ignored, State}.
  139. handle_cast(_Msg, State) ->
  140. {noreply, State}.
  141. handle_info(_Info, State) ->
  142. {noreply, State}.
  143. terminate(_Reason, _State) ->
  144. ok.
  145. code_change(_OldVsn, State, _Extra) ->
  146. {ok, State}.
  147. endef
  148. define tpl_module
  149. -module($(n)).
  150. -export([
  151. ]).
  152. endef
  153. define tpl_cowboy_http
  154. -module($(n)).
  155. -behaviour(cowboy_http_handler).
  156. -export([
  157. init/3,
  158. handle/2,
  159. terminate/3
  160. ]).
  161. -record(state, {
  162. }).
  163. init(_, Req, _Opts) ->
  164. {ok, Req, #state{}}.
  165. handle(Req, State = #state{}) ->
  166. {ok, Req2} = cowboy_req:reply(200, Req),
  167. {ok, Req2, State}.
  168. terminate(_Reason, _Req, _State) ->
  169. ok.
  170. endef
  171. define tpl_gen_fsm
  172. -module($(n)).
  173. -behaviour(gen_fsm).
  174. %% API.
  175. -export([
  176. start_link/0
  177. ]).
  178. %% gen_fsm.
  179. -export([
  180. init/1,
  181. state_name/2,
  182. handle_event/3,
  183. state_name/3,
  184. handle_sync_event/4,
  185. handle_info/3,
  186. terminate/3,
  187. code_change/4
  188. ]).
  189. -record(state, {
  190. }).
  191. %% API.
  192. -spec start_link() -> {ok, pid()}.
  193. start_link() ->
  194. gen_fsm:start_link(?MODULE, [], []).
  195. %% gen_fsm.
  196. init([]) ->
  197. {ok, state_name, #state{}}.
  198. state_name(_Event, StateData) ->
  199. {next_state, state_name, StateData}.
  200. handle_event(_Event, StateName, StateData) ->
  201. {next_state, StateName, StateData}.
  202. state_name(_Event, _From, StateData) ->
  203. {reply, ignored, state_name, StateData}.
  204. handle_sync_event(_Event, _From, StateName, StateData) ->
  205. {reply, ignored, StateName, StateData}.
  206. handle_info(_Info, StateName, StateData) ->
  207. {next_state, StateName, StateData}.
  208. terminate(_Reason, _StateName, _StateData) ->
  209. ok.
  210. code_change(_OldVsn, StateName, StateData, _Extra) ->
  211. {ok, StateName, StateData}.
  212. endef
  213. define tpl_gen_statem
  214. -module($(n)).
  215. -behaviour(gen_statem).
  216. %% API.
  217. -export([
  218. start_link/0
  219. ]).
  220. %% gen_statem.
  221. -export([
  222. callback_mode/0,
  223. init/1,
  224. state_name/3,
  225. handle_event/4,
  226. terminate/3,
  227. code_change/4
  228. ]).
  229. -record(state, {
  230. }).
  231. %% API.
  232. -spec start_link() -> {ok, pid()}.
  233. start_link() ->
  234. gen_statem:start_link(?MODULE, [], []).
  235. %% gen_statem.
  236. callback_mode() ->
  237. state_functions.
  238. init([]) ->
  239. {ok, state_name, #state{}}.
  240. state_name(_EventType, _EventData, StateData) ->
  241. {next_state, state_name, StateData}.
  242. handle_event(_EventType, _EventData, StateName, StateData) ->
  243. {next_state, StateName, StateData}.
  244. terminate(_Reason, _StateName, _StateData) ->
  245. ok.
  246. code_change(_OldVsn, StateName, StateData, _Extra) ->
  247. {ok, StateName, StateData}.
  248. endef
  249. define tpl_cowboy_loop
  250. -module($(n)).
  251. -behaviour(cowboy_loop_handler).
  252. -export([
  253. init/3,
  254. info/3,
  255. terminate/3
  256. ]).
  257. -record(state, {
  258. }).
  259. init(_, Req, _Opts) ->
  260. {loop, Req, #state{}, 5000, hibernate}.
  261. info(_Info, Req, State) ->
  262. {loop, Req, State, hibernate}.
  263. terminate(_Reason, _Req, _State) ->
  264. ok.
  265. endef
  266. define tpl_cowboy_rest
  267. -module($(n)).
  268. -export([
  269. init/3,
  270. content_types_provided/2,
  271. get_html/2
  272. ]).
  273. init(_, _Req, _Opts) ->
  274. {upgrade, protocol, cowboy_rest}.
  275. content_types_provided(Req, State) ->
  276. {[{{<<"text">>, <<"html">>, '*'}, get_html}], Req, State}.
  277. get_html(Req, State) ->
  278. {<<"<html><body>This is REST!</body></html>">>, Req, State}.
  279. endef
  280. define tpl_cowboy_ws
  281. -module($(n)).
  282. -behaviour(cowboy_websocket_handler).
  283. -export([
  284. init/3,
  285. websocket_init/3,
  286. websocket_handle/3,
  287. websocket_info/3,
  288. websocket_terminate/3
  289. ]).
  290. -record(state, {
  291. }).
  292. init(_, _, _) ->
  293. {upgrade, protocol, cowboy_websocket}.
  294. websocket_init(_, Req, _Opts) ->
  295. Req2 = cowboy_req:compact(Req),
  296. {ok, Req2, #state{}}.
  297. websocket_handle({text, Data}, Req, State) ->
  298. {reply, {text, Data}, Req, State};
  299. websocket_handle({binary, Data}, Req, State) ->
  300. {reply, {binary, Data}, Req, State};
  301. websocket_handle(_Frame, Req, State) ->
  302. {ok, Req, State}.
  303. websocket_info(_Info, Req, State) ->
  304. {ok, Req, State}.
  305. websocket_terminate(_Reason, _Req, _State) ->
  306. ok.
  307. endef
  308. define tpl_ranch_protocol
  309. -module($(n)).
  310. -behaviour(ranch_protocol).
  311. -export([
  312. start_link/4,
  313. init/4
  314. ]).
  315. -type opts() :: [].
  316. -export_type([opts/0]).
  317. -record(state, {
  318. socket :: inet:socket(),
  319. transport :: module()
  320. }).
  321. start_link(Ref, Socket, Transport, Opts) ->
  322. Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
  323. {ok, Pid}.
  324. -spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
  325. init(Ref, Socket, Transport, _Opts) ->
  326. ok = ranch:accept_ack(Ref),
  327. loop(#state{socket=Socket, transport=Transport}).
  328. loop(State) ->
  329. loop(State).
  330. endef
  331. # Plugin-specific targets.
  332. define render_template
  333. $(verbose) printf -- '$(subst $(newline),\n,$(subst %,%%,$(subst ','\'',$(subst $(tab),$(WS),$(call $(1))))))\n' > $(2)
  334. endef
  335. ifndef WS
  336. ifdef SP
  337. WS = $(subst a,,a $(wordlist 1,$(SP),a a a a a a a a a a a a a a a a a a a a))
  338. else
  339. WS = $(tab)
  340. endif
  341. endif
  342. bootstrap:
  343. ifneq ($(wildcard src/),)
  344. $(error Error: src/ directory already exists)
  345. endif
  346. $(eval p := $(PROJECT))
  347. $(eval n := $(PROJECT)_sup)
  348. $(call render_template,bs_Makefile,Makefile)
  349. $(verbose) echo "include erlang.mk" >> Makefile
  350. $(verbose) mkdir src/
  351. ifdef LEGACY
  352. $(call render_template,bs_appsrc,src/$(PROJECT).app.src)
  353. endif
  354. $(call render_template,bs_app,src/$(PROJECT)_app.erl)
  355. $(call render_template,tpl_supervisor,src/$(PROJECT)_sup.erl)
  356. bootstrap-lib:
  357. ifneq ($(wildcard src/),)
  358. $(error Error: src/ directory already exists)
  359. endif
  360. $(eval p := $(PROJECT))
  361. $(call render_template,bs_Makefile,Makefile)
  362. $(verbose) echo "include erlang.mk" >> Makefile
  363. $(verbose) mkdir src/
  364. ifdef LEGACY
  365. $(call render_template,bs_appsrc_lib,src/$(PROJECT).app.src)
  366. endif
  367. bootstrap-rel:
  368. ifneq ($(wildcard relx.config),)
  369. $(error Error: relx.config already exists)
  370. endif
  371. ifneq ($(wildcard rel/),)
  372. $(error Error: rel/ directory already exists)
  373. endif
  374. $(eval p := $(PROJECT))
  375. $(call render_template,bs_relx_config,relx.config)
  376. $(verbose) mkdir rel/
  377. $(call render_template,bs_sys_config,rel/sys.config)
  378. $(call render_template,bs_vm_args,rel/vm.args)
  379. new-app:
  380. ifndef in
  381. $(error Usage: $(MAKE) new-app in=APP)
  382. endif
  383. ifneq ($(wildcard $(APPS_DIR)/$in),)
  384. $(error Error: Application $in already exists)
  385. endif
  386. $(eval p := $(in))
  387. $(eval n := $(in)_sup)
  388. $(verbose) mkdir -p $(APPS_DIR)/$p/src/
  389. $(call render_template,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile)
  390. ifdef LEGACY
  391. $(call render_template,bs_appsrc,$(APPS_DIR)/$p/src/$p.app.src)
  392. endif
  393. $(call render_template,bs_app,$(APPS_DIR)/$p/src/$p_app.erl)
  394. $(call render_template,tpl_supervisor,$(APPS_DIR)/$p/src/$p_sup.erl)
  395. new-lib:
  396. ifndef in
  397. $(error Usage: $(MAKE) new-lib in=APP)
  398. endif
  399. ifneq ($(wildcard $(APPS_DIR)/$in),)
  400. $(error Error: Application $in already exists)
  401. endif
  402. $(eval p := $(in))
  403. $(verbose) mkdir -p $(APPS_DIR)/$p/src/
  404. $(call render_template,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile)
  405. ifdef LEGACY
  406. $(call render_template,bs_appsrc_lib,$(APPS_DIR)/$p/src/$p.app.src)
  407. endif
  408. new:
  409. ifeq ($(wildcard src/)$(in),)
  410. $(error Error: src/ directory does not exist)
  411. endif
  412. ifndef t
  413. $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP])
  414. endif
  415. ifndef tpl_$(t)
  416. $(error Unknown template)
  417. endif
  418. ifndef n
  419. $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP])
  420. endif
  421. ifdef in
  422. $(verbose) $(MAKE) -C $(APPS_DIR)/$(in)/ new t=$t n=$n in=
  423. else
  424. $(call render_template,tpl_$(t),src/$(n).erl)
  425. endif
  426. list-templates:
  427. $(verbose) echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))