You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1279 lines
34 KiB

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