Makefile 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. CACHE_DIR = $(CURDIR)/$(APP).cache
  13. export CACHE_DIR
  14. # Erlang, quickly!
  15. ERL = erl +A0 -noinput -boot no_dot_erlang
  16. # Platform detection, condensed version.
  17. UNAME_S := $(shell uname -s)
  18. ifeq ($(UNAME_S),Darwin)
  19. PLATFORM = darwin
  20. else ifeq ($(UNAME_S),FreeBSD)
  21. PLATFORM = freebsd
  22. else ifeq ($(shell uname -o),Msys)
  23. PLATFORM = msys2
  24. else
  25. PLATFORM = unix
  26. endif
  27. # Some systems do not have sub-second file times resolution.
  28. # This is the case for older systems like OSX that uses the HFS+
  29. # file system. HFS+ has a 1 second time resolution. This is a
  30. # problem because the Erlang.mk tests rely on file modification
  31. # times to ensure files were rebuilt. To fix this issue, we
  32. # detect here whether the system supports sub-second resolution,
  33. # and maybe sleep during test execution.
  34. #
  35. # Also see:
  36. # * http://arstechnica.com/apple/2011/07/mac-os-x-10-7/12/#hfs-problems
  37. # * https://apple.stackexchange.com/questions/51650/linus-torvalds-and-the-os-x-filesystem
  38. 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)
  39. SLEEP = sleep 1
  40. else ifeq ($(shell touch a; touch b; touch c; test c -nt b -a b -nt a; echo $$?; rm a b c),1)
  41. SLEEP = sleep 0.01
  42. else
  43. SLEEP =
  44. endif
  45. # In some cases it is more appropriate to wait until a command succeeds or fails.
  46. # These functions run the given command every second and gives up after 10 tries.
  47. define wait_for_success
  48. count=10; \
  49. until [ $$count = 0 ] || $1; do \
  50. count=`expr $$count - 1`; \
  51. sleep 1; \
  52. done; \
  53. if [ $$count = 0 ]; then \
  54. false; \
  55. fi
  56. endef
  57. define wait_for_failure
  58. count=10; \
  59. while [ $$count != 0 ] && $1; do \
  60. count=`expr $$count - 1`; \
  61. sleep 1; \
  62. done; \
  63. if [ $$count = 0 ]; then \
  64. false; \
  65. fi
  66. endef
  67. # OTP master, for downloading files for testing.
  68. OTP_MASTER = https://raw.githubusercontent.com/erlang/otp/master
  69. # Verbosity.
  70. #
  71. # V=0: Show info messages only.
  72. # V=1: Show test commands.
  73. # V=2: Also show normal Erlang.mk output.
  74. # V=3: Also show verbose Erlang.mk output.
  75. # V=4: Also show a trace of each command after expansion.
  76. V ?= 0
  77. # t: Verbosity control for tests.
  78. # v: Verbosity control for erlang.mk.
  79. # i: Command to display (or suppress) info messages.
  80. ifeq ($V,0)
  81. t = @
  82. v = V=0 >/dev/null 2>&1
  83. i = @echo $@:
  84. else ifeq ($V,1)
  85. t =
  86. v = V=0 >/dev/null 2>&1
  87. i = @echo == $@:
  88. else ifeq ($V,2)
  89. t = @echo " TEST " $@;
  90. v = V=0
  91. i = @echo == $@:
  92. else
  93. t =
  94. v = V=$(shell echo $$(($(V)-2)))
  95. i = @echo == $@:
  96. endif
  97. # Automatic listing of targets from test files.
  98. define list_targets
  99. $(sort $(shell grep ^$1- $(lastword $(MAKEFILE_LIST)) | cut -d: -f1))
  100. endef
  101. # Main targets.
  102. .PHONY: all clean init
  103. all:: core
  104. clean::
  105. $t rm -rf erl_crash.dump packages/ $(filter-out test_rebar_git/,$(wildcard test_*/)) $(CACHE_DIR)
  106. init: clean
  107. $i "Prefetch Rebar if necessary"
  108. $t if [ ! -d test_rebar_git ]; then \
  109. git clone -q -n -- https://github.com/erlang/rebar3 test_rebar_git; \
  110. fi
  111. $i "Generate a bleeding edge Erlang.mk"
  112. $t cd .. && $(MAKE) $v
  113. REBAR3_GIT = file://$(CURDIR)/test_rebar_git
  114. export REBAR3_GIT
  115. # Core.
  116. .PHONY: core
  117. define include_core
  118. core:: core-$1
  119. include core_$1.mk
  120. endef
  121. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst core_%,%,$(wildcard core_*.mk))),$(call include_core,$t)))
  122. # Plugins.
  123. define include_plugin
  124. all:: $1
  125. include plugin_$1.mk
  126. endef
  127. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst plugin_%,%,$(wildcard plugin_*.mk))),$(call include_plugin,$t)))
  128. # Packages.
  129. PACKAGES = $(foreach pkg,$(sort $(wildcard ../index/*.mk)),$(notdir $(basename $(pkg))))
  130. PATCHES = ELIXIR_PATCH=1 HUT_PATCH=1
  131. EXCLUDE_FROM_CHECK = ['ci.erlang.mk', elvis_mk, esh_mk, hexer_mk, inaka_mk, 'lfe.mk', pmod_transform, rust_mk]
  132. EXCLUDE_FROM_APP_CHECK = esh_mk pmod_transform rust_mk
  133. packages: $(addprefix pkg-,$(PACKAGES))
  134. define pkg_target
  135. .PHONY: pkg-$1
  136. pkg-$1: init
  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. $(if $(filter $1,$(EXCLUDE_FROM_APP_CHECK)),,
  153. $i "Check that $1 has a .app file"
  154. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  155. echo "$1: no .app file" >> packages/errors.log; \
  156. false; \
  157. fi)
  158. $i "Check that all applications and their modules can be loaded"
  159. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  160. Apps0 = [list_to_atom(App) || \"deps/\" ++ App \
  161. <- filelib:wildcard(\"deps/*\")], \
  162. Apps = [App || App <- Apps0, not lists:member(App, $(EXCLUDE_FROM_CHECK))], \
  163. [begin \
  164. io:format(\"Loading application ~p~n\", [App]), \
  165. case application:load(App) of \
  166. ok -> ok; \
  167. {error, {already_loaded, App}} -> ok \
  168. end, \
  169. {ok, Mods} = application:get_key(App, modules), \
  170. [try io:format(\" Loading module ~p~n\", [Mod]), \
  171. {module, Mod} = code:load_file(Mod) \
  172. catch C:R -> timer:sleep(500), erlang:C(R) \
  173. end || Mod <- Mods] \
  174. end || App <- Apps], \
  175. halt()." ); then \
  176. echo "$1: load error" >> packages/errors.log; \
  177. false; \
  178. fi
  179. $i "Recompile package $1"
  180. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) FULL=1 $v ); then \
  181. echo "$(1): recompile error" >> packages/errors.log; \
  182. false; \
  183. fi
  184. $(if $(filter $1,$(EXCLUDE_FROM_APP_CHECK)),,
  185. $i "Check that $1 has a .app file"
  186. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  187. echo "$1: no .app file" >> packages/errors.log; \
  188. false; \
  189. fi)
  190. $i "Check that all applications and their modules can still be loaded"
  191. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  192. Apps0 = [list_to_atom(App) || \"deps/\" ++ App \
  193. <- filelib:wildcard(\"deps/*\")], \
  194. Apps = [App || App <- Apps0, not lists:member(App, $(EXCLUDE_FROM_CHECK))], \
  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. # Hex.pm packages.
  221. ifdef HEXPM
  222. HEXPM_PACKAGES =
  223. define hexpm_pkg_target
  224. HEXPM_PACKAGES += $1
  225. .PHONY: hexpm-pkg-$1
  226. hexpm-pkg-$1: init
  227. # Make sure $@ is defined inside the define.
  228. $(eval @ = hexpm-pkg-$1)
  229. # @todo Get the real application's name. How?
  230. $(eval APP_NAME := $1)
  231. $i "Bootstrap a new OTP library in packages/$1_pkg"
  232. $t mkdir -p packages/$1_pkg/
  233. $t cp ../erlang.mk packages/$1_pkg/
  234. $t cd packages/$1_pkg/ && $(MAKE) -f erlang.mk bootstrap-lib $v
  235. $i "Add package $1 to the Makefile"
  236. $t perl -ni.bak -e 'print;if ($$$$.==1) {print "DEPS = $1\ndep_$1 = hex $2\n"}' packages/$1_pkg/Makefile
  237. $i "Compile package $1"
  238. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) $v ); then \
  239. echo "$1: compile error" >> packages/errors.log; \
  240. false; \
  241. fi
  242. # $(if $(filter $1,$(EXCLUDE_FROM_APP_CHECK)),,
  243. $i "Check that $1 has a .app file"
  244. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  245. echo "$1: no .app file" >> packages/errors.log; \
  246. false; \
  247. fi
  248. # )
  249. $i "Check that all applications and their modules can be loaded"
  250. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  251. Apps0 = [list_to_atom(App) || \"deps/\" ++ App \
  252. <- filelib:wildcard(\"deps/*\")], \
  253. Apps = [App || App <- Apps0, not lists:member(App, $(EXCLUDE_FROM_CHECK))], \
  254. [begin \
  255. io:format(\"Loading application ~p~n\", [App]), \
  256. case application:load(App) of \
  257. ok -> ok; \
  258. {error, {already_loaded, App}} -> ok \
  259. end, \
  260. {ok, Mods} = application:get_key(App, modules), \
  261. [try io:format(\" Loading module ~p~n\", [Mod]), \
  262. {module, Mod} = code:load_file(Mod) \
  263. catch C:R -> timer:sleep(500), erlang:C(R) \
  264. end || Mod <- Mods] \
  265. end || App <- Apps], \
  266. halt()." ); then \
  267. echo "$1: load error" >> packages/errors.log; \
  268. false; \
  269. fi
  270. $i "Recompile package $1"
  271. $t if ! ( cd packages/$1_pkg/ && $(MAKE) $(PATCHES) FULL=1 $v ); then \
  272. echo "$(1): recompile error" >> packages/errors.log; \
  273. false; \
  274. fi
  275. # $(if $(filter $1,$(EXCLUDE_FROM_APP_CHECK)),,
  276. $i "Check that $1 has a .app file"
  277. $t if ! test -f packages/$1_pkg/deps/$(APP_NAME)/ebin/$(APP_NAME).app; then \
  278. echo "$1: no .app file" >> packages/errors.log; \
  279. false; \
  280. fi
  281. # )
  282. $i "Check that all applications and their modules can still be loaded"
  283. $t if ! ( cd packages/$1_pkg/ && $(ERL) -pa deps/*/ebin/ -eval " \
  284. Apps0 = [list_to_atom(App) || \"deps/\" ++ App \
  285. <- filelib:wildcard(\"deps/*\")], \
  286. Apps = [App || App <- Apps0, not lists:member(App, $(EXCLUDE_FROM_CHECK))], \
  287. [begin \
  288. io:format(\"Loading application ~p~n\", [App]), \
  289. case application:load(App) of \
  290. ok -> ok; \
  291. {error, {already_loaded, App}} -> ok \
  292. end, \
  293. {ok, Mods} = application:get_key(App, modules), \
  294. [try io:format(\" Loading module ~p~n\", [Mod]), \
  295. {module, Mod} = code:load_file(Mod) \
  296. catch C:R -> timer:sleep(500), erlang:C(R) \
  297. end || Mod <- Mods] \
  298. end || App <- Apps], \
  299. halt()." ); then \
  300. echo "$1: recompile+load error" >> packages/errors.log; \
  301. false; \
  302. fi
  303. $i "Check that no erl_crash.dump file exists"
  304. $t if ( ! find packages/$1_pkg/ -type f -name erl_crash.dump ); then \
  305. echo "$(1): erl_crash.dump found" >> packages/errors.log; \
  306. fi
  307. $(if $(KEEP_BUILDS),,
  308. $i "OK; delete the build directory"
  309. $t rm -rf packages/$1_pkg/)
  310. endef
  311. $(foreach pkg,$(shell grep -v '^#' hexpm_packages.txt | sed 's/ /@/'),$(eval $(call hexpm_pkg_target,$(firstword $(subst @, ,$(pkg))),$(lastword $(subst @, ,$(pkg))))))
  312. hexpm-packages: $(addprefix hexpm-pkg-,$(HEXPM_PACKAGES))
  313. endif