Makefile 12 KB

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