core.mk 5.7 KB

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