Project

General

Profile

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
nice := nice -n +10 # +5 still leaves the shell sluggish
52

    
53
# Terminal
54
esc := '['
55
reset := $(esc)'0m'
56
emph := $(esc)'7m '
57
endEmph := ' '$(reset)
58

    
59
# User interaction
60

    
61
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
62

    
63
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
64

    
65
# Requires `SHELL := /bin/bash`
66
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
67
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
68
$(error Aborting))
69

    
70
## SVN
71

    
72
addFile = $(if $(wildcard $(1)),,touch $(1) && )svn add --force $(1)
73
addDir = $(if $(wildcard $(1)/),svn add --force --depth=empty $(1),svn mkdir $(1))
74
add* = $(if $(1),svn add --force --depth=empty $(wildcard $(1)))
75
	# $(wildcard __): prevent error "Could not add all targets because some
76
	# targets don't exist"
77

    
78
# Revisions
79
revision = $(shell svn info $(1)|$(sed) -n 's/^Last Changed Rev: (.*)$$/\1/p')
80
rootRevision = $(call revision,$(root))
81
version ?= r$(rootRevision)
82

    
83
# rsync
84
rsync* := "time" rsync$(if $(test), --dry-run) --archive --no-group --update \
85
--verbose --itemize-changes --progress
86
rsync := $(rsync*) --exclude=".svn/" --exclude="*\#" --exclude=".DS_Store"\
87
--exclude=".lk*"
88
local := ./
89
localDirName := $(notdir $(realpath $(local)))
90
remote := $(remote_user)@$(remote_host):$(remote_basepath)/$(localDirName)/
91

    
92
# DB
93
postgres := postgres
94
asAdmin := sudo -E -u $(postgres)
95
psqlOpts := --set ON_ERROR_STOP=1 --quiet
96
psqlAsAdmin := $(asAdmin) psql -U postgres $(psqlOpts)
97
    # -E preserves env vars so PGOPTIONS is passed to psql
98
mkSchemaCmd = 'CREATE SCHEMA "$(1)";'
99
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
100

    
101
##### General targets
102

    
103
all: # must be first target in Makefile, so add an empty version here
104

    
105
.SUFFIXES: # turn off built-in suffix rules
106
.SECONDARY: # don't automatically delete intermediate files
107
.DELETE_ON_ERROR: # delete target if recipe fails
108

    
109
_always:
110
.PHONY: _always
111

    
112
clean: # make sure `make clean` always works
113

    
114
%/remake: _always
115
	$(MAKE) $(@D)/clean
116
	$(MAKE) $(@D)/
117
# re-run make so that cache of existing files is reset
118

    
119
%-remake: _always
120
	rm -f $*
121
	$(MAKE) $*
122
# re-run make so that cache of existing files is reset
123

    
124
%/reinstall: _always %/uninstall %/install ;
125

    
126
%/live: _always
127
	$(MAKE) $* live=1
128

    
129
# Run with `make -s` to avoid echoing make commands
130
version: _always
131
	echo $(version)
132

    
133
##### Checksums
134

    
135
%.md5: %
136
	$(nice) md5sum $<|cut -d ' ' -f 1 >$@
137

    
138
# Run with `make -s` to avoid echoing make commands
139
%.md5/test: %.md5 % _always
140
	echo '$(shell cat $<)  $*'|$(nice) md5sum -$(if $(isMac),v)c /dev/stdin
141

    
142
##### Compression
143

    
144
define gunzip
145
gunzip <$< >$@
146
touch -r $< $@
147
endef
148

    
149
%:: %.gz
150
	$(if $(wildcard $@),,$(gunzip))
151

    
152
define gzip
153
gzip <$< >$@
154
touch -r $< $@
155
endef
156

    
157
ifneq ($(filter %.gz,$(MAKECMDGOALS)),)
158
%.gz: %
159
	$(if $(wildcard $@),,$(gzip))
160
endif
(10-10/49)