Project

General

Profile

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