erlang.mk 35 KB

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