# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.PHONY: all app deps search rel docs install-docs check tests clean distclean help erlang-mk
ERLANG_MK_FILENAME := $(realpath $(lastword $(MAKEFILE_LIST)))
ERLANG_MK_VERSION = 1
# Core configuration.
PROJECT ?= $(notdir $(CURDIR))
PROJECT := $(strip $(PROJECT))
PROJECT_VERSION ?= rolling
# Verbosity.
V ?= 0
verbose_0 = @
verbose = $(verbose_$(V))
gen_verbose_0 = @echo " GEN " $@;
gen_verbose = $(gen_verbose_$(V))
# Temporary files directory.
ERLANG_MK_TMP ?= $(CURDIR)/.erlang.mk
export ERLANG_MK_TMP
# "erl" command.
ERL = erl +A0 -noinput -boot start_clean
# Platform detection.
ifeq ($(PLATFORM),)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
PLATFORM = linux
else ifeq ($(UNAME_S),Darwin)
PLATFORM = darwin
else ifeq ($(UNAME_S),SunOS)
PLATFORM = solaris
else ifeq ($(UNAME_S),GNU)
PLATFORM = gnu
else ifeq ($(UNAME_S),FreeBSD)
PLATFORM = freebsd
else ifeq ($(UNAME_S),NetBSD)
PLATFORM = netbsd
else ifeq ($(UNAME_S),OpenBSD)
PLATFORM = openbsd
else ifeq ($(UNAME_S),DragonFly)
PLATFORM = dragonfly
else ifeq ($(shell uname -o),Msys)
PLATFORM = msys2
else
$(error Unable to detect platform. Please open a ticket with the output of uname -a.)
endif
export PLATFORM
endif
# Core targets.
.NOTPARALLEL:
all:: deps app rel
# Noop to avoid a Make warning when there's nothing to do.
rel::
$(verbose) echo -n
check:: clean app tests
clean:: clean-crashdump
clean-crashdump:
ifneq ($(wildcard erl_crash.dump),)
$(gen_verbose) rm -f erl_crash.dump
endif
distclean:: clean distclean-tmp
distclean-tmp:
$(gen_verbose) rm -rf $(ERLANG_MK_TMP)
help::
$(verbose) printf "%s\n" \
"erlang.mk (version $(ERLANG_MK_VERSION)) is distributed under the terms of the ISC License." \
"Copyright (c) 2013-2015 Loïc Hoguin <[email protected]>" \
"" \
"Usage: [V=1] $(MAKE) [target]..." \
"" \
"Core targets:" \
" all Run deps, app and rel targets in that order" \
" app Compile the project" \
" deps Fetch dependencies (if needed) and compile them" \
" search q=... Search for a package in the built-in index" \
" rel Build a release for this project, if applicable" \
" docs Build the documentation for this project" \
" install-docs Install the man pages for this project" \
" check Compile and run all tests and analysis for this project" \
" tests Run the tests for this project" \
" clean Delete temporary and output files from most targets" \
" distclean Delete all temporary and output files" \
" help Display this help and exit" \
" erlang-mk Update erlang.mk to the latest version"
# Core functions.
empty :=
space := $(empty) $(empty)
tab := $(empty) $(empty)
comma := ,
define newline
endef
define comma_list
$(subst $(space),$(comma),$(strip $(1)))
endef
# Adding erlang.mk to make Erlang scripts who call init:get_plain_arguments() happy.
define erlang
$(ERL) $(2) -pz $(ERLANG_MK_TMP)/rebar/ebin -eval "$(subst $(newline),,$(subst ",\",$(1)))" -- erlang.mk
endef
ifeq ($(PLATFORM),msys2)
core_native_path = $(subst \,\\\\,$(shell cygpath -w $1))
else
core_native_path = $1
endif
ifeq ($(shell which wget 2>/dev/null | wc -l), 1)
define core_http_get
wget --no-check-certificate -O $(1) $(2)|| rm $(1)
endef
else
define core_http_get.erl
ssl:start(),
inets:start(),
case httpc:request(get, {"$(2)", []}, [{autoredirect, true}], []) of
{ok, {{_, 200, _}, _, Body}} ->
case file:write_file("$(1)", Body) of
ok -> ok;
{error, R1} -> halt(R1)
end;
{error, R2} ->
halt(R2)
end,
halt(0).
endef
define core_http_get
$(call erlang,$(call core_http_get.erl,$(call core_native_path,$1),$2))
endef
endif
core_eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
core_find = $(if $(wildcard $1),$(shell find $(1:%/=%) -type f -name $(subst *,\*,$2)))
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)))))))))))))))))))))))))))
# @todo On Windows: $(shell dir /B $(1)); make sure to handle when no file exists.
core_ls = $(filter-out $(1),$(shell echo $(1)))
# Automated update.
ERLANG_MK_REPO ?= https://github.com/ninenines/erlang.mk
ERLANG_MK_COMMIT ?=
ERLANG_MK_BUILD_CONFIG ?= build.config
ERLANG_MK_BUILD_DIR ?= .erlang.mk.build
erlang-mk:
git clone $(ERLANG_MK_REPO) $(ERLANG_MK_BUILD_DIR)
if [ -f $(ERLANG_MK_BUILD_CONFIG) ]; then cp $(ERLANG_MK_BUILD_CONFIG) $(ERLANG_MK_BUILD_DIR)/build.config; fi
cd $(ERLANG_MK_BUILD_DIR) && $(if $(ERLANG_MK_COMMIT),git checkout $(ERLANG_MK_COMMIT) &&) $(MAKE)
cp $(ERLANG_MK_BUILD_DIR)/erlang.mk ./erlang.mk
rm -rf $(ERLANG_MK_BUILD_DIR)
# The erlang.mk package index is bundled in the default erlang.mk build.
# Search for the string "copyright" to skip to the rest of the code.