core.mk 4.3 KB

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