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