erlang.mk 21 KB

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