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/ = $(shell bash -c 'shopt -s nullglob; echo $(1)')
|
33
|
# needed because builtin $(wildcard) doesn't do / suffix correctly
|
34
|
no/ = $(1:/=)# remove trailing /
|
35
|
+w = $(wildcard $+)# existing items in $+
|
36
|
CP := cp -p
|
37
|
cp = $(CP) $< $@
|
38
|
mkdir = $(if $(wildcard $(1)),,mkdir -p $(1))
|
39
|
|
40
|
# File editing
|
41
|
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
|
42
|
|
43
|
# System
|
44
|
os := $(shell uname)
|
45
|
isMac := $(filter Darwin,$(os))
|
46
|
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
|
47
|
dateFmt := %-Y-%-m-%-d %-H:%M:%S %Z
|
48
|
date = $(shell date +"$(dateFmt)")
|
49
|
mtime = $(shell $(if $(isMac),stat -f %Sm -t "$(dateFmt)" $(1),date\
|
50
|
--reference=$(1) +"$(dateFmt)"))
|
51
|
|
52
|
# Terminal
|
53
|
esc := '['
|
54
|
reset := $(esc)'0m'
|
55
|
emph := $(esc)'7m '
|
56
|
endEmph := ' '$(reset)
|
57
|
|
58
|
# User interaction
|
59
|
|
60
|
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
|
61
|
|
62
|
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
|
63
|
|
64
|
# Requires `SHELL := /bin/bash`
|
65
|
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
|
66
|
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
|
67
|
$(error Aborting))
|
68
|
|
69
|
## SVN
|
70
|
|
71
|
addFile = $(if $(wildcard $(1)),,touch $(1) && )svn add $(1)
|
72
|
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
|
73
|
add* = $(if $(1),svn add --depth=empty $(1))
|
74
|
|
75
|
# Revisions
|
76
|
revision = $(shell svn info $(1)|$(sed) -n 's/^Last Changed Rev: (.*)$$/\1/p')
|
77
|
rootRevision = $(call revision,$(root))
|
78
|
version ?= r$(rootRevision)
|
79
|
|
80
|
# rsync
|
81
|
rsync* := "time" rsync$(if $(test), --dry-run) --archive --no-group --update \
|
82
|
--verbose --itemize-changes --progress
|
83
|
rsync := $(rsync*) --exclude=".svn/" --exclude="*\#" --exclude=".DS_Store"\
|
84
|
--exclude=".lk*"
|
85
|
local := ./
|
86
|
localDirName := $(notdir $(realpath $(local)))
|
87
|
remote := $(remote_user)@$(remote_host):$(remote_basepath)/$(localDirName)/
|
88
|
|
89
|
# DB
|
90
|
asAdmin := sudo -E -u postgres
|
91
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
92
|
psqlAsAdmin := $(asAdmin) psql -U postgres $(psqlOpts)
|
93
|
# -E preserves env vars so PGOPTIONS is passed to psql
|
94
|
mkSchemaCmd = 'CREATE SCHEMA $(1);'
|
95
|
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
|
96
|
|
97
|
##### General targets
|
98
|
|
99
|
all: # must be first target in Makefile, so add an empty version here
|
100
|
|
101
|
.SUFFIXES: # turn off built-in suffix rules
|
102
|
.SECONDARY: # don't automatically delete intermediate files
|
103
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
104
|
|
105
|
_always:
|
106
|
.PHONY: _always
|
107
|
|
108
|
clean: # make sure `make clean` always works
|
109
|
|
110
|
%/remake: _always
|
111
|
$(MAKE) $(@D)/clean
|
112
|
$(MAKE) $(@D)/
|
113
|
# re-run make so that cache of existing files is reset
|
114
|
|
115
|
%-remake: _always
|
116
|
rm -f $*
|
117
|
$(MAKE) $*
|
118
|
# re-run make so that cache of existing files is reset
|
119
|
|
120
|
%/reinstall: _always %/uninstall %/install ;
|
121
|
|
122
|
# Run with `make -s` to avoid echoing make commands
|
123
|
version: _always
|
124
|
echo $(version)
|
125
|
|
126
|
##### Checksums
|
127
|
|
128
|
%.md5: %
|
129
|
nice -n +5 md5sum $<|cut -d ' ' -f 1 >$@
|
130
|
|
131
|
# Run with `make -s` to avoid echoing make commands
|
132
|
%.md5/test: %.md5 % _always
|
133
|
echo '$(shell cat $<) $*'|nice -n +5 md5sum -$(if $(isMac),v)c /dev/stdin
|
134
|
|
135
|
##### Compression
|
136
|
|
137
|
define gunzip
|
138
|
gunzip <$< >$@
|
139
|
touch -r $< $@
|
140
|
endef
|
141
|
|
142
|
%:: %.gz
|
143
|
$(if $(wildcard $@),,$(gunzip))
|
144
|
|
145
|
define gzip
|
146
|
gzip <$< >$@
|
147
|
touch -r $< $@
|
148
|
endef
|
149
|
|
150
|
ifneq ($(filter %.gz,$(MAKECMDGOALS)),)
|
151
|
%.gz: %
|
152
|
$(if $(wildcard $@),,$(gzip))
|
153
|
endif
|