Makefile 12 KB

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