erlang.mk 21 KB

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