erlang.mk 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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_vars +warn_shadow_vars \
  166. +warn_obsolete_guard # +bin_opt_info +warn_export_all +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. $(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 -finline-functions -Wall -Wmissing-prototypes
  515. CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
  516. LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
  517. else ifeq ($(UNAME_SYS), FreeBSD)
  518. CC ?= cc
  519. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  520. CXXFLAGS ?= -O3 -finline-functions -Wall
  521. else ifeq ($(UNAME_SYS), Linux)
  522. CC ?= gcc
  523. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  524. CXXFLAGS ?= -O3 -finline-functions -Wall
  525. endif
  526. CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
  527. CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
  528. LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
  529. LDFLAGS += -shared
  530. # Verbosity.
  531. c_verbose_0 = @echo " C " $(?F);
  532. c_verbose = $(c_verbose_$(V))
  533. cpp_verbose_0 = @echo " CPP " $(?F);
  534. cpp_verbose = $(cpp_verbose_$(V))
  535. link_verbose_0 = @echo " LD " $(@F);
  536. link_verbose = $(link_verbose_$(V))
  537. # Targets.
  538. ifeq ($(wildcard $(C_SRC_DIR)),)
  539. else ifneq ($(wildcard $(C_SRC_DIR)/Makefile),)
  540. app::
  541. $(MAKE) -C $(C_SRC_DIR)
  542. clean::
  543. $(MAKE) -C $(C_SRC_DIR) clean
  544. else
  545. SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
  546. OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
  547. COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
  548. COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
  549. app:: $(C_SRC_ENV) $(C_SRC_OUTPUT)
  550. $(C_SRC_OUTPUT): $(OBJECTS)
  551. @mkdir -p priv/
  552. $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT)
  553. %.o: %.c
  554. $(COMPILE_C) $(OUTPUT_OPTION) $<
  555. %.o: %.cc
  556. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  557. %.o: %.C
  558. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  559. %.o: %.cpp
  560. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  561. $(C_SRC_ENV):
  562. @erl -noshell -noinput -eval "file:write_file(\"$(C_SRC_ENV)\", \
  563. io_lib:format( \
  564. \"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \
  565. \"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \
  566. \"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \
  567. [code:root_dir(), erlang:system_info(version), \
  568. code:lib_dir(erl_interface, include), \
  569. code:lib_dir(erl_interface, lib)])), \
  570. erlang:halt()."
  571. clean:: clean-c_src
  572. clean-c_src:
  573. $(gen_verbose) rm -f $(C_SRC_OUTPUT) $(OBJECTS)
  574. distclean:: distclean-c_src-env
  575. distclean-c_src-env:
  576. $(gen_verbose) rm -f $(C_SRC_ENV)
  577. -include $(C_SRC_ENV)
  578. endif
  579. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  580. # This file is part of erlang.mk and subject to the terms of the ISC License.
  581. .PHONY: build-ct-deps build-ct-suites tests-ct clean-ct distclean-ct
  582. # Configuration.
  583. CT_OPTS ?=
  584. ifneq ($(wildcard test/),)
  585. CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(shell find test -type f -name \*_SUITE.erl -exec basename {} \;)))
  586. else
  587. CT_SUITES ?=
  588. endif
  589. TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
  590. TEST_ERLC_OPTS += -DTEST=1 -DEXTRA=1 +'{parse_transform, eunit_autoexport}'
  591. # Core targets.
  592. tests:: tests-ct
  593. clean:: clean-ct
  594. distclean:: distclean-ct
  595. help::
  596. @printf "%s\n" "" \
  597. "All your common_test suites have their associated targets." \
  598. "A suite named http_SUITE can be ran using the ct-http target."
  599. # Plugin-specific targets.
  600. ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
  601. CT_RUN = ct_run \
  602. -no_auto_compile \
  603. -noshell \
  604. -pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
  605. -dir test \
  606. -logdir logs
  607. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  608. build-ct-deps: $(ALL_TEST_DEPS_DIRS)
  609. @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  610. build-ct-suites: build-ct-deps
  611. $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -I include/ -o test/ \
  612. $(wildcard test/*.erl test/*/*.erl) -pa ebin/
  613. tests-ct: ERLC_OPTS = $(TEST_ERLC_OPTS)
  614. tests-ct: clean deps app build-ct-suites
  615. @if [ -d "test" ] ; \
  616. then \
  617. mkdir -p logs/ ; \
  618. $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) $(CT_OPTS) ; \
  619. fi
  620. $(gen_verbose) rm -f test/*.beam
  621. define ct_suite_target
  622. ct-$(1): ERLC_OPTS = $(TEST_ERLC_OPTS)
  623. ct-$(1): clean deps app build-ct-suites
  624. @if [ -d "test" ] ; \
  625. then \
  626. mkdir -p logs/ ; \
  627. $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) $(CT_OPTS) ; \
  628. fi
  629. $(gen_verbose) rm -f test/*.beam
  630. endef
  631. $(foreach test,$(CT_SUITES),$(eval $(call ct_suite_target,$(test))))
  632. clean-ct:
  633. $(gen_verbose) rm -rf test/*.beam
  634. distclean-ct:
  635. $(gen_verbose) rm -rf logs/
  636. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  637. # This file is part of erlang.mk and subject to the terms of the ISC License.
  638. .PHONY: plt distclean-plt dialyze
  639. # Configuration.
  640. DIALYZER_PLT ?= $(CURDIR)/.$(PROJECT).plt
  641. export DIALYZER_PLT
  642. PLT_APPS ?=
  643. DIALYZER_DIRS ?= --src -r src
  644. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
  645. -Wunmatched_returns # -Wunderspecs
  646. # Core targets.
  647. distclean:: distclean-plt
  648. help::
  649. @printf "%s\n" "" \
  650. "Dialyzer targets:" \
  651. " plt Build a PLT file for this project" \
  652. " dialyze Analyze the project using Dialyzer"
  653. # Plugin-specific targets.
  654. $(DIALYZER_PLT): deps app
  655. @dialyzer --build_plt --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
  656. plt: $(DIALYZER_PLT)
  657. distclean-plt:
  658. $(gen_verbose) rm -f $(DIALYZER_PLT)
  659. ifneq ($(wildcard $(DIALYZER_PLT)),)
  660. dialyze:
  661. else
  662. dialyze: $(DIALYZER_PLT)
  663. endif
  664. @dialyzer --no_native $(DIALYZER_DIRS) $(DIALYZER_OPTS)
  665. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  666. # This file is part of erlang.mk and subject to the terms of the ISC License.
  667. .PHONY: distclean-edoc
  668. # Configuration.
  669. EDOC_OPTS ?=
  670. # Core targets.
  671. docs:: distclean-edoc
  672. $(gen_verbose) erl -noshell \
  673. -eval 'edoc:application($(PROJECT), ".", [$(EDOC_OPTS)]), init:stop().'
  674. distclean:: distclean-edoc
  675. # Plugin-specific targets.
  676. distclean-edoc:
  677. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  678. # Copyright (c) 2014, Juan Facorro <juan@inaka.net>
  679. # This file is part of erlang.mk and subject to the terms of the ISC License.
  680. .PHONY: elvis distclean-elvis
  681. # Configuration.
  682. ELVIS_CONFIG ?= $(CURDIR)/elvis.config
  683. ELVIS ?= $(CURDIR)/elvis
  684. export ELVIS
  685. ELVIS_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis
  686. ELVIS_CONFIG_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis.config
  687. ELVIS_OPTS ?=
  688. # Core targets.
  689. help::
  690. @printf "%s\n" "" \
  691. "Elvis targets:" \
  692. " elvis Run Elvis using the local elvis.config or download the default otherwise"
  693. ifneq ($(wildcard $(ELVIS_CONFIG)),)
  694. rel:: distclean-elvis
  695. endif
  696. distclean:: distclean-elvis
  697. # Plugin-specific targets.
  698. $(ELVIS):
  699. @$(call core_http_get,$(ELVIS_CONFIG),$(ELVIS_CONFIG_URL))
  700. @$(call core_http_get,$(ELVIS),$(ELVIS_URL))
  701. @chmod +x $(ELVIS)
  702. elvis: $(ELVIS)
  703. @$(ELVIS) rock -c $(ELVIS_CONFIG) $(ELVIS_OPTS)
  704. distclean-elvis:
  705. $(gen_verbose) rm -rf $(ELVIS)
  706. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  707. # This file is part of erlang.mk and subject to the terms of the ISC License.
  708. # Verbosity.
  709. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  710. dtl_verbose = $(dtl_verbose_$(V))
  711. # Core targets.
  712. define compile_erlydtl
  713. $(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
  714. Compile = fun(F) -> \
  715. Module = list_to_atom( \
  716. string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
  717. erlydtl:compile(F, Module, [{out_dir, "ebin/"}]) \
  718. end, \
  719. _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
  720. init:stop()'
  721. endef
  722. ifneq ($(wildcard src/),)
  723. ebin/$(PROJECT).app:: $(shell find templates -type f -name \*.dtl 2>/dev/null)
  724. $(if $(strip $?),$(call compile_erlydtl,$?))
  725. endif
  726. # Copyright (c) 2014 Dave Cottlehuber <dch@skunkwerks.at>
  727. # This file is part of erlang.mk and subject to the terms of the ISC License.
  728. .PHONY: distclean-escript escript
  729. # Configuration.
  730. ESCRIPT_NAME ?= $(PROJECT)
  731. ESCRIPT_COMMENT ?= This is an -*- erlang -*- file
  732. ESCRIPT_BEAMS ?= "ebin/*", "deps/*/ebin/*"
  733. ESCRIPT_SYS_CONFIG ?= "rel/sys.config"
  734. ESCRIPT_EMU_ARGS ?= -pa . \
  735. -noshell -noinput \
  736. -sasl errlog_type error \
  737. -escript main $(ESCRIPT_NAME)
  738. ESCRIPT_SHEBANG ?= /usr/bin/env escript
  739. ESCRIPT_STATIC ?= "deps/*/priv/**", "priv/**"
  740. # Core targets.
  741. distclean:: distclean-escript
  742. help::
  743. @printf "%s\n" "" \
  744. "Escript targets:" \
  745. " escript Build an executable escript archive" \
  746. # Plugin-specific targets.
  747. # Based on https://github.com/synrc/mad/blob/master/src/mad_bundle.erl
  748. # Copyright (c) 2013 Maxim Sokhatsky, Synrc Research Center
  749. # Modified MIT License, https://github.com/synrc/mad/blob/master/LICENSE :
  750. # Software may only be used for the great good and the true happiness of all
  751. # sentient beings.
  752. define ESCRIPT_RAW
  753. 'Read = fun(F) -> {ok, B} = file:read_file(filename:absname(F)), B end,'\
  754. 'Files = fun(L) -> A = lists:concat([filelib:wildcard(X)||X<- L ]),'\
  755. ' [F || F <- A, not filelib:is_dir(F) ] end,'\
  756. 'Squash = fun(L) -> [{filename:basename(F), Read(F) } || F <- L ] end,'\
  757. 'Zip = fun(A, L) -> {ok,{_,Z}} = zip:create(A, L, [{compress,all},memory]), Z end,'\
  758. 'Ez = fun(Escript) ->'\
  759. ' Static = Files([$(ESCRIPT_STATIC)]),'\
  760. ' Beams = Squash(Files([$(ESCRIPT_BEAMS), $(ESCRIPT_SYS_CONFIG)])),'\
  761. ' Archive = Beams ++ [{ "static.gz", Zip("static.gz", Static)}],'\
  762. ' escript:create(Escript, [ $(ESCRIPT_OPTIONS)'\
  763. ' {archive, Archive, [memory]},'\
  764. ' {shebang, "$(ESCRIPT_SHEBANG)"},'\
  765. ' {comment, "$(ESCRIPT_COMMENT)"},'\
  766. ' {emu_args, " $(ESCRIPT_EMU_ARGS)"}'\
  767. ' ]),'\
  768. ' file:change_mode(Escript, 8#755)'\
  769. 'end,'\
  770. 'Ez("$(ESCRIPT_NAME)").'
  771. endef
  772. ESCRIPT_COMMAND = $(subst ' ',,$(ESCRIPT_RAW))
  773. escript:: distclean-escript deps app
  774. $(gen_verbose) erl -noshell -eval $(ESCRIPT_COMMAND) -s init stop
  775. distclean-escript:
  776. $(gen_verbose) rm -f $(ESCRIPT_NAME)
  777. # Copyright (c) 2013-2014, Loïc Hoguin <essen@ninenines.eu>
  778. # This file is part of erlang.mk and subject to the terms of the ISC License.
  779. .PHONY: relx-rel distclean-relx-rel distclean-relx
  780. # Configuration.
  781. RELX_CONFIG ?= $(CURDIR)/relx.config
  782. RELX ?= $(CURDIR)/relx
  783. export RELX
  784. RELX_URL ?= https://github.com/erlware/relx/releases/download/v1.0.2/relx
  785. RELX_OPTS ?=
  786. RELX_OUTPUT_DIR ?= _rel
  787. ifeq ($(firstword $(RELX_OPTS)),-o)
  788. RELX_OUTPUT_DIR = $(word 2,$(RELX_OPTS))
  789. else
  790. RELX_OPTS += -o $(RELX_OUTPUT_DIR)
  791. endif
  792. # Core targets.
  793. ifneq ($(wildcard $(RELX_CONFIG)),)
  794. rel:: distclean-relx-rel relx-rel
  795. endif
  796. distclean:: distclean-relx-rel distclean-relx
  797. # Plugin-specific targets.
  798. define relx_fetch
  799. $(call core_http_get,$(RELX),$(RELX_URL))
  800. chmod +x $(RELX)
  801. endef
  802. $(RELX):
  803. @$(call relx_fetch)
  804. relx-rel: $(RELX)
  805. @$(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
  806. distclean-relx-rel:
  807. $(gen_verbose) rm -rf $(RELX_OUTPUT_DIR)
  808. distclean-relx:
  809. $(gen_verbose) rm -rf $(RELX)
  810. # Copyright (c) 2014, M Robert Martin <rob@version2beta.com>
  811. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  812. .PHONY: shell
  813. # Configuration.
  814. SHELL_PATH ?= -pa ../$(PROJECT)/ebin $(DEPS_DIR)/*/ebin
  815. SHELL_OPTS ?=
  816. ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS))
  817. # Core targets
  818. help::
  819. @printf "%s\n" "" \
  820. "Shell targets:" \
  821. " shell Run an erlang shell with SHELL_OPTS or reasonable default"
  822. # Plugin-specific targets.
  823. $(foreach dep,$(SHELL_DEPS),$(eval $(call dep_target,$(dep))))
  824. build-shell-deps: $(ALL_SHELL_DEPS_DIRS)
  825. @for dep in $(ALL_SHELL_DEPS_DIRS) ; do $(MAKE) -C $$dep ; done
  826. shell: build-shell-deps
  827. $(gen_verbose) erl $(SHELL_PATH) $(SHELL_OPTS)