core.mk 5.9 KB

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