core.mk 5.3 KB

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