Project

General

Profile

1 3697 aaronmk
##### Configuration
2
3 7557 aaronmk
remote_user ?= $(USER)
4 7564 aaronmk
remote_host ?= jupiter
5
remote_basepath ?= /data/dev/aaronmk/bien
6 3697 aaronmk
test ?=
7
8
##### Vars/functions
9
10
# Make
11 3763 aaronmk
null :=
12
space := $(null) $(null)
13 5483 aaronmk
comma := ,
14 7046 aaronmk
and = $(if $(1),$(2))
15
or = $(1)$(2)
16
not = $(if $(1),,1)
17 8271 aaronmk
require_var = $(if $($(1)),,$(error requires var $$$(1)))
18 3697 aaronmk
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
19
topDir = $(word 1,$(pathParts))
20
subPath = $(word 2,$(pathParts))
21 11266 aaronmk
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
22 3697 aaronmk
# input.Makefile path is relative to subdir
23
24 3763 aaronmk
# Formatting
25 5452 aaronmk
sed = sed -$(if $(filter Darwin,$(os)),E,r)
26 3763 aaronmk
_2Space = $(subst _,$(space),$(1))
27
*2Space = $(call _2Space,$*)
28 7385 aaronmk
ucase = $(shell echo "$(1)"|tr "[a-z]" "[A-Z]")
29
ci = $(1) $(ucase)
30 3763 aaronmk
31 3792 aaronmk
# Filesystem
32 14760 aaronmk
wildcard/ = $(wildcard $(shell bash -c 'shopt -s nullglob; echo $(1)'))
33 4136 aaronmk
    # needed because builtin $(wildcard) doesn't do / suffix correctly
34 14760 aaronmk
    # need final pass with $(wildcard) to support inputs without wildcard chars
35 4602 aaronmk
no/ = $(1:/=)# remove trailing /
36 4603 aaronmk
+w = $(wildcard $+)# existing items in $+
37 3794 aaronmk
CP := cp -p
38
cp = $(CP) $< $@
39 7381 aaronmk
mkdir = $(if $(wildcard $(1)),,mkdir -p $(1))
40 3792 aaronmk
41 6915 aaronmk
# File editing
42
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
43
44 5451 aaronmk
# System
45 5458 aaronmk
os := $(shell uname)
46 6965 aaronmk
isMac := $(filter Darwin,$(os))
47 5458 aaronmk
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
48 6966 aaronmk
dateFmt := %-Y-%-m-%-d %-H:%M:%S %Z
49
date = $(shell date +"$(dateFmt)")
50 6967 aaronmk
mtime = $(shell $(if $(isMac),stat -f %Sm -t "$(dateFmt)" $(1),date\
51
--reference=$(1) +"$(dateFmt)"))
52 13019 aaronmk
nice := nice -n +10 # +5 still leaves the shell sluggish
53 5451 aaronmk
54 6915 aaronmk
# 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 6978 aaronmk
# Requires `SHELL := /bin/bash`
67 6915 aaronmk
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 5460 aaronmk
## SVN
72
73 12405 aaronmk
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 12920 aaronmk
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 5460 aaronmk
79
# Revisions
80 5454 aaronmk
revision = $(shell svn info $(1)|$(sed) -n 's/^Last Changed Rev: (.*)$$/\1/p')
81 5455 aaronmk
rootRevision = $(call revision,$(root))
82 6927 aaronmk
version ?= r$(rootRevision)
83 3697 aaronmk
84
# rsync
85 7569 aaronmk
rsync* := "time" rsync$(if $(test), --dry-run) --archive --no-group --update \
86
--verbose --itemize-changes --progress
87 7728 aaronmk
rsync := $(rsync*) --exclude=".svn/" --exclude="*\#" --exclude=".DS_Store"\
88
--exclude=".lk*"
89 3697 aaronmk
local := ./
90 3700 aaronmk
localDirName := $(notdir $(realpath $(local)))
91 7558 aaronmk
remote := $(remote_user)@$(remote_host):$(remote_basepath)/$(localDirName)/
92 3697 aaronmk
93 6915 aaronmk
# DB
94 11662 aaronmk
postgres := postgres
95 10127 aaronmk
asAdmin := sudo -E -u $(postgres)
96 10124 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
97 10126 aaronmk
psqlAsAdmin := $(asAdmin) psql -U postgres $(psqlOpts)
98 10124 aaronmk
    # -E preserves env vars so PGOPTIONS is passed to psql
99 10882 aaronmk
mkSchemaCmd = 'CREATE SCHEMA "$(1)";'
100 6915 aaronmk
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
101
102 3697 aaronmk
##### General targets
103
104 3765 aaronmk
all: # must be first target in Makefile, so add an empty version here
105 3697 aaronmk
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 3765 aaronmk
113
clean: # make sure `make clean` always works
114 6929 aaronmk
115 9703 aaronmk
%/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 9702 aaronmk
%/reinstall: _always %/uninstall %/install ;
126
127 11895 aaronmk
%/live: _always
128
	$(MAKE) $* live=1
129
130 7076 aaronmk
# Run with `make -s` to avoid echoing make commands
131 6929 aaronmk
version: _always
132
	echo $(version)
133 7559 aaronmk
134
##### Checksums
135
136
%.md5: %
137 13018 aaronmk
	$(nice) md5sum $<|cut -d ' ' -f 1 >$@
138 7559 aaronmk
139
# Run with `make -s` to avoid echoing make commands
140
%.md5/test: %.md5 % _always
141 13018 aaronmk
	echo '$(shell cat $<)  $*'|$(nice) md5sum -$(if $(isMac),v)c /dev/stdin
142 8269 aaronmk
143
##### Compression
144
145 8767 aaronmk
define gunzip
146
gunzip <$< >$@
147 9744 aaronmk
touch -r $< $@
148 8767 aaronmk
endef
149
150 8269 aaronmk
%:: %.gz
151 8767 aaronmk
	$(if $(wildcard $@),,$(gunzip))
152 8269 aaronmk
153 8767 aaronmk
define gzip
154
gzip <$< >$@
155 9744 aaronmk
touch -r $< $@
156 8767 aaronmk
endef
157
158 8269 aaronmk
ifneq ($(filter %.gz,$(MAKECMDGOALS)),)
159
%.gz: %
160 8767 aaronmk
	$(if $(wildcard $@),,$(gzip))
161 8269 aaronmk
endif