Makefile 13 KB

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