erlang.mk 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. # Copyright (c) 2013-2015, 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. # "erl" command.
  24. ERL = erl +A0 -noinput -boot start_clean
  25. # Core targets.
  26. ifneq ($(words $(MAKECMDGOALS)),1)
  27. .NOTPARALLEL:
  28. endif
  29. all:: deps
  30. @$(MAKE) --no-print-directory app
  31. @$(MAKE) --no-print-directory rel
  32. # Noop to avoid a Make warning when there's nothing to do.
  33. rel::
  34. @echo -n
  35. clean:: clean-crashdump
  36. clean-crashdump:
  37. ifneq ($(wildcard erl_crash.dump),)
  38. $(gen_verbose) rm -f erl_crash.dump
  39. endif
  40. distclean:: clean
  41. help::
  42. @printf "%s\n" \
  43. "erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \
  44. "Copyright (c) 2013-2014 Loïc Hoguin <essen@ninenines.eu>" \
  45. "" \
  46. "Usage: [V=1] make [-jNUM] [target]" \
  47. "" \
  48. "Core targets:" \
  49. " all Run deps, app and rel targets in that order" \
  50. " deps Fetch dependencies (if needed) and compile them" \
  51. " app Compile the project" \
  52. " rel Build a release for this project, if applicable" \
  53. " docs Build the documentation for this project" \
  54. " tests Run the tests for this project" \
  55. " clean Delete temporary and output files from most targets" \
  56. " distclean Delete all temporary and output files" \
  57. " help Display this help and exit" \
  58. "" \
  59. "The target clean only removes files that are commonly removed." \
  60. "Dependencies and releases are left untouched." \
  61. "" \
  62. "Setting V=1 when calling make enables verbose mode." \
  63. "Parallel execution is supported through the -j Make flag."
  64. # Core functions.
  65. ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
  66. define core_http_get
  67. wget --no-check-certificate -O $(1) $(2)|| rm $(1)
  68. endef
  69. else
  70. define core_http_get
  71. $(ERL) -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).'
  72. endef
  73. endif
  74. # Automated update.
  75. ERLANG_MK_BUILD_CONFIG ?= build.config
  76. ERLANG_MK_BUILD_DIR ?= .erlang.mk.build
  77. erlang-mk:
  78. git clone https://github.com/ninenines/erlang.mk $(ERLANG_MK_BUILD_DIR)
  79. if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR); fi
  80. cd $(ERLANG_MK_BUILD_DIR) && make
  81. cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
  82. rm -rf $(ERLANG_MK_BUILD_DIR)
  83. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  84. # This file is part of erlang.mk and subject to the terms of the ISC License.
  85. .PHONY: distclean-deps distclean-pkg pkg-list pkg-search
  86. # Configuration.
  87. AUTOPATCH ?= edown gen_leader gproc
  88. export AUTOPATCH
  89. DEPS_DIR ?= $(CURDIR)/deps
  90. export DEPS_DIR
  91. REBAR_DEPS_DIR = $(DEPS_DIR)
  92. export REBAR_DEPS_DIR
  93. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DEPS))
  94. ifeq ($(filter $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
  95. ifeq ($(ERL_LIBS),)
  96. ERL_LIBS = $(DEPS_DIR)
  97. else
  98. ERL_LIBS := $(ERL_LIBS):$(DEPS_DIR)
  99. endif
  100. endif
  101. export ERL_LIBS
  102. PKG_FILE2 ?= $(CURDIR)/.erlang.mk.packages.v2
  103. export PKG_FILE2
  104. PKG_FILE_URL ?= https://raw.githubusercontent.com/ninenines/erlang.mk/master/packages.v2.tsv
  105. # Core targets.
  106. deps:: $(ALL_DEPS_DIRS)
  107. @for dep in $(ALL_DEPS_DIRS) ; do \
  108. if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ] ; then \
  109. $(MAKE) -C $$dep || exit $$? ; \
  110. else \
  111. echo "ERROR: No makefile to build dependency $$dep. Consider adding it to AUTOPATCH." ; \
  112. exit 1 ; \
  113. fi ; \
  114. done
  115. distclean:: distclean-deps distclean-pkg
  116. # Deps related targets.
  117. define dep_autopatch
  118. $(ERL) -eval " \
  119. DepDir = \"$(DEPS_DIR)/$(1)/\", \
  120. fun() -> \
  121. {ok, Conf} = file:consult(DepDir ++ \"rebar.config\"), \
  122. File = case lists:keyfind(deps, 1, Conf) of false -> []; {_, Deps} -> \
  123. [begin {Method, Repo, Commit} = case Repos of \
  124. {git, R} -> {git, R, master}; \
  125. {M, R, {branch, C}} -> {M, R, C}; \
  126. {M, R, {tag, C}} -> {M, R, C}; \
  127. {M, R, C} -> {M, R, C} \
  128. end, \
  129. io_lib:format(\"DEPS += ~s\ndep_~s = ~s ~s ~s~n\", [Name, Name, Method, Repo, Commit]) \
  130. end || {Name, _, Repos} <- Deps] \
  131. end, \
  132. ok = file:write_file(\"$(DEPS_DIR)/$(1)/Makefile\", [\"ERLC_OPTS = +debug_info\n\n\", File, \"\ninclude erlang.mk\"]) \
  133. end(), \
  134. AppSrcOut = \"$(DEPS_DIR)/$(1)/src/$(1).app.src\", \
  135. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> \"$(DEPS_DIR)/$(1)/ebin/$(1).app\"; true -> AppSrcOut end, \
  136. fun() -> \
  137. {ok, [{application, $(1), L}]} = file:consult(AppSrcIn), \
  138. L2 = case lists:keyfind(modules, 1, L) of {_, _} -> L; false -> [{modules, []}|L] end, \
  139. L3 = case lists:keyfind(vsn, 1, L2) of {vsn, git} -> lists:keyreplace(vsn, 1, L2, {vsn, \"git\"}); _ -> L2 end, \
  140. ok = file:write_file(AppSrcOut, io_lib:format(\"~p.~n\", [{application, $(1), L3}])) \
  141. end(), \
  142. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end, \
  143. halt()."
  144. endef
  145. ifeq ($(V),0)
  146. define dep_autopatch_verbose
  147. @echo " PATCH " $(1);
  148. endef
  149. endif
  150. define dep_fetch
  151. if [ "$$$$VS" = "git" ]; then \
  152. git clone -n -- $$$$REPO $(DEPS_DIR)/$(1); \
  153. cd $(DEPS_DIR)/$(1) && git checkout -q $$$$COMMIT; \
  154. elif [ "$$$$VS" = "hg" ]; then \
  155. hg clone -U $$$$REPO $(DEPS_DIR)/$(1); \
  156. cd $(DEPS_DIR)/$(1) && hg update -q $$$$COMMIT; \
  157. elif [ "$$$$VS" = "svn" ]; then \
  158. svn checkout $$$$REPO $(DEPS_DIR)/$(1); \
  159. else \
  160. echo "Unknown or invalid dependency: $(1). Please consult the erlang.mk README for instructions." >&2; \
  161. exit 78; \
  162. fi
  163. endef
  164. define dep_target
  165. $(DEPS_DIR)/$(1):
  166. @mkdir -p $(DEPS_DIR)
  167. ifeq (,$(dep_$(1)))
  168. @if [ ! -f $(PKG_FILE2) ]; then $(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL)); fi
  169. @DEPPKG=$$$$(awk 'BEGIN { FS = "\t" }; $$$$1 == "$(1)" { print $$$$2 " " $$$$3 " " $$$$4 }' $(PKG_FILE2);); \
  170. VS=$$$$(echo $$$$DEPPKG | cut -d " " -f1); \
  171. REPO=$$$$(echo $$$$DEPPKG | cut -d " " -f2); \
  172. COMMIT=$$$$(echo $$$$DEPPKG | cut -d " " -f3); \
  173. $(call dep_fetch,$(1))
  174. else
  175. @VS=$(word 1,$(dep_$(1))); \
  176. REPO=$(word 2,$(dep_$(1))); \
  177. COMMIT=$(word 3,$(dep_$(1))); \
  178. $(call dep_fetch,$(1))
  179. endif
  180. ifneq ($(filter $(1),$(AUTOPATCH)),)
  181. $(call dep_autopatch_verbose,$(1)) if [ -f $(DEPS_DIR)/$(1)/rebar.config ]; then \
  182. $(call dep_autopatch,$(1)); \
  183. cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk; \
  184. elif [ ! -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  185. echo "ERLC_OPTS = +debug_info\ninclude erlang.mk" > $(DEPS_DIR)/$(1)/Makefile; \
  186. cd $(DEPS_DIR)/$(1)/ && ln -s ../../erlang.mk; \
  187. fi
  188. endif
  189. endef
  190. $(foreach dep,$(DEPS),$(eval $(call dep_target,$(dep))))
  191. distclean-deps:
  192. $(gen_verbose) rm -rf $(DEPS_DIR)
  193. # Packages related targets.
  194. $(PKG_FILE2):
  195. @$(call core_http_get,$(PKG_FILE2),$(PKG_FILE_URL))
  196. pkg-list: $(PKG_FILE2)
  197. @cat $(PKG_FILE2) | awk 'BEGIN { FS = "\t" }; { print \
  198. "Name:\t\t" $$1 "\n" \
  199. "Repository:\t" $$3 "\n" \
  200. "Website:\t" $$5 "\n" \
  201. "Description:\t" $$6 "\n" }'
  202. ifdef q
  203. pkg-search: $(PKG_FILE2)
  204. @cat $(PKG_FILE2) | grep -i ${q} | awk 'BEGIN { FS = "\t" }; { print \
  205. "Name:\t\t" $$1 "\n" \
  206. "Repository:\t" $$3 "\n" \
  207. "Website:\t" $$5 "\n" \
  208. "Description:\t" $$6 "\n" }'
  209. else
  210. pkg-search:
  211. $(error Usage: make pkg-search q=STRING)
  212. endif
  213. ifeq ($(PKG_FILE2),$(CURDIR)/.erlang.mk.packages.v2)
  214. distclean-pkg:
  215. $(gen_verbose) rm -f $(PKG_FILE2)
  216. endif
  217. help::
  218. @printf "%s\n" "" \
  219. "Package-related targets:" \
  220. " pkg-list List all known packages" \
  221. " pkg-search q=STRING Search for STRING in the package index"
  222. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  223. # This file is part of erlang.mk and subject to the terms of the ISC License.
  224. .PHONY: clean-app
  225. # Configuration.
  226. ERLC_OPTS ?= -Werror +debug_info +warn_export_vars +warn_shadow_vars \
  227. +warn_obsolete_guard # +bin_opt_info +warn_export_all +warn_missing_spec
  228. COMPILE_FIRST ?=
  229. COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
  230. ERLC_EXCLUDE ?=
  231. ERLC_EXCLUDE_PATHS = $(addprefix src/,$(addsuffix .erl,$(ERLC_EXCLUDE)))
  232. ERLC_MIB_OPTS ?=
  233. COMPILE_MIB_FIRST ?=
  234. COMPILE_MIB_FIRST_PATHS = $(addprefix mibs/,$(addsuffix .mib,$(COMPILE_MIB_FIRST)))
  235. # Verbosity.
  236. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  237. appsrc_verbose = $(appsrc_verbose_$(V))
  238. erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
  239. $(filter %.erl %.core,$(?F)));
  240. erlc_verbose = $(erlc_verbose_$(V))
  241. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  242. xyrl_verbose = $(xyrl_verbose_$(V))
  243. mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
  244. mib_verbose = $(mib_verbose_$(V))
  245. # Targets.
  246. ifeq ($(wildcard ebin/test),)
  247. app:: app-build
  248. else
  249. app:: clean app-build
  250. endif
  251. app-build: erlc-include ebin/$(PROJECT).app
  252. $(eval MODULES := $(shell find ebin -type f -name \*.beam \
  253. | sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
  254. @if [ -z "$$(grep -E '^[^%]*{modules,' src/$(PROJECT).app.src)" ]; then \
  255. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  256. exit 1; \
  257. fi
  258. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  259. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  260. | sed "s/{modules,[[:space:]]*\[\]}/{modules, \[$(MODULES)\]}/" \
  261. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(GITDESCRIBE)\"}/" \
  262. > ebin/$(PROJECT).app
  263. erlc-include:
  264. -@if [ -d ebin/ ]; then \
  265. find include/ src/ -type f -name \*.hrl -newer ebin -exec touch $(shell find src/ -type f -name "*.erl") \; 2>/dev/null || printf ''; \
  266. fi
  267. define compile_erl
  268. $(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
  269. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),\
  270. $(COMPILE_FIRST_PATHS) $(1))
  271. endef
  272. define compile_xyrl
  273. $(xyrl_verbose) erlc -v -o ebin/ $(1)
  274. $(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
  275. @rm ebin/*.erl
  276. endef
  277. define compile_mib
  278. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ \
  279. -I priv/mibs/ $(COMPILE_MIB_FIRST_PATHS) $(1)
  280. $(mib_verbose) erlc -o include/ -- priv/mibs/*.bin
  281. endef
  282. ifneq ($(wildcard src/),)
  283. ebin/$(PROJECT).app::
  284. @mkdir -p ebin/
  285. ifneq ($(wildcard mibs/),)
  286. ebin/$(PROJECT).app:: $(shell find mibs -type f -name \*.mib)
  287. @mkdir -p priv/mibs/ include
  288. $(if $(strip $?),$(call compile_mib,$?))
  289. endif
  290. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.erl) \
  291. $(shell find src -type f -name \*.core)
  292. $(if $(strip $?),$(call compile_erl,$?))
  293. ebin/$(PROJECT).app:: $(shell find src -type f -name \*.xrl) \
  294. $(shell find src -type f -name \*.yrl)
  295. $(if $(strip $?),$(call compile_xyrl,$?))
  296. endif
  297. clean:: clean-app
  298. clean-app:
  299. $(gen_verbose) rm -rf ebin/ priv/mibs/ \
  300. $(addprefix include/,$(addsuffix .hrl,$(notdir $(basename $(wildcard mibs/*.mib)))))
  301. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  302. # This file is part of erlang.mk and subject to the terms of the ISC License.
  303. .PHONY: test-deps test-dir test-build clean-test-dir
  304. # Configuration.
  305. TEST_DIR ?= test
  306. ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
  307. TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
  308. TEST_ERLC_OPTS += -DTEST=1
  309. # Targets.
  310. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  311. test-deps: $(ALL_TEST_DEPS_DIRS)
  312. @for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  313. ifneq ($(strip $(TEST_DIR)),)
  314. test-dir:
  315. $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -I include/ -o $(TEST_DIR) \
  316. $(wildcard $(TEST_DIR)/*.erl $(TEST_DIR)/*/*.erl) -pa ebin/
  317. endif
  318. ifeq ($(wildcard ebin/test),)
  319. test-build: ERLC_OPTS=$(TEST_ERLC_OPTS)
  320. test-build: clean deps test-deps
  321. @$(MAKE) --no-print-directory app-build test-dir ERLC_OPTS="$(TEST_ERLC_OPTS)"
  322. $(gen_verbose) touch ebin/test
  323. else
  324. test-build: ERLC_OPTS=$(TEST_ERLC_OPTS)
  325. test-build: deps test-deps
  326. @$(MAKE) --no-print-directory app-build test-dir ERLC_OPTS="$(TEST_ERLC_OPTS)"
  327. endif
  328. clean:: clean-test-dir
  329. clean-test-dir:
  330. ifneq ($(wildcard $(TEST_DIR)/*.beam),)
  331. $(gen_verbose) rm -f $(TEST_DIR)/*.beam
  332. endif
  333. # Copyright (c) 2014-2015, Loïc Hoguin <essen@ninenines.eu>
  334. # This file is part of erlang.mk and subject to the terms of the ISC License.
  335. .PHONY: bootstrap bootstrap-lib bootstrap-rel new list-templates
  336. # Core targets.
  337. help::
  338. @printf "%s\n" "" \
  339. "Bootstrap targets:" \
  340. " bootstrap Generate a skeleton of an OTP application" \
  341. " bootstrap-lib Generate a skeleton of an OTP library" \
  342. " bootstrap-rel Generate the files needed to build a release" \
  343. " new t=TPL n=NAME Generate a module NAME based on the template TPL" \
  344. " list-templates List available templates"
  345. # Bootstrap templates.
  346. bs_appsrc = "{application, $(PROJECT), [" \
  347. " {description, \"\"}," \
  348. " {vsn, \"0.1.0\"}," \
  349. " {id, \"git\"}," \
  350. " {modules, []}," \
  351. " {registered, []}," \
  352. " {applications, [" \
  353. " kernel," \
  354. " stdlib" \
  355. " ]}," \
  356. " {mod, {$(PROJECT)_app, []}}," \
  357. " {env, []}" \
  358. "]}."
  359. bs_appsrc_lib = "{application, $(PROJECT), [" \
  360. " {description, \"\"}," \
  361. " {vsn, \"0.1.0\"}," \
  362. " {id, \"git\"}," \
  363. " {modules, []}," \
  364. " {registered, []}," \
  365. " {applications, [" \
  366. " kernel," \
  367. " stdlib" \
  368. " ]}" \
  369. "]}."
  370. bs_Makefile = "PROJECT = $(PROJECT)" \
  371. "include erlang.mk"
  372. bs_app = "-module($(PROJECT)_app)." \
  373. "-behaviour(application)." \
  374. "" \
  375. "-export([start/2])." \
  376. "-export([stop/1])." \
  377. "" \
  378. "start(_Type, _Args) ->" \
  379. " $(PROJECT)_sup:start_link()." \
  380. "" \
  381. "stop(_State) ->" \
  382. " ok."
  383. bs_relx_config = "{release, {$(PROJECT)_release, \"1\"}, [$(PROJECT)]}." \
  384. "{extended_start_script, true}." \
  385. "{sys_config, \"rel/sys.config\"}." \
  386. "{vm_args, \"rel/vm.args\"}."
  387. bs_sys_config = "[" \
  388. "]."
  389. bs_vm_args = "-name $(PROJECT)@127.0.0.1" \
  390. "-setcookie $(PROJECT)" \
  391. "-heart"
  392. # Normal templates.
  393. tpl_supervisor = "-module($(n))." \
  394. "-behaviour(supervisor)." \
  395. "" \
  396. "-export([start_link/0])." \
  397. "-export([init/1])." \
  398. "" \
  399. "start_link() ->" \
  400. " supervisor:start_link({local, ?MODULE}, ?MODULE, [])." \
  401. "" \
  402. "init([]) ->" \
  403. " Procs = []," \
  404. " {ok, {{one_for_one, 1, 5}, Procs}}."
  405. tpl_gen_server = "-module($(n))." \
  406. "-behaviour(gen_server)." \
  407. "" \
  408. "%% API." \
  409. "-export([start_link/0])." \
  410. "" \
  411. "%% gen_server." \
  412. "-export([init/1])." \
  413. "-export([handle_call/3])." \
  414. "-export([handle_cast/2])." \
  415. "-export([handle_info/2])." \
  416. "-export([terminate/2])." \
  417. "-export([code_change/3])." \
  418. "" \
  419. "-record(state, {" \
  420. "})." \
  421. "" \
  422. "%% API." \
  423. "" \
  424. "-spec start_link() -> {ok, pid()}." \
  425. "start_link() ->" \
  426. " gen_server:start_link(?MODULE, [], [])." \
  427. "" \
  428. "%% gen_server." \
  429. "" \
  430. "init([]) ->" \
  431. " {ok, \#state{}}." \
  432. "" \
  433. "handle_call(_Request, _From, State) ->" \
  434. " {reply, ignored, State}." \
  435. "" \
  436. "handle_cast(_Msg, State) ->" \
  437. " {noreply, State}." \
  438. "" \
  439. "handle_info(_Info, State) ->" \
  440. " {noreply, State}." \
  441. "" \
  442. "terminate(_Reason, _State) ->" \
  443. " ok." \
  444. "" \
  445. "code_change(_OldVsn, State, _Extra) ->" \
  446. " {ok, State}."
  447. tpl_gen_fsm = "-module($(n))." \
  448. "-behaviour(gen_fsm)." \
  449. "" \
  450. "%% API." \
  451. "-export([start_link/0])." \
  452. "" \
  453. "%% gen_fsm." \
  454. "-export([init/1])." \
  455. "-export([state_name/2])." \
  456. "-export([handle_event/3])." \
  457. "-export([state_name/3])." \
  458. "-export([handle_sync_event/4])." \
  459. "-export([handle_info/3])." \
  460. "-export([terminate/3])." \
  461. "-export([code_change/4])." \
  462. "" \
  463. "-record(state, {" \
  464. "})." \
  465. "" \
  466. "%% API." \
  467. "" \
  468. "-spec start_link() -> {ok, pid()}." \
  469. "start_link() ->" \
  470. " gen_fsm:start_link(?MODULE, [], [])." \
  471. "" \
  472. "%% gen_fsm." \
  473. "" \
  474. "init([]) ->" \
  475. " {ok, state_name, \#state{}}." \
  476. "" \
  477. "state_name(_Event, StateData) ->" \
  478. " {next_state, state_name, StateData}." \
  479. "" \
  480. "handle_event(_Event, StateName, StateData) ->" \
  481. " {next_state, StateName, StateData}." \
  482. "" \
  483. "state_name(_Event, _From, StateData) ->" \
  484. " {reply, ignored, state_name, StateData}." \
  485. "" \
  486. "handle_sync_event(_Event, _From, StateName, StateData) ->" \
  487. " {reply, ignored, StateName, StateData}." \
  488. "" \
  489. "handle_info(_Info, StateName, StateData) ->" \
  490. " {next_state, StateName, StateData}." \
  491. "" \
  492. "terminate(_Reason, _StateName, _StateData) ->" \
  493. " ok." \
  494. "" \
  495. "code_change(_OldVsn, StateName, StateData, _Extra) ->" \
  496. " {ok, StateName, StateData}."
  497. tpl_cowboy_http = "-module($(n))." \
  498. "-behaviour(cowboy_http_handler)." \
  499. "" \
  500. "-export([init/3])." \
  501. "-export([handle/2])." \
  502. "-export([terminate/3])." \
  503. "" \
  504. "-record(state, {" \
  505. "})." \
  506. "" \
  507. "init(_, Req, _Opts) ->" \
  508. " {ok, Req, \#state{}}." \
  509. "" \
  510. "handle(Req, State=\#state{}) ->" \
  511. " {ok, Req2} = cowboy_req:reply(200, Req)," \
  512. " {ok, Req2, State}." \
  513. "" \
  514. "terminate(_Reason, _Req, _State) ->" \
  515. " ok."
  516. tpl_cowboy_loop = "-module($(n))." \
  517. "-behaviour(cowboy_loop_handler)." \
  518. "" \
  519. "-export([init/3])." \
  520. "-export([info/3])." \
  521. "-export([terminate/3])." \
  522. "" \
  523. "-record(state, {" \
  524. "})." \
  525. "" \
  526. "init(_, Req, _Opts) ->" \
  527. " {loop, Req, \#state{}, 5000, hibernate}." \
  528. "" \
  529. "info(_Info, Req, State) ->" \
  530. " {loop, Req, State, hibernate}." \
  531. "" \
  532. "terminate(_Reason, _Req, _State) ->" \
  533. " ok."
  534. tpl_cowboy_rest = "-module($(n))." \
  535. "" \
  536. "-export([init/3])." \
  537. "-export([content_types_provided/2])." \
  538. "-export([get_html/2])." \
  539. "" \
  540. "init(_, _Req, _Opts) ->" \
  541. " {upgrade, protocol, cowboy_rest}." \
  542. "" \
  543. "content_types_provided(Req, State) ->" \
  544. " {[{{<<\"text\">>, <<\"html\">>, '*'}, get_html}], Req, State}." \
  545. "" \
  546. "get_html(Req, State) ->" \
  547. " {<<\"<html><body>This is REST!</body></html>\">>, Req, State}."
  548. tpl_cowboy_ws = "-module($(n))." \
  549. "-behaviour(cowboy_websocket_handler)." \
  550. "" \
  551. "-export([init/3])." \
  552. "-export([websocket_init/3])." \
  553. "-export([websocket_handle/3])." \
  554. "-export([websocket_info/3])." \
  555. "-export([websocket_terminate/3])." \
  556. "" \
  557. "-record(state, {" \
  558. "})." \
  559. "" \
  560. "init(_, _, _) ->" \
  561. " {upgrade, protocol, cowboy_websocket}." \
  562. "" \
  563. "websocket_init(_, Req, _Opts) ->" \
  564. " Req2 = cowboy_req:compact(Req)," \
  565. " {ok, Req2, \#state{}}." \
  566. "" \
  567. "websocket_handle({text, Data}, Req, State) ->" \
  568. " {reply, {text, Data}, Req, State};" \
  569. "websocket_handle({binary, Data}, Req, State) ->" \
  570. " {reply, {binary, Data}, Req, State};" \
  571. "websocket_handle(_Frame, Req, State) ->" \
  572. " {ok, Req, State}." \
  573. "" \
  574. "websocket_info(_Info, Req, State) ->" \
  575. " {ok, Req, State}." \
  576. "" \
  577. "websocket_terminate(_Reason, _Req, _State) ->" \
  578. " ok."
  579. tpl_ranch_protocol = "-module($(n))." \
  580. "-behaviour(ranch_protocol)." \
  581. "" \
  582. "-export([start_link/4])." \
  583. "-export([init/4])." \
  584. "" \
  585. "-type opts() :: []." \
  586. "-export_type([opts/0])." \
  587. "" \
  588. "-record(state, {" \
  589. " socket :: inet:socket()," \
  590. " transport :: module()" \
  591. "})." \
  592. "" \
  593. "start_link(Ref, Socket, Transport, Opts) ->" \
  594. " Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts])," \
  595. " {ok, Pid}." \
  596. "" \
  597. "-spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok." \
  598. "init(Ref, Socket, Transport, _Opts) ->" \
  599. " ok = ranch:accept_ack(Ref)," \
  600. " loop(\#state{socket=Socket, transport=Transport})." \
  601. "" \
  602. "loop(State) ->" \
  603. " loop(State)."
  604. # Plugin-specific targets.
  605. bootstrap:
  606. ifneq ($(wildcard src/),)
  607. $(error Error: src/ directory already exists)
  608. endif
  609. @printf "%s\n" $(bs_Makefile) > Makefile
  610. @mkdir src/
  611. @printf "%s\n" $(bs_appsrc) > src/$(PROJECT).app.src
  612. @printf "%s\n" $(bs_app) > src/$(PROJECT)_app.erl
  613. $(eval n := $(PROJECT)_sup)
  614. @printf "%s\n" $(tpl_supervisor) > src/$(PROJECT)_sup.erl
  615. bootstrap-lib:
  616. ifneq ($(wildcard src/),)
  617. $(error Error: src/ directory already exists)
  618. endif
  619. @printf "%s\n" $(bs_Makefile) > Makefile
  620. @mkdir src/
  621. @printf "%s\n" $(bs_appsrc_lib) > src/$(PROJECT).app.src
  622. bootstrap-rel:
  623. ifneq ($(wildcard relx.config),)
  624. $(error Error: relx.config already exists)
  625. endif
  626. ifneq ($(wildcard rel/),)
  627. $(error Error: rel/ directory already exists)
  628. endif
  629. @printf "%s\n" $(bs_relx_config) > relx.config
  630. @mkdir rel/
  631. @printf "%s\n" $(bs_sys_config) > rel/sys.config
  632. @printf "%s\n" $(bs_vm_args) > rel/vm.args
  633. new:
  634. ifeq ($(wildcard src/),)
  635. $(error Error: src/ directory does not exist)
  636. endif
  637. ifndef t
  638. $(error Usage: make new t=TEMPLATE n=NAME)
  639. endif
  640. ifndef tpl_$(t)
  641. $(error Unknown template)
  642. endif
  643. ifndef n
  644. $(error Usage: make new t=TEMPLATE n=NAME)
  645. endif
  646. @printf "%s\n" $(tpl_$(t)) > src/$(n).erl
  647. list-templates:
  648. @echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))
  649. # Copyright (c) 2014-2015, Loïc Hoguin <essen@ninenines.eu>
  650. # This file is part of erlang.mk and subject to the terms of the ISC License.
  651. .PHONY: clean-c_src distclean-c_src-env
  652. # todo
  653. # Configuration.
  654. C_SRC_DIR = $(CURDIR)/c_src
  655. C_SRC_ENV ?= $(C_SRC_DIR)/env.mk
  656. C_SRC_OUTPUT ?= $(CURDIR)/priv/$(PROJECT).so
  657. # System type and C compiler/flags.
  658. UNAME_SYS := $(shell uname -s)
  659. ifeq ($(UNAME_SYS), Darwin)
  660. CC ?= cc
  661. CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes
  662. CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
  663. LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
  664. else ifeq ($(UNAME_SYS), FreeBSD)
  665. CC ?= cc
  666. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  667. CXXFLAGS ?= -O3 -finline-functions -Wall
  668. else ifeq ($(UNAME_SYS), Linux)
  669. CC ?= gcc
  670. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  671. CXXFLAGS ?= -O3 -finline-functions -Wall
  672. endif
  673. CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
  674. CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
  675. LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
  676. LDFLAGS += -shared
  677. # Verbosity.
  678. c_verbose_0 = @echo " C " $(?F);
  679. c_verbose = $(c_verbose_$(V))
  680. cpp_verbose_0 = @echo " CPP " $(?F);
  681. cpp_verbose = $(cpp_verbose_$(V))
  682. link_verbose_0 = @echo " LD " $(@F);
  683. link_verbose = $(link_verbose_$(V))
  684. # Targets.
  685. ifeq ($(wildcard $(C_SRC_DIR)),)
  686. else ifneq ($(wildcard $(C_SRC_DIR)/Makefile),)
  687. app::
  688. $(MAKE) -C $(C_SRC_DIR)
  689. clean::
  690. $(MAKE) -C $(C_SRC_DIR) clean
  691. else
  692. SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
  693. OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
  694. COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
  695. COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
  696. app:: $(C_SRC_ENV) $(C_SRC_OUTPUT)
  697. $(C_SRC_OUTPUT): $(OBJECTS)
  698. @mkdir -p priv/
  699. $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT)
  700. %.o: %.c
  701. $(COMPILE_C) $(OUTPUT_OPTION) $<
  702. %.o: %.cc
  703. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  704. %.o: %.C
  705. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  706. %.o: %.cpp
  707. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  708. $(C_SRC_ENV):
  709. @$(ERL) -eval "file:write_file(\"$(C_SRC_ENV)\", \
  710. io_lib:format( \
  711. \"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \
  712. \"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \
  713. \"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \
  714. [code:root_dir(), erlang:system_info(version), \
  715. code:lib_dir(erl_interface, include), \
  716. code:lib_dir(erl_interface, lib)])), \
  717. halt()."
  718. clean:: clean-c_src
  719. clean-c_src:
  720. $(gen_verbose) rm -f $(C_SRC_OUTPUT) $(OBJECTS)
  721. distclean:: distclean-c_src-env
  722. distclean-c_src-env:
  723. $(gen_verbose) rm -f $(C_SRC_ENV)
  724. -include $(C_SRC_ENV)
  725. endif
  726. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  727. # This file is part of erlang.mk and subject to the terms of the ISC License.
  728. .PHONY: ct distclean-ct
  729. # Configuration.
  730. CT_OPTS ?=
  731. ifneq ($(wildcard $(TEST_DIR)),)
  732. CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(shell find $(TEST_DIR) -type f -name \*_SUITE.erl -exec basename {} \;)))
  733. else
  734. CT_SUITES ?=
  735. endif
  736. # Core targets.
  737. tests:: ct
  738. distclean:: distclean-ct
  739. help::
  740. @printf "%s\n" "" \
  741. "Common_test targets:" \
  742. " ct Run all the common_test suites for this project" \
  743. "" \
  744. "All your common_test suites have their associated targets." \
  745. "A suite named http_SUITE can be ran using the ct-http target."
  746. # Plugin-specific targets.
  747. CT_RUN = ct_run \
  748. -no_auto_compile \
  749. -noinput \
  750. -pa ebin $(DEPS_DIR)/*/ebin \
  751. -dir $(TEST_DIR) \
  752. -logdir logs
  753. ifeq ($(CT_SUITES),)
  754. ct:
  755. else
  756. ct: test-build
  757. @mkdir -p logs/
  758. $(gen_verbose) $(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) $(CT_OPTS)
  759. endif
  760. define ct_suite_target
  761. ct-$(1): test-build
  762. @mkdir -p logs/
  763. $(gen_verbose) $(CT_RUN) -suite $(addsuffix _SUITE,$(1)) $(CT_OPTS)
  764. endef
  765. $(foreach test,$(CT_SUITES),$(eval $(call ct_suite_target,$(test))))
  766. distclean-ct:
  767. $(gen_verbose) rm -rf logs/
  768. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  769. # This file is part of erlang.mk and subject to the terms of the ISC License.
  770. .PHONY: plt distclean-plt dialyze
  771. # Configuration.
  772. DIALYZER_PLT ?= $(CURDIR)/.$(PROJECT).plt
  773. export DIALYZER_PLT
  774. PLT_APPS ?=
  775. DIALYZER_DIRS ?= --src -r src
  776. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
  777. -Wunmatched_returns # -Wunderspecs
  778. # Core targets.
  779. distclean:: distclean-plt
  780. help::
  781. @printf "%s\n" "" \
  782. "Dialyzer targets:" \
  783. " plt Build a PLT file for this project" \
  784. " dialyze Analyze the project using Dialyzer"
  785. # Plugin-specific targets.
  786. $(DIALYZER_PLT): deps app
  787. @dialyzer --build_plt --apps erts kernel stdlib $(PLT_APPS) $(ALL_DEPS_DIRS)
  788. plt: $(DIALYZER_PLT)
  789. distclean-plt:
  790. $(gen_verbose) rm -f $(DIALYZER_PLT)
  791. ifneq ($(wildcard $(DIALYZER_PLT)),)
  792. dialyze:
  793. else
  794. dialyze: $(DIALYZER_PLT)
  795. endif
  796. @dialyzer --no_native $(DIALYZER_DIRS) $(DIALYZER_OPTS)
  797. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  798. # Copyright (c) 2015, Viktor Söderqvist <viktor@zuiderkwast.se>
  799. # This file is part of erlang.mk and subject to the terms of the ISC License.
  800. .PHONY: distclean-edoc build-doc-deps
  801. # Configuration.
  802. EDOC_OPTS ?=
  803. # Core targets.
  804. docs:: distclean-edoc build-doc-deps
  805. $(gen_verbose) $(ERL) -eval 'edoc:application($(PROJECT), ".", [$(EDOC_OPTS)]), halt().'
  806. distclean:: distclean-edoc
  807. # Plugin-specific targets.
  808. DOC_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DOC_DEPS))
  809. $(foreach dep,$(DOC_DEPS),$(eval $(call dep_target,$(dep))))
  810. build-doc-deps: $(DOC_DEPS_DIRS)
  811. @for dep in $(DOC_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  812. distclean-edoc:
  813. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  814. # Copyright (c) 2014, Juan Facorro <juan@inaka.net>
  815. # This file is part of erlang.mk and subject to the terms of the ISC License.
  816. .PHONY: elvis distclean-elvis
  817. # Configuration.
  818. ELVIS_CONFIG ?= $(CURDIR)/elvis.config
  819. ELVIS ?= $(CURDIR)/elvis
  820. export ELVIS
  821. ELVIS_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis
  822. ELVIS_CONFIG_URL ?= https://github.com/inaka/elvis/releases/download/0.2.3/elvis.config
  823. ELVIS_OPTS ?=
  824. # Core targets.
  825. help::
  826. @printf "%s\n" "" \
  827. "Elvis targets:" \
  828. " elvis Run Elvis using the local elvis.config or download the default otherwise"
  829. distclean:: distclean-elvis
  830. # Plugin-specific targets.
  831. $(ELVIS):
  832. @$(call core_http_get,$(ELVIS),$(ELVIS_URL))
  833. @chmod +x $(ELVIS)
  834. $(ELVIS_CONFIG):
  835. @$(call core_http_get,$(ELVIS_CONFIG),$(ELVIS_CONFIG_URL))
  836. elvis: $(ELVIS) $(ELVIS_CONFIG)
  837. @$(ELVIS) rock -c $(ELVIS_CONFIG) $(ELVIS_OPTS)
  838. distclean-elvis:
  839. $(gen_verbose) rm -rf $(ELVIS)
  840. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  841. # This file is part of erlang.mk and subject to the terms of the ISC License.
  842. # Configuration.
  843. DTL_FULL_PATH ?= 0
  844. # Verbosity.
  845. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  846. dtl_verbose = $(dtl_verbose_$(V))
  847. # Core targets.
  848. define compile_erlydtl
  849. $(dtl_verbose) $(ERL) -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
  850. Compile = fun(F) -> \
  851. S = fun (1) -> re:replace(filename:rootname(string:sub_string(F, 11), ".dtl"), "/", "_", [{return, list}, global]); \
  852. (0) -> filename:basename(F, ".dtl") \
  853. end, \
  854. Module = list_to_atom(string:to_lower(S($(DTL_FULL_PATH))) ++ "_dtl"), \
  855. {ok, _} = erlydtl:compile(F, Module, [{out_dir, "ebin/"}, return_errors, {doc_root, "templates"}]) \
  856. end, \
  857. _ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
  858. halt().'
  859. endef
  860. ifneq ($(wildcard src/),)
  861. ebin/$(PROJECT).app:: $(shell find templates -type f -name \*.dtl 2>/dev/null)
  862. $(if $(strip $?),$(call compile_erlydtl,$?))
  863. endif
  864. # Copyright (c) 2014 Dave Cottlehuber <dch@skunkwerks.at>
  865. # This file is part of erlang.mk and subject to the terms of the ISC License.
  866. .PHONY: distclean-escript escript
  867. # Configuration.
  868. ESCRIPT_NAME ?= $(PROJECT)
  869. ESCRIPT_COMMENT ?= This is an -*- erlang -*- file
  870. ESCRIPT_BEAMS ?= "ebin/*", "deps/*/ebin/*"
  871. ESCRIPT_SYS_CONFIG ?= "rel/sys.config"
  872. ESCRIPT_EMU_ARGS ?= -pa . \
  873. -sasl errlog_type error \
  874. -escript main $(ESCRIPT_NAME)
  875. ESCRIPT_SHEBANG ?= /usr/bin/env escript
  876. ESCRIPT_STATIC ?= "deps/*/priv/**", "priv/**"
  877. # Core targets.
  878. distclean:: distclean-escript
  879. help::
  880. @printf "%s\n" "" \
  881. "Escript targets:" \
  882. " escript Build an executable escript archive" \
  883. # Plugin-specific targets.
  884. # Based on https://github.com/synrc/mad/blob/master/src/mad_bundle.erl
  885. # Copyright (c) 2013 Maxim Sokhatsky, Synrc Research Center
  886. # Modified MIT License, https://github.com/synrc/mad/blob/master/LICENSE :
  887. # Software may only be used for the great good and the true happiness of all
  888. # sentient beings.
  889. define ESCRIPT_RAW
  890. 'Read = fun(F) -> {ok, B} = file:read_file(filename:absname(F)), B end,'\
  891. 'Files = fun(L) -> A = lists:concat([filelib:wildcard(X)||X<- L ]),'\
  892. ' [F || F <- A, not filelib:is_dir(F) ] end,'\
  893. 'Squash = fun(L) -> [{filename:basename(F), Read(F) } || F <- L ] end,'\
  894. 'Zip = fun(A, L) -> {ok,{_,Z}} = zip:create(A, L, [{compress,all},memory]), Z end,'\
  895. 'Ez = fun(Escript) ->'\
  896. ' Static = Files([$(ESCRIPT_STATIC)]),'\
  897. ' Beams = Squash(Files([$(ESCRIPT_BEAMS), $(ESCRIPT_SYS_CONFIG)])),'\
  898. ' Archive = Beams ++ [{ "static.gz", Zip("static.gz", Static)}],'\
  899. ' escript:create(Escript, [ $(ESCRIPT_OPTIONS)'\
  900. ' {archive, Archive, [memory]},'\
  901. ' {shebang, "$(ESCRIPT_SHEBANG)"},'\
  902. ' {comment, "$(ESCRIPT_COMMENT)"},'\
  903. ' {emu_args, " $(ESCRIPT_EMU_ARGS)"}'\
  904. ' ]),'\
  905. ' file:change_mode(Escript, 8#755)'\
  906. 'end,'\
  907. 'Ez("$(ESCRIPT_NAME)"),'\
  908. 'halt().'
  909. endef
  910. ESCRIPT_COMMAND = $(subst ' ',,$(ESCRIPT_RAW))
  911. escript:: distclean-escript deps app
  912. $(gen_verbose) $(ERL) -eval $(ESCRIPT_COMMAND)
  913. distclean-escript:
  914. $(gen_verbose) rm -f $(ESCRIPT_NAME)
  915. # Copyright (c) 2014, Enrique Fernandez <enrique.fernandez@erlang-solutions.com>
  916. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  917. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  918. .PHONY: eunit
  919. # Configuration
  920. ifeq ($(strip $(TEST_DIR)),)
  921. TAGGED_EUNIT_TESTS = {dir,"ebin"}
  922. else
  923. ifeq ($(wildcard $(TEST_DIR)),)
  924. TAGGED_EUNIT_TESTS = {dir,"ebin"}
  925. else
  926. # All modules in TEST_DIR
  927. TEST_DIR_MODS = $(notdir $(basename $(shell find $(TEST_DIR) -type f -name *.beam)))
  928. # All modules in 'ebin'
  929. EUNIT_EBIN_MODS = $(notdir $(basename $(shell find ebin -type f -name *.beam)))
  930. # Only those modules in TEST_DIR with no matching module in 'ebin'.
  931. # This is done to avoid some tests being executed twice.
  932. EUNIT_MODS = $(filter-out $(patsubst %,%_tests,$(EUNIT_EBIN_MODS)),$(TEST_DIR_MODS))
  933. TAGGED_EUNIT_TESTS = {dir,"ebin"} $(foreach mod,$(EUNIT_MODS),$(shell echo $(mod) | sed -e 's/\(.*\)/{module,\1}/g'))
  934. endif
  935. endif
  936. EUNIT_OPTS ?=
  937. # Utility functions
  938. define str-join
  939. $(shell echo '$(strip $(1))' | sed -e "s/ /,/g")
  940. endef
  941. # Core targets.
  942. tests:: eunit
  943. help::
  944. @printf "%s\n" "" \
  945. "EUnit targets:" \
  946. " eunit Run all the EUnit tests for this project"
  947. # Plugin-specific targets.
  948. EUNIT_RUN = $(ERL) \
  949. -pa $(TEST_DIR) $(DEPS_DIR)/*/ebin \
  950. -pz ebin \
  951. -eval 'case eunit:test([$(call str-join,$(TAGGED_EUNIT_TESTS))], [$(EUNIT_OPTS)]) of ok -> halt(0); error -> halt(1) end.'
  952. eunit: test-build
  953. $(gen_verbose) $(EUNIT_RUN)
  954. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  955. # This file is part of erlang.mk and subject to the terms of the ISC License.
  956. .PHONY: relx-rel distclean-relx-rel distclean-relx
  957. # Configuration.
  958. RELX_CONFIG ?= $(CURDIR)/relx.config
  959. RELX ?= $(CURDIR)/relx
  960. export RELX
  961. RELX_URL ?= https://github.com/erlware/relx/releases/download/v1.2.0/relx
  962. RELX_OPTS ?=
  963. RELX_OUTPUT_DIR ?= _rel
  964. ifeq ($(firstword $(RELX_OPTS)),-o)
  965. RELX_OUTPUT_DIR = $(word 2,$(RELX_OPTS))
  966. else
  967. RELX_OPTS += -o $(RELX_OUTPUT_DIR)
  968. endif
  969. # Core targets.
  970. ifneq ($(wildcard $(RELX_CONFIG)),)
  971. rel:: distclean-relx-rel relx-rel
  972. endif
  973. distclean:: distclean-relx-rel distclean-relx
  974. # Plugin-specific targets.
  975. define relx_fetch
  976. $(call core_http_get,$(RELX),$(RELX_URL))
  977. chmod +x $(RELX)
  978. endef
  979. $(RELX):
  980. @$(call relx_fetch)
  981. relx-rel: $(RELX)
  982. @$(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
  983. distclean-relx-rel:
  984. $(gen_verbose) rm -rf $(RELX_OUTPUT_DIR)
  985. distclean-relx:
  986. $(gen_verbose) rm -rf $(RELX)
  987. # Copyright (c) 2014, M Robert Martin <rob@version2beta.com>
  988. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  989. .PHONY: shell
  990. # Configuration.
  991. SHELL_PATH ?= -pa $(CURDIR)/ebin $(DEPS_DIR)/*/ebin
  992. SHELL_OPTS ?=
  993. ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS))
  994. # Core targets
  995. help::
  996. @printf "%s\n" "" \
  997. "Shell targets:" \
  998. " shell Run an erlang shell with SHELL_OPTS or reasonable default"
  999. # Plugin-specific targets.
  1000. $(foreach dep,$(SHELL_DEPS),$(eval $(call dep_target,$(dep))))
  1001. build-shell-deps: $(ALL_SHELL_DEPS_DIRS)
  1002. @for dep in $(ALL_SHELL_DEPS_DIRS) ; do $(MAKE) -C $$dep ; done
  1003. shell: build-shell-deps
  1004. $(gen_verbose) erl $(SHELL_PATH) $(SHELL_OPTS)
  1005. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  1006. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1007. ifneq ($(wildcard $(DEPS_DIR)/triq),)
  1008. .PHONY: triq
  1009. # Targets.
  1010. tests:: triq
  1011. define triq_run
  1012. $(ERL) -pa $(CURDIR)/ebin $(DEPS_DIR)/*/ebin \
  1013. -eval "try $(1) of true -> halt(0); _ -> halt(1) catch error:undef -> io:format(\"Undefined property or module~n\"), halt() end."
  1014. endef
  1015. ifdef t
  1016. ifeq (,$(findstring :,$(t)))
  1017. triq: test-build
  1018. @$(call triq_run,triq:check($(t)))
  1019. else
  1020. triq: test-build
  1021. @echo Testing $(t)/0
  1022. @$(call triq_run,triq:check($(t)()))
  1023. endif
  1024. else
  1025. triq: test-build
  1026. $(eval MODULES := $(shell find ebin -type f -name \*.beam \
  1027. | sed "s/ebin\//'/;s/\.beam/',/" | sed '$$s/.$$//'))
  1028. $(gen_verbose) $(call triq_run,[true] =:= lists:usort([triq:check(M) || M <- [$(MODULES)]]))
  1029. endif
  1030. endif