erlang.mk 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  2. #
  3. # Permission to use, copy, modify, and/or distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. .PHONY: all deps app rel docs tests clean distclean help erlang-mk
  15. ERLANG_MK_VERSION = 1
  16. # Core configuration.
  17. PROJECT ?= $(notdir $(CURDIR))
  18. PROJECT := $(strip $(PROJECT))
  19. # Verbosity.
  20. V ?= 0
  21. gen_verbose_0 = @echo " GEN " $@;
  22. gen_verbose = $(gen_verbose_$(V))
  23. # Core targets.
  24. all:: deps app rel
  25. clean::
  26. $(gen_verbose) rm -f erl_crash.dump
  27. distclean:: clean
  28. help::
  29. @printf "%s\n" \
  30. "erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \
  31. "Copyright (c) 2013-2014 Loïc Hoguin <essen@ninenines.eu>" \
  32. "" \
  33. "Usage: [V=1] make [target]" \
  34. "" \
  35. "Core targets:" \
  36. " all Run deps, app and rel targets in that order" \
  37. " deps Fetch dependencies (if needed) and compile them" \
  38. " app Compile the project" \
  39. " rel Build a release for this project, if applicable" \
  40. " docs Build the documentation for this project" \
  41. " tests Run the tests for this project" \
  42. " clean Delete temporary and output files from most targets" \
  43. " distclean Delete all temporary and output files" \
  44. " help Display this help and exit" \
  45. "" \
  46. "The target clean only removes files that are commonly removed." \
  47. "Dependencies and releases are left untouched." \
  48. "" \
  49. "Setting V=1 when calling make enables verbose mode."
  50. # Core functions.
  51. ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
  52. define core_http_get
  53. wget --no-check-certificate -O $(1) $(2)|| rm $(1)
  54. endef
  55. else
  56. define core_http_get
  57. erl -noshell -eval 'ssl:start(), inets:start(), case httpc:request(get, {"$(2)", []}, [{autoredirect, true}], []) of {ok, {{_, 200, _}, _, Body}} -> case file:write_file("$(1)", Body) of ok -> ok; {error, R1} -> halt(R1) end; {error, R2} -> halt(R2) end, halt(0).'
  58. endef
  59. endif
  60. # Automated update.
  61. ERLANG_MK_BUILD_CONFIG ?= build.config
  62. ERLANG_MK_BUILD_DIR ?= .erlang.mk.build
  63. erlang-mk:
  64. git clone https://github.com/ninenines/erlang.mk $(ERLANG_MK_BUILD_DIR)
  65. if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR); fi
  66. cd $(ERLANG_MK_BUILD_DIR) && make
  67. cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
  68. rm -rf $(ERLANG_MK_BUILD_DIR)
  69. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  70. # This file is part of erlang.mk and subject to the terms of the ISC License.
  71. .PHONY: distclean-deps distclean-pkg pkg-list pkg-search
  72. # Configuration.
  73. DEPS_DIR ?= $(CURDIR)/deps
  74. export DEPS_DIR
  75. REBAR_DEPS_DIR = $(DEPS_DIR)
  76. export REBAR_DEPS_DIR
  77. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DEPS))
  78. ifeq ($(filter $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
  79. ifeq ($(ERL_LIBS),)
  80. ERL_LIBS = $(DEPS_DIR)
  81. else
  82. ERL_LIBS := $(ERL_LIBS):$(DEPS_DIR)
  83. endif
  84. endif
  85. export ERL_LIBS
  86. PKG_FILE2 ?= $(CURDIR)/.erlang.mk.packages.v2
  87. export PKG_FILE2
  88. PKG_FILE_URL ?= https://raw.githubusercontent.com/ninenines/erlang.mk/master/packages.v2.tsv
  89. # Core targets.
  90. deps:: $(ALL_DEPS_DIRS)
  91. @for dep in $(ALL_DEPS_DIRS) ; do \
  92. if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ] ; then \
  93. $(MAKE) -C $$dep ; \
  94. else \
  95. echo "include $(CURDIR)/erlang.mk" | ERLC_OPTS=+debug_info $(MAKE) -f - -C $$dep ; \
  96. fi ; \
  97. done
  98. distclean:: distclean-deps distclean-pkg
  99. # Deps related targets.
  100. define dep_fetch
  101. if [ "$$$$VS" = "git" ]; then \
  102. git clone -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  103. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  104. elif [ "$$$$VS" = "hg" ]; then \
  105. hg clone -U $$$$REPO $(DEPS_DIR)/$(1); \
  106. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  107. else \
  108. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  109. exit 78; \
  110. fi
  111. endef
  112. define dep_target
  113. $(DEPS_DIR)/$(1):
  114. @mkdir -p $(DEPS_DIR)
  115. ifeq (,$(dep_$(1)))
  116. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  117. @DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  118. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  119. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  120. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  121. $(call dep_fetch,$(1))
  122. else
  123. @VS=$(word 1,$(dep_$(1))); \
  124. REPO=$(word 2,$(dep_$(1))); \
  125. COMMIT=$(word 3,$(dep_$(1))); \
  126. $(call dep_fetch,$(1))
  127. endif
  128. endef
  129. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  130. distclean-deps:
  131. $(gen_verbose) rm -rf $(DEPS_DIR)
  132. # Packages related targets.
  133. $(PKG_FILE2):
  134. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  135. pkg-list: $(PKG_FILE2)
  136. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  137. "Name:\t\t" $$1 "\n" \
  138. "Repository:\t" $$3 "\n" \
  139. "Website:\t" $$5 "\n" \
  140. "Description:\t" $$6 "\n" }'
  141. ifdef q
  142. pkg-search: $(PKG_FILE2)
  143. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  144. "Name:\t\t" $$1 "\n" \
  145. "Repository:\t" $$3 "\n" \
  146. "Website:\t" $$5 "\n" \
  147. "Description:\t" $$6 "\n" }'
  148. else
  149. pkg-search:
  150. $(error Usage: make pkg-search q=STRING)
  151. endif
  152. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  153. distclean-pkg:
  154. $(gen_verbose) rm -f $(PKG_FILE2)
  155. endif
  156. help::
  157. @printf "%s\n" "" \
  158. "Package-related targets:" \
  159. " pkg-list List all known packages" \
  160. " pkg-search q=STRING Search for STRING in the package index"
  161. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  162. # This file is part of erlang.mk and subject to the terms of the ISC License.
  163. .PHONY: clean-app
  164. # Configuration.
  165. ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \
  166. +warn_shadow_vars +warn_obsolete_guard # +bin_opt_info +warn_missing_spec
  167. COMPILE_FIRST ?=
  168. COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
  169. ERLC_EXCLUDE ?=
  170. ERLC_EXCLUDE_PATHS = $(addprefix src/,$(addsuffix .erl,$(ERLC_EXCLUDE)))
  171. ERLC_MIB_OPTS ?=
  172. COMPILE_MIB_FIRST ?=
  173. COMPILE_MIB_FIRST_PATHS = $(addprefix mibs/,$(addsuffix .mib,$(COMPILE_MIB_FIRST)))
  174. # Verbosity.
  175. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  176. appsrc_verbose = $(appsrc_verbose_$(V))
  177. erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
  178. $(filter %.erl %.core,$(?F)));
  179. erlc_verbose = $(erlc_verbose_$(V))
  180. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  181. xyrl_verbose = $(xyrl_verbose_$(V))
  182. mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
  183. mib_verbose = $(mib_verbose_$(V))
  184. # Core targets.
  185. app:: erlc-include ebin/$(PROJECT).app
  186. $(eval MODULES := $(shell find ebin -type f -name \*.beam \
  187. | sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
  188. @if [ -z "$$(grep -E '^[^%]*{modules,' src/$(PROJECT).app.src)" ]; then \
  189. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  190. exit 1; \
  191. fi
  192. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  193. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  194. | sed "s/{modules,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/" \
  195. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(GITDESCRIBE)\"}/" \
  196. > ebin/$(PROJECT).app
  197. define compile_erl
  198. $(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
  199. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),\
  200. $(COMPILE_FIRST_PATHS) $(1))
  201. endef
  202. define compile_xyrl
  203. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  204. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  205. @rm ebin/*.erl
  206. endef
  207. define compile_mib
  208. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ \
  209. -I priv/mibs/ $(COMPILE_MIB_FIRST_PATHS) $(1)
  210. $(mib_verbose) erlc -o include/ -- priv/mibs/*.bin
  211. endef
  212. ifneq ($(wildcard src/),)
  213. ebin/$(PROJECT).app::
  214. @mkdir -p ebin/
  215. ebin/$(PROJECT).app:: $(shell find mibs -type f -name \*.mib)
  216. @mkdir -p priv/mibs/ include
  217. $(if $(strip $?),$(call compile_mib,$?))
  218. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.erl) \
  219. $(shell find src -type f -name \*.core)
  220. $(if $(strip $?),$(call compile_erl,$?))
  221. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.xrl) \
  222. $(shell find src -type f -name \*.yrl)
  223. $(if $(strip $?),$(call compile_xyrl,$?))
  224. endif
  225. clean:: clean-app
  226. # Extra targets.
  227. erlc-include:
  228. -@if [ -d ebin/ ]; then \
  229. find include/ src/ -type f -name \*.hrl -newer ebin -exec touch $(shell find src/ -type f -name "*.erl") \; 2>/dev/null || printf ''; \
  230. fi
  231. clean-app:
  232. $(gen_verbose) rm -rf ebin/ priv/mibs/
  233. $(gen_verbose) rm -f $(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(wildcard mibs/*.mib)))))
  234. # Copyright (c) 2014, Loïc Hoguin <essen@ninenines.eu>
  235. # This file is part of erlang.mk and subject to the terms of the ISC License.
  236. .PHONY: bootstrap bootstrap-lib bootstrap-rel new list-templates
  237. # Core targets.
  238. help::
  239. @printf "%s\n" "" \
  240. "Bootstrap targets:" \
  241. " bootstrap Generate a skeleton of an OTP application" \
  242. " bootstrap-lib Generate a skeleton of an OTP library" \
  243. " bootstrap-rel Generate the files needed to build a release" \
  244. " new t=TPL n=NAME Generate a module NAME based on the template TPL" \
  245. " list-templates List available templates"
  246. # Bootstrap templates.
  247. bs_appsrc = "{application, $(PROJECT), [" \
  248. " {description, \"\"}," \
  249. " {vsn, \"0.1.0\"}," \
  250. " {id, \"git\"}," \
  251. " {modules, []}," \
  252. " {registered, []}," \
  253. " {applications, [" \
  254. " kernel," \
  255. " stdlib" \
  256. " ]}," \
  257. " {mod, {$(PROJECT)_app, []}}," \
  258. " {env, []}" \
  259. "]}."
  260. bs_appsrc_lib = "{application, $(PROJECT), [" \
  261. " {description, \"\"}," \
  262. " {vsn, \"0.1.0\"}," \
  263. " {id, \"git\"}," \
  264. " {modules, []}," \
  265. " {registered, []}," \
  266. " {applications, [" \
  267. " kernel," \
  268. " stdlib" \
  269. " ]}" \
  270. "]}."
  271. bs_Makefile = "PROJECT = $(PROJECT)" \
  272. "include erlang.mk"
  273. bs_app = "-module($(PROJECT)_app)." \
  274. "-behaviour(application)." \
  275. "" \
  276. "-export([start/2])." \
  277. "-export([stop/1])." \
  278. "" \
  279. "start(_Type, _Args) ->" \
  280. " $(PROJECT)_sup:start_link()." \
  281. "" \
  282. "stop(_State) ->" \
  283. " ok."
  284. bs_relx_config = "{release, {$(PROJECT)_release, \"1\"}, [$(PROJECT)]}." \
  285. "{extended_start_script, true}." \
  286. "{sys_config, \"rel/sys.config\"}." \
  287. "{vm_args, \"rel/vm.args\"}."
  288. bs_sys_config = "[" \
  289. "]."
  290. bs_vm_args = "-name $(PROJECT)@127.0.0.1" \
  291. "-setcookie $(PROJECT)" \
  292. "-heart"
  293. # Normal templates.
  294. tpl_supervisor = "-module($(n))." \
  295. "-behaviour(supervisor)." \
  296. "" \
  297. "-export([start_link/0])." \
  298. "-export([init/1])." \
  299. "" \
  300. "start_link() ->" \
  301. " supervisor:start_link({local, ?MODULE}, ?MODULE, [])." \
  302. "" \
  303. "init([]) ->" \
  304. " Procs = []," \
  305. " {ok, {{one_for_one, 1, 5}, Procs}}."
  306. tpl_gen_server = "-module($(n))." \
  307. "-behaviour(gen_server)." \
  308. "" \
  309. "%% API." \
  310. "-export([start_link/0])." \
  311. "" \
  312. "%% gen_server." \
  313. "-export([init/1])." \
  314. "-export([handle_call/3])." \
  315. "-export([handle_cast/2])." \
  316. "-export([handle_info/2])." \
  317. "-export([terminate/2])." \
  318. "-export([code_change/3])." \
  319. "" \
  320. "-record(state, {" \
  321. "})." \
  322. "" \
  323. "%% API." \
  324. "" \
  325. "-spec start_link() -> {ok, pid()}." \
  326. "start_link() ->" \
  327. " gen_server:start_link(?MODULE, [], [])." \
  328. "" \
  329. "%% gen_server." \
  330. "" \
  331. "init([]) ->" \
  332. " {ok, \#state{}}." \
  333. "" \
  334. "handle_call(_Request, _From, State) ->" \
  335. " {reply, ignored, State}." \
  336. "" \
  337. "handle_cast(_Msg, State) ->" \
  338. " {noreply, State}." \
  339. "" \
  340. "handle_info(_Info, State) ->" \
  341. " {noreply, State}." \
  342. "" \
  343. "terminate(_Reason, _State) ->" \
  344. " ok." \
  345. "" \
  346. "code_change(_OldVsn, State, _Extra) ->" \
  347. " {ok, State}."
  348. tpl_cowboy_http = "-module($(n))." \
  349. "-behaviour(cowboy_http_handler)." \
  350. "" \
  351. "-export([init/3])." \
  352. "-export([handle/2])." \
  353. "-export([terminate/3])." \
  354. "" \
  355. "-record(state, {" \
  356. "})." \
  357. "" \
  358. "init(_, Req, _Opts) ->" \
  359. " {ok, Req, \#state{}}." \
  360. "" \
  361. "handle(Req, State=\#state{}) ->" \
  362. " {ok, Req2} = cowboy_req:reply(200, Req)," \
  363. " {ok, Req2, State}." \
  364. "" \
  365. "terminate(_Reason, _Req, _State) ->" \
  366. " ok."
  367. tpl_cowboy_loop = "-module($(n))." \
  368. "-behaviour(cowboy_loop_handler)." \
  369. "" \
  370. "-export([init/3])." \
  371. "-export([info/3])." \
  372. "-export([terminate/3])." \
  373. "" \
  374. "-record(state, {" \
  375. "})." \
  376. "" \
  377. "init(_, Req, _Opts) ->" \
  378. " {loop, Req, \#state{}, 5000, hibernate}." \
  379. "" \
  380. "info(_Info, Req, State) ->" \
  381. " {loop, Req, State, hibernate}." \
  382. "" \
  383. "terminate(_Reason, _Req, _State) ->" \
  384. " ok."
  385. tpl_cowboy_rest = "-module($(n))." \
  386. "" \
  387. "-export([init/3])." \
  388. "-export([content_types_provided/2])." \
  389. "-export([get_html/2])." \
  390. "" \
  391. "init(_, _Req, _Opts) ->" \
  392. " {upgrade, protocol, cowboy_rest}." \
  393. "" \
  394. "content_types_provided(Req, State) ->" \
  395. " {[{{<<\"text\">>, <<\"html\">>, '*'}, get_html}], Req, State}." \
  396. "" \
  397. "get_html(Req, State) ->" \
  398. " {<<\"<html><body>This is REST!</body></html>\">>, Req, State}."
  399. tpl_cowboy_ws = "-module($(n))." \
  400. "-behaviour(cowboy_websocket_handler)." \
  401. "" \
  402. "-export([init/3])." \
  403. "-export([websocket_init/3])." \
  404. "-export([websocket_handle/3])." \
  405. "-export([websocket_info/3])." \
  406. "-export([websocket_terminate/3])." \
  407. "" \
  408. "-record(state, {" \
  409. "})." \
  410. "" \
  411. "init(_, _, _) ->" \
  412. " {upgrade, protocol, cowboy_websocket}." \
  413. "" \
  414. "websocket_init(_, Req, _Opts) ->" \
  415. " Req2 = cowboy_req:compact(Req)," \
  416. " {ok, Req2, \#state{}}." \
  417. "" \
  418. "websocket_handle({text, Data}, Req, State) ->" \
  419. " {reply, {text, Data}, Req, State};" \
  420. "websocket_handle({binary, Data}, Req, State) ->" \
  421. " {reply, {binary, Data}, Req, State};" \
  422. "websocket_handle(_Frame, Req, State) ->" \
  423. " {ok, Req, State}." \
  424. "" \
  425. "websocket_info(_Info, Req, State) ->" \
  426. " {ok, Req, State}." \
  427. "" \
  428. "websocket_terminate(_Reason, _Req, _State) ->" \
  429. " ok."
  430. tpl_ranch_protocol = "-module($(n))." \
  431. "-behaviour(ranch_protocol)." \
  432. "" \
  433. "-export([start_link/4])." \
  434. "-export([init/4])." \
  435. "" \
  436. "-type opts() :: []." \
  437. "-export_type([opts/0])." \
  438. "" \
  439. "-record(state, {" \
  440. " socket :: inet:socket()," \
  441. " transport :: module()" \
  442. "})." \
  443. "" \
  444. "start_link(Ref, Socket, Transport, Opts) ->" \
  445. " Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts])," \
  446. " {ok, Pid}." \
  447. "" \
  448. "-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok." \
  449. "init(Ref, Socket, Transport, _Opts) ->" \
  450. " ok = ranch:accept_ack(Ref)," \
  451. " loop(\#state{socket=Socket, transport=Transport})." \
  452. "" \
  453. "loop(State) ->" \
  454. " loop(State)."
  455. # Plugin-specific targets.
  456. bootstrap:
  457. ifneq ($(wildcard src/),)
  458. $(error Error: src/ directory already exists)
  459. endif
  460. @printf "%s\n" $(bs_Makefile) > Makefile
  461. @mkdir src/
  462. @printf "%s\n" $(bs_appsrc) > src/$(PROJECT).app.src
  463. @printf "%s\n" $(bs_app) > src/$(PROJECT)_app.erl
  464. $(eval n := $(PROJECT)_sup)
  465. @printf "%s\n" $(tpl_supervisor) > src/$(PROJECT)_sup.erl
  466. bootstrap-lib:
  467. ifneq ($(wildcard src/),)
  468. $(error Error: src/ directory already exists)
  469. endif
  470. @printf "%s\n" $(bs_Makefile) > Makefile
  471. @mkdir src/
  472. @printf "%s\n" $(bs_appsrc_lib) > src/$(PROJECT).app.src
  473. bootstrap-rel:
  474. ifneq ($(wildcard relx.config),)
  475. $(error Error: relx.config already exists)
  476. endif
  477. ifneq ($(wildcard rel/),)
  478. $(error Error: rel/ directory already exists)
  479. endif
  480. @printf "%s\n" $(bs_relx_config) > relx.config
  481. @mkdir rel/
  482. @printf "%s\n" $(bs_sys_config) > rel/sys.config
  483. @printf "%s\n" $(bs_vm_args) > rel/vm.args
  484. new:
  485. ifeq ($(wildcard src/),)
  486. $(error Error: src/ directory does not exist)
  487. endif
  488. ifndef t
  489. $(error Usage: make new t=TEMPLATE n=NAME)
  490. endif
  491. ifndef tpl_$(t)
  492. $(error Unknown template)
  493. endif
  494. ifndef n
  495. $(error Usage: make new t=TEMPLATE n=NAME)
  496. endif
  497. @printf "%s\n" $(tpl_$(t)) > src/$(n).erl
  498. list-templates:
  499. @echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))
  500. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  501. # This file is part of erlang.mk and subject to the terms of the ISC License.
  502. .PHONY: build-ct-deps build-ct-suites tests-ct clean-ct distclean-ct
  503. # Configuration.
  504. CT_OPTS ?=
  505. ifneq ($(wildcard test/),)
  506. CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(shell find test -type f -name \*_SUITE.erl -exec basename {} \;)))
  507. else
  508. CT_SUITES ?=
  509. endif
  510. TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
  511. TEST_ERLC_OPTS += -DTEST=1 -DEXTRA=1 +'{parse_transform, eunit_autoexport}'
  512. # Core targets.
  513. tests:: tests-ct
  514. clean:: clean-ct
  515. distclean:: distclean-ct
  516. help::
  517. @printf "%s\n" "" \
  518. "All your common_test suites have their associated targets." \
  519. "A suite named http_SUITE can be ran using the ct-http target."
  520. # Plugin-specific targets.
  521. ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
  522. CT_RUN = ct_run \
  523. -no_auto_compile \
  524. -noshell \
  525. -pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
  526. -dir test \
  527. -logdir logs
  528. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  529. build-ct-deps: $(ALL_TEST_DEPS_DIRS)
  530. @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  531. build-ct-suites: build-ct-deps
  532. $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -I include/ -o test/ \
  533. $(wildcard test/*.erl test/*/*.erl) -pa ebin/
  534. tests-ct: ERLC_OPTS = $(TEST_ERLC_OPTS)
  535. tests-ct: clean deps app build-ct-suites
  536. @if [ -d "test" ] ; \
  537. then \
  538. mkdir -p logs/ ; \
  539. $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) $(CT_OPTS) ; \
  540. fi
  541. $(gen_verbose) rm -f test/*.beam
  542. define ct_suite_target
  543. ct-$(1): ERLC_OPTS = $(TEST_ERLC_OPTS)
  544. ct-$(1): clean deps app build-ct-suites
  545. @if [ -d "test" ] ; \
  546. then \
  547. mkdir -p logs/ ; \
  548. $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) $(CT_OPTS) ; \
  549. fi
  550. $(gen_verbose) rm -f test/*.beam
  551. endef
  552. $(foreach test,$(CT_SUITES),$(eval $(call ct_suite_target,$(test))))
  553. clean-ct:
  554. $(gen_verbose) rm -rf test/*.beam
  555. distclean-ct:
  556. $(gen_verbose) rm -rf logs/
  557. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  558. # This file is part of erlang.mk and subject to the terms of the ISC License.
  559. .PHONY: plt distclean-plt dialyze
  560. # Configuration.
  561. DIALYZER_PLT ?= $(CURDIR)/.$(PROJECT).plt
  562. export DIALYZER_PLT
  563. PLT_APPS ?=
  564. DIALYZER_DIRS ?= --src -r src
  565. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
  566. -Wunmatched_returns # -Wunderspecs
  567. # Core targets.
  568. distclean:: distclean-plt
  569. help::
  570. @printf "%s\n" "" \
  571. "Dialyzer targets:" \
  572. " plt Build a PLT file for this project" \
  573. " dialyze Analyze the project using Dialyzer"
  574. # Plugin-specific targets.
  575. $(DIALYZER_PLT): deps app
  576. @dialyzer --build_plt --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
  577. plt: $(DIALYZER_PLT)
  578. distclean-plt:
  579. $(gen_verbose) rm -f $(DIALYZER_PLT)
  580. ifneq ($(wildcard $(DIALYZER_PLT)),)
  581. dialyze:
  582. else
  583. dialyze: $(DIALYZER_PLT)
  584. endif
  585. @dialyzer --no_native $(DIALYZER_DIRS) $(DIALYZER_OPTS)
  586. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  587. # This file is part of erlang.mk and subject to the terms of the ISC License.
  588. .PHONY: distclean-edoc
  589. # Configuration.
  590. EDOC_OPTS ?=
  591. # Core targets.
  592. docs:: distclean-edoc
  593. $(gen_verbose) erl -noshell \
  594. -eval 'edoc:application($(PROJECT), ".", [$(EDOC_OPTS)]), init:stop().'
  595. distclean:: distclean-edoc
  596. # Plugin-specific targets.
  597. distclean-edoc:
  598. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  599. # Copyright (c) 2014, Juan Facorro <juan@inaka.net>
  600. # This file is part of erlang.mk and subject to the terms of the ISC License.
  601. .PHONY: elvis distclean-elvis
  602. # Configuration.
  603. ELVIS_CONFIG ?= $(CURDIR)/elvis.config
  604. ELVIS ?= $(CURDIR)/elvis
  605. export ELVIS
  606. ELVIS_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis
  607. ELVIS_CONFIG_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis.config
  608. ELVIS_OPTS ?=
  609. # Core targets.
  610. help::
  611. @printf "%s\n" "" \
  612. "Elvis targets:" \
  613. " elvis Run Elvis using the local elvis.config or download the default otherwise"
  614. ifneq ($(wildcard $(ELVIS_CONFIG)),)
  615. rel:: distclean-elvis
  616. endif
  617. distclean:: distclean-elvis
  618. # Plugin-specific targets.
  619. $(ELVIS):
  620. @$(call core_http_get,$(ELVIS_CONFIG),$(ELVIS_CONFIG_URL))
  621. @$(call core_http_get,$(ELVIS),$(ELVIS_URL))
  622. @chmod +x $(ELVIS)
  623. elvis: $(ELVIS)
  624. @$(ELVIS) rock -c $(ELVIS_CONFIG) $(ELVIS_OPTS)
  625. distclean-elvis:
  626. $(gen_verbose) rm -rf $(ELVIS)
  627. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  628. # This file is part of erlang.mk and subject to the terms of the ISC License.
  629. # Verbosity.
  630. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  631. dtl_verbose = $(dtl_verbose_$(V))
  632. # Core targets.
  633. define compile_erlydtl
  634. $(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
  635. Compile = fun(F) -> \
  636. Module = list_to_atom( \
  637. string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
  638. erlydtl:compile(F, Module, [{out_dir, "ebin/"}]) \
  639. end, \
  640. _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
  641. init:stop()'
  642. endef
  643. ifneq ($(wildcard src/),)
  644. ebin/$(PROJECT).app:: $(shell find templates -type f -name \*.dtl 2>/dev/null)
  645. $(if $(strip $?),$(call compile_erlydtl,$?))
  646. endif
  647. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  648. # This file is part of erlang.mk and subject to the terms of the ISC License.
  649. .PHONY: relx-rel distclean-relx-rel distclean-relx
  650. # Configuration.
  651. RELX_CONFIG ?= $(CURDIR)/relx.config
  652. RELX ?= $(CURDIR)/relx
  653. export RELX
  654. RELX_URL ?= https://github.com/erlware/relx/releases/download/v1.0.2/relx
  655. RELX_OPTS ?=
  656. RELX_OUTPUT_DIR ?= _rel
  657. ifeq ($(firstword $(RELX_OPTS)),-o)
  658. RELX_OUTPUT_DIR = $(word 2,$(RELX_OPTS))
  659. else
  660. RELX_OPTS += -o $(RELX_OUTPUT_DIR)
  661. endif
  662. # Core targets.
  663. ifneq ($(wildcard $(RELX_CONFIG)),)
  664. rel:: distclean-relx-rel relx-rel
  665. endif
  666. distclean:: distclean-relx-rel distclean-relx
  667. # Plugin-specific targets.
  668. define relx_fetch
  669. $(call core_http_get,$(RELX),$(RELX_URL))
  670. chmod +x $(RELX)
  671. endef
  672. $(RELX):
  673. @$(call relx_fetch)
  674. relx-rel: $(RELX)
  675. @$(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
  676. distclean-relx-rel:
  677. $(gen_verbose) rm -rf $(RELX_OUTPUT_DIR)
  678. distclean-relx:
  679. $(gen_verbose) rm -rf $(RELX)
  680. # Copyright (c) 2014, M Robert Martin <rob@version2beta.com>
  681. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  682. .PHONY: shell
  683. # Configuration.
  684. SHELL_PATH ?= -pa ../$(PROJECT)/ebin $(DEPS_DIR)/*/ebin
  685. SHELL_OPTS ?=
  686. ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS))
  687. # Core targets
  688. help::
  689. @printf "%s\n" "" \
  690. "Shell targets:" \
  691. " shell Run an erlang shell with SHELL_OPTS or reasonable default"
  692. # Plugin-specific targets.
  693. $(foreach dep,$(SHELL_DEPS),$(eval $(call dep_target,$(dep))))
  694. build-shell-deps: $(ALL_SHELL_DEPS_DIRS)
  695. @for dep in $(ALL_SHELL_DEPS_DIRS) ; do $(MAKE) -C $$dep ; done
  696. shell: build-shell-deps
  697. $(gen_verbose) erl $(SHELL_PATH) $(SHELL_OPTS)