1
|
##### Configuration
|
2
|
|
3
|
remote_user ?= $(USER)
|
4
|
remote_host ?= jupiter
|
5
|
remote_basepath ?= /data/dev/aaronmk/bien
|
6
|
test ?=
|
7
|
|
8
|
##### Vars/functions
|
9
|
|
10
|
# Make
|
11
|
null :=
|
12
|
space := $(null) $(null)
|
13
|
comma := ,
|
14
|
and = $(if $(1),$(2))
|
15
|
or = $(1)$(2)
|
16
|
not = $(if $(1),,1)
|
17
|
require_var = $(if $($(1)),,$(error requires var $$$(1)))
|
18
|
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
|
19
|
topDir = $(word 1,$(pathParts))
|
20
|
subPath = $(word 2,$(pathParts))
|
21
|
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
|
22
|
# input.Makefile path is relative to subdir
|
23
|
|
24
|
# Formatting
|
25
|
sed = sed -$(if $(filter Darwin,$(os)),E,r)
|
26
|
_2Space = $(subst _,$(space),$(1))
|
27
|
*2Space = $(call _2Space,$*)
|
28
|
ucase = $(shell echo "$(1)"|tr "[a-z]" "[A-Z]")
|
29
|
ci = $(1) $(ucase)
|
30
|
|
31
|
# Filesystem
|
32
|
wildcard/ = $(wildcard $(shell bash -c 'shopt -s nullglob; echo $(1)'))
|
33
|
# needed because builtin $(wildcard) doesn't do / suffix correctly
|
34
|
# need final pass with $(wildcard) to support inputs without wildcard chars
|
35
|
wildcard+suffix = $(wildcard/:%=%$(2))
|
36
|
# on Linux, builtin $(wildcard) doesn't handle suffixes that don't exist
|
37
|
no/ = $(1:/=)# remove trailing /
|
38
|
+w = $(wildcard $+)# existing items in $+
|
39
|
CP := cp -p
|
40
|
cp = $(CP) $< $@
|
41
|
mkdir = $(if $(wildcard $(1)),,mkdir -p $(1))
|
42
|
|
43
|
# File editing
|
44
|
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
|
45
|
|
46
|
# System
|
47
|
os := $(shell uname)
|
48
|
isMac := $(filter Darwin,$(os))
|
49
|
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
|
50
|
dateFmt := %-Y-%-m-%-d %-H:%M:%S %Z
|
51
|
date = $(shell date +"$(dateFmt)")
|
52
|
mtime = $(shell $(if $(isMac),stat -f %Sm -t "$(dateFmt)" $(1),date\
|
53
|
--reference=$(1) +"$(dateFmt)"))
|
54
|
nice := nice -n +10 # +5 still leaves the shell sluggish
|
55
|
|
56
|
# Terminal
|
57
|
esc := '['
|
58
|
reset := $(esc)'0m'
|
59
|
emph := $(esc)'7m '
|
60
|
endEmph := ' '$(reset)
|
61
|
|
62
|
# User interaction
|
63
|
|
64
|
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
|
65
|
|
66
|
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
|
67
|
|
68
|
# Requires `SHELL := /bin/bash`
|
69
|
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
|
70
|
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
|
71
|
$(error Aborting))
|
72
|
|
73
|
## SVN
|
74
|
|
75
|
addFile = $(if $(wildcard $(1)),,touch $(1) && )svn add --force $(1)
|
76
|
addDir = $(if $(wildcard $(1)/),svn add --force --depth=empty $(1),svn mkdir $(1))
|
77
|
add* = $(if $(1),svn add --force --depth=empty $(wildcard $(1)))
|
78
|
# $(wildcard __): prevent error "Could not add all targets because some
|
79
|
# targets don't exist"
|
80
|
|
81
|
# Revisions
|
82
|
revision = $(shell svn info $(1)|$(sed) -n 's/^Last Changed Rev: (.*)$$/\1/p')
|
83
|
rootRevision = $(call revision,$(root))
|
84
|
version ?= r$(rootRevision)
|
85
|
|
86
|
# rsync
|
87
|
rsync* := "time" rsync$(if $(test), --dry-run) --archive --no-group --update \
|
88
|
--verbose --itemize-changes --progress
|
89
|
rsync := $(rsync*) --exclude=".svn/" --exclude="*\#" --exclude=".DS_Store"\
|
90
|
--exclude=".lk*"
|
91
|
local := ./
|
92
|
localDirName := $(notdir $(realpath $(local)))
|
93
|
remote := $(remote_user)@$(remote_host):$(remote_basepath)/$(localDirName)/
|
94
|
|
95
|
# DB
|
96
|
postgres := postgres
|
97
|
asAdmin := sudo -E -u $(postgres)
|
98
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
99
|
psqlAsAdmin := $(asAdmin) psql -U postgres $(psqlOpts)
|
100
|
# -E preserves env vars so PGOPTIONS is passed to psql
|
101
|
mkSchemaCmd = 'CREATE SCHEMA "$(1)";'
|
102
|
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
|
103
|
|
104
|
##### General targets
|
105
|
|
106
|
all: # must be first target in Makefile, so add an empty version here
|
107
|
|
108
|
.SUFFIXES: # turn off built-in suffix rules
|
109
|
.SECONDARY: # don't automatically delete intermediate files
|
110
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
111
|
|
112
|
_always:
|
113
|
.PHONY: _always
|
114
|
|
115
|
clean: # make sure `make clean` always works
|
116
|
|
117
|
%/remake: _always
|
118
|
$(MAKE) $(@D)/clean
|
119
|
$(MAKE) $(@D)/
|
120
|
# re-run make so that cache of existing files is reset
|
121
|
|
122
|
%-remake: _always
|
123
|
rm -f $*
|
124
|
$(MAKE) $*
|
125
|
# re-run make so that cache of existing files is reset
|
126
|
|
127
|
%/reinstall: _always %/uninstall %/install ;
|
128
|
|
129
|
%/live: _always
|
130
|
$(MAKE) $* live=1
|
131
|
|
132
|
# Run with `make -s` to avoid echoing make commands
|
133
|
version: _always
|
134
|
echo $(version)
|
135
|
|
136
|
##### Checksums
|
137
|
|
138
|
%.md5: %
|
139
|
$(nice) md5sum $<|cut -d ' ' -f 1 >$@
|
140
|
|
141
|
# Run with `make -s` to avoid echoing make commands
|
142
|
%.md5/test: %.md5 % _always
|
143
|
echo '$(shell cat $<) $*'|$(nice) md5sum -$(if $(isMac),v)c /dev/stdin
|
144
|
|
145
|
##### Compression
|
146
|
|
147
|
define gunzip
|
148
|
gunzip <$< >$@
|
149
|
touch -r $< $@
|
150
|
endef
|
151
|
|
152
|
%:: %.gz
|
153
|
$(if $(wildcard $@),,$(gunzip))
|
154
|
|
155
|
define gzip
|
156
|
gzip <$< >$@
|
157
|
touch -r $< $@
|
158
|
endef
|
159
|
|
160
|
ifneq ($(filter %.gz,$(MAKECMDGOALS)),)
|
161
|
%.gz: %
|
162
|
$(if $(wildcard $@),,$(gzip))
|
163
|
endif
|