erlang.mk 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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. # All modules in TEST_DIR
  920. ifeq ($(strip $(TEST_DIR)),)
  921. TEST_DIR_MODS =
  922. else
  923. TEST_DIR_MODS = $(notdir $(basename $(shell find $(TEST_DIR) -type f -name *.beam)))
  924. endif
  925. # All modules in 'ebin'
  926. EUNIT_EBIN_MODS = $(notdir $(basename $(shell find ebin -type f -name *.beam)))
  927. # Only those modules in TEST_DIR with no matching module in 'ebin'.
  928. # This is done to avoid some tests being executed twice.
  929. EUNIT_MODS = $(filter-out $(patsubst %,%_tests,$(EUNIT_EBIN_MODS)),$(TEST_DIR_MODS))
  930. TAGGED_EUNIT_TESTS = $(foreach mod,$(EUNIT_EBIN_MODS) $(EUNIT_MODS),{module,$(mod)})
  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_BEFORE ?=
  944. EUNIT_RUN_AFTER ?=
  945. EUNIT_RUN = $(ERL) \
  946. -pa $(TEST_DIR) $(DEPS_DIR)/*/ebin \
  947. -pz ebin \
  948. $(EUNIT_RUN_BEFORE) \
  949. -eval 'case eunit:test([$(call str-join,$(TAGGED_EUNIT_TESTS))],\
  950. [$(EUNIT_OPTS)]) of ok -> ok; error -> halt(1) end.' \
  951. $(EUNIT_RUN_AFTER) \
  952. -eval 'halt(0).'
  953. eunit: test-build
  954. $(gen_verbose) $(EUNIT_RUN)
  955. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  956. # This file is part of erlang.mk and subject to the terms of the ISC License.
  957. .PHONY: relx-rel distclean-relx-rel distclean-relx
  958. # Configuration.
  959. RELX_CONFIG ?= $(CURDIR)/relx.config
  960. RELX ?= $(CURDIR)/relx
  961. export RELX
  962. RELX_URL ?= https://github.com/erlware/relx/releases/download/v1.1.0/relx
  963. RELX_OPTS ?=
  964. RELX_OUTPUT_DIR ?= _rel
  965. ifeq ($(firstword $(RELX_OPTS)),-o)
  966. RELX_OUTPUT_DIR = $(word 2,$(RELX_OPTS))
  967. else
  968. RELX_OPTS += -o $(RELX_OUTPUT_DIR)
  969. endif
  970. # Core targets.
  971. ifneq ($(wildcard $(RELX_CONFIG)),)
  972. rel:: distclean-relx-rel relx-rel
  973. endif
  974. distclean:: distclean-relx-rel distclean-relx
  975. # Plugin-specific targets.
  976. define relx_fetch
  977. $(call core_http_get,$(RELX),$(RELX_URL))
  978. chmod +x $(RELX)
  979. endef
  980. $(RELX):
  981. @$(call relx_fetch)
  982. relx-rel: $(RELX)
  983. @$(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
  984. distclean-relx-rel:
  985. $(gen_verbose) rm -rf $(RELX_OUTPUT_DIR)
  986. distclean-relx:
  987. $(gen_verbose) rm -rf $(RELX)
  988. # Copyright (c) 2014, M Robert Martin <rob@version2beta.com>
  989. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  990. .PHONY: shell
  991. # Configuration.
  992. SHELL_PATH ?= -pa $(CURDIR)/ebin $(DEPS_DIR)/*/ebin
  993. SHELL_OPTS ?=
  994. ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS))
  995. # Core targets
  996. help::
  997. @printf "%s\n" "" \
  998. "Shell targets:" \
  999. " shell Run an erlang shell with SHELL_OPTS or reasonable default"
  1000. # Plugin-specific targets.
  1001. $(foreach dep,$(SHELL_DEPS),$(eval $(call dep_target,$(dep))))
  1002. build-shell-deps: $(ALL_SHELL_DEPS_DIRS)
  1003. @for dep in $(ALL_SHELL_DEPS_DIRS) ; do $(MAKE) -C $$dep ; done
  1004. shell: build-shell-deps
  1005. $(gen_verbose) erl $(SHELL_PATH) $(SHELL_OPTS)
  1006. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  1007. # This file is part of erlang.mk and subject to the terms of the ISC License.
  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. # Copyright 2015, Viktor Söderqvist <viktor@zuiderkwast.se>
  1031. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1032. COVER_REPORT_DIR = cover
  1033. # utility variables for representing special symbols
  1034. empty :=
  1035. space := $(empty) $(empty)
  1036. comma := ,
  1037. # Hook in coverage to eunit
  1038. ifdef COVER
  1039. ifdef EUNIT_RUN
  1040. EUNIT_RUN_BEFORE += -eval \
  1041. 'case cover:compile_beam_directory("ebin") of \
  1042. {error, _} -> halt(1); \
  1043. _ -> ok \
  1044. end.'
  1045. EUNIT_RUN_AFTER += -eval 'cover:export("eunit.coverdata").'
  1046. endif
  1047. endif
  1048. # Hook in coverage to ct
  1049. ifdef COVER
  1050. ifdef CT_RUN
  1051. # All modules in 'ebin'
  1052. COVER_MODS = $(notdir $(basename $(shell echo ebin/*.beam)))
  1053. test-build:: ct.cover.spec
  1054. ct.cover.spec:
  1055. @echo Cover mods: $(COVER_MODS)
  1056. $(gen_verbose) printf "%s\n" \
  1057. '{incl_mods,[$(subst $(space),$(comma),$(COVER_MODS))]}.' \
  1058. '{export,"$(CURDIR)/ct.coverdata"}.' > $@
  1059. CT_RUN += -cover ct.cover.spec
  1060. endif
  1061. endif
  1062. # Core targets
  1063. ifdef COVER
  1064. ifneq ($(COVER_REPORT_DIR),)
  1065. tests::
  1066. @$(MAKE) make --no-print-directory cover-report
  1067. endif
  1068. endif
  1069. clean:: coverdata-clean
  1070. ifneq ($(COVER_REPORT_DIR),)
  1071. distclean:: cover-report-clean
  1072. endif
  1073. help::
  1074. @printf "%s\n" "" \
  1075. "Cover targets:" \
  1076. " cover-report Generate a HTML coverage report from previously collected" \
  1077. " cover data." \
  1078. " all.coverdata Merge {eunit,ct}.coverdata into one coverdata file." \
  1079. "" \
  1080. "If COVER=1 is set, coverage data is generated by the targets eunit and ct. The" \
  1081. "target tests additionally generates a HTML coverage report from the combined" \
  1082. "coverdata files from each of these testing tools. HTML reports can be disabled" \
  1083. "by setting COVER_REPORT_DIR to empty."
  1084. # Plugin specific targets
  1085. COVERDATA = $(filter-out all.coverdata,$(wildcard *.coverdata))
  1086. .PHONY: coverdata-clean
  1087. coverdata-clean:
  1088. $(gen_verbose) rm -f *.coverdata ct.cover.spec
  1089. # Merge all coverdata files into one.
  1090. all.coverdata: $(COVERDATA)
  1091. $(gen_verbose) $(ERL) -eval ' \
  1092. $(foreach f,$(COVERDATA),cover:import("$(f)") == ok orelse halt(1),) \
  1093. cover:export("$@"), halt(0).'
  1094. # These are only defined if COVER_REPORT_DIR is non-empty. Set COVER_REPORT_DIR to
  1095. # empty if you want the coverdata files but not the HTML report.
  1096. ifneq ($(COVER_REPORT_DIR),)
  1097. .PHONY: cover-report-clean cover-report
  1098. cover-report-clean:
  1099. $(gen_verbose) rm -rf $(COVER_REPORT_DIR)
  1100. ifeq ($(COVERDATA),)
  1101. cover-report:
  1102. else
  1103. # Modules which include eunit.hrl always contain one line without coverage
  1104. # because eunit defines test/0 which is never called. We compensate for this.
  1105. EUNIT_HRL_MODS = $(subst $(space),$(comma),$(shell \
  1106. grep -e '^\s*-include.*include/eunit\.hrl"' src/*.erl \
  1107. | sed "s/^src\/\(.*\)\.erl:.*/'\1'/" | uniq))
  1108. cover-report:
  1109. $(gen_verbose) mkdir -p $(COVER_REPORT_DIR)
  1110. $(gen_verbose) $(ERL) -eval ' \
  1111. $(foreach f,$(COVERDATA),cover:import("$(f)") == ok orelse halt(1),) \
  1112. Ms = cover:imported_modules(), \
  1113. [cover:analyse_to_file(M, "$(COVER_REPORT_DIR)/" ++ atom_to_list(M) \
  1114. ++ ".COVER.html", [html]) || M <- Ms], \
  1115. Report = [begin {ok, R} = cover:analyse(M, module), R end || M <- Ms], \
  1116. EunitHrlMods = [$(EUNIT_HRL_MODS)], \
  1117. Report1 = [{M, {Y, case lists:member(M, EunitHrlMods) of \
  1118. true -> N - 1; false -> N end}} || {M, {Y, N}} <- Report], \
  1119. TotalY = lists:sum([Y || {_, {Y, _}} <- Report1]), \
  1120. TotalN = lists:sum([N || {_, {_, N}} <- Report1]), \
  1121. TotalPerc = round(100 * TotalY / (TotalY + TotalN)), \
  1122. {ok, F} = file:open("$(COVER_REPORT_DIR)/index.html", [write]), \
  1123. io:format(F, "<!DOCTYPE html><html>~n" \
  1124. "<head><meta charset=\"UTF-8\">~n" \
  1125. "<title>Coverage report</title></head>~n" \
  1126. "<body>~n", []), \
  1127. io:format(F, "<h1>Coverage</h1>~n<p>Total: ~p%</p>~n", [TotalPerc]),\
  1128. io:format(F, "<table><tr><th>Module</th><th>Coverage</th></tr>~n", []), \
  1129. [io:format(F, "<tr><td><a href=\"~p.COVER.html\">~p</a></td>" \
  1130. "<td>~p%</td></tr>~n", \
  1131. [M, M, round(100 * Y / (Y + N))]) || {M, {Y, N}} <- Report1], \
  1132. How = "$(subst $(space),$(comma)$(space),$(basename $(COVERDATA)))", \
  1133. Date = "$(shell date -u "+%Y-%m-%dT%H:%M:%SZ")", \
  1134. io:format(F, "</table>~n" \
  1135. "<p>Generated using ~s and erlang.mk on ~s.</p>~n" \
  1136. "</body></html>", [How, Date]), \
  1137. halt().'
  1138. endif
  1139. endif # ifneq ($(COVER_REPORT_DIR),)