core.mk 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. 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. relup:: deps app
  75. check:: tests
  76. clean:: clean-crashdump
  77. clean-crashdump:
  78. ifneq ($(wildcard erl_crash.dump),)
  79. $(gen_verbose) rm -f erl_crash.dump
  80. endif
  81. distclean:: clean distclean-tmp
  82. distclean-tmp:
  83. $(gen_verbose) rm -rf $(ERLANG_MK_TMP)
  84. help::
  85. $(verbose) printf "%s\n" \
  86. "erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \
  87. "Copyright (c) 2013-2016 Loïc Hoguin <essen@ninenines.eu>" \
  88. "" \
  89. "Usage: [V=1] $(MAKE) [target]..." \
  90. "" \
  91. "Core targets:" \
  92. " all Run deps, app and rel targets in that order" \
  93. " app Compile the project" \
  94. " deps Fetch dependencies (if needed) and compile them" \
  95. " fetch-deps Fetch dependencies recursively (if needed) without compiling them" \
  96. " list-deps List dependencies recursively on stdout" \
  97. " search q=... Search for a package in the built-in index" \
  98. " rel Build a release for this project, if applicable" \
  99. " docs Build the documentation for this project" \
  100. " install-docs Install the man pages for this project" \
  101. " check Compile and run all tests and analysis for this project" \
  102. " tests Run the tests for this project" \
  103. " clean Delete temporary and output files from most targets" \
  104. " distclean Delete all temporary and output files" \
  105. " help Display this help and exit" \
  106. " erlang-mk Update erlang.mk to the latest version"
  107. # Core functions.
  108. empty :=
  109. space := $(empty) $(empty)
  110. tab := $(empty) $(empty)
  111. comma := ,
  112. define newline
  113. endef
  114. define comma_list
  115. $(subst $(space),$(comma),$(strip $(1)))
  116. endef
  117. # Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy.
  118. define erlang
  119. $(ERL) $(2) -pz $(ERLANG_MK_TMP)/rebar/ebin -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
  120. endef
  121. ifeq ($(PLATFORM),msys2)
  122. core_native_path = $(subst \,\\\\,$(shell cygpath -w $1))
  123. else
  124. core_native_path = $1
  125. endif
  126. core_http_get = curl -Lf$(if $(filter-out 0,$(V)),,s)o $(call core_native_path,$1) $2
  127. core_eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
  128. core_find = $(if $(wildcard $1),$(shell find $(1:%/=%) -type f -name $(subst *,\*,$2)))
  129. 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)))))))))))))))))))))))))))
  130. core_ls = $(filter-out $(1),$(shell echo $(1)))
  131. # @todo Use a solution that does not require using perl.
  132. core_relpath = $(shell perl -e 'use File::Spec; print File::Spec->abs2rel(@ARGV) . "\n"' $1 $2)
  133. # Automated update.
  134. ERLANG_MK_REPO ?= https://github.com/ninenines/erlang.mk
  135. ERLANG_MK_COMMIT ?=
  136. ERLANG_MK_BUILD_CONFIG ?= build.config
  137. ERLANG_MK_BUILD_DIR ?= .erlang.mk.build
  138. erlang-mk:
  139. git clone $(ERLANG_MK_REPO) $(ERLANG_MK_BUILD_DIR)
  140. ifdef ERLANG_MK_COMMIT
  141. cd $(ERLANG_MK_BUILD_DIR) && git checkout $(ERLANG_MK_COMMIT)
  142. endif
  143. if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR)/build.config; fi
  144. $(MAKE) -C $(ERLANG_MK_BUILD_DIR)
  145. cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
  146. rm -rf $(ERLANG_MK_BUILD_DIR)
  147. # The erlang.mk package index is bundled in the default erlang.mk build.
  148. # Search for the string "copyright" to skip to the rest of the code.