erlang.mk 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741
  1. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  2. #
  3. # Permission to use, copy, modify, and/or distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. .PHONY: all app apps deps search rel docs install-docs check tests clean distclean help erlang-mk
  15. ERLANG_MK_FILENAME := $(realpath $(lastword $(MAKEFILE_LIST)))
  16. ERLANG_MK_VERSION = 2.0.0-pre.2-130-gc6fe5ea
  17. # Core configuration.
  18. PROJECT ?= $(notdir $(CURDIR))
  19. PROJECT := $(strip $(PROJECT))
  20. PROJECT_VERSION ?= rolling
  21. PROJECT_MOD ?= $(PROJECT)_app
  22. # Verbosity.
  23. V ?= 0
  24. verbose_0 = @
  25. verbose_2 = set -x;
  26. verbose = $(verbose_$(V))
  27. gen_verbose_0 = @echo " GEN " $@;
  28. gen_verbose_2 = set -x;
  29. gen_verbose = $(gen_verbose_$(V))
  30. # Temporary files directory.
  31. ERLANG_MK_TMP ?= $(CURDIR)/.erlang.mk
  32. export ERLANG_MK_TMP
  33. # "erl" command.
  34. ERL = erl +A0 -noinput -boot start_clean
  35. # Platform detection.
  36. ifeq ($(PLATFORM),)
  37. UNAME_S := $(shell uname -s)
  38. ifeq ($(UNAME_S),Linux)
  39. PLATFORM = linux
  40. else ifeq ($(UNAME_S),Darwin)
  41. PLATFORM = darwin
  42. else ifeq ($(UNAME_S),SunOS)
  43. PLATFORM = solaris
  44. else ifeq ($(UNAME_S),GNU)
  45. PLATFORM = gnu
  46. else ifeq ($(UNAME_S),FreeBSD)
  47. PLATFORM = freebsd
  48. else ifeq ($(UNAME_S),NetBSD)
  49. PLATFORM = netbsd
  50. else ifeq ($(UNAME_S),OpenBSD)
  51. PLATFORM = openbsd
  52. else ifeq ($(UNAME_S),DragonFly)
  53. PLATFORM = dragonfly
  54. else ifeq ($(shell uname -o),Msys)
  55. PLATFORM = msys2
  56. else
  57. $(error Unable to detect platform. Please open a ticket with the output of uname -a.)
  58. endif
  59. export PLATFORM
  60. endif
  61. # Core targets.
  62. all:: deps app rel
  63. # Noop to avoid a Make warning when there's nothing to do.
  64. rel::
  65. $(verbose) :
  66. check:: tests
  67. clean:: clean-crashdump
  68. clean-crashdump:
  69. ifneq ($(wildcard erl_crash.dump),)
  70. $(gen_verbose) rm -f erl_crash.dump
  71. endif
  72. distclean:: clean distclean-tmp
  73. distclean-tmp:
  74. $(gen_verbose) rm -rf $(ERLANG_MK_TMP)
  75. help::
  76. $(verbose) printf "%s\n" \
  77. "erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \
  78. "Copyright (c) 2013-2015 Loïc Hoguin <essen@ninenines.eu>" \
  79. "" \
  80. "Usage: [V=1] $(MAKE) [target]..." \
  81. "" \
  82. "Core targets:" \
  83. " all Run deps, app and rel targets in that order" \
  84. " app Compile the project" \
  85. " deps Fetch dependencies (if needed) and compile them" \
  86. " search q=... Search for a package in the built-in index" \
  87. " rel Build a release for this project, if applicable" \
  88. " docs Build the documentation for this project" \
  89. " install-docs Install the man pages for this project" \
  90. " check Compile and run all tests and analysis for this project" \
  91. " tests Run the tests for this project" \
  92. " clean Delete temporary and output files from most targets" \
  93. " distclean Delete all temporary and output files" \
  94. " help Display this help and exit" \
  95. " erlang-mk Update erlang.mk to the latest version"
  96. # Core functions.
  97. empty :=
  98. space := $(empty) $(empty)
  99. tab := $(empty) $(empty)
  100. comma := ,
  101. define newline
  102. endef
  103. define comma_list
  104. $(subst $(space),$(comma),$(strip $(1)))
  105. endef
  106. # Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy.
  107. define erlang
  108. $(ERL) $(2) -pz $(ERLANG_MK_TMP)/rebar/ebin -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
  109. endef
  110. ifeq ($(PLATFORM),msys2)
  111. core_native_path = $(subst \,\\\\,$(shell cygpath -w $1))
  112. else
  113. core_native_path = $1
  114. endif
  115. ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
  116. define core_http_get
  117. wget --no-check-certificate -O $(1) $(2)|| rm $(1)
  118. endef
  119. else
  120. define core_http_get.erl
  121. ssl:start(),
  122. inets:start(),
  123. case httpc:request(get, {"$(2)", []}, [{autoredirect, true}], []) of
  124. {ok, {{_, 200, _}, _, Body}} ->
  125. case file:write_file("$(1)", Body) of
  126. ok -> ok;
  127. {error, R1} -> halt(R1)
  128. end;
  129. {error, R2} ->
  130. halt(R2)
  131. end,
  132. halt(0).
  133. endef
  134. define core_http_get
  135. $(call erlang,$(call core_http_get.erl,$(call core_native_path,$1),$2))
  136. endef
  137. endif
  138. core_eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
  139. core_find = $(if $(wildcard $1),$(shell find $(1:%/=%) -type f -name $(subst *,\*,$2)))
  140. core_lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$(1)))))))))))))))))))))))))))
  141. core_ls = $(filter-out $(1),$(shell echo $(1)))
  142. # @todo Use a solution that does not require using perl.
  143. core_relpath = $(shell perl -e 'use File::Spec; print File::Spec->abs2rel(@ARGV) . "\n"' $1 $2)
  144. # Automated update.
  145. ERLANG_MK_REPO ?= https://github.com/ninenines/erlang.mk
  146. ERLANG_MK_COMMIT ?=
  147. ERLANG_MK_BUILD_CONFIG ?= build.config
  148. ERLANG_MK_BUILD_DIR ?= .erlang.mk.build
  149. erlang-mk:
  150. git clone $(ERLANG_MK_REPO) $(ERLANG_MK_BUILD_DIR)
  151. ifdef ERLANG_MK_COMMIT
  152. cd $(ERLANG_MK_BUILD_DIR) && git checkout $(ERLANG_MK_COMMIT)
  153. endif
  154. if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR)/build.config; fi
  155. $(MAKE) -C $(ERLANG_MK_BUILD_DIR)
  156. cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
  157. rm -rf $(ERLANG_MK_BUILD_DIR)
  158. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  159. # This file is part of erlang.mk and subject to the terms of the ISC License.
  160. .PHONY: search
  161. define pkg_print
  162. $(verbose) printf "%s\n" \
  163. $(if $(call core_eq,$(1),$(pkg_$(1)_name)),,"Pkg name: $(1)") \
  164. "App name: $(pkg_$(1)_name)" \
  165. "Description: $(pkg_$(1)_description)" \
  166. "Home page: $(pkg_$(1)_homepage)" \
  167. "Fetch with: $(pkg_$(1)_fetch)" \
  168. "Repository: $(pkg_$(1)_repo)" \
  169. "Commit: $(pkg_$(1)_commit)" \
  170. ""
  171. endef
  172. search:
  173. ifdef q
  174. $(foreach p,$(PACKAGES), \
  175. $(if $(findstring $(call core_lc,$(q)),$(call core_lc,$(pkg_$(p)_name) $(pkg_$(p)_description))), \
  176. $(call pkg_print,$(p))))
  177. else
  178. $(foreach p,$(PACKAGES),$(call pkg_print,$(p)))
  179. endif
  180. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  181. # This file is part of erlang.mk and subject to the terms of the ISC License.
  182. .PHONY: distclean-deps
  183. # Configuration.
  184. ifdef OTP_DEPS
  185. $(warning The variable OTP_DEPS is deprecated in favor of LOCAL_DEPS.)
  186. endif
  187. IGNORE_DEPS ?=
  188. export IGNORE_DEPS
  189. APPS_DIR ?= $(CURDIR)/apps
  190. export APPS_DIR
  191. DEPS_DIR ?= $(CURDIR)/deps
  192. export DEPS_DIR
  193. REBAR_DEPS_DIR = $(DEPS_DIR)
  194. export REBAR_DEPS_DIR
  195. dep_name = $(if $(dep_$(1)),$(1),$(if $(pkg_$(1)_name),$(pkg_$(1)_name),$(1)))
  196. dep_repo = $(patsubst git://github.com/%,https://github.com/%, \
  197. $(if $(dep_$(1)),$(word 2,$(dep_$(1))),$(pkg_$(1)_repo)))
  198. dep_commit = $(if $(dep_$(1)_commit),$(dep_$(1)_commit),$(if $(dep_$(1)),$(word 3,$(dep_$(1))),$(pkg_$(1)_commit)))
  199. ALL_APPS_DIRS = $(if $(wildcard $(APPS_DIR)/),$(filter-out $(APPS_DIR),$(shell find $(APPS_DIR) -maxdepth 1 -type d)))
  200. ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(foreach dep,$(filter-out $(IGNORE_DEPS),$(BUILD_DEPS) $(DEPS)),$(call dep_name,$(dep))))
  201. ifeq ($(filter $(APPS_DIR) $(DEPS_DIR),$(subst :, ,$(ERL_LIBS))),)
  202. ifeq ($(ERL_LIBS),)
  203. ERL_LIBS = $(APPS_DIR):$(DEPS_DIR)
  204. else
  205. ERL_LIBS := $(ERL_LIBS):$(APPS_DIR):$(DEPS_DIR)
  206. endif
  207. endif
  208. export ERL_LIBS
  209. export NO_AUTOPATCH
  210. # Verbosity.
  211. dep_verbose_0 = @echo " DEP " $(1);
  212. dep_verbose_2 = set -x;
  213. dep_verbose = $(dep_verbose_$(V))
  214. # Core targets.
  215. ifdef IS_APP
  216. apps::
  217. else
  218. apps:: $(ALL_APPS_DIRS)
  219. ifeq ($(IS_APP)$(IS_DEP),)
  220. $(verbose) rm -f $(ERLANG_MK_TMP)/apps.log
  221. endif
  222. $(verbose) mkdir -p $(ERLANG_MK_TMP)
  223. # Create ebin directory for all apps to make sure Erlang recognizes them
  224. # as proper OTP applications when using -include_lib. This is a temporary
  225. # fix, a proper fix would be to compile apps/* in the right order.
  226. $(verbose) for dep in $(ALL_APPS_DIRS) ; do \
  227. mkdir -p $$dep/ebin || exit $$?; \
  228. done
  229. $(verbose) for dep in $(ALL_APPS_DIRS) ; do \
  230. if grep -qs ^$$dep$$ $(ERLANG_MK_TMP)/apps.log; then \
  231. :; \
  232. else \
  233. echo $$dep >> $(ERLANG_MK_TMP)/apps.log; \
  234. $(MAKE) -C $$dep IS_APP=1 || exit $$?; \
  235. fi \
  236. done
  237. endif
  238. ifneq ($(SKIP_DEPS),)
  239. deps::
  240. else
  241. deps:: $(ALL_DEPS_DIRS) apps
  242. ifeq ($(IS_APP)$(IS_DEP),)
  243. $(verbose) rm -f $(ERLANG_MK_TMP)/deps.log
  244. endif
  245. $(verbose) mkdir -p $(ERLANG_MK_TMP)
  246. $(verbose) for dep in $(ALL_DEPS_DIRS) ; do \
  247. if grep -qs ^$$dep$$ $(ERLANG_MK_TMP)/deps.log; then \
  248. :; \
  249. else \
  250. echo $$dep >> $(ERLANG_MK_TMP)/deps.log; \
  251. if [ -f $$dep/GNUmakefile ] || [ -f $$dep/makefile ] || [ -f $$dep/Makefile ]; then \
  252. $(MAKE) -C $$dep IS_DEP=1 || exit $$?; \
  253. else \
  254. echo "Error: No Makefile to build dependency $$dep."; \
  255. exit 2; \
  256. fi \
  257. fi \
  258. done
  259. endif
  260. # Deps related targets.
  261. # @todo rename GNUmakefile and makefile into Makefile first, if they exist
  262. # While Makefile file could be GNUmakefile or makefile,
  263. # in practice only Makefile is needed so far.
  264. define dep_autopatch
  265. if [ -f $(DEPS_DIR)/$(1)/erlang.mk ]; then \
  266. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1))); \
  267. $(call dep_autopatch_erlang_mk,$(1)); \
  268. elif [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  269. if [ 0 != `grep -c "include ../\w*\.mk" $(DEPS_DIR)/$(1)/Makefile` ]; then \
  270. $(call dep_autopatch2,$(1)); \
  271. elif [ 0 != `grep -ci rebar $(DEPS_DIR)/$(1)/Makefile` ]; then \
  272. $(call dep_autopatch2,$(1)); \
  273. elif [ -n "`find $(DEPS_DIR)/$(1)/ -type f -name \*.mk -not -name erlang.mk -exec grep -i rebar '{}' \;`" ]; then \
  274. $(call dep_autopatch2,$(1)); \
  275. else \
  276. $(call erlang,$(call dep_autopatch_app.erl,$(1))); \
  277. fi \
  278. else \
  279. if [ ! -d $(DEPS_DIR)/$(1)/src/ ]; then \
  280. $(call dep_autopatch_noop,$(1)); \
  281. else \
  282. $(call dep_autopatch2,$(1)); \
  283. fi \
  284. fi
  285. endef
  286. define dep_autopatch2
  287. if [ -f $(DEPS_DIR)/$1/src/$1.app.src.script ]; then \
  288. $(call erlang,$(call dep_autopatch_appsrc_script.erl,$(1))); \
  289. fi; \
  290. $(call erlang,$(call dep_autopatch_appsrc.erl,$(1))); \
  291. if [ -f $(DEPS_DIR)/$(1)/rebar -o -f $(DEPS_DIR)/$(1)/rebar.config -o -f $(DEPS_DIR)/$(1)/rebar.config.script ]; then \
  292. $(call dep_autopatch_fetch_rebar); \
  293. $(call dep_autopatch_rebar,$(1)); \
  294. else \
  295. $(call dep_autopatch_gen,$(1)); \
  296. fi
  297. endef
  298. define dep_autopatch_noop
  299. printf "noop:\n" > $(DEPS_DIR)/$(1)/Makefile
  300. endef
  301. # Overwrite erlang.mk with the current file by default.
  302. ifeq ($(NO_AUTOPATCH_ERLANG_MK),)
  303. define dep_autopatch_erlang_mk
  304. echo "include $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(DEPS_DIR)/app)/erlang.mk" \
  305. > $(DEPS_DIR)/$1/erlang.mk
  306. endef
  307. else
  308. define dep_autopatch_erlang_mk
  309. :
  310. endef
  311. endif
  312. define dep_autopatch_gen
  313. printf "%s\n" \
  314. "ERLC_OPTS = +debug_info" \
  315. "include ../../erlang.mk" > $(DEPS_DIR)/$(1)/Makefile
  316. endef
  317. define dep_autopatch_fetch_rebar
  318. mkdir -p $(ERLANG_MK_TMP); \
  319. if [ ! -d $(ERLANG_MK_TMP)/rebar ]; then \
  320. git clone -q -n -- https://github.com/rebar/rebar $(ERLANG_MK_TMP)/rebar; \
  321. cd $(ERLANG_MK_TMP)/rebar; \
  322. git checkout -q 791db716b5a3a7671e0b351f95ddf24b848ee173; \
  323. $(MAKE); \
  324. cd -; \
  325. fi
  326. endef
  327. define dep_autopatch_rebar
  328. if [ -f $(DEPS_DIR)/$(1)/Makefile ]; then \
  329. mv $(DEPS_DIR)/$(1)/Makefile $(DEPS_DIR)/$(1)/Makefile.orig.mk; \
  330. fi; \
  331. $(call erlang,$(call dep_autopatch_rebar.erl,$(1))); \
  332. rm -f $(DEPS_DIR)/$(1)/ebin/$(1).app
  333. endef
  334. define dep_autopatch_rebar.erl
  335. application:load(rebar),
  336. application:set_env(rebar, log_level, debug),
  337. Conf1 = case file:consult("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config)") of
  338. {ok, Conf0} -> Conf0;
  339. _ -> []
  340. end,
  341. {Conf, OsEnv} = fun() ->
  342. case filelib:is_file("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)") of
  343. false -> {Conf1, []};
  344. true ->
  345. Bindings0 = erl_eval:new_bindings(),
  346. Bindings1 = erl_eval:add_binding('CONFIG', Conf1, Bindings0),
  347. Bindings = erl_eval:add_binding('SCRIPT', "$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)", Bindings1),
  348. Before = os:getenv(),
  349. {ok, Conf2} = file:script("$(call core_native_path,$(DEPS_DIR)/$1/rebar.config.script)", Bindings),
  350. {Conf2, lists:foldl(fun(E, Acc) -> lists:delete(E, Acc) end, os:getenv(), Before)}
  351. end
  352. end(),
  353. Write = fun (Text) ->
  354. file:write_file("$(call core_native_path,$(DEPS_DIR)/$1/Makefile)", Text, [append])
  355. end,
  356. Escape = fun (Text) ->
  357. re:replace(Text, "\\\\$$", "\$$$$", [global, {return, list}])
  358. end,
  359. Write("IGNORE_DEPS += edown eper eunit_formatters meck node_package "
  360. "rebar_lock_deps_plugin rebar_vsn_plugin reltool_util\n"),
  361. Write("C_SRC_DIR = /path/do/not/exist\n"),
  362. Write("C_SRC_TYPE = rebar\n"),
  363. Write("DRV_CFLAGS = -fPIC\nexport DRV_CFLAGS\n"),
  364. Write(["ERLANG_ARCH = ", rebar_utils:wordsize(), "\nexport ERLANG_ARCH\n"]),
  365. fun() ->
  366. Write("ERLC_OPTS = +debug_info\nexport ERLC_OPTS\n"),
  367. case lists:keyfind(erl_opts, 1, Conf) of
  368. false -> ok;
  369. {_, ErlOpts} ->
  370. lists:foreach(fun
  371. ({d, D}) ->
  372. Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
  373. ({i, I}) ->
  374. Write(["ERLC_OPTS += -I ", I, "\n"]);
  375. ({platform_define, Regex, D}) ->
  376. case rebar_utils:is_arch(Regex) of
  377. true -> Write("ERLC_OPTS += -D" ++ atom_to_list(D) ++ "=1\n");
  378. false -> ok
  379. end;
  380. ({parse_transform, PT}) ->
  381. Write("ERLC_OPTS += +'{parse_transform, " ++ atom_to_list(PT) ++ "}'\n");
  382. (_) -> ok
  383. end, ErlOpts)
  384. end,
  385. Write("\n")
  386. end(),
  387. fun() ->
  388. File = case lists:keyfind(deps, 1, Conf) of
  389. false -> [];
  390. {_, Deps} ->
  391. [begin case case Dep of
  392. {N, S} when is_atom(N), is_list(S) -> {N, {hex, S}};
  393. {N, S} when is_tuple(S) -> {N, S};
  394. {N, _, S} -> {N, S};
  395. {N, _, S, _} -> {N, S};
  396. _ -> false
  397. end of
  398. false -> ok;
  399. {Name, Source} ->
  400. {Method, Repo, Commit} = case Source of
  401. {hex, V} -> {hex, V, undefined};
  402. {git, R} -> {git, R, master};
  403. {M, R, {branch, C}} -> {M, R, C};
  404. {M, R, {ref, C}} -> {M, R, C};
  405. {M, R, {tag, C}} -> {M, R, C};
  406. {M, R, C} -> {M, R, C}
  407. end,
  408. Write(io_lib:format("DEPS += ~s\ndep_~s = ~s ~s ~s~n", [Name, Name, Method, Repo, Commit]))
  409. end end || Dep <- Deps]
  410. end
  411. end(),
  412. fun() ->
  413. case lists:keyfind(erl_first_files, 1, Conf) of
  414. false -> ok;
  415. {_, Files} ->
  416. Names = [[" ", case lists:reverse(F) of
  417. "lre." ++ Elif -> lists:reverse(Elif);
  418. Elif -> lists:reverse(Elif)
  419. end] || "src/" ++ F <- Files],
  420. Write(io_lib:format("COMPILE_FIRST +=~s\n", [Names]))
  421. end
  422. end(),
  423. Write("\n\nrebar_dep: preprocess pre-deps deps pre-app app\n"),
  424. Write("\npreprocess::\n"),
  425. Write("\npre-deps::\n"),
  426. Write("\npre-app::\n"),
  427. PatchHook = fun(Cmd) ->
  428. case Cmd of
  429. "make -C" ++ Cmd1 -> "$$\(MAKE) -C" ++ Escape(Cmd1);
  430. "gmake -C" ++ Cmd1 -> "$$\(MAKE) -C" ++ Escape(Cmd1);
  431. "make " ++ Cmd1 -> "$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
  432. "gmake " ++ Cmd1 -> "$$\(MAKE) -f Makefile.orig.mk " ++ Escape(Cmd1);
  433. _ -> Escape(Cmd)
  434. end
  435. end,
  436. fun() ->
  437. case lists:keyfind(pre_hooks, 1, Conf) of
  438. false -> ok;
  439. {_, Hooks} ->
  440. [case H of
  441. {'get-deps', Cmd} ->
  442. Write("\npre-deps::\n\t" ++ PatchHook(Cmd) ++ "\n");
  443. {compile, Cmd} ->
  444. Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  445. {Regex, compile, Cmd} ->
  446. case rebar_utils:is_arch(Regex) of
  447. true -> Write("\npre-app::\n\tCC=$$\(CC) " ++ PatchHook(Cmd) ++ "\n");
  448. false -> ok
  449. end;
  450. _ -> ok
  451. end || H <- Hooks]
  452. end
  453. end(),
  454. ShellToMk = fun(V) ->
  455. re:replace(re:replace(V, "(\\\\$$)(\\\\w*)", "\\\\1(\\\\2)", [global]),
  456. "-Werror\\\\b", "", [{return, list}, global])
  457. end,
  458. PortSpecs = fun() ->
  459. case lists:keyfind(port_specs, 1, Conf) of
  460. false ->
  461. case filelib:is_dir("$(call core_native_path,$(DEPS_DIR)/$1/c_src)") of
  462. false -> [];
  463. true ->
  464. [{"priv/" ++ proplists:get_value(so_name, Conf, "$(1)_drv.so"),
  465. proplists:get_value(port_sources, Conf, ["c_src/*.c"]), []}]
  466. end;
  467. {_, Specs} ->
  468. lists:flatten([case S of
  469. {Output, Input} -> {ShellToMk(Output), Input, []};
  470. {Regex, Output, Input} ->
  471. case rebar_utils:is_arch(Regex) of
  472. true -> {ShellToMk(Output), Input, []};
  473. false -> []
  474. end;
  475. {Regex, Output, Input, [{env, Env}]} ->
  476. case rebar_utils:is_arch(Regex) of
  477. true -> {ShellToMk(Output), Input, Env};
  478. false -> []
  479. end
  480. end || S <- Specs])
  481. end
  482. end(),
  483. PortSpecWrite = fun (Text) ->
  484. file:write_file("$(call core_native_path,$(DEPS_DIR)/$1/c_src/Makefile.erlang.mk)", Text, [append])
  485. end,
  486. case PortSpecs of
  487. [] -> ok;
  488. _ ->
  489. Write("\npre-app::\n\t$$\(MAKE) -f c_src/Makefile.erlang.mk\n"),
  490. PortSpecWrite(io_lib:format("ERL_CFLAGS = -finline-functions -Wall -fPIC -I \\"~s/erts-~s/include\\" -I \\"~s\\"\n",
  491. [code:root_dir(), erlang:system_info(version), code:lib_dir(erl_interface, include)])),
  492. PortSpecWrite(io_lib:format("ERL_LDFLAGS = -L \\"~s\\" -lerl_interface -lei\n",
  493. [code:lib_dir(erl_interface, lib)])),
  494. [PortSpecWrite(["\n", E, "\n"]) || E <- OsEnv],
  495. FilterEnv = fun(Env) ->
  496. lists:flatten([case E of
  497. {_, _} -> E;
  498. {Regex, K, V} ->
  499. case rebar_utils:is_arch(Regex) of
  500. true -> {K, V};
  501. false -> []
  502. end
  503. end || E <- Env])
  504. end,
  505. MergeEnv = fun(Env) ->
  506. lists:foldl(fun ({K, V}, Acc) ->
  507. case lists:keyfind(K, 1, Acc) of
  508. false -> [{K, rebar_utils:expand_env_variable(V, K, "")}|Acc];
  509. {_, V0} -> [{K, rebar_utils:expand_env_variable(V, K, V0)}|Acc]
  510. end
  511. end, [], Env)
  512. end,
  513. PortEnv = case lists:keyfind(port_env, 1, Conf) of
  514. false -> [];
  515. {_, PortEnv0} -> FilterEnv(PortEnv0)
  516. end,
  517. PortSpec = fun ({Output, Input0, Env}) ->
  518. filelib:ensure_dir("$(call core_native_path,$(DEPS_DIR)/$1/)" ++ Output),
  519. Input = [[" ", I] || I <- Input0],
  520. PortSpecWrite([
  521. [["\n", K, " = ", ShellToMk(V)] || {K, V} <- lists:reverse(MergeEnv(PortEnv))],
  522. case $(PLATFORM) of
  523. darwin -> "\n\nLDFLAGS += -flat_namespace -undefined suppress";
  524. _ -> ""
  525. end,
  526. "\n\nall:: ", Output, "\n\n",
  527. "%.o: %.c\n\t$$\(CC) -c -o $$\@ $$\< $$\(CFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
  528. "%.o: %.C\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
  529. "%.o: %.cc\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
  530. "%.o: %.cpp\n\t$$\(CXX) -c -o $$\@ $$\< $$\(CXXFLAGS) $$\(ERL_CFLAGS) $$\(DRV_CFLAGS) $$\(EXE_CFLAGS)\n\n",
  531. [[Output, ": ", K, " = ", ShellToMk(V), "\n"] || {K, V} <- lists:reverse(MergeEnv(FilterEnv(Env)))],
  532. Output, ": $$\(foreach ext,.c .C .cc .cpp,",
  533. "$$\(patsubst %$$\(ext),%.o,$$\(filter %$$\(ext),$$\(wildcard", Input, "))))\n",
  534. "\t$$\(CC) -o $$\@ $$\? $$\(LDFLAGS) $$\(ERL_LDFLAGS) $$\(DRV_LDFLAGS) $$\(EXE_LDFLAGS)",
  535. case {filename:extension(Output), $(PLATFORM)} of
  536. {[], _} -> "\n";
  537. {_, darwin} -> "\n";
  538. _ -> " -shared\n"
  539. end])
  540. end,
  541. [PortSpec(S) || S <- PortSpecs]
  542. end,
  543. Write("\ninclude $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(DEPS_DIR)/app)/erlang.mk"),
  544. RunPlugin = fun(Plugin, Step) ->
  545. case erlang:function_exported(Plugin, Step, 2) of
  546. false -> ok;
  547. true ->
  548. c:cd("$(call core_native_path,$(DEPS_DIR)/$1/)"),
  549. Ret = Plugin:Step({config, "", Conf, dict:new(), dict:new(), dict:new(),
  550. dict:store(base_dir, "", dict:new())}, undefined),
  551. io:format("rebar plugin ~p step ~p ret ~p~n", [Plugin, Step, Ret])
  552. end
  553. end,
  554. fun() ->
  555. case lists:keyfind(plugins, 1, Conf) of
  556. false -> ok;
  557. {_, Plugins} ->
  558. [begin
  559. case lists:keyfind(deps, 1, Conf) of
  560. false -> ok;
  561. {_, Deps} ->
  562. case lists:keyfind(P, 1, Deps) of
  563. false -> ok;
  564. _ ->
  565. Path = "$(call core_native_path,$(DEPS_DIR)/)" ++ atom_to_list(P),
  566. io:format("~s", [os:cmd("$(MAKE) -C $(call core_native_path,$(DEPS_DIR)/$1) " ++ Path)]),
  567. io:format("~s", [os:cmd("$(MAKE) -C " ++ Path ++ " IS_DEP=1")]),
  568. code:add_patha(Path ++ "/ebin")
  569. end
  570. end
  571. end || P <- Plugins],
  572. [case code:load_file(P) of
  573. {module, P} -> ok;
  574. _ ->
  575. case lists:keyfind(plugin_dir, 1, Conf) of
  576. false -> ok;
  577. {_, PluginsDir} ->
  578. ErlFile = "$(call core_native_path,$(DEPS_DIR)/$1/)" ++ PluginsDir ++ "/" ++ atom_to_list(P) ++ ".erl",
  579. {ok, P, Bin} = compile:file(ErlFile, [binary]),
  580. {module, P} = code:load_binary(P, ErlFile, Bin)
  581. end
  582. end || P <- Plugins],
  583. [RunPlugin(P, preprocess) || P <- Plugins],
  584. [RunPlugin(P, pre_compile) || P <- Plugins],
  585. [RunPlugin(P, compile) || P <- Plugins]
  586. end
  587. end(),
  588. halt()
  589. endef
  590. define dep_autopatch_app.erl
  591. UpdateModules = fun(App) ->
  592. case filelib:is_regular(App) of
  593. false -> ok;
  594. true ->
  595. {ok, [{application, '$(1)', L0}]} = file:consult(App),
  596. Mods = filelib:fold_files("$(call core_native_path,$(DEPS_DIR)/$1/src)", "\\\\.erl$$", true,
  597. fun (F, Acc) -> [list_to_atom(filename:rootname(filename:basename(F)))|Acc] end, []),
  598. L = lists:keystore(modules, 1, L0, {modules, Mods}),
  599. ok = file:write_file(App, io_lib:format("~p.~n", [{application, '$(1)', L}]))
  600. end
  601. end,
  602. UpdateModules("$(call core_native_path,$(DEPS_DIR)/$1/ebin/$1.app)"),
  603. halt()
  604. endef
  605. define dep_autopatch_appsrc_script.erl
  606. AppSrc = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)",
  607. AppSrcScript = AppSrc ++ ".script",
  608. Bindings = erl_eval:new_bindings(),
  609. {ok, Conf} = file:script(AppSrcScript, Bindings),
  610. ok = file:write_file(AppSrc, io_lib:format("~p.~n", [Conf])),
  611. halt()
  612. endef
  613. define dep_autopatch_appsrc.erl
  614. AppSrcOut = "$(call core_native_path,$(DEPS_DIR)/$1/src/$1.app.src)",
  615. AppSrcIn = case filelib:is_regular(AppSrcOut) of false -> "$(call core_native_path,$(DEPS_DIR)/$1/ebin/$1.app)"; true -> AppSrcOut end,
  616. case filelib:is_regular(AppSrcIn) of
  617. false -> ok;
  618. true ->
  619. {ok, [{application, $(1), L0}]} = file:consult(AppSrcIn),
  620. L1 = lists:keystore(modules, 1, L0, {modules, []}),
  621. L2 = case lists:keyfind(vsn, 1, L1) of {_, git} -> lists:keyreplace(vsn, 1, L1, {vsn, "git"}); _ -> L1 end,
  622. L3 = case lists:keyfind(registered, 1, L2) of false -> [{registered, []}|L2]; _ -> L2 end,
  623. ok = file:write_file(AppSrcOut, io_lib:format("~p.~n", [{application, $(1), L3}])),
  624. case AppSrcOut of AppSrcIn -> ok; _ -> ok = file:delete(AppSrcIn) end
  625. end,
  626. halt()
  627. endef
  628. define dep_fetch_git
  629. git clone -q -n -- $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
  630. cd $(DEPS_DIR)/$(call dep_name,$(1)) && git checkout -q $(call dep_commit,$(1));
  631. endef
  632. define dep_fetch_git-submodule
  633. git submodule update --init -- $(DEPS_DIR)/$1;
  634. endef
  635. define dep_fetch_hg
  636. hg clone -q -U $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1)); \
  637. cd $(DEPS_DIR)/$(call dep_name,$(1)) && hg update -q $(call dep_commit,$(1));
  638. endef
  639. define dep_fetch_svn
  640. svn checkout -q $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  641. endef
  642. define dep_fetch_cp
  643. cp -R $(call dep_repo,$(1)) $(DEPS_DIR)/$(call dep_name,$(1));
  644. endef
  645. define dep_fetch_hex.erl
  646. ssl:start(),
  647. inets:start(),
  648. {ok, {{_, 200, _}, _, Body}} = httpc:request(get,
  649. {"https://s3.amazonaws.com/s3.hex.pm/tarballs/$(1)-$(2).tar", []},
  650. [], [{body_format, binary}]),
  651. {ok, Files} = erl_tar:extract({binary, Body}, [memory]),
  652. {_, Source} = lists:keyfind("contents.tar.gz", 1, Files),
  653. ok = erl_tar:extract({binary, Source}, [{cwd, "$(call core_native_path,$(DEPS_DIR)/$1)"}, compressed]),
  654. halt()
  655. endef
  656. # Hex only has a package version. No need to look in the Erlang.mk packages.
  657. define dep_fetch_hex
  658. $(call erlang,$(call dep_fetch_hex.erl,$(1),$(strip $(word 2,$(dep_$(1))))));
  659. endef
  660. define dep_fetch_fail
  661. echo "Error: Unknown or invalid dependency: $(1)." >&2; \
  662. exit 78;
  663. endef
  664. # Kept for compatibility purposes with older Erlang.mk configuration.
  665. define dep_fetch_legacy
  666. $(warning WARNING: '$(1)' dependency configuration uses deprecated format.) \
  667. git clone -q -n -- $(word 1,$(dep_$(1))) $(DEPS_DIR)/$(1); \
  668. cd $(DEPS_DIR)/$(1) && git checkout -q $(if $(word 2,$(dep_$(1))),$(word 2,$(dep_$(1))),master);
  669. endef
  670. define dep_fetch
  671. $(if $(dep_$(1)), \
  672. $(if $(dep_fetch_$(word 1,$(dep_$(1)))), \
  673. $(word 1,$(dep_$(1))), \
  674. $(if $(IS_DEP),legacy,fail)), \
  675. $(if $(filter $(1),$(PACKAGES)), \
  676. $(pkg_$(1)_fetch), \
  677. fail))
  678. endef
  679. define dep_target
  680. $(DEPS_DIR)/$(call dep_name,$1):
  681. $(eval DEP_NAME := $(call dep_name,$1))
  682. $(eval DEP_STR := $(if $(filter-out $1,$(DEP_NAME)),$1,"$1 ($(DEP_NAME))"))
  683. $(verbose) if test -d $(APPS_DIR)/$(DEP_NAME); then \
  684. echo "Error: Dependency" $(DEP_STR) "conflicts with application found in $(APPS_DIR)/$(DEP_NAME)."; \
  685. exit 17; \
  686. fi
  687. $(verbose) mkdir -p $(DEPS_DIR)
  688. $(dep_verbose) $(call dep_fetch_$(strip $(call dep_fetch,$(1))),$(1))
  689. $(verbose) if [ -f $(DEPS_DIR)/$(1)/configure.ac -o -f $(DEPS_DIR)/$(1)/configure.in ] \
  690. && [ ! -f $(DEPS_DIR)/$(1)/configure ]; then \
  691. echo " AUTO " $(1); \
  692. cd $(DEPS_DIR)/$(1) && autoreconf -Wall -vif -I m4; \
  693. fi
  694. - $(verbose) if [ -f $(DEPS_DIR)/$(DEP_NAME)/configure ]; then \
  695. echo " CONF " $(DEP_STR); \
  696. cd $(DEPS_DIR)/$(DEP_NAME) && ./configure; \
  697. fi
  698. ifeq ($(filter $(1),$(NO_AUTOPATCH)),)
  699. $(verbose) if [ "$(1)" = "amqp_client" -a "$(RABBITMQ_CLIENT_PATCH)" ]; then \
  700. if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
  701. echo " PATCH Downloading rabbitmq-codegen"; \
  702. git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
  703. fi; \
  704. if [ ! -d $(DEPS_DIR)/rabbitmq-server ]; then \
  705. echo " PATCH Downloading rabbitmq-server"; \
  706. git clone https://github.com/rabbitmq/rabbitmq-server.git $(DEPS_DIR)/rabbitmq-server; \
  707. fi; \
  708. ln -s $(DEPS_DIR)/amqp_client/deps/rabbit_common-0.0.0 $(DEPS_DIR)/rabbit_common; \
  709. elif [ "$(1)" = "rabbit" -a "$(RABBITMQ_SERVER_PATCH)" ]; then \
  710. if [ ! -d $(DEPS_DIR)/rabbitmq-codegen ]; then \
  711. echo " PATCH Downloading rabbitmq-codegen"; \
  712. git clone https://github.com/rabbitmq/rabbitmq-codegen.git $(DEPS_DIR)/rabbitmq-codegen; \
  713. fi \
  714. else \
  715. $$(call dep_autopatch,$(DEP_NAME)) \
  716. fi
  717. endif
  718. endef
  719. $(foreach dep,$(BUILD_DEPS) $(DEPS),$(eval $(call dep_target,$(dep))))
  720. ifndef IS_APP
  721. clean:: clean-apps
  722. clean-apps:
  723. $(verbose) for dep in $(ALL_APPS_DIRS) ; do \
  724. $(MAKE) -C $$dep clean IS_APP=1 || exit $$?; \
  725. done
  726. distclean:: distclean-apps
  727. distclean-apps:
  728. $(verbose) for dep in $(ALL_APPS_DIRS) ; do \
  729. $(MAKE) -C $$dep distclean IS_APP=1 || exit $$?; \
  730. done
  731. endif
  732. ifndef SKIP_DEPS
  733. distclean:: distclean-deps
  734. distclean-deps:
  735. $(gen_verbose) rm -rf $(DEPS_DIR)
  736. endif
  737. # External plugins.
  738. DEP_PLUGINS ?=
  739. define core_dep_plugin
  740. -include $(DEPS_DIR)/$(1)
  741. $(DEPS_DIR)/$(1): $(DEPS_DIR)/$(2) ;
  742. endef
  743. $(foreach p,$(DEP_PLUGINS),\
  744. $(eval $(if $(findstring /,$p),\
  745. $(call core_dep_plugin,$p,$(firstword $(subst /, ,$p))),\
  746. $(call core_dep_plugin,$p/plugins.mk,$p))))
  747. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  748. # This file is part of erlang.mk and subject to the terms of the ISC License.
  749. # Configuration.
  750. DTL_FULL_PATH ?=
  751. DTL_PATH ?= templates/
  752. DTL_SUFFIX ?= _dtl
  753. DTL_OPTS ?=
  754. # Verbosity.
  755. dtl_verbose_0 = @echo " DTL " $(filter %.dtl,$(?F));
  756. dtl_verbose = $(dtl_verbose_$(V))
  757. # Core targets.
  758. DTL_FILES = $(sort $(call core_find,$(DTL_PATH),*.dtl))
  759. ifneq ($(DTL_FILES),)
  760. ifdef DTL_FULL_PATH
  761. BEAM_FILES += $(addprefix ebin/,$(patsubst %.dtl,%_dtl.beam,$(subst /,_,$(DTL_FILES:$(DTL_PATH)%=%))))
  762. else
  763. BEAM_FILES += $(addprefix ebin/,$(patsubst %.dtl,%_dtl.beam,$(notdir $(DTL_FILES))))
  764. endif
  765. # Rebuild templates when the Makefile changes.
  766. $(DTL_FILES): $(MAKEFILE_LIST)
  767. @touch $@
  768. define erlydtl_compile.erl
  769. [begin
  770. Module0 = case "$(strip $(DTL_FULL_PATH))" of
  771. "" ->
  772. filename:basename(F, ".dtl");
  773. _ ->
  774. "$(DTL_PATH)" ++ F2 = filename:rootname(F, ".dtl"),
  775. re:replace(F2, "/", "_", [{return, list}, global])
  776. end,
  777. Module = list_to_atom(string:to_lower(Module0) ++ "$(DTL_SUFFIX)"),
  778. case erlydtl:compile(F, Module, [$(DTL_OPTS)] ++ [{out_dir, "ebin/"}, return_errors, {doc_root, "templates"}]) of
  779. ok -> ok;
  780. {ok, _} -> ok
  781. end
  782. end || F <- string:tokens("$(1)", " ")],
  783. halt().
  784. endef
  785. ebin/$(PROJECT).app:: $(DTL_FILES) | ebin/
  786. $(if $(strip $?),\
  787. $(dtl_verbose) $(call erlang,$(call erlydtl_compile.erl,$?),-pa ebin/ $(DEPS_DIR)/erlydtl/ebin/))
  788. endif
  789. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  790. # This file is part of erlang.mk and subject to the terms of the ISC License.
  791. # Verbosity.
  792. proto_verbose_0 = @echo " PROTO " $(filter %.proto,$(?F));
  793. proto_verbose = $(proto_verbose_$(V))
  794. # Core targets.
  795. define compile_proto
  796. $(verbose) mkdir -p ebin/ include/
  797. $(proto_verbose) $(call erlang,$(call compile_proto.erl,$(1)))
  798. $(proto_verbose) erlc +debug_info -o ebin/ ebin/*.erl
  799. $(verbose) rm ebin/*.erl
  800. endef
  801. define compile_proto.erl
  802. [begin
  803. Dir = filename:dirname(filename:dirname(F)),
  804. protobuffs_compile:generate_source(F,
  805. [{output_include_dir, Dir ++ "/include"},
  806. {output_src_dir, Dir ++ "/ebin"}])
  807. end || F <- string:tokens("$(1)", " ")],
  808. halt().
  809. endef
  810. ifneq ($(wildcard src/),)
  811. ebin/$(PROJECT).app:: $(sort $(call core_find,src/,*.proto))
  812. $(if $(strip $?),$(call compile_proto,$?))
  813. endif
  814. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  815. # This file is part of erlang.mk and subject to the terms of the ISC License.
  816. .PHONY: clean-app
  817. # Configuration.
  818. ERLC_OPTS ?= -Werror +debug_info +warn_export_vars +warn_shadow_vars \
  819. +warn_obsolete_guard # +bin_opt_info +warn_export_all +warn_missing_spec
  820. COMPILE_FIRST ?=
  821. COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
  822. ERLC_EXCLUDE ?=
  823. ERLC_EXCLUDE_PATHS = $(addprefix src/,$(addsuffix .erl,$(ERLC_EXCLUDE)))
  824. ERLC_MIB_OPTS ?=
  825. COMPILE_MIB_FIRST ?=
  826. COMPILE_MIB_FIRST_PATHS = $(addprefix mibs/,$(addsuffix .mib,$(COMPILE_MIB_FIRST)))
  827. # Verbosity.
  828. app_verbose_0 = @echo " APP " $(PROJECT);
  829. app_verbose_2 = set -x;
  830. app_verbose = $(app_verbose_$(V))
  831. appsrc_verbose_0 = @echo " APP " $(PROJECT).app.src;
  832. appsrc_verbose_2 = set -x;
  833. appsrc_verbose = $(appsrc_verbose_$(V))
  834. makedep_verbose_0 = @echo " DEPEND" $(PROJECT).d;
  835. makedep_verbose_2 = set -x;
  836. makedep_verbose = $(makedep_verbose_$(V))
  837. erlc_verbose_0 = @echo " ERLC " $(filter-out $(patsubst %,%.erl,$(ERLC_EXCLUDE)),\
  838. $(filter %.erl %.core,$(?F)));
  839. erlc_verbose_2 = set -x;
  840. erlc_verbose = $(erlc_verbose_$(V))
  841. xyrl_verbose_0 = @echo " XYRL " $(filter %.xrl %.yrl,$(?F));
  842. xyrl_verbose_2 = set -x;
  843. xyrl_verbose = $(xyrl_verbose_$(V))
  844. asn1_verbose_0 = @echo " ASN1 " $(filter %.asn1,$(?F));
  845. asn1_verbose_2 = set -x;
  846. asn1_verbose = $(asn1_verbose_$(V))
  847. mib_verbose_0 = @echo " MIB " $(filter %.bin %.mib,$(?F));
  848. mib_verbose_2 = set -x;
  849. mib_verbose = $(mib_verbose_$(V))
  850. ifneq ($(wildcard src/),)
  851. # Targets.
  852. ifeq ($(wildcard ebin/test),)
  853. app:: deps $(PROJECT).d
  854. $(verbose) $(MAKE) --no-print-directory app-build
  855. else
  856. app:: clean deps $(PROJECT).d
  857. $(verbose) $(MAKE) --no-print-directory app-build
  858. endif
  859. ifeq ($(wildcard src/$(PROJECT_MOD).erl),)
  860. define app_file
  861. {application, $(PROJECT), [
  862. {description, "$(PROJECT_DESCRIPTION)"},
  863. {vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP),
  864. {id$(comma)$(space)"$(1)"}$(comma))
  865. {modules, [$(call comma_list,$(2))]},
  866. {registered, []},
  867. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(DEPS))]}
  868. ]}.
  869. endef
  870. else
  871. define app_file
  872. {application, $(PROJECT), [
  873. {description, "$(PROJECT_DESCRIPTION)"},
  874. {vsn, "$(PROJECT_VERSION)"},$(if $(IS_DEP),
  875. {id$(comma)$(space)"$(1)"}$(comma))
  876. {modules, [$(call comma_list,$(2))]},
  877. {registered, [$(call comma_list,$(PROJECT)_sup $(PROJECT_REGISTERED))]},
  878. {applications, [$(call comma_list,kernel stdlib $(OTP_DEPS) $(LOCAL_DEPS) $(DEPS))]},
  879. {mod, {$(PROJECT_MOD), []}}
  880. ]}.
  881. endef
  882. endif
  883. app-build: ebin/$(PROJECT).app
  884. $(verbose) :
  885. # Source files.
  886. ERL_FILES = $(sort $(call core_find,src/,*.erl))
  887. CORE_FILES = $(sort $(call core_find,src/,*.core))
  888. # ASN.1 files.
  889. ifneq ($(wildcard asn1/),)
  890. ASN1_FILES = $(sort $(call core_find,asn1/,*.asn1))
  891. ERL_FILES += $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
  892. define compile_asn1
  893. $(verbose) mkdir -p include/
  894. $(asn1_verbose) erlc -v -I include/ -o asn1/ +noobj $(1)
  895. $(verbose) mv asn1/*.erl src/
  896. $(verbose) mv asn1/*.hrl include/
  897. $(verbose) mv asn1/*.asn1db include/
  898. endef
  899. $(PROJECT).d:: $(ASN1_FILES)
  900. $(if $(strip $?),$(call compile_asn1,$?))
  901. endif
  902. # SNMP MIB files.
  903. ifneq ($(wildcard mibs/),)
  904. MIB_FILES = $(sort $(call core_find,mibs/,*.mib))
  905. $(PROJECT).d:: $(COMPILE_MIB_FIRST_PATHS) $(MIB_FILES)
  906. $(verbose) mkdir -p include/ priv/mibs/
  907. $(mib_verbose) erlc -v $(ERLC_MIB_OPTS) -o priv/mibs/ -I priv/mibs/ $?
  908. $(mib_verbose) erlc -o include/ -- $(addprefix priv/mibs/,$(patsubst %.mib,%.bin,$(notdir $?)))
  909. endif
  910. # Leex and Yecc files.
  911. XRL_FILES = $(sort $(call core_find,src/,*.xrl))
  912. XRL_ERL_FILES = $(addprefix src/,$(patsubst %.xrl,%.erl,$(notdir $(XRL_FILES))))
  913. ERL_FILES += $(XRL_ERL_FILES)
  914. YRL_FILES = $(sort $(call core_find,src/,*.yrl))
  915. YRL_ERL_FILES = $(addprefix src/,$(patsubst %.yrl,%.erl,$(notdir $(YRL_FILES))))
  916. ERL_FILES += $(YRL_ERL_FILES)
  917. $(PROJECT).d:: $(XRL_FILES) $(YRL_FILES)
  918. $(if $(strip $?),$(xyrl_verbose) erlc -v -o src/ $?)
  919. # Erlang and Core Erlang files.
  920. define makedep.erl
  921. E = ets:new(makedep, [bag]),
  922. G = digraph:new([acyclic]),
  923. ErlFiles = lists:usort(string:tokens("$(ERL_FILES)", " ")),
  924. Modules = [{list_to_atom(filename:basename(F, ".erl")), F} || F <- ErlFiles],
  925. Add = fun (Mod, Dep) ->
  926. case lists:keyfind(Dep, 1, Modules) of
  927. false -> ok;
  928. {_, DepFile} ->
  929. {_, ModFile} = lists:keyfind(Mod, 1, Modules),
  930. ets:insert(E, {ModFile, DepFile}),
  931. digraph:add_vertex(G, Mod),
  932. digraph:add_vertex(G, Dep),
  933. digraph:add_edge(G, Mod, Dep)
  934. end
  935. end,
  936. AddHd = fun (F, Mod, DepFile) ->
  937. case file:open(DepFile, [read]) of
  938. {error, enoent} -> ok;
  939. {ok, Fd} ->
  940. F(F, Fd, Mod),
  941. {_, ModFile} = lists:keyfind(Mod, 1, Modules),
  942. ets:insert(E, {ModFile, DepFile})
  943. end
  944. end,
  945. Attr = fun
  946. (F, Mod, behavior, Dep) -> Add(Mod, Dep);
  947. (F, Mod, behaviour, Dep) -> Add(Mod, Dep);
  948. (F, Mod, compile, {parse_transform, Dep}) -> Add(Mod, Dep);
  949. (F, Mod, compile, Opts) when is_list(Opts) ->
  950. case proplists:get_value(parse_transform, Opts) of
  951. undefined -> ok;
  952. Dep -> Add(Mod, Dep)
  953. end;
  954. (F, Mod, include, Hrl) ->
  955. case filelib:is_file("include/" ++ Hrl) of
  956. true -> AddHd(F, Mod, "include/" ++ Hrl);
  957. false ->
  958. case filelib:is_file("src/" ++ Hrl) of
  959. true -> AddHd(F, Mod, "src/" ++ Hrl);
  960. false -> false
  961. end
  962. end;
  963. (F, Mod, include_lib, "$1/include/" ++ Hrl) -> AddHd(F, Mod, "include/" ++ Hrl);
  964. (F, Mod, include_lib, Hrl) -> AddHd(F, Mod, "include/" ++ Hrl);
  965. (F, Mod, import, {Imp, _}) ->
  966. case filelib:is_file("src/" ++ atom_to_list(Imp) ++ ".erl") of
  967. false -> ok;
  968. true -> Add(Mod, Imp)
  969. end;
  970. (_, _, _, _) -> ok
  971. end,
  972. MakeDepend = fun(F, Fd, Mod) ->
  973. case io:parse_erl_form(Fd, undefined) of
  974. {ok, {attribute, _, Key, Value}, _} ->
  975. Attr(F, Mod, Key, Value),
  976. F(F, Fd, Mod);
  977. {eof, _} ->
  978. file:close(Fd);
  979. _ ->
  980. F(F, Fd, Mod)
  981. end
  982. end,
  983. [begin
  984. Mod = list_to_atom(filename:basename(F, ".erl")),
  985. {ok, Fd} = file:open(F, [read]),
  986. MakeDepend(MakeDepend, Fd, Mod)
  987. end || F <- ErlFiles],
  988. Depend = sofs:to_external(sofs:relation_to_family(sofs:relation(ets:tab2list(E)))),
  989. CompileFirst = [X || X <- lists:reverse(digraph_utils:topsort(G)), [] =/= digraph:in_neighbours(G, X)],
  990. ok = file:write_file("$(1)", [
  991. [[F, "::", [[" ", D] || D <- Deps], "; @touch \$$@\n"] || {F, Deps} <- Depend],
  992. "\nCOMPILE_FIRST +=", [[" ", atom_to_list(CF)] || CF <- CompileFirst], "\n"
  993. ]),
  994. halt()
  995. endef
  996. ifeq ($(if $(NO_MAKEDEP),$(wildcard $(PROJECT).d),),)
  997. $(PROJECT).d:: $(ERL_FILES) $(call core_find,include/,*.hrl)
  998. $(makedep_verbose) $(call erlang,$(call makedep.erl,$@))
  999. endif
  1000. # Rebuild everything when the Makefile changes.
  1001. $(ERL_FILES) $(CORE_FILES) $(ASN1_FILES) $(MIB_FILES) $(XRL_FILES) $(YRL_FILES):: $(MAKEFILE_LIST)
  1002. @touch $@
  1003. -include $(PROJECT).d
  1004. ebin/$(PROJECT).app:: ebin/
  1005. ebin/:
  1006. $(verbose) mkdir -p ebin/
  1007. define compile_erl
  1008. $(erlc_verbose) erlc -v $(if $(IS_DEP),$(filter-out -Werror,$(ERLC_OPTS)),$(ERLC_OPTS)) -o ebin/ \
  1009. -pa ebin/ -I include/ $(filter-out $(ERLC_EXCLUDE_PATHS),$(COMPILE_FIRST_PATHS) $(1))
  1010. endef
  1011. ebin/$(PROJECT).app:: $(ERL_FILES) $(CORE_FILES) $(wildcard src/$(PROJECT).app.src)
  1012. $(eval FILES_TO_COMPILE := $(filter-out src/$(PROJECT).app.src,$?))
  1013. $(if $(strip $(FILES_TO_COMPILE)),$(call compile_erl,$(FILES_TO_COMPILE)))
  1014. $(eval GITDESCRIBE := $(shell git describe --dirty --abbrev=7 --tags --always --first-parent 2>/dev/null || true))
  1015. $(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename \
  1016. $(filter-out $(ERLC_EXCLUDE_PATHS),$(ERL_FILES) $(CORE_FILES) $(BEAM_FILES)))))))
  1017. ifeq ($(wildcard src/$(PROJECT).app.src),)
  1018. $(app_verbose) printf "$(subst $(newline),\n,$(subst ",\",$(call app_file,$(GITDESCRIBE),$(MODULES))))" \
  1019. > ebin/$(PROJECT).app
  1020. else
  1021. $(verbose) if [ -z "$$(grep -e '^[^%]*{\s*modules\s*,' src/$(PROJECT).app.src)" ]; then \
  1022. echo "Empty modules entry not found in $(PROJECT).app.src. Please consult the erlang.mk README for instructions." >&2; \
  1023. exit 1; \
  1024. fi
  1025. $(appsrc_verbose) cat src/$(PROJECT).app.src \
  1026. | sed "s/{[[:space:]]*modules[[:space:]]*,[[:space:]]*\[\]}/{modules, \[$(call comma_list,$(MODULES))\]}/" \
  1027. | sed "s/{id,[[:space:]]*\"git\"}/{id, \"$(subst /,\/,$(GITDESCRIBE))\"}/" \
  1028. > ebin/$(PROJECT).app
  1029. endif
  1030. clean:: clean-app
  1031. clean-app:
  1032. $(gen_verbose) rm -rf $(PROJECT).d ebin/ priv/mibs/ $(XRL_ERL_FILES) $(YRL_ERL_FILES) \
  1033. $(addprefix include/,$(patsubst %.mib,%.hrl,$(notdir $(MIB_FILES)))) \
  1034. $(addprefix include/,$(patsubst %.asn1,%.hrl,$(notdir $(ASN1_FILES)))) \
  1035. $(addprefix include/,$(patsubst %.asn1,%.asn1db,$(notdir $(ASN1_FILES)))) \
  1036. $(addprefix src/,$(patsubst %.asn1,%.erl,$(notdir $(ASN1_FILES))))
  1037. endif
  1038. # Copyright (c) 2015, Viktor Söderqvist <viktor@zuiderkwast.se>
  1039. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1040. .PHONY: docs-deps
  1041. # Configuration.
  1042. ALL_DOC_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DOC_DEPS))
  1043. # Targets.
  1044. $(foreach dep,$(DOC_DEPS),$(eval $(call dep_target,$(dep))))
  1045. ifneq ($(SKIP_DEPS),)
  1046. doc-deps:
  1047. else
  1048. doc-deps: $(ALL_DOC_DEPS_DIRS)
  1049. $(verbose) for dep in $(ALL_DOC_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  1050. endif
  1051. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  1052. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1053. .PHONY: rel-deps
  1054. # Configuration.
  1055. ALL_REL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(REL_DEPS))
  1056. # Targets.
  1057. $(foreach dep,$(REL_DEPS),$(eval $(call dep_target,$(dep))))
  1058. ifneq ($(SKIP_DEPS),)
  1059. rel-deps:
  1060. else
  1061. rel-deps: $(ALL_REL_DEPS_DIRS)
  1062. $(verbose) for dep in $(ALL_REL_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
  1063. endif
  1064. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  1065. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1066. .PHONY: test-deps test-dir test-build clean-test-dir
  1067. # Configuration.
  1068. TEST_DIR ?= $(CURDIR)/test
  1069. ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
  1070. TEST_ERLC_OPTS ?= +debug_info +warn_export_vars +warn_shadow_vars +warn_obsolete_guard
  1071. TEST_ERLC_OPTS += -DTEST=1
  1072. # Targets.
  1073. $(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
  1074. ifneq ($(SKIP_DEPS),)
  1075. test-deps:
  1076. else
  1077. test-deps: $(ALL_TEST_DEPS_DIRS)
  1078. $(verbose) for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep IS_DEP=1; done
  1079. endif
  1080. ifneq ($(wildcard $(TEST_DIR)),)
  1081. test-dir:
  1082. $(gen_verbose) erlc -v $(TEST_ERLC_OPTS) -I include/ -o $(TEST_DIR) \
  1083. $(call core_find,$(TEST_DIR)/,*.erl) -pa ebin/
  1084. endif
  1085. ifeq ($(wildcard src),)
  1086. test-build:: ERLC_OPTS=$(TEST_ERLC_OPTS)
  1087. test-build:: clean deps test-deps
  1088. $(verbose) $(MAKE) --no-print-directory test-dir ERLC_OPTS="$(TEST_ERLC_OPTS)"
  1089. else
  1090. ifeq ($(wildcard ebin/test),)
  1091. test-build:: ERLC_OPTS=$(TEST_ERLC_OPTS)
  1092. test-build:: clean deps test-deps $(PROJECT).d
  1093. $(verbose) $(MAKE) --no-print-directory app-build test-dir ERLC_OPTS="$(TEST_ERLC_OPTS)"
  1094. $(gen_verbose) touch ebin/test
  1095. else
  1096. test-build:: ERLC_OPTS=$(TEST_ERLC_OPTS)
  1097. test-build:: deps test-deps $(PROJECT).d
  1098. $(verbose) $(MAKE) --no-print-directory app-build test-dir ERLC_OPTS="$(TEST_ERLC_OPTS)"
  1099. endif
  1100. clean:: clean-test-dir
  1101. clean-test-dir:
  1102. ifneq ($(wildcard $(TEST_DIR)/*.beam),)
  1103. $(gen_verbose) rm -f $(TEST_DIR)/*.beam
  1104. endif
  1105. endif
  1106. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  1107. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1108. .PHONY: rebar.config
  1109. # We strip out -Werror because we don't want to fail due to
  1110. # warnings when used as a dependency.
  1111. compat_prepare_erlc_opts = $(shell echo "$1" | sed 's/, */,/g')
  1112. define compat_convert_erlc_opts
  1113. $(if $(filter-out -Werror,$1),\
  1114. $(if $(findstring +,$1),\
  1115. $(shell echo $1 | cut -b 2-)))
  1116. endef
  1117. define compat_erlc_opts_to_list
  1118. [$(call comma_list,$(foreach o,$(call compat_prepare_erlc_opts,$1),$(call compat_convert_erlc_opts,$o)))]
  1119. endef
  1120. define compat_rebar_config
  1121. {deps, [
  1122. $(call comma_list,$(foreach d,$(DEPS),\
  1123. $(if $(filter hex,$(call dep_fetch,$d)),\
  1124. {$(call dep_name,$d)$(comma)"$(call dep_repo,$d)"},\
  1125. {$(call dep_name,$d)$(comma)".*"$(comma){git,"$(call dep_repo,$d)"$(comma)"$(call dep_commit,$d)"}})))
  1126. ]}.
  1127. {erl_opts, $(call compat_erlc_opts_to_list,$(ERLC_OPTS))}.
  1128. endef
  1129. $(eval _compat_rebar_config = $$(compat_rebar_config))
  1130. $(eval export _compat_rebar_config)
  1131. rebar.config:
  1132. $(gen_verbose) echo "$${_compat_rebar_config}" > rebar.config
  1133. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  1134. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1135. .PHONY: asciidoc asciidoc-guide asciidoc-manual install-asciidoc distclean-asciidoc
  1136. MAN_INSTALL_PATH ?= /usr/local/share/man
  1137. MAN_SECTIONS ?= 3 7
  1138. docs:: asciidoc
  1139. asciidoc: asciidoc-guide asciidoc-manual
  1140. ifeq ($(wildcard doc/src/guide/book.asciidoc),)
  1141. asciidoc-guide:
  1142. else
  1143. asciidoc-guide: distclean-asciidoc doc-deps
  1144. a2x -v -f pdf doc/src/guide/book.asciidoc && mv doc/src/guide/book.pdf doc/guide.pdf
  1145. a2x -v -f chunked doc/src/guide/book.asciidoc && mv doc/src/guide/book.chunked/ doc/html/
  1146. endif
  1147. ifeq ($(wildcard doc/src/manual/*.asciidoc),)
  1148. asciidoc-manual:
  1149. else
  1150. asciidoc-manual: distclean-asciidoc doc-deps
  1151. for f in doc/src/manual/*.asciidoc ; do \
  1152. a2x -v -f manpage $$f ; \
  1153. done
  1154. for s in $(MAN_SECTIONS); do \
  1155. mkdir -p doc/man$$s/ ; \
  1156. mv doc/src/manual/*.$$s doc/man$$s/ ; \
  1157. gzip doc/man$$s/*.$$s ; \
  1158. done
  1159. install-docs:: install-asciidoc
  1160. install-asciidoc: asciidoc-manual
  1161. for s in $(MAN_SECTIONS); do \
  1162. mkdir -p $(MAN_INSTALL_PATH)/man$$s/ ; \
  1163. install -g `id -u` -o `id -g` -m 0644 doc/man$$s/*.gz $(MAN_INSTALL_PATH)/man$$s/ ; \
  1164. done
  1165. endif
  1166. distclean:: distclean-asciidoc
  1167. distclean-asciidoc:
  1168. $(gen_verbose) rm -rf doc/html/ doc/guide.pdf doc/man3/ doc/man7/
  1169. # Copyright (c) 2014-2015, Loïc Hoguin <essen@ninenines.eu>
  1170. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1171. .PHONY: bootstrap bootstrap-lib bootstrap-rel new list-templates
  1172. # Core targets.
  1173. help::
  1174. $(verbose) printf "%s\n" "" \
  1175. "Bootstrap targets:" \
  1176. " bootstrap Generate a skeleton of an OTP application" \
  1177. " bootstrap-lib Generate a skeleton of an OTP library" \
  1178. " bootstrap-rel Generate the files needed to build a release" \
  1179. " new-app in=NAME Create a new local OTP application NAME" \
  1180. " new-lib in=NAME Create a new local OTP library NAME" \
  1181. " new t=TPL n=NAME Generate a module NAME based on the template TPL" \
  1182. " new t=T n=N in=APP Generate a module NAME based on the template TPL in APP" \
  1183. " list-templates List available templates"
  1184. # Bootstrap templates.
  1185. define bs_appsrc
  1186. {application, $p, [
  1187. {description, ""},
  1188. {vsn, "0.1.0"},
  1189. {id, "git"},
  1190. {modules, []},
  1191. {registered, []},
  1192. {applications, [
  1193. kernel,
  1194. stdlib
  1195. ]},
  1196. {mod, {$p_app, []}},
  1197. {env, []}
  1198. ]}.
  1199. endef
  1200. define bs_appsrc_lib
  1201. {application, $p, [
  1202. {description, ""},
  1203. {vsn, "0.1.0"},
  1204. {id, "git"},
  1205. {modules, []},
  1206. {registered, []},
  1207. {applications, [
  1208. kernel,
  1209. stdlib
  1210. ]}
  1211. ]}.
  1212. endef
  1213. # To prevent autocompletion issues with ZSH, we add "include erlang.mk"
  1214. # separately during the actual bootstrap.
  1215. ifdef SP
  1216. define bs_Makefile
  1217. PROJECT = $p
  1218. PROJECT_DESCRIPTION = New project
  1219. PROJECT_VERSION = 0.0.1
  1220. # Whitespace to be used when creating files from templates.
  1221. SP = $(SP)
  1222. endef
  1223. else
  1224. define bs_Makefile
  1225. PROJECT = $p
  1226. PROJECT_DESCRIPTION = New project
  1227. PROJECT_VERSION = 0.0.1
  1228. endef
  1229. endif
  1230. define bs_apps_Makefile
  1231. PROJECT = $p
  1232. PROJECT_DESCRIPTION = New project
  1233. PROJECT_VERSION = 0.0.1
  1234. include $(call core_relpath,$(dir $(ERLANG_MK_FILENAME)),$(APPS_DIR)/app)/erlang.mk
  1235. endef
  1236. define bs_app
  1237. -module($p_app).
  1238. -behaviour(application).
  1239. -export([start/2]).
  1240. -export([stop/1]).
  1241. start(_Type, _Args) ->
  1242. $p_sup:start_link().
  1243. stop(_State) ->
  1244. ok.
  1245. endef
  1246. define bs_relx_config
  1247. {release, {$p_release, "1"}, [$p]}.
  1248. {extended_start_script, true}.
  1249. {sys_config, "rel/sys.config"}.
  1250. {vm_args, "rel/vm.args"}.
  1251. endef
  1252. define bs_sys_config
  1253. [
  1254. ].
  1255. endef
  1256. define bs_vm_args
  1257. -name $p@127.0.0.1
  1258. -setcookie $p
  1259. -heart
  1260. endef
  1261. # Normal templates.
  1262. define tpl_supervisor
  1263. -module($(n)).
  1264. -behaviour(supervisor).
  1265. -export([start_link/0]).
  1266. -export([init/1]).
  1267. start_link() ->
  1268. supervisor:start_link({local, ?MODULE}, ?MODULE, []).
  1269. init([]) ->
  1270. Procs = [],
  1271. {ok, {{one_for_one, 1, 5}, Procs}}.
  1272. endef
  1273. define tpl_gen_server
  1274. -module($(n)).
  1275. -behaviour(gen_server).
  1276. %% API.
  1277. -export([start_link/0]).
  1278. %% gen_server.
  1279. -export([init/1]).
  1280. -export([handle_call/3]).
  1281. -export([handle_cast/2]).
  1282. -export([handle_info/2]).
  1283. -export([terminate/2]).
  1284. -export([code_change/3]).
  1285. -record(state, {
  1286. }).
  1287. %% API.
  1288. -spec start_link() -> {ok, pid()}.
  1289. start_link() ->
  1290. gen_server:start_link(?MODULE, [], []).
  1291. %% gen_server.
  1292. init([]) ->
  1293. {ok, #state{}}.
  1294. handle_call(_Request, _From, State) ->
  1295. {reply, ignored, State}.
  1296. handle_cast(_Msg, State) ->
  1297. {noreply, State}.
  1298. handle_info(_Info, State) ->
  1299. {noreply, State}.
  1300. terminate(_Reason, _State) ->
  1301. ok.
  1302. code_change(_OldVsn, State, _Extra) ->
  1303. {ok, State}.
  1304. endef
  1305. define tpl_module
  1306. -module($(n)).
  1307. -export([]).
  1308. endef
  1309. define tpl_cowboy_http
  1310. -module($(n)).
  1311. -behaviour(cowboy_http_handler).
  1312. -export([init/3]).
  1313. -export([handle/2]).
  1314. -export([terminate/3]).
  1315. -record(state, {
  1316. }).
  1317. init(_, Req, _Opts) ->
  1318. {ok, Req, #state{}}.
  1319. handle(Req, State=#state{}) ->
  1320. {ok, Req2} = cowboy_req:reply(200, Req),
  1321. {ok, Req2, State}.
  1322. terminate(_Reason, _Req, _State) ->
  1323. ok.
  1324. endef
  1325. define tpl_gen_fsm
  1326. -module($(n)).
  1327. -behaviour(gen_fsm).
  1328. %% API.
  1329. -export([start_link/0]).
  1330. %% gen_fsm.
  1331. -export([init/1]).
  1332. -export([state_name/2]).
  1333. -export([handle_event/3]).
  1334. -export([state_name/3]).
  1335. -export([handle_sync_event/4]).
  1336. -export([handle_info/3]).
  1337. -export([terminate/3]).
  1338. -export([code_change/4]).
  1339. -record(state, {
  1340. }).
  1341. %% API.
  1342. -spec start_link() -> {ok, pid()}.
  1343. start_link() ->
  1344. gen_fsm:start_link(?MODULE, [], []).
  1345. %% gen_fsm.
  1346. init([]) ->
  1347. {ok, state_name, #state{}}.
  1348. state_name(_Event, StateData) ->
  1349. {next_state, state_name, StateData}.
  1350. handle_event(_Event, StateName, StateData) ->
  1351. {next_state, StateName, StateData}.
  1352. state_name(_Event, _From, StateData) ->
  1353. {reply, ignored, state_name, StateData}.
  1354. handle_sync_event(_Event, _From, StateName, StateData) ->
  1355. {reply, ignored, StateName, StateData}.
  1356. handle_info(_Info, StateName, StateData) ->
  1357. {next_state, StateName, StateData}.
  1358. terminate(_Reason, _StateName, _StateData) ->
  1359. ok.
  1360. code_change(_OldVsn, StateName, StateData, _Extra) ->
  1361. {ok, StateName, StateData}.
  1362. endef
  1363. define tpl_cowboy_loop
  1364. -module($(n)).
  1365. -behaviour(cowboy_loop_handler).
  1366. -export([init/3]).
  1367. -export([info/3]).
  1368. -export([terminate/3]).
  1369. -record(state, {
  1370. }).
  1371. init(_, Req, _Opts) ->
  1372. {loop, Req, #state{}, 5000, hibernate}.
  1373. info(_Info, Req, State) ->
  1374. {loop, Req, State, hibernate}.
  1375. terminate(_Reason, _Req, _State) ->
  1376. ok.
  1377. endef
  1378. define tpl_cowboy_rest
  1379. -module($(n)).
  1380. -export([init/3]).
  1381. -export([content_types_provided/2]).
  1382. -export([get_html/2]).
  1383. init(_, _Req, _Opts) ->
  1384. {upgrade, protocol, cowboy_rest}.
  1385. content_types_provided(Req, State) ->
  1386. {[{{<<"text">>, <<"html">>, '*'}, get_html}], Req, State}.
  1387. get_html(Req, State) ->
  1388. {<<"<html><body>This is REST!</body></html>">>, Req, State}.
  1389. endef
  1390. define tpl_cowboy_ws
  1391. -module($(n)).
  1392. -behaviour(cowboy_websocket_handler).
  1393. -export([init/3]).
  1394. -export([websocket_init/3]).
  1395. -export([websocket_handle/3]).
  1396. -export([websocket_info/3]).
  1397. -export([websocket_terminate/3]).
  1398. -record(state, {
  1399. }).
  1400. init(_, _, _) ->
  1401. {upgrade, protocol, cowboy_websocket}.
  1402. websocket_init(_, Req, _Opts) ->
  1403. Req2 = cowboy_req:compact(Req),
  1404. {ok, Req2, #state{}}.
  1405. websocket_handle({text, Data}, Req, State) ->
  1406. {reply, {text, Data}, Req, State};
  1407. websocket_handle({binary, Data}, Req, State) ->
  1408. {reply, {binary, Data}, Req, State};
  1409. websocket_handle(_Frame, Req, State) ->
  1410. {ok, Req, State}.
  1411. websocket_info(_Info, Req, State) ->
  1412. {ok, Req, State}.
  1413. websocket_terminate(_Reason, _Req, _State) ->
  1414. ok.
  1415. endef
  1416. define tpl_ranch_protocol
  1417. -module($(n)).
  1418. -behaviour(ranch_protocol).
  1419. -export([start_link/4]).
  1420. -export([init/4]).
  1421. -type opts() :: [].
  1422. -export_type([opts/0]).
  1423. -record(state, {
  1424. socket :: inet:socket(),
  1425. transport :: module()
  1426. }).
  1427. start_link(Ref, Socket, Transport, Opts) ->
  1428. Pid = spawn_link(?MODULE, init, [Ref, Socket, Transport, Opts]),
  1429. {ok, Pid}.
  1430. -spec init(ranch:ref(), inet:socket(), module(), opts()) -> ok.
  1431. init(Ref, Socket, Transport, _Opts) ->
  1432. ok = ranch:accept_ack(Ref),
  1433. loop(#state{socket=Socket, transport=Transport}).
  1434. loop(State) ->
  1435. loop(State).
  1436. endef
  1437. # Plugin-specific targets.
  1438. define render_template
  1439. $(verbose) printf -- '$(subst $(newline),\n,$(subst %,%%,$(subst ','\'',$(subst $(tab),$(WS),$(call $(1))))))\n' > $(2)
  1440. endef
  1441. ifndef WS
  1442. ifdef SP
  1443. WS = $(subst a,,a $(wordlist 1,$(SP),a a a a a a a a a a a a a a a a a a a a))
  1444. else
  1445. WS = $(tab)
  1446. endif
  1447. endif
  1448. bootstrap:
  1449. ifneq ($(wildcard src/),)
  1450. $(error Error: src/ directory already exists)
  1451. endif
  1452. $(eval p := $(PROJECT))
  1453. $(eval n := $(PROJECT)_sup)
  1454. $(call render_template,bs_Makefile,Makefile)
  1455. $(verbose) echo "include erlang.mk" >> Makefile
  1456. $(verbose) mkdir src/
  1457. ifdef LEGACY
  1458. $(call render_template,bs_appsrc,src/$(PROJECT).app.src)
  1459. endif
  1460. $(call render_template,bs_app,src/$(PROJECT)_app.erl)
  1461. $(call render_template,tpl_supervisor,src/$(PROJECT)_sup.erl)
  1462. bootstrap-lib:
  1463. ifneq ($(wildcard src/),)
  1464. $(error Error: src/ directory already exists)
  1465. endif
  1466. $(eval p := $(PROJECT))
  1467. $(call render_template,bs_Makefile,Makefile)
  1468. $(verbose) echo "include erlang.mk" >> Makefile
  1469. $(verbose) mkdir src/
  1470. ifdef LEGACY
  1471. $(call render_template,bs_appsrc_lib,src/$(PROJECT).app.src)
  1472. endif
  1473. bootstrap-rel:
  1474. ifneq ($(wildcard relx.config),)
  1475. $(error Error: relx.config already exists)
  1476. endif
  1477. ifneq ($(wildcard rel/),)
  1478. $(error Error: rel/ directory already exists)
  1479. endif
  1480. $(eval p := $(PROJECT))
  1481. $(call render_template,bs_relx_config,relx.config)
  1482. $(verbose) mkdir rel/
  1483. $(call render_template,bs_sys_config,rel/sys.config)
  1484. $(call render_template,bs_vm_args,rel/vm.args)
  1485. new-app:
  1486. ifndef in
  1487. $(error Usage: $(MAKE) new-app in=APP)
  1488. endif
  1489. ifneq ($(wildcard $(APPS_DIR)/$in),)
  1490. $(error Error: Application $in already exists)
  1491. endif
  1492. $(eval p := $(in))
  1493. $(eval n := $(in)_sup)
  1494. $(verbose) mkdir -p $(APPS_DIR)/$p/src/
  1495. $(call render_template,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile)
  1496. ifdef LEGACY
  1497. $(call render_template,bs_appsrc,$(APPS_DIR)/$p/src/$p.app.src)
  1498. endif
  1499. $(call render_template,bs_app,$(APPS_DIR)/$p/src/$p_app.erl)
  1500. $(call render_template,tpl_supervisor,$(APPS_DIR)/$p/src/$p_sup.erl)
  1501. new-lib:
  1502. ifndef in
  1503. $(error Usage: $(MAKE) new-lib in=APP)
  1504. endif
  1505. ifneq ($(wildcard $(APPS_DIR)/$in),)
  1506. $(error Error: Application $in already exists)
  1507. endif
  1508. $(eval p := $(in))
  1509. $(verbose) mkdir -p $(APPS_DIR)/$p/src/
  1510. $(call render_template,bs_apps_Makefile,$(APPS_DIR)/$p/Makefile)
  1511. ifdef LEGACY
  1512. $(call render_template,bs_appsrc_lib,$(APPS_DIR)/$p/src/$p.app.src)
  1513. endif
  1514. new:
  1515. ifeq ($(wildcard src/)$(in),)
  1516. $(error Error: src/ directory does not exist)
  1517. endif
  1518. ifndef t
  1519. $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP])
  1520. endif
  1521. ifndef tpl_$(t)
  1522. $(error Unknown template)
  1523. endif
  1524. ifndef n
  1525. $(error Usage: $(MAKE) new t=TEMPLATE n=NAME [in=APP])
  1526. endif
  1527. ifdef in
  1528. $(verbose) $(MAKE) -C $(APPS_DIR)/$(in)/ new t=$t n=$n in=
  1529. else
  1530. $(call render_template,tpl_$(t),src/$(n).erl)
  1531. endif
  1532. list-templates:
  1533. $(verbose) echo Available templates: $(sort $(patsubst tpl_%,%,$(filter tpl_%,$(.VARIABLES))))
  1534. # Copyright (c) 2014-2015, Loïc Hoguin <essen@ninenines.eu>
  1535. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1536. .PHONY: clean-c_src distclean-c_src-env
  1537. # Configuration.
  1538. C_SRC_DIR ?= $(CURDIR)/c_src
  1539. C_SRC_ENV ?= $(C_SRC_DIR)/env.mk
  1540. C_SRC_OUTPUT ?= $(CURDIR)/priv/$(PROJECT)
  1541. C_SRC_TYPE ?= shared
  1542. # System type and C compiler/flags.
  1543. ifeq ($(PLATFORM),msys2)
  1544. C_SRC_OUTPUT_EXECUTABLE_EXTENSION ?= .exe
  1545. C_SRC_OUTPUT_SHARED_EXTENSION ?= .dll
  1546. else
  1547. C_SRC_OUTPUT_EXECUTABLE_EXTENSION ?=
  1548. C_SRC_OUTPUT_SHARED_EXTENSION ?= .so
  1549. endif
  1550. ifeq ($(C_SRC_TYPE),shared)
  1551. C_SRC_OUTPUT_FILE = $(C_SRC_OUTPUT)$(C_SRC_OUTPUT_SHARED_EXTENSION)
  1552. else
  1553. C_SRC_OUTPUT_FILE = $(C_SRC_OUTPUT)$(C_SRC_OUTPUT_EXECUTABLE_EXTENSION)
  1554. endif
  1555. ifeq ($(PLATFORM),msys2)
  1556. # We hardcode the compiler used on MSYS2. The default CC=cc does
  1557. # not produce working code. The "gcc" MSYS2 package also doesn't.
  1558. CC = /mingw64/bin/gcc
  1559. export CC
  1560. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  1561. CXXFLAGS ?= -O3 -finline-functions -Wall
  1562. else ifeq ($(PLATFORM),darwin)
  1563. CC ?= cc
  1564. CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes
  1565. CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
  1566. LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
  1567. else ifeq ($(PLATFORM),freebsd)
  1568. CC ?= cc
  1569. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  1570. CXXFLAGS ?= -O3 -finline-functions -Wall
  1571. else ifeq ($(PLATFORM),linux)
  1572. CC ?= gcc
  1573. CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
  1574. CXXFLAGS ?= -O3 -finline-functions -Wall
  1575. endif
  1576. ifneq ($(PLATFORM),msys2)
  1577. CFLAGS += -fPIC
  1578. CXXFLAGS += -fPIC
  1579. endif
  1580. CFLAGS += -I"$(ERTS_INCLUDE_DIR)" -I"$(ERL_INTERFACE_INCLUDE_DIR)"
  1581. CXXFLAGS += -I"$(ERTS_INCLUDE_DIR)" -I"$(ERL_INTERFACE_INCLUDE_DIR)"
  1582. LDLIBS += -L"$(ERL_INTERFACE_LIB_DIR)" -lerl_interface -lei
  1583. # Verbosity.
  1584. c_verbose_0 = @echo " C " $(?F);
  1585. c_verbose = $(c_verbose_$(V))
  1586. cpp_verbose_0 = @echo " CPP " $(?F);
  1587. cpp_verbose = $(cpp_verbose_$(V))
  1588. link_verbose_0 = @echo " LD " $(@F);
  1589. link_verbose = $(link_verbose_$(V))
  1590. # Targets.
  1591. ifeq ($(wildcard $(C_SRC_DIR)),)
  1592. else ifneq ($(wildcard $(C_SRC_DIR)/Makefile),)
  1593. app:: app-c_src
  1594. test-build:: app-c_src
  1595. app-c_src:
  1596. $(MAKE) -C $(C_SRC_DIR)
  1597. clean::
  1598. $(MAKE) -C $(C_SRC_DIR) clean
  1599. else
  1600. ifeq ($(SOURCES),)
  1601. SOURCES := $(sort $(foreach pat,*.c *.C *.cc *.cpp,$(call core_find,$(C_SRC_DIR)/,$(pat))))
  1602. endif
  1603. OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
  1604. COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
  1605. COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
  1606. app:: $(C_SRC_ENV) $(C_SRC_OUTPUT_FILE)
  1607. test-build:: $(C_SRC_ENV) $(C_SRC_OUTPUT_FILE)
  1608. $(C_SRC_OUTPUT_FILE): $(OBJECTS)
  1609. $(verbose) mkdir -p priv/
  1610. $(link_verbose) $(CC) $(OBJECTS) \
  1611. $(LDFLAGS) $(if $(filter $(C_SRC_TYPE),shared),-shared) $(LDLIBS) \
  1612. -o $(C_SRC_OUTPUT_FILE)
  1613. %.o: %.c
  1614. $(COMPILE_C) $(OUTPUT_OPTION) $<
  1615. %.o: %.cc
  1616. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  1617. %.o: %.C
  1618. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  1619. %.o: %.cpp
  1620. $(COMPILE_CPP) $(OUTPUT_OPTION) $<
  1621. clean:: clean-c_src
  1622. clean-c_src:
  1623. $(gen_verbose) rm -f $(C_SRC_OUTPUT_FILE) $(OBJECTS)
  1624. endif
  1625. ifneq ($(wildcard $(C_SRC_DIR)),)
  1626. $(C_SRC_ENV):
  1627. $(verbose) $(ERL) -eval "file:write_file(\"$(call core_native_path,$(C_SRC_ENV))\", \
  1628. io_lib:format( \
  1629. \"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \
  1630. \"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \
  1631. \"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \
  1632. [code:root_dir(), erlang:system_info(version), \
  1633. code:lib_dir(erl_interface, include), \
  1634. code:lib_dir(erl_interface, lib)])), \
  1635. halt()."
  1636. distclean:: distclean-c_src-env
  1637. distclean-c_src-env:
  1638. $(gen_verbose) rm -f $(C_SRC_ENV)
  1639. -include $(C_SRC_ENV)
  1640. endif
  1641. # Templates.
  1642. define bs_c_nif
  1643. #include "erl_nif.h"
  1644. static int loads = 0;
  1645. static int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
  1646. {
  1647. /* Initialize private data. */
  1648. *priv_data = NULL;
  1649. loads++;
  1650. return 0;
  1651. }
  1652. static int upgrade(ErlNifEnv* env, void** priv_data, void** old_priv_data, ERL_NIF_TERM load_info)
  1653. {
  1654. /* Convert the private data to the new version. */
  1655. *priv_data = *old_priv_data;
  1656. loads++;
  1657. return 0;
  1658. }
  1659. static void unload(ErlNifEnv* env, void* priv_data)
  1660. {
  1661. if (loads == 1) {
  1662. /* Destroy the private data. */
  1663. }
  1664. loads--;
  1665. }
  1666. static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
  1667. {
  1668. if (enif_is_atom(env, argv[0])) {
  1669. return enif_make_tuple2(env,
  1670. enif_make_atom(env, "hello"),
  1671. argv[0]);
  1672. }
  1673. return enif_make_tuple2(env,
  1674. enif_make_atom(env, "error"),
  1675. enif_make_atom(env, "badarg"));
  1676. }
  1677. static ErlNifFunc nif_funcs[] = {
  1678. {"hello", 1, hello}
  1679. };
  1680. ERL_NIF_INIT($n, nif_funcs, load, NULL, upgrade, unload)
  1681. endef
  1682. define bs_erl_nif
  1683. -module($n).
  1684. -export([hello/1]).
  1685. -on_load(on_load/0).
  1686. on_load() ->
  1687. PrivDir = case code:priv_dir(?MODULE) of
  1688. {error, _} ->
  1689. AppPath = filename:dirname(filename:dirname(code:which(?MODULE))),
  1690. filename:join(AppPath, "priv");
  1691. Path ->
  1692. Path
  1693. end,
  1694. erlang:load_nif(filename:join(PrivDir, atom_to_list(?MODULE)), 0).
  1695. hello(_) ->
  1696. erlang:nif_error({not_loaded, ?MODULE}).
  1697. endef
  1698. new-nif:
  1699. ifneq ($(wildcard $(C_SRC_DIR)/$n.c),)
  1700. $(error Error: $(C_SRC_DIR)/$n.c already exists)
  1701. endif
  1702. ifneq ($(wildcard src/$n.erl),)
  1703. $(error Error: src/$n.erl already exists)
  1704. endif
  1705. ifdef in
  1706. $(verbose) $(MAKE) -C $(APPS_DIR)/$(in)/ new-nif n=$n in=
  1707. else
  1708. $(verbose) mkdir -p $(C_SRC_DIR) src/
  1709. $(call render_template,bs_c_nif,$(C_SRC_DIR)/$n.c)
  1710. $(call render_template,bs_erl_nif,src/$n.erl)
  1711. endif
  1712. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  1713. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1714. .PHONY: ci ci-setup distclean-kerl
  1715. KERL ?= $(CURDIR)/kerl
  1716. export KERL
  1717. KERL_URL ?= https://raw.githubusercontent.com/yrashk/kerl/master/kerl
  1718. OTP_GIT ?= https://github.com/erlang/otp
  1719. CI_INSTALL_DIR ?= $(HOME)/erlang
  1720. CI_OTP ?=
  1721. ifeq ($(strip $(CI_OTP)),)
  1722. ci::
  1723. else
  1724. ci:: $(addprefix ci-,$(CI_OTP))
  1725. ci-prepare: $(addprefix $(CI_INSTALL_DIR)/,$(CI_OTP))
  1726. ci-setup::
  1727. ci_verbose_0 = @echo " CI " $(1);
  1728. ci_verbose = $(ci_verbose_$(V))
  1729. define ci_target
  1730. ci-$(1): $(CI_INSTALL_DIR)/$(1)
  1731. $(ci_verbose) \
  1732. PATH="$(CI_INSTALL_DIR)/$(1)/bin:$(PATH)" \
  1733. CI_OTP_RELEASE="$(1)" \
  1734. CT_OPTS="-label $(1)" \
  1735. $(MAKE) clean ci-setup tests
  1736. endef
  1737. $(foreach otp,$(CI_OTP),$(eval $(call ci_target,$(otp))))
  1738. define ci_otp_target
  1739. ifeq ($(wildcard $(CI_INSTALL_DIR)/$(1)),)
  1740. $(CI_INSTALL_DIR)/$(1): $(KERL)
  1741. $(KERL) build git $(OTP_GIT) $(1) $(1)
  1742. $(KERL) install $(1) $(CI_INSTALL_DIR)/$(1)
  1743. endif
  1744. endef
  1745. $(foreach otp,$(CI_OTP),$(eval $(call ci_otp_target,$(otp))))
  1746. $(KERL):
  1747. $(gen_verbose) $(call core_http_get,$(KERL),$(KERL_URL))
  1748. $(verbose) chmod +x $(KERL)
  1749. help::
  1750. $(verbose) printf "%s\n" "" \
  1751. "Continuous Integration targets:" \
  1752. " ci Run '$(MAKE) tests' on all configured Erlang versions." \
  1753. "" \
  1754. "The CI_OTP variable must be defined with the Erlang versions" \
  1755. "that must be tested. For example: CI_OTP = OTP-17.3.4 OTP-17.5.3"
  1756. distclean:: distclean-kerl
  1757. distclean-kerl:
  1758. $(gen_verbose) rm -rf $(KERL)
  1759. endif
  1760. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  1761. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1762. .PHONY: ct apps-ct distclean-ct
  1763. # Configuration.
  1764. CT_OPTS ?=
  1765. ifneq ($(wildcard $(TEST_DIR)),)
  1766. CT_SUITES ?= $(sort $(subst _SUITE.erl,,$(notdir $(call core_find,$(TEST_DIR)/,*_SUITE.erl))))
  1767. else
  1768. CT_SUITES ?=
  1769. endif
  1770. # Core targets.
  1771. tests:: ct
  1772. distclean:: distclean-ct
  1773. help::
  1774. $(verbose) printf "%s\n" "" \
  1775. "Common_test targets:" \
  1776. " ct Run all the common_test suites for this project" \
  1777. "" \
  1778. "All your common_test suites have their associated targets." \
  1779. "A suite named http_SUITE can be ran using the ct-http target."
  1780. # Plugin-specific targets.
  1781. CT_RUN = ct_run \
  1782. -no_auto_compile \
  1783. -noinput \
  1784. -pa $(CURDIR)/ebin $(DEPS_DIR)/*/ebin $(DEPS_DIR)/gen_rpc/_build/dev/lib/*/ebin $(APPS_DIR)/*/ebin $(TEST_DIR) \
  1785. -dir $(TEST_DIR) \
  1786. -logdir $(CURDIR)/logs
  1787. ifeq ($(CT_SUITES),)
  1788. ct: $(if $(IS_APP),,apps-ct)
  1789. else
  1790. ct: test-build $(if $(IS_APP),,apps-ct)
  1791. $(verbose) mkdir -p $(CURDIR)/logs/
  1792. $(gen_verbose) $(CT_RUN) -sname ct_$(PROJECT) -suite $(addsuffix _SUITE,$(CT_SUITES)) $(CT_OPTS)
  1793. endif
  1794. ifneq ($(ALL_APPS_DIRS),)
  1795. define ct_app_target
  1796. apps-ct-$1:
  1797. $(MAKE) -C $1 ct IS_APP=1
  1798. endef
  1799. $(foreach app,$(ALL_APPS_DIRS),$(eval $(call ct_app_target,$(app))))
  1800. apps-ct: test-build $(addprefix apps-ct-,$(ALL_APPS_DIRS))
  1801. endif
  1802. ifndef t
  1803. CT_EXTRA =
  1804. else
  1805. ifeq (,$(findstring :,$t))
  1806. CT_EXTRA = -group $t
  1807. else
  1808. t_words = $(subst :, ,$t)
  1809. CT_EXTRA = -group $(firstword $(t_words)) -case $(lastword $(t_words))
  1810. endif
  1811. endif
  1812. define ct_suite_target
  1813. ct-$(1): test-build
  1814. $(verbose) mkdir -p $(CURDIR)/logs/
  1815. $(gen_verbose) $(CT_RUN) -sname ct_$(PROJECT) -suite $(addsuffix _SUITE,$(1)) $(CT_EXTRA) $(CT_OPTS)
  1816. endef
  1817. $(foreach test,$(CT_SUITES),$(eval $(call ct_suite_target,$(test))))
  1818. distclean-ct:
  1819. $(gen_verbose) rm -rf $(CURDIR)/logs/
  1820. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  1821. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1822. .PHONY: plt distclean-plt dialyze
  1823. # Configuration.
  1824. DIALYZER_PLT ?= $(CURDIR)/.$(PROJECT).plt
  1825. export DIALYZER_PLT
  1826. PLT_APPS ?=
  1827. DIALYZER_DIRS ?= --src -r $(wildcard src) $(ALL_APPS_DIRS)
  1828. DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions -Wunmatched_returns # -Wunderspecs
  1829. # Core targets.
  1830. check:: dialyze
  1831. distclean:: distclean-plt
  1832. help::
  1833. $(verbose) printf "%s\n" "" \
  1834. "Dialyzer targets:" \
  1835. " plt Build a PLT file for this project" \
  1836. " dialyze Analyze the project using Dialyzer"
  1837. # Plugin-specific targets.
  1838. define filter_opts.erl
  1839. Opts = binary:split(<<"$1">>, <<"-">>, [global]),
  1840. Filtered = lists:reverse(lists:foldl(fun
  1841. (O = <<"pa ", _/bits>>, Acc) -> [O|Acc];
  1842. (O = <<"D ", _/bits>>, Acc) -> [O|Acc];
  1843. (O = <<"I ", _/bits>>, Acc) -> [O|Acc];
  1844. (_, Acc) -> Acc
  1845. end, [], Opts)),
  1846. io:format("~s~n", [[["-", O] || O <- Filtered]]),
  1847. halt().
  1848. endef
  1849. $(DIALYZER_PLT): deps app
  1850. $(verbose) dialyzer --build_plt --apps erts kernel stdlib $(PLT_APPS) $(OTP_DEPS) $(LOCAL_DEPS) $(DEPS)
  1851. plt: $(DIALYZER_PLT)
  1852. distclean-plt:
  1853. $(gen_verbose) rm -f $(DIALYZER_PLT)
  1854. ifneq ($(wildcard $(DIALYZER_PLT)),)
  1855. dialyze:
  1856. else
  1857. dialyze: $(DIALYZER_PLT)
  1858. endif
  1859. $(verbose) dialyzer --no_native `$(call erlang,$(call filter_opts.erl,$(ERLC_OPTS)))` $(DIALYZER_DIRS) $(DIALYZER_OPTS)
  1860. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  1861. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1862. .PHONY: distclean-edoc edoc
  1863. # Configuration.
  1864. EDOC_OPTS ?=
  1865. # Core targets.
  1866. ifneq ($(wildcard doc/overview.edoc),)
  1867. docs:: edoc
  1868. endif
  1869. distclean:: distclean-edoc
  1870. # Plugin-specific targets.
  1871. edoc: distclean-edoc doc-deps
  1872. $(gen_verbose) $(ERL) -eval 'edoc:application($(PROJECT), ".", [$(EDOC_OPTS)]), halt().'
  1873. distclean-edoc:
  1874. $(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
  1875. # Copyright (c) 2014 Dave Cottlehuber <dch@skunkwerks.at>
  1876. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1877. .PHONY: distclean-escript escript
  1878. # Configuration.
  1879. ESCRIPT_NAME ?= $(PROJECT)
  1880. ESCRIPT_FILE ?= $(ESCRIPT_NAME)
  1881. ESCRIPT_COMMENT ?= This is an -*- erlang -*- file
  1882. ESCRIPT_BEAMS ?= "ebin/*", "deps/*/ebin/*"
  1883. ESCRIPT_SYS_CONFIG ?= "rel/sys.config"
  1884. ESCRIPT_EMU_ARGS ?= -pa . \
  1885. -sasl errlog_type error \
  1886. -escript main $(ESCRIPT_NAME)
  1887. ESCRIPT_SHEBANG ?= /usr/bin/env escript
  1888. ESCRIPT_STATIC ?= "deps/*/priv/**", "priv/**"
  1889. # Core targets.
  1890. distclean:: distclean-escript
  1891. help::
  1892. $(verbose) printf "%s\n" "" \
  1893. "Escript targets:" \
  1894. " escript Build an executable escript archive" \
  1895. # Plugin-specific targets.
  1896. # Based on https://github.com/synrc/mad/blob/master/src/mad_bundle.erl
  1897. # Copyright (c) 2013 Maxim Sokhatsky, Synrc Research Center
  1898. # Modified MIT License, https://github.com/synrc/mad/blob/master/LICENSE :
  1899. # Software may only be used for the great good and the true happiness of all
  1900. # sentient beings.
  1901. define ESCRIPT_RAW
  1902. 'Read = fun(F) -> {ok, B} = file:read_file(filename:absname(F)), B end,'\
  1903. 'Files = fun(L) -> A = lists:concat([filelib:wildcard(X)||X<- L ]),'\
  1904. ' [F || F <- A, not filelib:is_dir(F) ] end,'\
  1905. 'Squash = fun(L) -> [{filename:basename(F), Read(F) } || F <- L ] end,'\
  1906. 'Zip = fun(A, L) -> {ok,{_,Z}} = zip:create(A, L, [{compress,all},memory]), Z end,'\
  1907. 'Ez = fun(Escript) ->'\
  1908. ' Static = Files([$(ESCRIPT_STATIC)]),'\
  1909. ' Beams = Squash(Files([$(ESCRIPT_BEAMS), $(ESCRIPT_SYS_CONFIG)])),'\
  1910. ' Archive = Beams ++ [{ "static.gz", Zip("static.gz", Static)}],'\
  1911. ' escript:create(Escript, [ $(ESCRIPT_OPTIONS)'\
  1912. ' {archive, Archive, [memory]},'\
  1913. ' {shebang, "$(ESCRIPT_SHEBANG)"},'\
  1914. ' {comment, "$(ESCRIPT_COMMENT)"},'\
  1915. ' {emu_args, " $(ESCRIPT_EMU_ARGS)"}'\
  1916. ' ]),'\
  1917. ' file:change_mode(Escript, 8#755)'\
  1918. 'end,'\
  1919. 'Ez("$(ESCRIPT_FILE)"),'\
  1920. 'halt().'
  1921. endef
  1922. ESCRIPT_COMMAND = $(subst ' ',,$(ESCRIPT_RAW))
  1923. escript:: distclean-escript deps app
  1924. $(gen_verbose) $(ERL) -eval $(ESCRIPT_COMMAND)
  1925. distclean-escript:
  1926. $(gen_verbose) rm -f $(ESCRIPT_NAME)
  1927. # Copyright (c) 2014, Enrique Fernandez <enrique.fernandez@erlang-solutions.com>
  1928. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  1929. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  1930. .PHONY: eunit apps-eunit
  1931. # Configuration
  1932. EUNIT_OPTS ?=
  1933. EUNIT_ERL_OPTS ?=
  1934. # Core targets.
  1935. tests:: eunit
  1936. help::
  1937. $(verbose) printf "%s\n" "" \
  1938. "EUnit targets:" \
  1939. " eunit Run all the EUnit tests for this project"
  1940. # Plugin-specific targets.
  1941. define eunit.erl
  1942. case "$(COVER)" of
  1943. "" -> ok;
  1944. _ ->
  1945. case cover:compile_beam_directory("ebin") of
  1946. {error, _} -> halt(1);
  1947. _ -> ok
  1948. end
  1949. end,
  1950. case eunit:test($1, [$(EUNIT_OPTS)]) of
  1951. ok -> ok;
  1952. error -> halt(2)
  1953. end,
  1954. case "$(COVER)" of
  1955. "" -> ok;
  1956. _ ->
  1957. cover:export("eunit.coverdata")
  1958. end,
  1959. halt()
  1960. endef
  1961. EUNIT_ERL_OPTS += -pa $(TEST_DIR) $(DEPS_DIR)/*/ebin $(APPS_DIR)/*/ebin $(CURDIR)/ebin
  1962. ifdef t
  1963. ifeq (,$(findstring :,$(t)))
  1964. eunit: test-build
  1965. $(gen_verbose) $(call erlang,$(call eunit.erl,['$(t)']),$(EUNIT_ERL_OPTS))
  1966. else
  1967. eunit: test-build
  1968. $(gen_verbose) $(call erlang,$(call eunit.erl,fun $(t)/0),$(EUNIT_ERL_OPTS))
  1969. endif
  1970. else
  1971. EUNIT_EBIN_MODS = $(notdir $(basename $(ERL_FILES) $(BEAM_FILES)))
  1972. EUNIT_TEST_MODS = $(notdir $(basename $(call core_find,$(TEST_DIR)/,*.erl)))
  1973. EUNIT_MODS = $(foreach mod,$(EUNIT_EBIN_MODS) $(filter-out \
  1974. $(patsubst %,%_tests,$(EUNIT_EBIN_MODS)),$(EUNIT_TEST_MODS)),'$(mod)')
  1975. eunit: test-build $(if $(IS_APP),,apps-eunit)
  1976. $(gen_verbose) $(call erlang,$(call eunit.erl,[$(call comma_list,$(EUNIT_MODS))]),$(EUNIT_ERL_OPTS))
  1977. ifneq ($(ALL_APPS_DIRS),)
  1978. apps-eunit:
  1979. $(verbose) for app in $(ALL_APPS_DIRS); do $(MAKE) -C $$app eunit IS_APP=1; done
  1980. endif
  1981. endif
  1982. # Copyright (c) 2013-2015, Loïc Hoguin <essen@ninenines.eu>
  1983. # This file is part of erlang.mk and subject to the terms of the ISC License.
  1984. .PHONY: relx-rel distclean-relx-rel distclean-relx run
  1985. # Configuration.
  1986. RELX ?= $(CURDIR)/relx
  1987. RELX_CONFIG ?= $(CURDIR)/relx.config
  1988. RELX_URL ?= https://github.com/erlware/relx/releases/download/v3.19.0/relx
  1989. RELX_OPTS ?=
  1990. RELX_OUTPUT_DIR ?= _rel
  1991. ifeq ($(firstword $(RELX_OPTS)),-o)
  1992. RELX_OUTPUT_DIR = $(word 2,$(RELX_OPTS))
  1993. else
  1994. RELX_OPTS += -o $(RELX_OUTPUT_DIR)
  1995. endif
  1996. # Core targets.
  1997. ifeq ($(IS_DEP),)
  1998. ifneq ($(wildcard $(RELX_CONFIG)),)
  1999. rel:: relx-rel
  2000. endif
  2001. endif
  2002. distclean:: distclean-relx-rel distclean-relx
  2003. # Plugin-specific targets.
  2004. $(RELX):
  2005. $(gen_verbose) $(call core_http_get,$(RELX),$(RELX_URL))
  2006. $(verbose) chmod +x $(RELX)
  2007. relx-rel: $(RELX) rel-deps app
  2008. $(verbose) $(RELX) -c $(RELX_CONFIG) $(RELX_OPTS)
  2009. distclean-relx-rel:
  2010. $(gen_verbose) rm -rf $(RELX_OUTPUT_DIR)
  2011. distclean-relx:
  2012. $(gen_verbose) rm -rf $(RELX)
  2013. # Run target.
  2014. ifeq ($(wildcard $(RELX_CONFIG)),)
  2015. run:
  2016. else
  2017. define get_relx_release.erl
  2018. {ok, Config} = file:consult("$(RELX_CONFIG)"),
  2019. {release, {Name, _}, _} = lists:keyfind(release, 1, Config),
  2020. io:format("~s", [Name]),
  2021. halt(0).
  2022. endef
  2023. RELX_RELEASE = `$(call erlang,$(get_relx_release.erl))`
  2024. run: all
  2025. $(verbose) $(RELX_OUTPUT_DIR)/$(RELX_RELEASE)/bin/$(RELX_RELEASE) console
  2026. help::
  2027. $(verbose) printf "%s\n" "" \
  2028. "Relx targets:" \
  2029. " run Compile the project, build the release and run it"
  2030. endif
  2031. # Copyright (c) 2014, M Robert Martin <rob@version2beta.com>
  2032. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  2033. # This file is contributed to erlang.mk and subject to the terms of the ISC License.
  2034. .PHONY: shell
  2035. # Configuration.
  2036. SHELL_ERL ?= erl
  2037. SHELL_PATHS ?= $(CURDIR)/ebin $(APPS_DIR)/*/ebin $(DEPS_DIR)/*/ebin
  2038. SHELL_OPTS ?=
  2039. ALL_SHELL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(SHELL_DEPS))
  2040. # Core targets
  2041. help::
  2042. $(verbose) printf "%s\n" "" \
  2043. "Shell targets:" \
  2044. " shell Run an erlang shell with SHELL_OPTS or reasonable default"
  2045. # Plugin-specific targets.
  2046. $(foreach dep,$(SHELL_DEPS),$(eval $(call dep_target,$(dep))))
  2047. build-shell-deps: $(ALL_SHELL_DEPS_DIRS)
  2048. $(verbose) for dep in $(ALL_SHELL_DEPS_DIRS) ; do $(MAKE) -C $$dep ; done
  2049. shell: build-shell-deps
  2050. $(gen_verbose) $(SHELL_ERL) -pa $(SHELL_PATHS) $(SHELL_OPTS)
  2051. # Copyright (c) 2015, Loïc Hoguin <essen@ninenines.eu>
  2052. # This file is part of erlang.mk and subject to the terms of the ISC License.
  2053. ifeq ($(filter triq,$(DEPS) $(TEST_DEPS)),triq)
  2054. .PHONY: triq
  2055. # Targets.
  2056. tests:: triq
  2057. define triq_check.erl
  2058. code:add_pathsa(["$(CURDIR)/ebin", "$(DEPS_DIR)/*/ebin"]),
  2059. try
  2060. case $(1) of
  2061. all -> [true] =:= lists:usort([triq:check(M) || M <- [$(call comma_list,$(3))]]);
  2062. module -> triq:check($(2));
  2063. function -> triq:check($(2))
  2064. end
  2065. of
  2066. true -> halt(0);
  2067. _ -> halt(1)
  2068. catch error:undef ->
  2069. io:format("Undefined property or module~n"),
  2070. halt(0)
  2071. end.
  2072. endef
  2073. ifdef t
  2074. ifeq (,$(findstring :,$(t)))
  2075. triq: test-build
  2076. $(verbose) $(call erlang,$(call triq_check.erl,module,$(t)))
  2077. else
  2078. triq: test-build
  2079. $(verbose) echo Testing $(t)/0
  2080. $(verbose) $(call erlang,$(call triq_check.erl,function,$(t)()))
  2081. endif
  2082. else
  2083. triq: test-build
  2084. $(eval MODULES := $(patsubst %,'%',$(sort $(notdir $(basename $(wildcard ebin/*.beam))))))
  2085. $(gen_verbose) $(call erlang,$(call triq_check.erl,all,undefined,$(MODULES)))
  2086. endif
  2087. endif
  2088. # Copyright (c) 2015, Erlang Solutions Ltd.
  2089. # This file is part of erlang.mk and subject to the terms of the ISC License.
  2090. .PHONY: xref distclean-xref
  2091. # Configuration.
  2092. ifeq ($(XREF_CONFIG),)
  2093. XREF_ARGS :=
  2094. else
  2095. XREF_ARGS := -c $(XREF_CONFIG)
  2096. endif
  2097. XREFR ?= $(CURDIR)/xrefr
  2098. export XREFR
  2099. XREFR_URL ?= https://github.com/inaka/xref_runner/releases/download/0.2.2/xrefr
  2100. # Core targets.
  2101. help::
  2102. $(verbose) printf "%s\n" "" \
  2103. "Xref targets:" \
  2104. " xref Run Xrefr using $XREF_CONFIG as config file if defined"
  2105. distclean:: distclean-xref
  2106. # Plugin-specific targets.
  2107. $(XREFR):
  2108. $(gen_verbose) $(call core_http_get,$(XREFR),$(XREFR_URL))
  2109. $(verbose) chmod +x $(XREFR)
  2110. xref: deps app $(XREFR)
  2111. $(gen_verbose) $(XREFR) $(XREFR_ARGS)
  2112. distclean-xref:
  2113. $(gen_verbose) rm -rf $(XREFR)
  2114. # Copyright 2015, Viktor Söderqvist <viktor@zuiderkwast.se>
  2115. # This file is part of erlang.mk and subject to the terms of the ISC License.
  2116. COVER_REPORT_DIR = cover
  2117. # Hook in coverage to ct
  2118. ifdef COVER
  2119. ifdef CT_RUN
  2120. # All modules in 'ebin'
  2121. COVER_MODS = $(notdir $(basename $(call core_ls,ebin/*.beam)))
  2122. test-build:: $(TEST_DIR)/ct.cover.spec
  2123. $(TEST_DIR)/ct.cover.spec:
  2124. $(verbose) echo Cover mods: $(COVER_MODS)
  2125. $(gen_verbose) printf "%s\n" \
  2126. '{incl_mods,[$(subst $(space),$(comma),$(COVER_MODS))]}.' \
  2127. '{export,"$(CURDIR)/ct.coverdata"}.' > $@
  2128. CT_RUN += -cover $(TEST_DIR)/ct.cover.spec
  2129. endif
  2130. endif
  2131. # Core targets
  2132. ifdef COVER
  2133. ifneq ($(COVER_REPORT_DIR),)
  2134. tests::
  2135. $(verbose) $(MAKE) --no-print-directory cover-report
  2136. endif
  2137. endif
  2138. clean:: coverdata-clean
  2139. ifneq ($(COVER_REPORT_DIR),)
  2140. distclean:: cover-report-clean
  2141. endif
  2142. help::
  2143. $(verbose) printf "%s\n" "" \
  2144. "Cover targets:" \
  2145. " cover-report Generate a HTML coverage report from previously collected" \
  2146. " cover data." \
  2147. " all.coverdata Merge {eunit,ct}.coverdata into one coverdata file." \
  2148. "" \
  2149. "If COVER=1 is set, coverage data is generated by the targets eunit and ct. The" \
  2150. "target tests additionally generates a HTML coverage report from the combined" \
  2151. "coverdata files from each of these testing tools. HTML reports can be disabled" \
  2152. "by setting COVER_REPORT_DIR to empty."
  2153. # Plugin specific targets
  2154. COVERDATA = $(filter-out all.coverdata,$(wildcard *.coverdata))
  2155. .PHONY: coverdata-clean
  2156. coverdata-clean:
  2157. $(gen_verbose) rm -f *.coverdata ct.cover.spec
  2158. # Merge all coverdata files into one.
  2159. all.coverdata: $(COVERDATA)
  2160. $(gen_verbose) $(ERL) -eval ' \
  2161. $(foreach f,$(COVERDATA),cover:import("$(f)") == ok orelse halt(1),) \
  2162. cover:export("$@"), halt(0).'
  2163. # These are only defined if COVER_REPORT_DIR is non-empty. Set COVER_REPORT_DIR to
  2164. # empty if you want the coverdata files but not the HTML report.
  2165. ifneq ($(COVER_REPORT_DIR),)
  2166. .PHONY: cover-report-clean cover-report
  2167. cover-report-clean:
  2168. $(gen_verbose) rm -rf $(COVER_REPORT_DIR)
  2169. ifeq ($(COVERDATA),)
  2170. cover-report:
  2171. else
  2172. # Modules which include eunit.hrl always contain one line without coverage
  2173. # because eunit defines test/0 which is never called. We compensate for this.
  2174. EUNIT_HRL_MODS = $(subst $(space),$(comma),$(shell \
  2175. grep -e '^\s*-include.*include/eunit\.hrl"' src/*.erl \
  2176. | sed "s/^src\/\(.*\)\.erl:.*/'\1'/" | uniq))
  2177. define cover_report.erl
  2178. $(foreach f,$(COVERDATA),cover:import("$(f)") == ok orelse halt(1),)
  2179. Ms = cover:imported_modules(),
  2180. [cover:analyse_to_file(M, "$(COVER_REPORT_DIR)/" ++ atom_to_list(M)
  2181. ++ ".COVER.html", [html]) || M <- Ms],
  2182. Report = [begin {ok, R} = cover:analyse(M, module), R end || M <- Ms],
  2183. EunitHrlMods = [$(EUNIT_HRL_MODS)],
  2184. Report1 = [{M, {Y, case lists:member(M, EunitHrlMods) of
  2185. true -> N - 1; false -> N end}} || {M, {Y, N}} <- Report],
  2186. TotalY = lists:sum([Y || {_, {Y, _}} <- Report1]),
  2187. TotalN = lists:sum([N || {_, {_, N}} <- Report1]),
  2188. Perc = fun(Y, N) -> case Y + N of 0 -> 100; S -> round(100 * Y / S) end end,
  2189. TotalPerc = Perc(TotalY, TotalN),
  2190. {ok, F} = file:open("$(COVER_REPORT_DIR)/index.html", [write]),
  2191. io:format(F, "<!DOCTYPE html><html>~n"
  2192. "<head><meta charset=\"UTF-8\">~n"
  2193. "<title>Coverage report</title></head>~n"
  2194. "<body>~n", []),
  2195. io:format(F, "<h1>Coverage</h1>~n<p>Total: ~p%</p>~n", [TotalPerc]),
  2196. io:format(F, "<table><tr><th>Module</th><th>Coverage</th></tr>~n", []),
  2197. [io:format(F, "<tr><td><a href=\"~p.COVER.html\">~p</a></td>"
  2198. "<td>~p%</td></tr>~n",
  2199. [M, M, Perc(Y, N)]) || {M, {Y, N}} <- Report1],
  2200. How = "$(subst $(space),$(comma)$(space),$(basename $(COVERDATA)))",
  2201. Date = "$(shell date -u "+%Y-%m-%dT%H:%M:%SZ")",
  2202. io:format(F, "</table>~n"
  2203. "<p>Generated using ~s and erlang.mk on ~s.</p>~n"
  2204. "</body></html>", [How, Date]),
  2205. halt().
  2206. endef
  2207. cover-report:
  2208. $(gen_verbose) mkdir -p $(COVER_REPORT_DIR)
  2209. $(gen_verbose) $(call erlang,$(cover_report.erl))
  2210. endif
  2211. endif # ifneq ($(COVER_REPORT_DIR),)