core.mk 6.2 KB

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