Makefile 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. # Copyright (c) 2015-2016, 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 = test_$(subst -,_,$@)
  12. # Erlang, quickly!
  13. ERL = erl +A0 -noinput -boot start_clean
  14. # Platform detection, condensed version.
  15. UNAME_S := $(shell uname -s)
  16. ifeq ($(UNAME_S),Darwin)
  17. PLATFORM = darwin
  18. else ifeq ($(UNAME_S),FreeBSD)
  19. PLATFORM = freebsd
  20. else ifeq ($(shell uname -o),Msys)
  21. PLATFORM = msys2
  22. else
  23. PLATFORM = unix
  24. endif
  25. # Some systems do not have sub-second file times resolution.
  26. # This is the case for older systems like OSX that uses the HFS+
  27. # file system. HFS+ has a 1 second time resolution. This is a
  28. # problem because the Erlang.mk tests rely on file modification
  29. # times to ensure files were rebuilt. To fix this issue, we
  30. # detect here whether the system supports sub-second resolution,
  31. # and maybe sleep during test execution.
  32. #
  33. # Also see:
  34. # * http://arstechnica.com/apple/2011/07/mac-os-x-10-7/12/#hfs-problems
  35. # * https://apple.stackexchange.com/questions/51650/linus-torvalds-and-the-os-x-filesystem
  36. 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)
  37. SLEEP = sleep 1
  38. else
  39. SLEEP =
  40. endif
  41. # OTP master, for downloading files for testing.
  42. OTP_MASTER = https://raw.githubusercontent.com/erlang/otp/master
  43. # Verbosity.
  44. #
  45. # V=0: Show info messages only.
  46. # V=1: Show test commands.
  47. # V=2: Also show normal Erlang.mk output.
  48. # V=3: Also show verbose Erlang.mk output.
  49. # V=4: Also show a trace of each command after expansion.
  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=$(shell echo $$(($(V)-2)))
  69. i = @echo == $@:
  70. endif
  71. # Main targets.
  72. .PHONY: all clean build
  73. all:: core
  74. clean::
  75. $t rm -rf erl_crash.dump packages/ test_*/
  76. build:
  77. $i "Generate a bleeding edge Erlang.mk"
  78. $t cd .. && $(MAKE) $v
  79. # Core.
  80. .PHONY: core
  81. define include_core
  82. core:: core-$1
  83. include core_$1.mk
  84. endef
  85. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst core_%,%,$(wildcard core_*.mk))),$(call include_core,$t)))
  86. # Plugins.
  87. define include_plugin
  88. all:: $1
  89. include plugin_$1.mk
  90. endef
  91. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst plugin_%,%,$(wildcard plugin_*.mk))),$(call include_plugin,$t)))
  92. # Tests that don't easily fit into other categories.
  93. core:: core-clean-crash-dump core-distclean-tmp core-help
  94. .PHONY: core-clean-crash-dump core-distclean-tmp core-help
  95. core-clean-crash-dump: build clean
  96. $i "Bootstrap a new OTP library named $(APP)"
  97. $t mkdir $(APP)/
  98. $t cp ../erlang.mk $(APP)/
  99. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  100. $i "Create a fake erl_crash.dump file"
  101. $t touch $(APP)/erl_crash.dump
  102. $i "Clean the application"
  103. $t $(MAKE) -C $(APP) clean $v
  104. $i "Check that the crash dump is removed"
  105. $t test ! -e $(APP)/erl_crash.dump
  106. core-distclean-tmp: build clean
  107. $i "Bootstrap a new OTP application named $(APP)"
  108. $t mkdir $(APP)/
  109. $t cp ../erlang.mk $(APP)/
  110. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap all $v
  111. $i "Check that a .erlang.mk directory exists"
  112. $t test -d $(APP)/.erlang.mk
  113. $i "Distclean the application"
  114. $t $(MAKE) -C $(APP) distclean $v
  115. $i "Check that .erlang.mk directory got removed"
  116. $t test ! -e $(APP)/.erlang.mk
  117. core-help: build clean
  118. $i "Bootstrap a new OTP library named $(APP)"
  119. $t mkdir $(APP)/
  120. $t cp ../erlang.mk $(APP)/
  121. $t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
  122. $i "Run 'make help' and check that it prints help"
  123. $t test -n "`$(MAKE) -C $(APP) help` | grep Usage"
  124. # Packages.
  125. PACKAGES = $(foreach pkg,$(sort $(wildcard ../index/*.mk)),$(notdir $(basename $(pkg))))
  126. EXCLUDE_FROM_CHECK = [hexer_mk, inaka_mk, rabbitmq_codegen]
  127. packages: $(addprefix pkg-,$(PACKAGES))
  128. define pkg_target
  129. .PHONY: pkg-$1
  130. pkg-$1: build clean
  131. # Make sure $@ is defined inside the define.
  132. $(eval @ = pkg-$1)
  133. # Get the real application's name.
  134. $(eval APP_NAME := $(shell sed '2!d;s/pkg_$1_name = //' ../index/$1.mk))
  135. $i "Bootstrap a new OTP library in packages/$1_pkg"
  136. $t mkdir -p packages/$1_pkg/
  137. $t cp ../erlang.mk packages/$1_pkg/
  138. $t cd packages/$1_pkg/ && $(MAKE) -f erlang.mk bootstrap-lib $v
  139. $i "Add package $1 to the Makefile"
  140. $t perl -ni.bak -e 'print;if ($$$$.==1) {print "DEPS = $1\n"}' packages/$1_pkg/Makefile
  141. $i "Compile package $1"
  142. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) $v ); then \
  143. echo "$1: compile error" >> packages/errors.log; \
  144. false; \
  145. fi
  146. $i "Check that $1 has a .app file"
  147. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  148. echo "$1: no .app file" >> packages/errors.log; \
  149. false; \
  150. fi
  151. $i "Check that all applications and their modules can be loaded"
  152. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  153. Apps0 = [list_to_atom(App) || \"deps/\" ++ App \
  154. <- filelib:wildcard(\"deps/*\")], \
  155. Apps = [App || App <- Apps0, not lists:member(App, $(EXCLUDE_FROM_CHECK))], \
  156. [begin \
  157. io:format(\"Loading application ~p~n\", [App]), \
  158. case application:load(App) of \
  159. ok -> ok; \
  160. {error, {already_loaded, App}} -> ok \
  161. end, \
  162. {ok, Mods} = application:get_key(App, modules), \
  163. [try io:format(\" Loading module ~p~n\", [Mod]), \
  164. {module, Mod} = code:load_file(Mod) \
  165. catch C:R -> timer:sleep(500), erlang:C(R) \
  166. end || Mod <- Mods] \
  167. end || App <- Apps], \
  168. halt()." ); then \
  169. echo "$1: load error" >> packages/errors.log; \
  170. false; \
  171. fi
  172. $i "Recompile package $1"
  173. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) $v ); then \
  174. echo "$(1): recompile error" >> packages/errors.log; \
  175. false; \
  176. fi
  177. $i "Check that $1 has a .app file"
  178. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  179. echo "$1: no .app file" >> packages/errors.log; \
  180. false; \
  181. fi
  182. $i "Check that all applications and their modules can still be loaded"
  183. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  184. Apps0 = [list_to_atom(App) || \"deps/\" ++ App \
  185. <- filelib:wildcard(\"deps/*\")], \
  186. Apps = [App || App <- Apps0, not lists:member(App, $(EXCLUDE_FROM_CHECK))], \
  187. [begin \
  188. io:format(\"Loading application ~p~n\", [App]), \
  189. case application:load(App) of \
  190. ok -> ok; \
  191. {error, {already_loaded, App}} -> ok \
  192. end, \
  193. {ok, Mods} = application:get_key(App, modules), \
  194. [try io:format(\" Loading module ~p~n\", [Mod]), \
  195. {module, Mod} = code:load_file(Mod) \
  196. catch C:R -> timer:sleep(500), erlang:C(R) \
  197. end || Mod <- Mods] \
  198. end || App <- Apps], \
  199. halt()." ); then \
  200. echo "$1: recompile+load error" >> packages/errors.log; \
  201. false; \
  202. fi
  203. $i "Check that no erl_crash.dump file exists"
  204. $t if ( ! find packages/$1_pkg/ -type f -name erl_crash.dump ); then \
  205. echo "$(1): erl_crash.dump found" >> packages/errors.log; \
  206. fi
  207. $(if $(KEEP_BUILDS),,
  208. $i "OK; delete the build directory"
  209. $t rm -rf packages/$1_pkg/)
  210. endef
  211. $(foreach pkg,$(PACKAGES),$(eval $(call pkg_target,$(pkg))))
  212. ##################
  213. # Test application used for testing.
  214. app1:
  215. $(call app1_setup)
  216. # Extra module in app1 used for testing eunit
  217. define create-module-t
  218. printf '%s\n' \
  219. '-module(t).' \
  220. '-export([succ/1]).' \
  221. 'succ(N) -> N + 1.' \
  222. '-ifdef(TEST).' \
  223. '-include_lib("eunit/include/eunit.hrl").' \
  224. 'succ_test() ->' \
  225. ' ?assertEqual(2, succ(1)),' \
  226. ' os:cmd("echo t >> test-eunit.log").' \
  227. '-endif.' \
  228. > app1/src/t.erl
  229. endef
  230. # Legacy tests.
  231. #
  232. # The following tests are slowly being converted.
  233. # Do NOT use -j with legacy tests.
  234. .PHONY: legacy clean-legacy tests-cover
  235. legacy: clean-legacy tests-cover
  236. clean-legacy:
  237. $t rm -rf app1
  238. # TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
  239. tests-cover: app1
  240. $i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
  241. $i "Setting up eunit and ct suites."
  242. $t $(call create-module-t)
  243. $t mkdir -p app1/test
  244. $t printf "%s\n" \
  245. "-module(m_SUITE)." \
  246. "-export([all/0, testcase1/1])." \
  247. "all() -> [testcase1]." \
  248. "testcase1(_) -> 2 = m:succ(1)." \
  249. > app1/test/m_SUITE.erl
  250. $i "Running tests with coverage analysis."
  251. $t $(MAKE) -C app1 eunit ct COVER=1 $v
  252. $t [ -e app1/test-eunit.log ]
  253. $t [ -e app1/eunit.coverdata ]
  254. $t [ -e app1/ct.coverdata ]
  255. $i "Generating coverage report."
  256. $t $(MAKE) -C app1 cover-report COVER=1 $v
  257. $t [ -e app1/cover/m.COVER.html ]
  258. $t [ -e app1/cover/t.COVER.html ]
  259. $t [ -e app1/cover/index.html ]
  260. $i "Checking combined coverage from eunit and ct."
  261. $t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
  262. $i "Checking that cover-report-clean removes cover report."
  263. $t $(MAKE) -C app1 cover-report-clean $v
  264. $t [ ! -e app1/cover ]
  265. $i "Checking that coverdata-clean removes cover data."
  266. $t $(MAKE) -C app1 coverdata-clean $v
  267. $t [ ! -e app1/eunit.coverdata ]
  268. @# clean up
  269. $t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
  270. $t $(MAKE) -C app1 clean $v
  271. $i "Test 'tests-cover' passed."
  272. define app1_setup
  273. $i "Setting up app."
  274. $t mkdir -p app1
  275. $t cd .. && $(MAKE)
  276. $t cp ../erlang.mk app1/
  277. $t $(MAKE) -C app1 -f erlang.mk bootstrap-lib
  278. $t printf "%s\n" \
  279. "-module(m)." \
  280. "-export([succ/1])." \
  281. "succ(N) -> N + 1." \
  282. > app1/src/m.erl
  283. endef