Makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. packages: $(addprefix pkg-,$(PACKAGES))
  133. define pkg_target
  134. .PHONY: pkg-$1
  135. pkg-$1: clean build
  136. # Make sure $@ is defined inside the define.
  137. $(eval @ = pkg-$1)
  138. # Get the real application's name.
  139. $(eval APP_NAME := $(shell sed '2!d;s/pkg_$1_name = //' ../index/$1.mk))
  140. $i "Bootstrap a new OTP library in packages/$1_pkg"
  141. $t mkdir -p packages/$1_pkg/
  142. $t cp ../erlang.mk packages/$1_pkg/
  143. $t cd packages/$1_pkg/ && $(MAKE) -f erlang.mk bootstrap-lib $v
  144. $i "Add package $1 to the Makefile"
  145. $t perl -ni.bak -e 'print;if ($$$$.==1) {print "DEPS = $1\n"}' packages/$1_pkg/Makefile
  146. $i "Compile package $1"
  147. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) $v ); then \
  148. echo "$1: compile error" >> packages/errors.log; \
  149. false; \
  150. fi
  151. $i "Check that $1 has a .app file"
  152. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  153. echo "$1: no .app file" >> packages/errors.log; \
  154. false; \
  155. fi
  156. $i "Check that all applications and their modules can be loaded"
  157. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  158. Apps = [list_to_atom(App) || \"deps/\" ++ App \
  159. <- filelib:wildcard(\"deps/*\")], \
  160. [begin \
  161. io:format(\"Loading application ~p~n\", [App]), \
  162. case application:load(App) of \
  163. ok -> ok; \
  164. {error, {already_loaded, App}} -> ok \
  165. end, \
  166. {ok, Mods} = application:get_key(App, modules), \
  167. [try io:format(\" Loading module ~p~n\", [Mod]), \
  168. {module, Mod} = code:load_file(Mod) \
  169. catch C:R -> timer:sleep(500), erlang:C(R) \
  170. end || Mod <- Mods] \
  171. end || App <- Apps], \
  172. halt()." ); then \
  173. echo "$1: load error" >> packages/errors.log; \
  174. false; \
  175. fi
  176. $i "Recompile package $1"
  177. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) $v ); then \
  178. echo "$(1): recompile error" >> packages/errors.log; \
  179. false; \
  180. fi
  181. $i "Check that $1 has a .app file"
  182. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  183. echo "$1: no .app file" >> packages/errors.log; \
  184. false; \
  185. fi
  186. $i "Check that all applications and their modules can still be loaded"
  187. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  188. Apps = [list_to_atom(App) || \"deps/\" ++ App \
  189. <- filelib:wildcard(\"deps/*\")], \
  190. [begin \
  191. io:format(\"Loading application ~p~n\", [App]), \
  192. case application:load(App) of \
  193. ok -> ok; \
  194. {error, {already_loaded, App}} -> ok \
  195. end, \
  196. {ok, Mods} = application:get_key(App, modules), \
  197. [try io:format(\" Loading module ~p~n\", [Mod]), \
  198. {module, Mod} = code:load_file(Mod) \
  199. catch C:R -> timer:sleep(500), erlang:C(R) \
  200. end || Mod <- Mods] \
  201. end || App <- Apps], \
  202. halt()." ); then \
  203. echo "$1: recompile+load error" >> packages/errors.log; \
  204. false; \
  205. fi
  206. $i "Check that no erl_crash.dump file exists"
  207. $t if ( ! find packages/$1_pkg/ -type f -name erl_crash.dump ); then \
  208. echo "$(1): erl_crash.dump found" >> packages/errors.log; \
  209. fi
  210. $(if $(KEEP_BUILDS),,
  211. $i "OK; delete the build directory"
  212. $t rm -rf packages/$1_pkg/)
  213. endef
  214. $(foreach pkg,$(PACKAGES),$(eval $(call pkg_target,$(pkg))))
  215. ##################
  216. # Test application used for testing.
  217. app1:
  218. $(call app1_setup)
  219. # Extra module in app1 used for testing eunit
  220. define create-module-t
  221. printf '%s\n' \
  222. '-module(t).' \
  223. '-export([succ/1]).' \
  224. 'succ(N) -> N + 1.' \
  225. '-ifdef(TEST).' \
  226. '-include_lib("eunit/include/eunit.hrl").' \
  227. 'succ_test() ->' \
  228. ' ?assertEqual(2, succ(1)),' \
  229. ' os:cmd("echo t >> test-eunit.log").' \
  230. '-endif.' \
  231. > app1/src/t.erl
  232. endef
  233. # Legacy tests.
  234. #
  235. # The following tests are slowly being converted.
  236. # Do NOT use -j with legacy tests.
  237. .PHONY: legacy clean-legacy ct eunit tests-cover docs
  238. legacy: clean-legacy ct eunit tests-cover docs pkgs
  239. clean-legacy:
  240. $t rm -rf app1
  241. ct: app1
  242. $i "ct: Testing ct and related targets."
  243. $i "Setting up test suite."
  244. $t mkdir -p app1/test
  245. $t printf "%s\n" \
  246. "-module(m_SUITE)." \
  247. "-export([all/0, testcase1/1])." \
  248. "all() -> [testcase1]." \
  249. "testcase1(_) -> 2 = m:succ(1)." \
  250. > app1/test/m_SUITE.erl
  251. $t $(MAKE) -C app1 ct $v
  252. $i "Checking files created by '$(MAKE) ct'."
  253. $t [ -e app1/test/m_SUITE.beam ]
  254. $t [ -e app1/ebin/m.beam ]
  255. $t [ -e app1/logs ]
  256. $i "Checking that '$(MAKE) clean' does not delete logs."
  257. $t $(MAKE) -C app1 clean $v
  258. $t [ -e app1/logs ]
  259. $i "Testing target 'ct-mysuite' where mysuite_SUITE is a test suite."
  260. $t $(MAKE) -C app1 ct-m $v
  261. $i "Checking that '$(MAKE) ct' returns non-zero for a failing suite."
  262. $t printf "%s\n" \
  263. "-module(failing_SUITE)." \
  264. "-export([all/0, testcase1/1])." \
  265. "all() -> [testcase1]." \
  266. "testcase1(_) -> 42 = m:succ(1)." \
  267. > app1/test/failing_SUITE.erl
  268. $t ! $(MAKE) -C app1 ct-failing $v
  269. $i "Checking that '$(MAKE) distclean-ct' deletes logs."
  270. $t $(MAKE) -C app1 distclean-ct $v
  271. $t [ ! -e app1/logs ]
  272. $t [ -e app1/ebin/m.beam ]
  273. $i "Cleaning up test data."
  274. $t rm -rf app1/test
  275. $i "Test 'ct' passed."
  276. eunit: app1
  277. $i "eunit: Testing the 'eunit' target."
  278. $i "Running eunit test case inside module src/t.erl"
  279. $t $(call create-module-t)
  280. $t $(MAKE) -C app1 distclean $v
  281. $t $(MAKE) -C app1 eunit $v
  282. $i "Checking that the eunit test in module t."
  283. $t echo t | cmp app1/test-eunit.log -
  284. $t rm app1/test-eunit.log
  285. $i "Running eunit tests in a separate directory."
  286. $t mkdir -p app1/eunit
  287. $t printf '%s\n' \
  288. '-module(t_tests).' \
  289. '-include_lib("eunit/include/eunit.hrl").' \
  290. 'succ_test() ->' \
  291. ' ?assertEqual(2, t:succ(1)),' \
  292. ' os:cmd("echo t_tests >> test-eunit.log").' \
  293. > app1/eunit/t_tests.erl
  294. $t printf '%s\n' \
  295. '-module(x_tests).' \
  296. '-include_lib("eunit/include/eunit.hrl").' \
  297. 'succ_test() ->' \
  298. ' ?assertEqual(2, t:succ(1)),' \
  299. ' os:cmd("echo x_tests >> test-eunit.log").' \
  300. > app1/eunit/x_tests.erl
  301. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  302. $t $(MAKE) -C app1 eunit TEST_DIR=eunit $v
  303. $i "Checking that '$(MAKE) eunit' didn't run the tests in t_tests twice, etc."
  304. $t printf "%s\n" t t_tests x_tests | cmp app1/test-eunit.log -
  305. $t rm app1/test-eunit.log
  306. $i "Checking that '$(MAKE) eunit' returns non-zero for a failing test."
  307. $t rm -f app1/eunit/*
  308. $t printf "%s\n" \
  309. "-module(t_tests)." \
  310. '-include_lib("eunit/include/eunit.hrl").' \
  311. "succ_test() ->" \
  312. " ?assertEqual(42, t:succ(1))." \
  313. > app1/eunit/t_tests.erl
  314. $t $(MAKE) -C app1 distclean TEST_DIR=eunit $v
  315. $t ! $(MAKE) -C app1 eunit TEST_DIR=eunit $v
  316. $t rm -rf app1/eunit app1/src/t.erl app1/test-eunit.log
  317. $i "Test 'eunit' passed."
  318. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  319. tests-cover: app1
  320. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  321. $i "Setting up eunit and ct suites."
  322. $t $(call create-module-t)
  323. $t mkdir -p app1/test
  324. $t printf "%s\n" \
  325. "-module(m_SUITE)." \
  326. "-export([all/0, testcase1/1])." \
  327. "all() -> [testcase1]." \
  328. "testcase1(_) -> 2 = m:succ(1)." \
  329. > app1/test/m_SUITE.erl
  330. $i "Running tests with coverage analysis."
  331. $t $(MAKE) -C app1 eunit ct COVER=1 $v
  332. $t [ -e app1/test-eunit.log ]
  333. $t [ -e app1/eunit.coverdata ]
  334. $t [ -e app1/ct.coverdata ]
  335. $i "Generating coverage report."
  336. $t $(MAKE) -C app1 cover-report COVER=1 $v
  337. $t [ -e app1/cover/m.COVER.html ]
  338. $t [ -e app1/cover/t.COVER.html ]
  339. $t [ -e app1/cover/index.html ]
  340. $i "Checking combined coverage from eunit and ct."
  341. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  342. $i "Checking that cover-report-clean removes cover report."
  343. $t $(MAKE) -C app1 cover-report-clean $v
  344. $t [ ! -e app1/cover ]
  345. $i "Checking that coverdata-clean removes cover data."
  346. $t $(MAKE) -C app1 coverdata-clean $v
  347. $t [ ! -e app1/eunit.coverdata ]
  348. @# clean up
  349. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  350. $t $(MAKE) -C app1 clean $v
  351. $i "Test 'tests-cover' passed."
  352. docs: app1
  353. $i "docs: Testing EDoc including DOC_DEPS."
  354. $t printf "%s\n" \
  355. "PROJECT = app1" \
  356. "DOC_DEPS = edown" \
  357. "dep_edown = git https://github.com/uwiger/edown.git 0.7" \
  358. "EDOC_OPTS = {doclet, edown_doclet}" \
  359. "include erlang.mk" \
  360. "distclean:: distclean-doc-md" \
  361. "distclean-doc-md:" \
  362. " rm -rf doc/*.md" \
  363. > app1/Makefile-doc
  364. $i "Downloading doc deps (edown) and building docs."
  365. $t $(MAKE) -C app1 -f Makefile-doc docs $v
  366. $i "Checking that '$(MAKE) docs' using edown generated a markdown file."
  367. $t [ -e app1/doc/m.md ]
  368. $i "Checking that '$(MAKE) distclean' deletes all generated doc files."
  369. $t $(MAKE) -C app1 -f Makefile-doc distclean $v
  370. $t [ "`ls app1/doc/`" = "" ]
  371. $i "Cleaning up test data."
  372. $t rm app1/Makefile-doc
  373. $i "Test 'docs' passed."
  374. define app1_setup
  375. $i "Setting up app."
  376. $t mkdir -p app1
  377. $t cd .. && $(MAKE)
  378. $t cp ../erlang.mk app1/
  379. $t $(MAKE) -C app1 -f erlang.mk bootstrap-lib
  380. $t printf "%s\n" \
  381. "-module(m)." \
  382. "-export([succ/1])." \
  383. "succ(N) -> N + 1." \
  384. > app1/src/m.erl
  385. endef