core.mk 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # Copyright (c) 2013-2016, 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 = rolling
  17. # Make 3.81 and 3.82 are deprecated.
  18. ifeq ($(MAKE_VERSION),3.81)
  19. $(warning Please upgrade to GNU Make 4 or later: https://erlang.mk/guide/installation.html)
  20. endif
  21. ifeq ($(MAKE_VERSION),3.82)
  22. $(warning Please upgrade to GNU Make 4 or later: https://erlang.mk/guide/installation.html)
  23. endif
  24. # Core configuration.
  25. PROJECT ?= $(notdir $(CURDIR))
  26. PROJECT := $(strip $(PROJECT))
  27. PROJECT_VERSION ?= rolling
  28. PROJECT_MOD ?= $(PROJECT)_app
  29. PROJECT_ENV ?= []
  30. # Verbosity.
  31. V ?= 0
  32. verbose_0 = @
  33. verbose_2 = set -x;
  34. verbose = $(verbose_$(V))
  35. gen_verbose_0 = @echo " GEN " $@;
  36. gen_verbose_2 = set -x;
  37. gen_verbose = $(gen_verbose_$(V))
  38. # Temporary files directory.
  39. ERLANG_MK_TMP ?= $(CURDIR)/.erlang.mk
  40. export ERLANG_MK_TMP
  41. # "erl" command.
  42. ERL = erl +A0 -noinput -boot start_clean
  43. # Platform detection.
  44. ifeq ($(PLATFORM),)
  45. UNAME_S := $(shell uname -s)
  46. ifeq ($(UNAME_S),Linux)
  47. PLATFORM = linux
  48. else ifeq ($(UNAME_S),Darwin)
  49. PLATFORM = darwin
  50. else ifeq ($(UNAME_S),SunOS)
  51. PLATFORM = solaris
  52. else ifeq ($(UNAME_S),GNU)
  53. PLATFORM = gnu
  54. else ifeq ($(UNAME_S),FreeBSD)
  55. PLATFORM = freebsd
  56. else ifeq ($(UNAME_S),NetBSD)
  57. PLATFORM = netbsd
  58. else ifeq ($(UNAME_S),OpenBSD)
  59. PLATFORM = openbsd
  60. else ifeq ($(UNAME_S),DragonFly)
  61. PLATFORM = dragonfly
  62. else ifeq ($(shell uname -o),Msys)
  63. PLATFORM = msys2
  64. else
  65. $(error Unable to detect platform. Please open a ticket with the output of uname -a.)
  66. endif
  67. export PLATFORM
  68. endif
  69. # Core targets.
  70. all:: deps app rel
  71. # Noop to avoid a Make warning when there's nothing to do.
  72. rel::
  73. $(verbose) :
  74. check:: tests
  75. clean:: clean-crashdump
  76. clean-crashdump:
  77. ifneq ($(wildcard erl_crash.dump),)
  78. $(gen_verbose) rm -f erl_crash.dump
  79. endif
  80. distclean:: clean distclean-tmp
  81. distclean-tmp:
  82. $(gen_verbose) rm -rf $(ERLANG_MK_TMP)
  83. help::
  84. $(verbose) printf "%s\n" \
  85. "erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \
  86. "Copyright (c) 2013-2016 Loïc Hoguin <essen@ninenines.eu>" \
  87. "" \
  88. "Usage: [V=1] $(MAKE) [target]..." \
  89. "" \
  90. "Core targets:" \
  91. " all Run deps, app and rel targets in that order" \
  92. " app Compile the project" \
  93. " deps Fetch dependencies (if needed) and compile them" \
  94. " search q=... Search for a package in the built-in index" \
  95. " rel Build a release for this project, if applicable" \
  96. " docs Build the documentation for this project" \
  97. " install-docs Install the man pages for this project" \
  98. " check Compile and run all tests and analysis for this project" \
  99. " tests Run the tests for this project" \
  100. " clean Delete temporary and output files from most targets" \
  101. " distclean Delete all temporary and output files" \
  102. " help Display this help and exit" \
  103. " erlang-mk Update erlang.mk to the latest version"
  104. # Core functions.
  105. empty :=
  106. space := $(empty) $(empty)
  107. tab := $(empty) $(empty)
  108. comma := ,
  109. define newline
  110. endef
  111. define comma_list
  112. $(subst $(space),$(comma),$(strip $(1)))
  113. endef
  114. # Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy.
  115. define erlang
  116. $(ERL) $(2) -pz $(ERLANG_MK_TMP)/rebar/ebin -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
  117. endef
  118. ifeq ($(PLATFORM),msys2)
  119. core_native_path = $(subst \,\\\\,$(shell cygpath -w $1))
  120. else
  121. core_native_path = $1
  122. endif
  123. ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
  124. define core_http_get
  125. wget --no-check-certificate -O $(1) $(2)|| rm $(1)
  126. endef
  127. else
  128. define core_http_get.erl
  129. ssl:start(),
  130. inets:start(),
  131. case httpc:request(get, {"$(2)", []}, [{autoredirect, true}], []) of
  132. {ok, {{_, 200, _}, _, Body}} ->
  133. case file:write_file("$(1)", Body) of
  134. ok -> ok;
  135. {error, R1} -> halt(R1)
  136. end;
  137. {error, R2} ->
  138. halt(R2)
  139. end,
  140. halt(0).
  141. endef
  142. define core_http_get
  143. $(call erlang,$(call core_http_get.erl,$(call core_native_path,$1),$2))
  144. endef
  145. endif
  146. core_eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
  147. core_find = $(if $(wildcard $1),$(shell find $(1:%/=%) -type f -name $(subst *,\*,$2)))
  148. 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)))))))))))))))))))))))))))
  149. core_ls = $(filter-out $(1),$(shell echo $(1)))
  150. # @todo Use a solution that does not require using perl.
  151. core_relpath = $(shell perl -e 'use File::Spec; print File::Spec->abs2rel(@ARGV) . "\n"' $1 $2)
  152. # Automated update.
  153. ERLANG_MK_REPO ?= https://github.com/ninenines/erlang.mk
  154. ERLANG_MK_COMMIT ?=
  155. ERLANG_MK_BUILD_CONFIG ?= build.config
  156. ERLANG_MK_BUILD_DIR ?= .erlang.mk.build
  157. erlang-mk:
  158. git clone $(ERLANG_MK_REPO) $(ERLANG_MK_BUILD_DIR)
  159. ifdef ERLANG_MK_COMMIT
  160. cd $(ERLANG_MK_BUILD_DIR) && git checkout $(ERLANG_MK_COMMIT)
  161. endif
  162. if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR)/build.config; fi
  163. $(MAKE) -C $(ERLANG_MK_BUILD_DIR)
  164. cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
  165. rm -rf $(ERLANG_MK_BUILD_DIR)
  166. # The erlang.mk package index is bundled in the default erlang.mk build.
  167. # Search for the string "copyright" to skip to the rest of the code.