Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  2. # Copyright (c) 2014, Viktor Söderqvist <viktor@zuiderkwast.se>
  3. # This file is part of erlang.mk and subject to the terms of the ISC License.
  4. # Temporary application name, taken from rule name.
  5. APP = $(subst -,_,$@)
  6. APP_TO_CLEAN = $(subst -,_,$(patsubst clean-%,%,$@))
  7. # Erlang, quickly!
  8. ERL = erl +A0 -noinput -boot start_clean
  9. # Platform, condensed version.
  10. ifeq ($(shell uname -o),Msys)
  11. PLATFORM = msys2
  12. else
  13. PLATFORM = unix
  14. endif
  15. # OTP master, for downloading files for testing.
  16. OTP_MASTER = https://raw.githubusercontent.com/erlang/otp/master
  17. # Verbosity.
  18. #
  19. # V=0: Show info messages only.
  20. # V=1: Show test commands.
  21. # V=2: Also show normal Erlang.mk output.
  22. # V=3: Also show verbose Erlang.mk output.
  23. V ?= 0
  24. # t: Verbosity control for tests.
  25. # v: Verbosity control for erlang.mk.
  26. # i: Command to display (or suppress) info messages.
  27. ifeq ($V,0)
  28. t = @
  29. v = V=0 >/dev/null 2>&1
  30. i = @echo $@:
  31. else ifeq ($V,1)
  32. t =
  33. v = V=0 >/dev/null 2>&1
  34. i = @echo == $@:
  35. else ifeq ($V,2)
  36. t = @echo " TEST " $@;
  37. v = V=0
  38. i = @echo == $@:
  39. else
  40. t =
  41. v = V=1
  42. i = @echo == $@:
  43. endif
  44. # Main targets.
  45. .PHONY: all clean build
  46. all:: core
  47. clean:: clean-core
  48. $t rm -f erl_crash.dump
  49. build:
  50. $i "Generate a bleeding edge Erlang.mk"
  51. $t cd .. && $(MAKE) $v
  52. # Core.
  53. .PHONY: core clean-core
  54. define include_core
  55. core:: core-$1
  56. clean-core:: clean-core-$1
  57. include core_$1.mk
  58. endef
  59. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst core_%,%,$(wildcard core_*.mk))),$(call include_core,$t)))
  60. # Plugins.
  61. define include_plugin
  62. all:: $1
  63. clean:: clean-$1
  64. include plugin_$1.mk
  65. endef
  66. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst plugin_%,%,$(wildcard plugin_*.mk))),$(call include_plugin,$t)))
  67. # Tests that don't easily fit into other categories.
  68. core:: core-clean-crash-dump core-distclean-tmp core-help
  69. clean-core:: clean-core-clean-crash-dump clean-core-distclean-tmp clean-core-help
  70. .PHONY: core-clean-crash-dump core-distclean-tmp core-help clean-core-clean-crash-dump clean-core-distclean-tmp clean-core-help
  71. clean-core-clean-crash-dump clean-core-distclean-tmp clean-core-help:
  72. $t rm -rf $(APP_TO_CLEAN)/
  73. core-clean-crash-dump: build clean-core-clean-crash-dump
  74. $i "Bootstrap a new OTP library named $(APP)"
  75. $t mkdir $(APP)/
  76. $t cp ../erlang.mk $(APP)/
  77. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  78. $i "Create a fake erl_crash.dump file"
  79. $t touch $(APP)/erl_crash.dump
  80. $i "Clean the application"
  81. $t $(MAKE) -C $(APP) clean $v
  82. $i "Check that the crash dump is removed"
  83. $t test ! -e $(APP)/erl_crash.dump
  84. core-distclean-tmp: build clean-core-distclean-tmp
  85. $i "Bootstrap a new OTP application named $(APP)"
  86. $t mkdir $(APP)/
  87. $t cp ../erlang.mk $(APP)/
  88. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap all $v
  89. $i "Check that a .erlang.mk directory exists"
  90. $t test -d $(APP)/.erlang.mk
  91. $i "Distclean the application"
  92. $t $(MAKE) -C $(APP) distclean $v
  93. $i "Check if .erlang.mk directory got removed"
  94. $t test ! -e $(APP)/.erlang.mk
  95. core-help: build clean-core-help
  96. $i "Bootstrap a new OTP library named $(APP)"
  97. $t mkdir $(APP)/
  98. $t cp ../erlang.mk $(APP)/
  99. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  100. $i "Run 'make help' and check that it prints help"
  101. $t test -n "`$(MAKE) -C $(APP) help` | grep Usage"
  102. # Legacy tests.
  103. #
  104. # The following tests are slowly being converted.
  105. # Do NOT use -j with legacy tests.
  106. .PHONY: legacy clean-legacy ct eunit tests-cover docs
  107. legacy: clean-legacy ct eunit tests-cover docs pkgs
  108. clean-legacy:
  109. $t rm -rf app1 pkgs.log
  110. ct: app1
  111. $i "ct: Testing ct and related targets."
  112. $i "Setting up test suite."
  113. $t mkdir -p app1/test
  114. $t printf "%s\n" \
  115. "-module(m_SUITE)." \
  116. "-export([all/0, testcase1/1])." \
  117. "all() -> [testcase1]." \
  118. "testcase1(_) -> 2 = m:succ(1)." \
  119. > app1/test/m_SUITE.erl
  120. $t $(MAKE) -C app1 ct $v
  121. $i "Checking files created by '$(MAKE) ct'."
  122. $t [ -e app1/test/m_SUITE.beam ]
  123. $t [ -e app1/ebin/m.beam ]
  124. $t [ -e app1/logs ]
  125. $i "Checking that '$(MAKE) clean' does not delete logs."
  126. $t $(MAKE) -C app1 clean $v
  127. $t [ -e app1/logs ]
  128. $i "Testing target 'ct-mysuite' where mysuite_SUITE is a test suite."
  129. $t $(MAKE) -C app1 ct-m $v
  130. $i "Checking that '$(MAKE) ct' returns non-zero for a failing suite."
  131. $t printf "%s\n" \
  132. "-module(failing_SUITE)." \
  133. "-export([all/0, testcase1/1])." \
  134. "all() -> [testcase1]." \
  135. "testcase1(_) -> 42 = m:succ(1)." \
  136. > app1/test/failing_SUITE.erl
  137. $t if $(MAKE) -C app1 ct-failing $v ; then false ; fi
  138. $i "Checking that '$(MAKE) distclean-ct' deletes logs."
  139. $t $(MAKE) -C app1 distclean-ct $v
  140. $t [ ! -e app1/logs ]
  141. $t [ -e app1/ebin/m.beam ]
  142. $i "Cleaning up test data."
  143. $t rm -rf app1/test
  144. $i "Test 'ct' passed."
  145. eunit: app1
  146. $i "eunit: Testing the 'eunit' target."
  147. $i "Running eunit test case inside module src/t.erl"
  148. $t $(call create-module-t)
  149. $t $(MAKE) -C app1 distclean $v
  150. $t $(MAKE) -C app1 eunit $v
  151. $i "Checking that the eunit test in module t."
  152. $t echo t | cmp app1/test-eunit.log -
  153. $t rm app1/test-eunit.log
  154. $i "Running eunit tests in a separate directory."
  155. $t mkdir -p app1/eunit
  156. $t printf '%s\n' \
  157. '-module(t_tests).' \
  158. '-include_lib("eunit/include/eunit.hrl").' \
  159. 'succ_test() ->' \
  160. ' ?assertEqual(2, t:succ(1)),' \
  161. ' os:cmd("echo t_tests >> test-eunit.log").' \
  162. > app1/eunit/t_tests.erl
  163. $t printf '%s\n' \
  164. '-module(x_tests).' \
  165. '-include_lib("eunit/include/eunit.hrl").' \
  166. 'succ_test() ->' \
  167. ' ?assertEqual(2, t:succ(1)),' \
  168. ' os:cmd("echo x_tests >> test-eunit.log").' \
  169. > app1/eunit/x_tests.erl
  170. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  171. $t $(MAKE) -C app1 eunit TEST_DIR=eunit $v
  172. $i "Checking that '$(MAKE) eunit' didn't run the tests in t_tests twice, etc."
  173. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  174. $t rm app1/test-eunit.log
  175. $i "Checking that '$(MAKE) eunit' returns non-zero for a failing test."
  176. $t rm -f app1/eunit/*
  177. $t printf "%s\n" \
  178. "-module(t_tests)." \
  179. '-include_lib("eunit/include/eunit.hrl").' \
  180. "succ_test() ->" \
  181. " ?assertEqual(42, t:succ(1))." \
  182. > app1/eunit/t_tests.erl
  183. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  184. $t if $(MAKE) -C app1 eunit TEST_DIR=eunit $v ; then false ; fi
  185. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  186. $i "Test 'eunit' passed."
  187. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  188. tests-cover: app1
  189. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  190. $i "Setting up eunit and ct suites."
  191. $t $(call create-module-t)
  192. $t mkdir -p app1/test
  193. $t printf "%s\n" \
  194. "-module(m_SUITE)." \
  195. "-export([all/0, testcase1/1])." \
  196. "all() -> [testcase1]." \
  197. "testcase1(_) -> 2 = m:succ(1)." \
  198. > app1/test/m_SUITE.erl
  199. $i "Running tests with coverage analysis."
  200. $t $(MAKE) -C app1 eunit ct COVER=1 $v
  201. $t [ -e app1/test-eunit.log ]
  202. $t [ -e app1/eunit.coverdata ]
  203. $t [ -e app1/ct.coverdata ]
  204. $i "Generating coverage report."
  205. $t $(MAKE) -C app1 cover-report COVER=1 $v
  206. $t [ -e app1/cover/m.COVER.html ]
  207. $t [ -e app1/cover/t.COVER.html ]
  208. $t [ -e app1/cover/index.html ]
  209. $i "Checking combined coverage from eunit and ct."
  210. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  211. $i "Checking that cover-report-clean removes cover report."
  212. $t $(MAKE) -C app1 cover-report-clean $v
  213. $t [ ! -e app1/cover ]
  214. $i "Checking that coverdata-clean removes cover data."
  215. $t $(MAKE) -C app1 coverdata-clean $v
  216. $t [ ! -e app1/eunit.coverdata ]
  217. @# clean up
  218. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  219. $t $(MAKE) -C app1 clean $v
  220. $i "Test 'tests-cover' passed."
  221. docs: app1
  222. $i "docs: Testing EDoc including DOC_DEPS."
  223. $t printf "%s\n" \
  224. "PROJECT = app1" \
  225. "DOC_DEPS = edown" \
  226. "dep_edown = git https://github.com/uwiger/edown.git 0.7" \
  227. "EDOC_OPTS = {doclet, edown_doclet}" \
  228. "include erlang.mk" \
  229. "distclean:: distclean-doc-md" \
  230. "distclean-doc-md:" \
  231. " rm -rf doc/*.md" \
  232. > app1/Makefile-doc
  233. $i "Downloading doc deps (edown) and building docs."
  234. $t $(MAKE) -C app1 -f Makefile-doc docs $v
  235. $i "Checking that '$(MAKE) docs' using edown generated a markdown file."
  236. $t [ -e app1/doc/m.md ]
  237. $i "Checking that '$(MAKE) distclean' deletes all generated doc files."
  238. $t $(MAKE) -C app1 -f Makefile-doc distclean $v
  239. $t [ "`ls app1/doc/`" = "" ]
  240. $i "Cleaning up test data."
  241. $t rm app1/Makefile-doc
  242. $i "Test 'docs' passed."
  243. define app1_setup
  244. $i "Setting up app."
  245. $t mkdir -p app1
  246. $t cd .. && $(MAKE)
  247. $t cp ../erlang.mk app1/
  248. $t $(MAKE) -C app1 -f erlang.mk bootstrap-lib
  249. $t printf "%s\n" \
  250. "-module(m)." \
  251. "-export([succ/1])." \
  252. "succ(N) -> N + 1." \
  253. > app1/src/m.erl
  254. endef
  255. define pkg_test_target
  256. pkg-$(1)-clean:
  257. $t rm -rf app1 erl_crash.dump
  258. pkg-$(1)-app1:
  259. $(call app1_setup)
  260. # Running 'make' twice to make sure it recompiles fine.
  261. pkg-$(1): pkg-$(1)-clean pkg-$(1)-app1
  262. $i
  263. $i " pkgs: Checking that '$(1)' builds correctly"
  264. $i
  265. $t printf "%s\n" \
  266. "PROJECT = app1" \
  267. "DEPS = $(1)" \
  268. "include erlang.mk" \
  269. > app1/Makefile
  270. $t \
  271. if [ "$(1)" = "amqp_client" ]; then \
  272. $(MAKE) -C app1 RABBITMQ_CLIENT_PATCH=1; \
  273. elif [ "$(1)" = "rabbit" ]; then \
  274. $(MAKE) -C app1 RABBITMQ_SERVER_PATCH=1; \
  275. else \
  276. $(MAKE) -C app1; \
  277. fi; \
  278. if [ $$$$? -ne 0 ]; then \
  279. echo "$(1): make error" >> pkgs.log; \
  280. else \
  281. $(MAKE) -C app1; if [ $$$$? -ne 0 ]; then \
  282. echo "$(1): re-make error" >> pkgs.log; \
  283. else \
  284. find . -type f -name erl_crash.dump; if [ $$$$? -ne 0 ]; then \
  285. echo "$(1): erl_crash.dump found" >> pkgs.log; \
  286. else \
  287. erl +A0 -noinput -boot start_clean -pa app1/deps/*/ebin -eval " \
  288. Apps = [list_to_atom(App) || \"app1/deps/\" ++ App \
  289. <- filelib:wildcard(\"app1/deps/*\")], \
  290. [begin \
  291. io:format(\"Loading application ~p~n\", [App]), \
  292. case application:load(App) of \
  293. {error, _} -> ok; \
  294. ok -> \
  295. {ok, Mods} = application:get_key(App, modules), \
  296. [try io:format(\" Loading module ~p~n\", [Mod]), \
  297. {module, Mod} = code:load_file(Mod) \
  298. catch C:R -> timer:sleep(500), erlang:C(R) \
  299. end || Mod <- Mods] \
  300. end \
  301. end || App <- Apps], \
  302. halt()."; if [ $$$$? -ne 0 ]; then \
  303. echo "$(1): load error" >> pkgs.log; \
  304. fi \
  305. fi \
  306. fi \
  307. fi
  308. endef
  309. PACKAGES = $(foreach pkg,$(sort $(wildcard ../index/*.mk)),$(notdir $(basename $(pkg))))
  310. $(foreach pkg,$(PACKAGES),$(eval $(call pkg_test_target,$(pkg))))
  311. pkgs: $(addprefix pkg-,$(PACKAGES))
  312. @if [ -f pkgs.log ]; then \
  313. echo "+-------------------------------+"; \
  314. echo "| ERRORS WHILE TESTING PACKAGES |"; \
  315. echo "+-------------------------------+"; \
  316. cat pkgs.log; \
  317. exit 33; \
  318. fi
  319. # Test application used for testing.
  320. app1:
  321. $(call app1_setup)
  322. # Extra module in app1 used for testing eunit
  323. define create-module-t
  324. printf '%s\n' \
  325. '-module(t).' \
  326. '-export([succ/1]).' \
  327. 'succ(N) -> N + 1.' \
  328. '-ifdef(TEST).' \
  329. '-include_lib("eunit/include/eunit.hrl").' \
  330. 'succ_test() ->' \
  331. ' ?assertEqual(2, succ(1)),' \
  332. ' os:cmd("echo t >> test-eunit.log").' \
  333. '-endif.' \
  334. > app1/src/t.erl
  335. endef