core.mk 6.7 KB

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