erlang.mk 34 KB

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