|
| 1 | +# Copyright (c) 2013-2015, Loïc Hoguin <[email protected]> |
| 2 | +# Copyright (c) 2016, Grégoire Lejeune |
| 3 | +# |
| 4 | +# Permission to use, copy, modify, and/or distribute this software for any |
| 5 | +# purpose with or without fee is hereby granted, provided that the above |
| 6 | +# copyright notice and this permission notice appear in all copies. |
| 7 | +# |
| 8 | +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + |
| 16 | +.PHONY: doc |
| 17 | + |
| 18 | +all: compile-erl |
| 19 | + |
| 20 | +# Verbosity. |
| 21 | + |
| 22 | +V ?= 0 |
| 23 | + |
| 24 | +verbose_0 = @ |
| 25 | +verbose_2 = set -x; |
| 26 | +verbose = $(verbose_$(V)) |
| 27 | + |
| 28 | +# Utils |
| 29 | + |
| 30 | +mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) |
| 31 | +current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) |
| 32 | + |
| 33 | +# Common |
| 34 | + |
| 35 | +CP = cp |
| 36 | +CP_R = cp -r |
| 37 | +RM = rm |
| 38 | +RM_RF = rm -rf |
| 39 | +RM_F = rm -f |
| 40 | +MKDIR_P = mkdir -p |
| 41 | + |
| 42 | +# Config |
| 43 | + |
| 44 | +NODE_HOST ?= 127.0.0.1 |
| 45 | +ifneq ("$(wildcard config/$(current_dir).config)","") |
| 46 | + ERL_CONFIG="config/$(current_dir).config" |
| 47 | +endif |
| 48 | + |
| 49 | +# Core functions. |
| 50 | + |
| 51 | +empty := |
| 52 | +space := $(empty) $(empty) |
| 53 | +tab := $(empty) $(empty) |
| 54 | +comma := , |
| 55 | + |
| 56 | +define newline |
| 57 | + |
| 58 | + |
| 59 | +endef |
| 60 | + |
| 61 | +# Template |
| 62 | + |
| 63 | +define render_template |
| 64 | + $(verbose) printf -- '$(subst $(newline),\n,$(subst %,%%,$(subst ','\'',$(subst $(tab),$(WS),$(call $(1))))))\n' > $(2) |
| 65 | +endef |
| 66 | + |
| 67 | +# Erlang |
| 68 | + |
| 69 | +ERL = erl +A0 -noinput -boot start_clean |
| 70 | + |
| 71 | +define erlang |
| 72 | +$(ERL) -noshell -s init stop -eval "$(subst $(newline),,$(subst ",\",$(1)))" |
| 73 | +endef |
| 74 | + |
| 75 | +define get_version.erl |
| 76 | + {ok, [{application, _, X}]} = file:consult("$(1)"), |
| 77 | + {vsn, VSN} = lists:keyfind(vsn, 1, X), |
| 78 | + io:format("~s", [VSN]). |
| 79 | +endef |
| 80 | + |
| 81 | +define get_app_name.erl |
| 82 | + {ok, [{application, AppName, _}]} = file:consult("$(1)"), |
| 83 | + io:format("~s", [AppName]). |
| 84 | +endef |
| 85 | + |
| 86 | +ifndef WS |
| 87 | +ifdef SP |
| 88 | +WS = $(subst a,,a $(wordlist 1,$(SP),a a a a a a a a a a a a a a a a a a a a)) |
| 89 | +else |
| 90 | +WS = $(tab) |
| 91 | +endif |
| 92 | +endif |
| 93 | + |
| 94 | +# rebar3 |
| 95 | + |
| 96 | +FIND_REBAR = \ |
| 97 | + REBAR_BIN=; \ |
| 98 | + for x in ./rebar3 rebar3; do \ |
| 99 | + if type "$${x%% *}" >/dev/null 2>/dev/null; then REBAR_BIN=$$x; break; fi; \ |
| 100 | + done; \ |
| 101 | + if [ -z "$$REBAR_BIN" ]; then echo 1>&2 "Unable to find rebar3"; exit 2; fi |
| 102 | +REBAR = $(FIND_REBAR); $$REBAR_BIN |
| 103 | + |
| 104 | +# Project |
| 105 | + |
| 106 | +APP_SRC=$(shell find src -name "*.app.src") |
| 107 | +ifeq ($(APP_SRC),) |
| 108 | +else |
| 109 | + APP_VERSION=$(shell $(call erlang,$(call get_version.erl,${APP_SRC}))) |
| 110 | + APP_NAME=$(shell $(call erlang,$(call get_app_name.erl,${APP_SRC}))) |
| 111 | +endif |
| 112 | + |
| 113 | +# mix |
| 114 | + |
| 115 | +MIX = mix |
| 116 | + |
| 117 | +# Default tasks |
| 118 | +ifeq ($(HAS_ELIXIR), 1) |
| 119 | + |
| 120 | +compile-ex: elixir clean |
| 121 | + $(verbose) $(MIX) deps.get |
| 122 | + $(verbose) $(MIX) deps.compile |
| 123 | + $(verbose) $(MIX) compile |
| 124 | + |
| 125 | +elixir: ## Generate Elixir bindings (mix.exs and libs) |
| 126 | + $(verbose) $(REBAR) elixir generate_mix |
| 127 | + $(verbose) $(REBAR) elixir generate_lib |
| 128 | + |
| 129 | +distclean-ex: clean-ex |
| 130 | + $(verbose) $(RM_F) mix.lock |
| 131 | + |
| 132 | +clean-ex: |
| 133 | + $(verbose) $(RM_RF) _build deps |
| 134 | + |
| 135 | +dist-ex: clean compile-ex |
| 136 | + |
| 137 | +COMPILE=compile-erl compile-ex |
| 138 | +CLEAN=clean-erl clean-ex |
| 139 | +DISTCLEAN=distclean-erl distclean-ex |
| 140 | +DIST=dist-erl dist-ex |
| 141 | +else |
| 142 | +COMPILE=compile-erl |
| 143 | +CLEAN=clean-erl |
| 144 | +DISTCLEAN=distclean-erl |
| 145 | +DIST=dist-erl |
| 146 | +endif |
| 147 | + |
| 148 | +ifdef NO_LINT |
| 149 | +LINT= |
| 150 | +else |
| 151 | +lint: |
| 152 | + $(verbose) $(REBAR) lint |
| 153 | + |
| 154 | +LINT=lint |
| 155 | +endif |
| 156 | + |
| 157 | +compile-erl: |
| 158 | + $(verbose) $(REBAR) update |
| 159 | + $(verbose) $(REBAR) compile |
| 160 | + |
| 161 | +tests: ## Run tests |
| 162 | + $(verbose) $(REBAR) eunit |
| 163 | + |
| 164 | +doc:: ## Generate doc |
| 165 | + $(verbose) $(REBAR) as doc edoc |
| 166 | + |
| 167 | +dist: $(DIST) ## Create a distribution |
| 168 | + |
| 169 | +clean: $(CLEAN) ## Clean |
| 170 | + |
| 171 | +distclean: $(DISTCLEAN) ## Clean the distribution |
| 172 | + |
| 173 | +dev: compile-erl |
| 174 | +ifdef ERL_CONFIG |
| 175 | + $(verbose) echo "Start with configuration $(ERL_CONFIG)" |
| 176 | + $(verbose) erl -pa _build/default/lib/*/ebin _build/default/lib/*/include -config ${ERL_CONFIG} -name ${current_dir}@${NODE_HOST} -setcookie ${current_dir} |
| 177 | +else |
| 178 | + $(verbose) erl -pa _build/default/lib/*/ebin _build/default/lib/*/include -name ${current_dir}@${NODE_HOST} -setcookie ${current_dir} |
| 179 | +endif |
| 180 | + |
| 181 | +dist-erl: clean compile-erl tests $(LINT) doc |
| 182 | + |
| 183 | +clean-erl: |
| 184 | + $(verbose) $(RM_RF) _build test/eunit |
| 185 | + |
| 186 | +distclean-erl: clean-erl |
| 187 | + $(verbose) $(RM_F) rebar.lock |
| 188 | + |
| 189 | +info: ## Display application informations |
| 190 | + $(verbose) echo "App source file: $(APP_SRC)" |
| 191 | + $(verbose) echo "App name: $(APP_NAME)" |
| 192 | + $(verbose) echo "App version: $(APP_VERSION)" |
| 193 | + |
| 194 | +tag: DO_TAG ?= $(shell read -p "tag version $(APP_VERSION) (y/n) [n]: " pwd; echo $$pwd) |
| 195 | + |
| 196 | +tag: ## Create a git tag |
| 197 | + $(verbose) echo $(if $(findstring $(DO_TAG),y,Y,yes,Yes,YES),$(shell git tag $(APP_VERSION)),) |
| 198 | + |
| 199 | +# Elixir |
| 200 | + |
| 201 | +local.hex: ## Install hexfor Mix |
| 202 | + $(MIX) local.hex --force |
| 203 | + |
| 204 | +local.rebar: ## Install rebar for Mix |
| 205 | + $(MIX) local.rebar --force |
| 206 | + |
| 207 | +# Update |
| 208 | + |
| 209 | +BU_MK_REPO ?= https://github.com/botsunit/bu.mk |
| 210 | +BU_MK_COMMIT ?= |
| 211 | +BU_MK_BUILD_DIR ?= .bu.mk.build |
| 212 | + |
| 213 | +bu-mk: ## Update bu.mk |
| 214 | + git clone $(BU_MK_REPO) $(BU_MK_BUILD_DIR) |
| 215 | +ifdef BU_MK_COMMIT |
| 216 | + cd $(BU_MK_BUILD_DIR) && git checkout $(BU_MK_COMMIT) |
| 217 | +endif |
| 218 | + $(CP) $(BU_MK_BUILD_DIR)/bu.mk ./bu.mk |
| 219 | + $(RM_RF) $(BU_MK_BUILD_DIR) |
| 220 | + |
| 221 | +# Help |
| 222 | + |
| 223 | +help: ## Show this help. |
| 224 | + $(verbose) echo "$$(grep -hE '^\S+:.*##' $(MAKEFILE_LIST) | sed -e 's/:.*##\s*/:/' -e 's/^\(.\+\):\(.*\)/\\033[33m\1\\033[m:\2/' | column -c2 -t -s :)" |
| 225 | + |
0 commit comments