Makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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=4: Also show a trace of each command after expansion.
  51. V ?= 0
  52. # t: Verbosity control for tests.
  53. # v: Verbosity control for erlang.mk.
  54. # i: Command to display (or suppress) info messages.
  55. ifeq ($V,0)
  56. t = @
  57. v = V=0 >/dev/null 2>&1
  58. i = @echo $@:
  59. else ifeq ($V,1)
  60. t =
  61. v = V=0 >/dev/null 2>&1
  62. i = @echo == $@:
  63. else ifeq ($V,2)
  64. t = @echo " TEST " $@;
  65. v = V=0
  66. i = @echo == $@:
  67. else
  68. t =
  69. v = V=$(shell echo $$(($(V)-2)))
  70. i = @echo == $@:
  71. endif
  72. # Main targets.
  73. .PHONY: all clean build
  74. all:: core
  75. clean:: clean-core
  76. $t rm -rf erl_crash.dump packages/
  77. build:
  78. $i "Generate a bleeding edge Erlang.mk"
  79. $t cd .. && $(MAKE) $v
  80. # Core.
  81. .PHONY: core clean-core
  82. define include_core
  83. core:: core-$1
  84. clean-core:: clean-core-$1
  85. include core_$1.mk
  86. endef
  87. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst core_%,%,$(wildcard core_*.mk))),$(call include_core,$t)))
  88. # Plugins.
  89. define include_plugin
  90. all:: $1
  91. clean:: clean-$1
  92. include plugin_$1.mk
  93. endef
  94. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst plugin_%,%,$(wildcard plugin_*.mk))),$(call include_plugin,$t)))
  95. # Tests that don't easily fit into other categories.
  96. core:: core-clean-crash-dump core-distclean-tmp core-help
  97. clean-core:: clean-core-clean-crash-dump clean-core-distclean-tmp clean-core-help
  98. .PHONY: core-clean-crash-dump core-distclean-tmp core-help clean-core-clean-crash-dump clean-core-distclean-tmp clean-core-help
  99. clean-core-clean-crash-dump clean-core-distclean-tmp clean-core-help:
  100. $t rm -rf $(APP_TO_CLEAN)
  101. core-clean-crash-dump: build clean-core-clean-crash-dump
  102. $i "Bootstrap a new OTP library named $(APP)"
  103. $t mkdir $(APP)/
  104. $t cp ../erlang.mk $(APP)/
  105. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  106. $i "Create a fake erl_crash.dump file"
  107. $t touch $(APP)/erl_crash.dump
  108. $i "Clean the application"
  109. $t $(MAKE) -C $(APP) clean $v
  110. $i "Check that the crash dump is removed"
  111. $t test ! -e $(APP)/erl_crash.dump
  112. core-distclean-tmp: build clean-core-distclean-tmp
  113. $i "Bootstrap a new OTP application named $(APP)"
  114. $t mkdir $(APP)/
  115. $t cp ../erlang.mk $(APP)/
  116. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap all $v
  117. $i "Check that a .erlang.mk directory exists"
  118. $t test -d $(APP)/.erlang.mk
  119. $i "Distclean the application"
  120. $t $(MAKE) -C $(APP) distclean $v
  121. $i "Check that .erlang.mk directory got removed"
  122. $t test ! -e $(APP)/.erlang.mk
  123. core-help: build clean-core-help
  124. $i "Bootstrap a new OTP library named $(APP)"
  125. $t mkdir $(APP)/
  126. $t cp ../erlang.mk $(APP)/
  127. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  128. $i "Run 'make help' and check that it prints help"
  129. $t test -n "`$(MAKE) -C $(APP) help` | grep Usage"
  130. # Packages.
  131. PACKAGES = $(foreach pkg,$(sort $(wildcard ../index/*.mk)),$(notdir $(basename $(pkg))))
  132. EXCLUDE_FROM_CHECK = [rabbitmq_codegen]
  133. packages: $(addprefix pkg-,$(PACKAGES))
  134. define pkg_target
  135. .PHONY: pkg-$1
  136. pkg-$1: clean build
  137. # Make sure $@ is defined inside the define.
  138. $(eval @ = pkg-$1)
  139. # Get the real application's name.
  140. $(eval APP_NAME := $(shell sed '2!d;s/pkg_$1_name = //' ../index/$1.mk))
  141. $i "Bootstrap a new OTP library in packages/$1_pkg"
  142. $t mkdir -p packages/$1_pkg/
  143. $t cp ../erlang.mk packages/$1_pkg/
  144. $t cd packages/$1_pkg/ && $(MAKE) -f erlang.mk bootstrap-lib $v
  145. $i "Add package $1 to the Makefile"
  146. $t perl -ni.bak -e 'print;if ($$$$.==1) {print "DEPS = $1\n"}' packages/$1_pkg/Makefile
  147. $i "Compile package $1"
  148. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) $v ); then \
  149. echo "$1: compile error" >> packages/errors.log; \
  150. false; \
  151. fi
  152. $i "Check that $1 has a .app file"
  153. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  154. echo "$1: no .app file" >> packages/errors.log; \
  155. false; \
  156. fi
  157. $i "Check that all applications and their modules can be loaded"
  158. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  159. Apps0 = [list_to_atom(App) || \"deps/\" ++ App \
  160. <- filelib:wildcard(\"deps/*\")], \
  161. Apps = [App || App <- Apps0, not lists:member(App, $(EXCLUDE_FROM_CHECK))], \
  162. [begin \
  163. io:format(\"Loading application ~p~n\", [App]), \
  164. case application:load(App) of \
  165. ok -> ok; \
  166. {error, {already_loaded, App}} -> ok \
  167. end, \
  168. {ok, Mods} = application:get_key(App, modules), \
  169. [try io:format(\" Loading module ~p~n\", [Mod]), \
  170. {module, Mod} = code:load_file(Mod) \
  171. catch C:R -> timer:sleep(500), erlang:C(R) \
  172. end || Mod <- Mods] \
  173. end || App <- Apps], \
  174. halt()." ); then \
  175. echo "$1: load error" >> packages/errors.log; \
  176. false; \
  177. fi
  178. $i "Recompile package $1"
  179. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) $v ); then \
  180. echo "$(1): recompile error" >> packages/errors.log; \
  181. false; \
  182. fi
  183. $i "Check that $1 has a .app file"
  184. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  185. echo "$1: no .app file" >> packages/errors.log; \
  186. false; \
  187. fi
  188. $i "Check that all applications and their modules can still be loaded"
  189. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  190. Apps0 = [list_to_atom(App) || \"deps/\" ++ App \
  191. <- filelib:wildcard(\"deps/*\")], \
  192. Apps = [App || App <- Apps0, not lists:member(App, $(EXCLUDE_FROM_CHECK))], \
  193. [begin \
  194. io:format(\"Loading application ~p~n\", [App]), \
  195. case application:load(App) of \
  196. ok -> ok; \
  197. {error, {already_loaded, App}} -> ok \
  198. end, \
  199. {ok, Mods} = application:get_key(App, modules), \
  200. [try io:format(\" Loading module ~p~n\", [Mod]), \
  201. {module, Mod} = code:load_file(Mod) \
  202. catch C:R -> timer:sleep(500), erlang:C(R) \
  203. end || Mod <- Mods] \
  204. end || App <- Apps], \
  205. halt()." ); then \
  206. echo "$1: recompile+load error" >> packages/errors.log; \
  207. false; \
  208. fi
  209. $i "Check that no erl_crash.dump file exists"
  210. $t if ( ! find packages/$1_pkg/ -type f -name erl_crash.dump ); then \
  211. echo "$(1): erl_crash.dump found" >> packages/errors.log; \
  212. fi
  213. $(if $(KEEP_BUILDS),,
  214. $i "OK; delete the build directory"
  215. $t rm -rf packages/$1_pkg/)
  216. endef
  217. $(foreach pkg,$(PACKAGES),$(eval $(call pkg_target,$(pkg))))
  218. ##################
  219. # Test application used for testing.
  220. app1:
  221. $(call app1_setup)
  222. # Extra module in app1 used for testing eunit
  223. define create-module-t
  224. printf '%s\n' \
  225. '-module(t).' \
  226. '-export([succ/1]).' \
  227. 'succ(N) -> N + 1.' \
  228. '-ifdef(TEST).' \
  229. '-include_lib("eunit/include/eunit.hrl").' \
  230. 'succ_test() ->' \
  231. ' ?assertEqual(2, succ(1)),' \
  232. ' os:cmd("echo t >> test-eunit.log").' \
  233. '-endif.' \
  234. > app1/src/t.erl
  235. endef
  236. # Legacy tests.
  237. #
  238. # The following tests are slowly being converted.
  239. # Do NOT use -j with legacy tests.
  240. .PHONY: legacy clean-legacy ct eunit tests-cover docs
  241. legacy: clean-legacy ct eunit tests-cover docs pkgs
  242. clean-legacy:
  243. $t rm -rf app1
  244. ct: app1
  245. $i "ct: Testing ct and related targets."
  246. $i "Setting up test suite."
  247. $t mkdir -p app1/test
  248. $t printf "%s\n" \
  249. "-module(m_SUITE)." \
  250. "-export([all/0, testcase1/1])." \
  251. "all() -> [testcase1]." \
  252. "testcase1(_) -> 2 = m:succ(1)." \
  253. > app1/test/m_SUITE.erl
  254. $t $(MAKE) -C app1 ct $v
  255. $i "Checking files created by '$(MAKE) ct'."
  256. $t [ -e app1/test/m_SUITE.beam ]
  257. $t [ -e app1/ebin/m.beam ]
  258. $t [ -e app1/logs ]
  259. $i "Checking that '$(MAKE) clean' does not delete logs."
  260. $t $(MAKE) -C app1 clean $v
  261. $t [ -e app1/logs ]
  262. $i "Testing target 'ct-mysuite' where mysuite_SUITE is a test suite."
  263. $t $(MAKE) -C app1 ct-m $v
  264. $i "Checking that '$(MAKE) ct' returns non-zero for a failing suite."
  265. $t printf "%s\n" \
  266. "-module(failing_SUITE)." \
  267. "-export([all/0, testcase1/1])." \
  268. "all() -> [testcase1]." \
  269. "testcase1(_) -> 42 = m:succ(1)." \
  270. > app1/test/failing_SUITE.erl
  271. $t ! $(MAKE) -C app1 ct-failing $v
  272. $i "Checking that '$(MAKE) distclean-ct' deletes logs."
  273. $t $(MAKE) -C app1 distclean-ct $v
  274. $t [ ! -e app1/logs ]
  275. $t [ -e app1/ebin/m.beam ]
  276. $i "Cleaning up test data."
  277. $t rm -rf app1/test
  278. $i "Test 'ct' passed."
  279. eunit: app1
  280. $i "eunit: Testing the 'eunit' target."
  281. $i "Running eunit test case inside module src/t.erl"
  282. $t $(call create-module-t)
  283. $t $(MAKE) -C app1 distclean $v
  284. $t $(MAKE) -C app1 eunit $v
  285. $i "Checking that the eunit test in module t."
  286. $t echo t | cmp app1/test-eunit.log -
  287. $t rm app1/test-eunit.log
  288. $i "Running eunit tests in a separate directory."
  289. $t mkdir -p app1/eunit
  290. $t printf '%s\n' \
  291. '-module(t_tests).' \
  292. '-include_lib("eunit/include/eunit.hrl").' \
  293. 'succ_test() ->' \
  294. ' ?assertEqual(2, t:succ(1)),' \
  295. ' os:cmd("echo t_tests >> test-eunit.log").' \
  296. > app1/eunit/t_tests.erl
  297. $t printf '%s\n' \
  298. '-module(x_tests).' \
  299. '-include_lib("eunit/include/eunit.hrl").' \
  300. 'succ_test() ->' \
  301. ' ?assertEqual(2, t:succ(1)),' \
  302. ' os:cmd("echo x_tests >> test-eunit.log").' \
  303. > app1/eunit/x_tests.erl
  304. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  305. $t $(MAKE) -C app1 eunit TEST_DIR=eunit $v
  306. $i "Checking that '$(MAKE) eunit' didn't run the tests in t_tests twice, etc."
  307. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  308. $t rm app1/test-eunit.log
  309. $i "Checking that '$(MAKE) eunit' returns non-zero for a failing test."
  310. $t rm -f app1/eunit/*
  311. $t printf "%s\n" \
  312. "-module(t_tests)." \
  313. '-include_lib("eunit/include/eunit.hrl").' \
  314. "succ_test() ->" \
  315. " ?assertEqual(42, t:succ(1))." \
  316. > app1/eunit/t_tests.erl
  317. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  318. $t ! $(MAKE) -C app1 eunit TEST_DIR=eunit $v
  319. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  320. $i "Test 'eunit' passed."
  321. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  322. tests-cover: app1
  323. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  324. $i "Setting up eunit and ct suites."
  325. $t $(call create-module-t)
  326. $t mkdir -p app1/test
  327. $t printf "%s\n" \
  328. "-module(m_SUITE)." \
  329. "-export([all/0, testcase1/1])." \
  330. "all() -> [testcase1]." \
  331. "testcase1(_) -> 2 = m:succ(1)." \
  332. > app1/test/m_SUITE.erl
  333. $i "Running tests with coverage analysis."
  334. $t $(MAKE) -C app1 eunit ct COVER=1 $v
  335. $t [ -e app1/test-eunit.log ]
  336. $t [ -e app1/eunit.coverdata ]
  337. $t [ -e app1/ct.coverdata ]
  338. $i "Generating coverage report."
  339. $t $(MAKE) -C app1 cover-report COVER=1 $v
  340. $t [ -e app1/cover/m.COVER.html ]
  341. $t [ -e app1/cover/t.COVER.html ]
  342. $t [ -e app1/cover/index.html ]
  343. $i "Checking combined coverage from eunit and ct."
  344. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  345. $i "Checking that cover-report-clean removes cover report."
  346. $t $(MAKE) -C app1 cover-report-clean $v
  347. $t [ ! -e app1/cover ]
  348. $i "Checking that coverdata-clean removes cover data."
  349. $t $(MAKE) -C app1 coverdata-clean $v
  350. $t [ ! -e app1/eunit.coverdata ]
  351. @# clean up
  352. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  353. $t $(MAKE) -C app1 clean $v
  354. $i "Test 'tests-cover' passed."
  355. docs: app1
  356. $i "docs: Testing EDoc including DOC_DEPS."
  357. $t printf "%s\n" \
  358. "PROJECT = app1" \
  359. "DOC_DEPS = edown" \
  360. "dep_edown = git https://github.com/uwiger/edown.git 0.7" \
  361. "EDOC_OPTS = {doclet, edown_doclet}" \
  362. "include erlang.mk" \
  363. "distclean:: distclean-doc-md" \
  364. "distclean-doc-md:" \
  365. " rm -rf doc/*.md" \
  366. > app1/Makefile-doc
  367. $i "Downloading doc deps (edown) and building docs."
  368. $t $(MAKE) -C app1 -f Makefile-doc docs $v
  369. $i "Checking that '$(MAKE) docs' using edown generated a markdown file."
  370. $t [ -e app1/doc/m.md ]
  371. $i "Checking that '$(MAKE) distclean' deletes all generated doc files."
  372. $t $(MAKE) -C app1 -f Makefile-doc distclean $v
  373. $t [ "`ls app1/doc/`" = "" ]
  374. $i "Cleaning up test data."
  375. $t rm app1/Makefile-doc
  376. $i "Test 'docs' passed."
  377. define app1_setup
  378. $i "Setting up app."
  379. $t mkdir -p app1
  380. $t cd .. && $(MAKE)
  381. $t cp ../erlang.mk app1/
  382. $t $(MAKE) -C app1 -f erlang.mk bootstrap-lib
  383. $t printf "%s\n" \
  384. "-module(m)." \
  385. "-export([succ/1])." \
  386. "succ(N) -> N + 1." \
  387. > app1/src/m.erl
  388. endef