Makefile 13 KB

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