erlang.mk 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. ifneq ($(wildcard mibs/),)
  216. ebin/$(PROJECT).app:: $(shell find mibs -type f -name \*.mib)
  217. @mkdir -p priv/mibs/ include
  218. $(if $(strip $?),$(call compile_mib,$?))
  219. endif
  220. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.erl) \
  221. $(shell find src -type f -name \*.core)
  222. $(if $(strip $?),$(call compile_erl,$?))
  223. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.xrl) \
  224. $(shell find src -type f -name \*.yrl)
  225. $(if $(strip $?),$(call compile_xyrl,$?))
  226. endif
  227. clean:: clean-app
  228. # Extra targets.
  229. erlc-include:
  230. -@if [ -d ebin/ ]; then \
  231. find include/ src/ -type f -name \*.hrl -newer ebin -exec touch $(shell find src/ -type f -name "*.erl") \; 2>/dev/null || printf ''; \
  232. fi
  233. clean-app:
  234. $(gen_verbose) rm -rf ebin/ priv/mibs/
  235. $(gen_verbose) rm -f $(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(wildcard mibs/*.mib)))))
  236. # Copyright (c) 2014, Loïc Hoguin <essen@ninenines.eu>
  237. # This file is part of erlang.mk and subject to the terms of the ISC License.
  238. .PHONY: bootstrap bootstrap-lib bootstrap-rel new list-templates
  239. # Core targets.
  240. help::
  241. @printf "%s\n" "" \
  242. "Bootstrap targets:" \
  243. " bootstrap Generate a skeleton of an OTP application" \
  244. " bootstrap-lib Generate a skeleton of an OTP library" \
  245. " bootstrap-rel Generate the files needed to build a release" \
  246. " new t=TPL n=NAME Generate a module NAME based on the template TPL" \
  247. " list-templates List available templates"
  248. # Bootstrap templates.
  249. bs_appsrc = "{application, $(PROJECT), [" \
  250. " {description, \"\"}," \
  251. " {vsn, \"0.1.0\"}," \
  252. " {id, \"git\"}," \
  253. " {modules, []}," \
  254. " {registered, []}," \
  255. " {applications, [" \
  256. " kernel," \
  257. " stdlib" \
  258. " ]}," \
  259. " {mod, {$(PROJECT)_app, []}}," \
  260. " {env, []}" \
  261. "]}."
  262. bs_appsrc_lib = "{application, $(PROJECT), [" \
  263. " {description, \"\"}," \
  264. " {vsn, \"0.1.0\"}," \
  265. " {id, \"git\"}," \
  266. " {modules, []}," \
  267. " {registered, []}," \
  268. " {applications, [" \
  269. " kernel," \
  270. " stdlib" \
  271. " ]}" \
  272. "]}."
  273. bs_Makefile = "PROJECT = $(PROJECT)" \
  274. "include erlang.mk"
  275. bs_app = "-module($(PROJECT)_app)." \
  276. "-behaviour(application)." \
  277. "" \
  278. "-export([start/2])." \
  279. "-export([stop/1])." \
  280. "" \
  281. "start(_Type, _Args) ->" \
  282. " $(PROJECT)_sup:start_link()." \
  283. "" \
  284. "stop(_State) ->" \
  285. " ok."
  286. bs_relx_config = "{release, {$(PROJECT)_release, \"1\"}, [$(PROJECT)]}." \
  287. "{extended_start_script, true}." \
  288. "{sys_config, \"rel/sys.config\"}." \
  289. "{vm_args, \"rel/vm.args\"}."
  290. bs_sys_config = "[" \
  291. "]."
  292. bs_vm_args = "-name $(PROJECT)@127.0.0.1" \
  293. "-setcookie $(PROJECT)" \
  294. "-heart"
  295. # Normal templates.
  296. tpl_supervisor = "-module($(n))." \
  297. "-behaviour(supervisor)." \
  298. "" \
  299. "-export([start_link/0])." \
  300. "-export([init/1])." \
  301. "" \
  302. "start_link() ->" \
  303. " supervisor:start_link({local, ?MODULE}, ?MODULE, [])." \
  304. "" \
  305. "init([]) ->" \
  306. " Procs = []," \
  307. " {ok, {{one_for_one, 1, 5}, Procs}}."
  308. tpl_gen_server = "-module($(n))." \
  309. "-behaviour(gen_server)." \
  310. "" \
  311. "%% API." \
  312. "-export([start_link/0])." \
  313. "" \
  314. "%% gen_server." \
  315. "-export([init/1])." \
  316. "-export([handle_call/3])." \
  317. "-export([handle_cast/2])." \
  318. "-export([handle_info/2])." \
  319. "-export([terminate/2])." \
  320. "-export([code_change/3])." \
  321. "" \
  322. "-record(state, {" \
  323. "})." \
  324. "" \
  325. "%% API." \
  326. "" \
  327. "-spec start_link() -> {ok, pid()}." \
  328. "start_link() ->" \
  329. " gen_server:start_link(?MODULE, [], [])." \
  330. "" \
  331. "%% gen_server." \
  332. "" \
  333. "init([]) ->" \
  334. " {ok, \#state{}}." \
  335. "" \
  336. "handle_call(_Request, _From, State) ->" \
  337. " {reply, ignored, State}." \
  338. "" \
  339. "handle_cast(_Msg, State) ->" \
  340. " {noreply, State}." \
  341. "" \
  342. "handle_info(_Info, State) ->" \
  343. " {noreply, State}." \
  344. "" \
  345. "terminate(_Reason, _State) ->" \
  346. " ok." \
  347. "" \
  348. "code_change(_OldVsn, State, _Extra) ->" \
  349. " {ok, State}."
  350. tpl_cowboy_http = "-module($(n))." \
  351. "-behaviour(cowboy_http_handler)." \
  352. "" \
  353. "-export([init/3])." \
  354. "-export([handle/2])." \
  355. "-export([terminate/3])." \
  356. "" \
  357. "-record(state, {" \
  358. "})." \
  359. "" \
  360. "init(_, Req, _Opts) ->" \
  361. " {ok, Req, \#state{}}." \
  362. "" \
  363. "handle(Req, State=\#state{}) ->" \
  364. " {ok, Req2} = cowboy_req:reply(200, Req)," \
  365. " {ok, Req2, State}." \
  366. "" \
  367. "terminate(_Reason, _Req, _State) ->" \
  368. " ok."
  369. tpl_cowboy_loop = "-module($(n))." \
  370. "-behaviour(cowboy_loop_handler)." \
  371. "" \
  372. "-export([init/3])." \
  373. "-export([info/3])." \
  374. "-export([terminate/3])." \
  375. "" \
  376. "-record(state, {" \
  377. "})." \
  378. "" \
  379. "init(_, Req, _Opts) ->" \
  380. " {loop, Req, \#state{}, 5000, hibernate}." \
  381. "" \
  382. "info(_Info, Req, State) ->" \
  383. " {loop, Req, State, hibernate}." \
  384. "" \
  385. "terminate(_Reason, _Req, _State) ->" \
  386. " ok."
  387. tpl_cowboy_rest = "-module($(n))." \
  388. "" \
  389. "-export([init/3])." \
  390. "-export([content_types_provided/2])." \
  391. "-export([get_html/2])." \
  392. "" \
  393. "init(_, _Req, _Opts) ->" \
  394. " {upgrade, protocol, cowboy_rest}." \
  395. "" \
  396. "content_types_provided(Req, State) ->" \
  397. " {[{{<<\"text\">>, <<\"html\">>, '*'}, get_html}], Req, State}." \
  398. "" \
  399. "get_html(Req, State) ->" \
  400. " {<<\"<html><body>This is REST!</body></html>\">>, Req, State}."
  401. tpl_cowboy_ws = "-module($(n))." \
  402. "-behaviour(cowboy_websocket_handler)." \
  403. "" \
  404. "-export([init/3])." \
  405. "-export([websocket_init/3])." \
  406. "-export([websocket_handle/3])." \
  407. "-export([websocket_info/3])." \
  408. "-export([websocket_terminate/3])." \
  409. "" \
  410. "-record(state, {" \
  411. "})." \
  412. "" \
  413. "init(_, _, _) ->" \
  414. " {upgrade, protocol, cowboy_websocket}." \
  415. "" \
  416. "websocket_init(_, Req, _Opts) ->" \
  417. " Req2 = cowboy_req:compact(Req)," \
  418. " {ok, Req2, \#state{}}." \
  419. "" \
  420. "websocket_handle({text, Data}, Req, State) ->" \
  421. " {reply, {text, Data}, Req, State};" \
  422. "websocket_handle({binary, Data}, Req, State) ->" \
  423. " {reply, {binary, Data}, Req, State};" \
  424. "websocket_handle(_Frame, Req, State) ->" \
  425. " {ok, Req, State}." \
  426. "" \
  427. "websocket_info(_Info, Req, State) ->" \
  428. " {ok, Req, State}." \
  429. "" \
  430. "websocket_terminate(_Reason, _Req, _State) ->" \
  431. " ok."
  432. tpl_ranch_protocol = "-module($(n))." \
  433. "-behaviour(ranch_protocol)." \
  434. "" \
  435. "-export([start_link/4])." \
  436. "-export([init/4])." \
  437. "" \
  438. "-type opts() :: []." \
  439. "-export_type([opts/0])." \
  440. "" \
  441. "-record(state, {" \
  442. " socket :: inet:socket()," \
  443. " transport :: module()" \
  444. "})." \
  445. "" \
  446. "start_link(Ref, Socket, Transport, Opts) ->" \
  447. " Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts])," \
  448. " {ok, Pid}." \
  449. "" \
  450. "-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok." \
  451. "init(Ref, Socket, Transport, _Opts) ->" \
  452. " ok = ranch:accept_ack(Ref)," \
  453. " loop(\#state{socket=Socket, transport=Transport})." \
  454. "" \
  455. "loop(State) ->" \
  456. " loop(State)."
  457. # Plugin-specific targets.
  458. bootstrap:
  459. ifneq ($(wildcard src/),)
  460. $(error Error: src/ directory already exists)
  461. endif
  462. @printf "%s\n" $(bs_Makefile) > Makefile
  463. @mkdir src/
  464. @printf "%s\n" $(bs_appsrc) > src/$(PROJECT).app.src
  465. @printf "%s\n" $(bs_app) > src/$(PROJECT)_app.erl
  466. $(eval n := $(PROJECT)_sup)
  467. @printf "%s\n" $(tpl_supervisor) > src/$(PROJECT)_sup.erl
  468. bootstrap-lib:
  469. ifneq ($(wildcard src/),)
  470. $(error Error: src/ directory already exists)
  471. endif
  472. @printf "%s\n" $(bs_Makefile) > Makefile
  473. @mkdir src/
  474. @printf "%s\n" $(bs_appsrc_lib) > src/$(PROJECT).app.src
  475. bootstrap-rel:
  476. ifneq ($(wildcard relx.config),)
  477. $(error Error: relx.config already exists)
  478. endif
  479. ifneq ($(wildcard rel/),)
  480. $(error Error: rel/ directory already exists)
  481. endif
  482. @printf "%s\n" $(bs_relx_config) > relx.config
  483. @mkdir rel/
  484. @printf "%s\n" $(bs_sys_config) > rel/sys.config
  485. @printf "%s\n" $(bs_vm_args) > rel/vm.args
  486. new:
  487. ifeq ($(wildcard src/),)
  488. $(error Error: src/ directory does not exist)
  489. endif
  490. ifndef t
  491. $(error Usage: make new t=TEMPLATE n=NAME)
  492. endif
  493. ifndef tpl_$(t)
  494. $(error Unknown template)
  495. endif
  496. ifndef n
  497. $(error Usage: make new t=TEMPLATE n=NAME)
  498. endif
  499. @printf "%s\n" $(tpl_$(t)) > src/$(n).erl
  500. list-templates:
  501. @echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))
  502. # Copyright (c) 2014, Loïc Hoguin <essen@ninenines.eu>
  503. # This file is part of erlang.mk and subject to the terms of the ISC License.
  504. .PHONY: clean-c_src distclean-c_src-env
  505. # todo
  506. # Configuration.
  507. C_SRC_DIR = $(CURDIR)/c_src
  508. C_SRC_ENV ?= $(C_SRC_DIR)/env.mk
  509. C_SRC_OUTPUT ?= $(CURDIR)/priv/$(PROJECT).so
  510. # System type and C compiler/flags.
  511. UNAME_SYS := $(shell uname -s)
  512. ifeq ($(UNAME_SYS), Darwin)
  513. CC ?= cc
  514. CFLAGS ?= -O3 -std=c99 -arch x86_64 -flat_namespace -undefined suppress -finline-functions -Wall -Wmissing-prototypes
  515. CXXFLAGS ?= -O3 -arch x86_64 -flat_namespace -undefined suppress -finline-functions -Wall
  516. else ifeq ($(UNAME_SYS), FreeBSD)
  517. CC ?= cc
  518. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  519. CXXFLAGS ?= -O3 -finline-functions -Wall
  520. else ifeq ($(UNAME_SYS), Linux)
  521. CC ?= gcc
  522. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  523. CXXFLAGS ?= -O3 -finline-functions -Wall
  524. endif
  525. CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
  526. CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
  527. LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
  528. LDFLAGS += -shared
  529. # Verbosity.
  530. c_verbose_0 = @echo " C " $(?F);
  531. c_verbose = $(c_verbose_$(V))
  532. cpp_verbose_0 = @echo " CPP " $(?F);
  533. cpp_verbose = $(cpp_verbose_$(V))
  534. link_verbose_0 = @echo " LD " $(@F);
  535. link_verbose = $(link_verbose_$(V))
  536. # Targets.
  537. ifeq ($(wildcard $(C_SRC_DIR)),)
  538. else ifneq ($(wildcard $(C_SRC_DIR)/Makefile),)
  539. app::
  540. $(MAKE) -C $(C_SRC_DIR)
  541. clean::
  542. $(MAKE) -C $(C_SRC_DIR) clean
  543. else
  544. SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
  545. OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
  546. COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
  547. COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
  548. app:: $(C_SRC_ENV) $(C_SRC_OUTPUT)
  549. $(C_SRC_OUTPUT): $(OBJECTS)
  550. @mkdir -p priv/
  551. $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT)
  552. %.o: %.c
  553. $(COMPILE_C) $(OUTPUT_OPTION) $<
  554. %.o: %.cc
  555. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  556. %.o: %.C
  557. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  558. %.o: %.cpp
  559. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  560. $(C_SRC_ENV):
  561. @erl -noshell -noinput -eval "file:write_file(\"$(C_SRC_ENV)\", \
  562. io_lib:format( \
  563. \"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \
  564. \"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \
  565. \"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \
  566. [code:root_dir(), erlang:system_info(version), \
  567. code:lib_dir(erl_interface, include), \
  568. code:lib_dir(erl_interface, lib)])), \
  569. erlang:halt()."
  570. clean:: clean-c_src
  571. clean-c_src:
  572. $(gen_verbose) rm -f $(C_SRC_OUTPUT) $(OBJECTS)
  573. distclean:: distclean-c_src-env
  574. distclean-c_src-env:
  575. $(gen_verbose) rm -f $(C_SRC_ENV)
  576. -include $(C_SRC_ENV)
  577. endif
  578. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  579. # This file is part of erlang.mk and subject to the terms of the ISC License.
  580. .PHONY: build-ct-deps build-ct-suites tests-ct clean-ct distclean-ct
  581. # Configuration.
  582. CT_OPTS ?=
  583. ifneq ($(wildcard test/),)
  584. CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(shell find test -type f -name \*_SUITE.erl -exec basename {} \;)))
  585. else
  586. CT_SUITES ?=
  587. endif
  588. TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
  589. TEST_ERLC_OPTS += -DTEST=1 -DEXTRA=1 +'{parse_transform, eunit_autoexport}'
  590. # Core targets.
  591. tests:: tests-ct
  592. clean:: clean-ct
  593. distclean:: distclean-ct
  594. help::
  595. @printf "%s\n" "" \
  596. "All your common_test suites have their associated targets." \
  597. "A suite named http_SUITE can be ran using the ct-http target."
  598. # Plugin-specific targets.
  599. ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
  600. CT_RUN = ct_run \
  601. -no_auto_compile \
  602. -noshell \
  603. -pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
  604. -dir test \
  605. -logdir logs
  606. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  607. build-ct-deps: $(ALL_TEST_DEPS_DIRS)
  608. @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  609. build-ct-suites: build-ct-deps
  610. $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -I include/ -o test/ \
  611. $(wildcard test/*.erl test/*/*.erl) -pa ebin/
  612. tests-ct: ERLC_OPTS = $(TEST_ERLC_OPTS)
  613. tests-ct: clean deps app build-ct-suites
  614. @if [ -d "test" ] ; \
  615. then \
  616. mkdir -p logs/ ; \
  617. $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) $(CT_OPTS) ; \
  618. fi
  619. $(gen_verbose) rm -f test/*.beam
  620. define ct_suite_target
  621. ct-$(1): ERLC_OPTS = $(TEST_ERLC_OPTS)
  622. ct-$(1): clean deps app build-ct-suites
  623. @if [ -d "test" ] ; \
  624. then \
  625. mkdir -p logs/ ; \
  626. $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) $(CT_OPTS) ; \
  627. fi
  628. $(gen_verbose) rm -f test/*.beam
  629. endef
  630. $(foreach test,$(CT_SUITES),$(eval $(call ct_suite_target,$(test))))
  631. clean-ct:
  632. $(gen_verbose) rm -rf test/*.beam
  633. distclean-ct:
  634. $(gen_verbose) rm -rf logs/
  635. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  636. # This file is part of erlang.mk and subject to the terms of the ISC License.
  637. .PHONY: plt distclean-plt dialyze
  638. # Configuration.
  639. DIALYZER_PLT ?= $(CURDIR)/.$(PROJECT).plt
  640. export DIALYZER_PLT
  641. PLT_APPS ?=
  642. DIALYZER_DIRS ?= --src -r src
  643. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
  644. -Wunmatched_returns # -Wunderspecs
  645. # Core targets.
  646. distclean:: distclean-plt
  647. help::
  648. @printf "%s\n" "" \
  649. "Dialyzer targets:" \
  650. " plt Build a PLT file for this project" \
  651. " dialyze Analyze the project using Dialyzer"
  652. # Plugin-specific targets.
  653. $(DIALYZER_PLT): deps app
  654. @dialyzer --build_plt --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
  655. plt: $(DIALYZER_PLT)
  656. distclean-plt:
  657. $(gen_verbose) rm -f $(DIALYZER_PLT)
  658. ifneq ($(wildcard $(DIALYZER_PLT)),)
  659. dialyze:
  660. else
  661. dialyze: $(DIALYZER_PLT)
  662. endif
  663. @dialyzer --no_native $(DIALYZER_DIRS) $(DIALYZER_OPTS)
  664. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  665. # This file is part of erlang.mk and subject to the terms of the ISC License.
  666. .PHONY: distclean-edoc
  667. # Configuration.
  668. EDOC_OPTS ?=
  669. # Core targets.
  670. docs:: distclean-edoc
  671. $(gen_verbose) erl -noshell \
  672. -eval 'edoc:application($(PROJECT), ".", [$(EDOC_OPTS)]), init:stop().'
  673. distclean:: distclean-edoc
  674. # Plugin-specific targets.
  675. distclean-edoc:
  676. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  677. # Copyright (c) 2014, Juan Facorro <juan@inaka.net>
  678. # This file is part of erlang.mk and subject to the terms of the ISC License.
  679. .PHONY: elvis distclean-elvis
  680. # Configuration.
  681. ELVIS_CONFIG ?= $(CURDIR)/elvis.config
  682. ELVIS ?= $(CURDIR)/elvis
  683. export ELVIS
  684. ELVIS_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis
  685. ELVIS_CONFIG_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis.config
  686. ELVIS_OPTS ?=
  687. # Core targets.
  688. help::
  689. @printf "%s\n" "" \
  690. "Elvis targets:" \
  691. " elvis Run Elvis using the local elvis.config or download the default otherwise"
  692. ifneq ($(wildcard $(ELVIS_CONFIG)),)
  693. rel:: distclean-elvis
  694. endif
  695. distclean:: distclean-elvis
  696. # Plugin-specific targets.
  697. $(ELVIS):
  698. @$(call core_http_get,$(ELVIS_CONFIG),$(ELVIS_CONFIG_URL))
  699. @$(call core_http_get,$(ELVIS),$(ELVIS_URL))
  700. @chmod +x $(ELVIS)
  701. elvis: $(ELVIS)
  702. @$(ELVIS) rock -c $(ELVIS_CONFIG) $(ELVIS_OPTS)
  703. distclean-elvis:
  704. $(gen_verbose) rm -rf $(ELVIS)
  705. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  706. # This file is part of erlang.mk and subject to the terms of the ISC License.
  707. # Verbosity.
  708. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  709. dtl_verbose = $(dtl_verbose_$(V))
  710. # Core targets.
  711. define compile_erlydtl
  712. $(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
  713. Compile = fun(F) -> \
  714. Module = list_to_atom( \
  715. string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
  716. erlydtl:compile(F, Module, [{out_dir, "ebin/"}]) \
  717. end, \
  718. _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
  719. init:stop()'
  720. endef
  721. ifneq ($(wildcard src/),)
  722. ebin/$(PROJECT).app:: $(shell find templates -type f -name \*.dtl 2>/dev/null)
  723. $(if $(strip $?),$(call compile_erlydtl,$?))
  724. endif
  725. # Copyright (c) 2014 Dave Cottlehuber <dch@skunkwerks.at>
  726. # This file is part of erlang.mk and subject to the terms of the ISC License.
  727. .PHONY: distclean-escript escript
  728. # Configuration.
  729. ESCRIPT_NAME ?= $(PROJECT)
  730. ESCRIPT_COMMENT ?= This is an -*- erlang -*- file
  731. ESCRIPT_BEAMS ?= "ebin/*", "deps/*/ebin/*"
  732. ESCRIPT_SYS_CONFIG ?= "rel/sys.config"
  733. ESCRIPT_EMU_ARGS ?= -pa . \
  734. -noshell -noinput \
  735. -sasl errlog_type error \
  736. -escript main $(ESCRIPT_NAME)
  737. ESCRIPT_SHEBANG ?= /usr/bin/env escript
  738. ESCRIPT_STATIC ?= "deps/*/priv/**", "priv/**"
  739. # Core targets.
  740. distclean:: distclean-escript
  741. help::
  742. @printf "%s\n" "" \
  743. "Escript targets:" \
  744. " escript Build an executable escript archive" \
  745. # Plugin-specific targets.
  746. # Based on https://github.com/synrc/mad/blob/master/src/mad_bundle.erl
  747. # Copyright (c) 2013 Maxim Sokhatsky, Synrc Research Center
  748. # Modified MIT License, https://github.com/synrc/mad/blob/master/LICENSE :
  749. # Software may only be used for the great good and the true happiness of all
  750. # sentient beings.
  751. define ESCRIPT_RAW
  752. 'Read = fun(F) -> {ok, B} = file:read_file(filename:absname(F)), B end,'\
  753. 'Files = fun(L) -> A = lists:concat([filelib:wildcard(X)||X<- L ]),'\
  754. ' [F || F <- A, not filelib:is_dir(F) ] end,'\
  755. 'Squash = fun(L) -> [{filename:basename(F), Read(F) } || F <- L ] end,'\
  756. 'Zip = fun(A, L) -> {ok,{_,Z}} = zip:create(A, L, [{compress,all},memory]), Z end,'\
  757. 'Ez = fun(Escript) ->'\
  758. ' Static = Files([$(ESCRIPT_STATIC)]),'\
  759. ' Beams = Squash(Files([$(ESCRIPT_BEAMS), $(ESCRIPT_SYS_CONFIG)])),'\
  760. ' Archive = Beams ++ [{ "static.gz", Zip("static.gz", Static)}],'\
  761. ' escript:create(Escript, [ $(ESCRIPT_OPTIONS)'\
  762. ' {archive, Archive, [memory]},'\
  763. ' {shebang, "$(ESCRIPT_SHEBANG)"},'\
  764. ' {comment, "$(ESCRIPT_COMMENT)"},'\
  765. ' {emu_args, " $(ESCRIPT_EMU_ARGS)"}'\
  766. ' ]),'\
  767. ' file:change_mode(Escript, 8#755)'\
  768. 'end,'\
  769. 'Ez("$(ESCRIPT_NAME)").'
  770. endef
  771. ESCRIPT_COMMAND = $(subst ' ',,$(ESCRIPT_RAW))
  772. escript:: distclean-escript deps app
  773. $(gen_verbose) erl -noshell -eval $(ESCRIPT_COMMAND) -s init stop
  774. distclean-escript:
  775. $(gen_verbose) rm -f $(ESCRIPT_NAME)
  776. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  777. # This file is part of erlang.mk and subject to the terms of the ISC License.
  778. .PHONY: relx-rel distclean-relx-rel distclean-relx
  779. # Configuration.
  780. RELX_CONFIG ?= $(CURDIR)/relx.config
  781. RELX ?= $(CURDIR)/relx
  782. export RELX
  783. RELX_URL ?= https://github.com/erlware/relx/releases/download/v1.0.2/relx
  784. RELX_OPTS ?=
  785. RELX_OUTPUT_DIR ?= _rel
  786. ifeq ($(firstword $(RELX_OPTS)),-o)
  787. RELX_OUTPUT_DIR = $(word 2,$(RELX_OPTS))
  788. else
  789. RELX_OPTS += -o $(RELX_OUTPUT_DIR)
  790. endif
  791. # Core targets.
  792. ifneq ($(wildcard $(RELX_CONFIG)),)
  793. rel:: distclean-relx-rel relx-rel
  794. endif
  795. distclean:: distclean-relx-rel distclean-relx
  796. # Plugin-specific targets.
  797. define relx_fetch
  798. $(call core_http_get,$(RELX),$(RELX_URL))
  799. chmod +x $(RELX)
  800. endef
  801. $(RELX):
  802. @$(call relx_fetch)
  803. relx-rel: $(RELX)
  804. @$(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
  805. distclean-relx-rel:
  806. $(gen_verbose) rm -rf $(RELX_OUTPUT_DIR)
  807. distclean-relx:
  808. $(gen_verbose) rm -rf $(RELX)
  809. # Copyright (c) 2014, M Robert Martin <rob@version2beta.com>
  810. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  811. .PHONY: shell
  812. # Configuration.
  813. SHELL_PATH ?= -pa ../$(PROJECT)/ebin $(DEPS_DIR)/*/ebin
  814. SHELL_OPTS ?=
  815. ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS))
  816. # Core targets
  817. help::
  818. @printf "%s\n" "" \
  819. "Shell targets:" \
  820. " shell Run an erlang shell with SHELL_OPTS or reasonable default"
  821. # Plugin-specific targets.
  822. $(foreach dep,$(SHELL_DEPS),$(eval $(call dep_target,$(dep))))
  823. build-shell-deps: $(ALL_SHELL_DEPS_DIRS)
  824. @for dep in $(ALL_SHELL_DEPS_DIRS) ; do $(MAKE) -C $$dep ; done
  825. shell: build-shell-deps
  826. $(gen_verbose) erl $(SHELL_PATH) $(SHELL_OPTS)