core.mk 4.6 KB

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