core.mk 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 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 = 1
  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. # The erlang.mk package index is bundled in the default erlang.mk build.
  159. # Search for the string "copyright" to skip to the rest of the code.