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
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
18
topDir = $(word 1,$(pathParts))
19
subPath = $(word 2,$(pathParts))
20
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
21
# input.Makefile path is relative to subdir
22

    
23
# Formatting
24
sed = sed -$(if $(filter Darwin,$(os)),E,r)
25
_2Space = $(subst _,$(space),$(1))
26
*2Space = $(call _2Space,$*)
27
ucase = $(shell echo "$(1)"|tr "[a-z]" "[A-Z]")
28
ci = $(1) $(ucase)
29

    
30
# Filesystem
31
wildcard/ = $(shell bash -c 'shopt -s nullglob; echo $(1)')
32
    # needed because builtin $(wildcard) doesn't do / suffix correctly
33
no/ = $(1:/=)# remove trailing /
34
+w = $(wildcard $+)# existing items in $+
35
CP := cp -p
36
cp = $(CP) $< $@
37
mkdir = $(if $(wildcard $(1)),,mkdir -p $(1))
38

    
39
# File editing
40
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
41

    
42
# System
43
os := $(shell uname)
44
isMac := $(filter Darwin,$(os))
45
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
46
dateFmt := %-Y-%-m-%-d %-H:%M:%S %Z
47
date = $(shell date +"$(dateFmt)")
48
mtime = $(shell $(if $(isMac),stat -f %Sm -t "$(dateFmt)" $(1),date\
49
--reference=$(1) +"$(dateFmt)"))
50

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

    
57
# User interaction
58

    
59
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
60

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

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

    
68
## SVN
69

    
70
addFile = $(if $(wildcard $(1)),,touch $(1) && )svn add $(1)
71
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
72
add* = $(if $(1),svn add --depth=empty $(1))
73

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

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

    
88
# DB
89
mkSchemaCmd = 'CREATE SCHEMA $(1);'
90
rmSchemaCmd = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
91

    
92
##### General targets
93

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

    
96
.SUFFIXES: # turn off built-in suffix rules
97
.SECONDARY: # don't automatically delete intermediate files
98
.DELETE_ON_ERROR: # delete target if recipe fails
99

    
100
_always:
101
.PHONY: _always
102

    
103
clean: # make sure `make clean` always works
104

    
105
# Run with `make -s` to avoid echoing make commands
106
version: _always
107
	echo $(version)
108

    
109
##### Checksums
110

    
111
%.md5: %
112
	nice -n +5 md5sum $<|cut -d ' ' -f 1 >$@
113

    
114
# Run with `make -s` to avoid echoing make commands
115
%.md5/test: %.md5 % _always
116
	echo '$(shell cat $<)  $*'|nice -n +5 md5sum -$(if $(isMac),v)c /dev/stdin
(9-9/48)