Makefile 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. # In some cases it is more appropriate to wait until a command succeeds or fails.
  42. # These functions run the given command every second and gives up after 10 tries.
  43. define wait_for_success
  44. count=10; \
  45. until [ $$count = 0 ] || $1; do \
  46. count=`expr $$count - 1`; \
  47. sleep 1; \
  48. done; \
  49. if [ $$count = 0 ]; then \
  50. false; \
  51. fi
  52. endef
  53. define wait_for_failure
  54. count=10; \
  55. while [ $$count != 0 ] && $1; do \
  56. count=`expr $$count - 1`; \
  57. sleep 1; \
  58. done; \
  59. if [ $$count = 0 ]; then \
  60. false; \
  61. fi
  62. endef
  63. # OTP master, for downloading files for testing.
  64. OTP_MASTER = https://raw.githubusercontent.com/erlang/otp/master
  65. # Verbosity.
  66. #
  67. # V=0: Show info messages only.
  68. # V=1: Show test commands.
  69. # V=2: Also show normal Erlang.mk output.
  70. # V=3: Also show verbose Erlang.mk output.
  71. # V=4: Also show a trace of each command after expansion.
  72. V ?= 0
  73. # t: Verbosity control for tests.
  74. # v: Verbosity control for erlang.mk.
  75. # i: Command to display (or suppress) info messages.
  76. ifeq ($V,0)
  77. t = @
  78. v = V=0 >/dev/null 2>&1
  79. i = @echo $@:
  80. else ifeq ($V,1)
  81. t =
  82. v = V=0 >/dev/null 2>&1
  83. i = @echo == $@:
  84. else ifeq ($V,2)
  85. t = @echo " TEST " $@;
  86. v = V=0
  87. i = @echo == $@:
  88. else
  89. t =
  90. v = V=$(shell echo $$(($(V)-2)))
  91. i = @echo == $@:
  92. endif
  93. # Automatic listing of targets from test files.
  94. define list_targets
  95. $(sort $(shell grep ^$1- $(lastword $(MAKEFILE_LIST)) | cut -d: -f1))
  96. endef
  97. # Main targets.
  98. .PHONY: all clean init
  99. all:: core
  100. clean::
  101. $t rm -rf erl_crash.dump packages/ test_*/
  102. init: clean
  103. $i "Prefetch Rebar if necessary"
  104. $t if [ ! -d test_rebar_git ]; then \
  105. git clone -q -n -- https://github.com/rebar/rebar test_rebar_git; \
  106. fi
  107. $i "Generate a bleeding edge Erlang.mk"
  108. $t cd .. && $(MAKE) $v
  109. REBAR_GIT = file://$(CURDIR)/test_rebar_git
  110. export REBAR_GIT
  111. # Core.
  112. .PHONY: core
  113. define include_core
  114. core:: core-$1
  115. include core_$1.mk
  116. endef
  117. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst core_%,%,$(wildcard core_*.mk))),$(call include_core,$t)))
  118. # Plugins.
  119. define include_plugin
  120. all:: $1
  121. include plugin_$1.mk
  122. endef
  123. $(eval $(foreach t,$(patsubst %.mk,%,$(patsubst plugin_%,%,$(wildcard plugin_*.mk))),$(call include_plugin,$t)))
  124. # Packages.
  125. PACKAGES = $(foreach pkg,$(sort $(wildcard ../index/*.mk)),$(notdir $(basename $(pkg))))
  126. EXCLUDE_FROM_CHECK = ['ci.erlang.mk', hexer_mk, inaka_mk, 'lfe.mk', rabbitmq_codegen]
  127. packages: $(addprefix pkg-,$(PACKAGES))
  128. define pkg_target
  129. .PHONY: pkg-$1
  130. pkg-$1: init
  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) FULL=1 $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))))