Project

General

Profile

1 640 aaronmk
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
2 250 aaronmk
3 368 aaronmk
# Make
4 640 aaronmk
SHELL := /bin/bash
5 415 aaronmk
subMake = $(MAKE) $(@F) --directory=$(@D)
6 404 aaronmk
+_ = $(+:_%=)
7 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
8
9 640 aaronmk
# Terminal
10
esc := '['
11
reset := $(esc)'0m'
12
emph := $(esc)'7m '
13
endEmph := ' '$(reset)
14
15 368 aaronmk
# Commands
16 395 aaronmk
CP = cp -p
17
DIFF = diff --unified=2
18 368 aaronmk
19 724 aaronmk
# Config
20
test_n ?= 2
21
tablesSort ?= plots organisms stems
22 760 aaronmk
coreMapTry ?= organisms specimens
23 724 aaronmk
24 630 aaronmk
+maps = $(filter maps/%,$(+_))
25
+in = $(filter-out maps/%,$(+_))
26
27 640 aaronmk
root := $(selfDir_uZPPqC)..
28 368 aaronmk
psqlAsBien := $(root)/bin/psql_vegbien
29 353 aaronmk
30 250 aaronmk
#####
31
32 418 aaronmk
all: _always maps ;
33 247 aaronmk
34
.SUFFIXES:
35
36 383 aaronmk
_always:
37
.PHONY: _always
38 250 aaronmk
39 383 aaronmk
clean: _always
40 256 aaronmk
	$(RM) $(all)
41
42 423 aaronmk
remake: _always clean all ;
43
44 515 aaronmk
%.out: %.make _always
45 368 aaronmk
	./$* >$@
46
.PRECIOUS: %.out
47
48 415 aaronmk
$(root)/%: _always
49
	+$(subMake)
50
51 250 aaronmk
#####
52
53 766 aaronmk
svn_props: _always
54 768 aaronmk
	svn propset svn:ignore $$'.~*\n*.log\n*.out\nsrc*' .
55 766 aaronmk
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
56
57
#####
58
59 418 aaronmk
reinstall: _always uninstall install ;
60 264 aaronmk
61
#####
62
63 728 aaronmk
allViaMaps := $(filter-out maps/VegBIEN.%.csv,$(wildcard maps/*.csv))
64
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
65 760 aaronmk
coreMap := $(firstword $(wildcard\
66
$(coreMapTry:%=$(root)/mappings/$(via)-VegBIEN.%.csv)))
67 749 aaronmk
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
68
viaMaps += $(filter-out $(viaMaps),$(wildcard maps/$(via).*.csv))
69 728 aaronmk
tables := $(viaMaps:maps/$(via).%.csv=%)
70
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
71
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
72
$(wildcard maps/VegBIEN.*.csv))
73
74 724 aaronmk
maps: $(directMaps) _always ;
75 368 aaronmk
76 728 aaronmk
.PRECIOUS: $(allViaMaps) $(directMaps)
77 368 aaronmk
78 728 aaronmk
all += $(autogenMaps)
79
80 724 aaronmk
maps/VegBIEN.%.csv: maps/$(via).%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
81 415 aaronmk
	$(root)/bin/join_sort <$+ >$@
82 728 aaronmk
.PRECIOUS: maps/VegBIEN.%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
83 264 aaronmk
84 368 aaronmk
#####
85
86 728 aaronmk
dbFile := $(firstword $(wildcard src/db.*.sql))
87
88 718 aaronmk
ifneq ($(wildcard $(dbFile)),)
89 368 aaronmk
90 718 aaronmk
import: _always import-all verify ;
91 368 aaronmk
92 718 aaronmk
log = $(@:-all=)$(if $(n),.n=$(n),).log
93
import = -(set -x; "time" env commit=1 $(map2db) $(+_)) \
94
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
95 636 aaronmk
# ignore import errors, which are often benign (e.g. invalid date format)
96 624 aaronmk
97 724 aaronmk
import-all: $(directMaps) _always
98 718 aaronmk
	$(import)
99
100
import-%: maps/VegBIEN.%.csv _always
101
	$(import)
102
103 339 aaronmk
logs := $(wildcard *.log)
104
105 383 aaronmk
rm_logs: _always
106 339 aaronmk
	$(RM) $(logs)
107
108 718 aaronmk
else
109
import: _always ;
110
import-%: _always ;
111
rm_logs: _always ;
112
endif
113
114 264 aaronmk
#####
115
116 624 aaronmk
ifneq ($(wildcard verify.ref*),)
117
118 514 aaronmk
verify: verify.ref verify.out _always
119 404 aaronmk
	$(DIFF) $(+_)
120 368 aaronmk
121 514 aaronmk
all += verify.out
122
123 383 aaronmk
%.out: %.sql _always
124 429 aaronmk
	$(out_cmd)
125 370 aaronmk
.PRECIOUS: %.out
126 429 aaronmk
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
127
--pset=null=NULL <$< >$@
128 369 aaronmk
129 624 aaronmk
else
130
verify: _always ;
131
endif
132
133 368 aaronmk
#####
134
135 624 aaronmk
ifneq ($(wildcard test/),)
136
137 724 aaronmk
tests :=
138 368 aaronmk
139 723 aaronmk
hasOwnRef = $(filter-out %.2-step.xml,$@)
140 630 aaronmk
testRef = $(1:.2-step.xml=.xml).ref
141
142 629 aaronmk
define test
143 664 aaronmk
env test=1 verbose=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) >$@
144 653 aaronmk
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
145 723 aaronmk
$(if $(hasOwnRef),echo $(emph)"To accept new test output:"$(endEmph);\
146
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";)\
147 652 aaronmk
exit $$e;}
148 628 aaronmk
endef
149
150 724 aaronmk
test/$(via).xml: $(viaMaps) _always
151 760 aaronmk
	$(call test,$(map))
152 724 aaronmk
tests += test/$(via).xml
153 629 aaronmk
154 724 aaronmk
test/VegBIEN.xml: $(directMaps) _always
155 656 aaronmk
	$(call test,$(map))
156 724 aaronmk
tests += test/VegBIEN.xml
157 628 aaronmk
158 760 aaronmk
test/VegBIEN.2-step.xml: test/$(via).xml $(coreMap) _always
159 722 aaronmk
	-$(call test,$(root)/map)
160
# Don't abort tester if only 2-step test fails, as it's often finicky
161 724 aaronmk
tests += test/VegBIEN.2-step.xml
162 630 aaronmk
163 724 aaronmk
test/import.out: $(directMaps) _always
164 718 aaronmk
	$(call test,$(map2db))
165 724 aaronmk
tests += test/import.out
166 627 aaronmk
167 724 aaronmk
test: _always $(tests) ;
168
169
.PRECIOUS: $(tests)
170
171
all += $(tests)
172
173 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
174 626 aaronmk
%-ok: _always
175 630 aaronmk
	$(CP) $* $(call testRef,$*)
176 502 aaronmk
177 624 aaronmk
else
178
test: _always ;
179
endif
180
181 368 aaronmk
#####
182
183 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
184
185 622 aaronmk
ifneq ($(dbFile),)
186 256 aaronmk
187 635 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
188
db := $(notdir $(realpath .))
189 622 aaronmk
190 621 aaronmk
%.ref: %.ref.sql
191
	$(inputDbAsBien) $(db) <$< >$@
192
.PRECIOUS: %.ref
193
194 368 aaronmk
####
195 256 aaronmk
196 621 aaronmk
install: _always db ;
197 339 aaronmk
198 621 aaronmk
uninstall: _always rm_db ;
199 368 aaronmk
200 621 aaronmk
####
201
202 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
203 256 aaronmk
204 622 aaronmk
ifeq ($(dbEngineExt),my)
205 621 aaronmk
206 622 aaronmk
dbEngine := MySQL
207
208 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
209
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
210
mysqlAsRoot := $(call mysqlAs,root)
211
dbAsBien := $(call mysqlAs,bien)
212
213 622 aaronmk
db: $(dbFile) _always
214 635 aaronmk
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
215
	-$(mysqlAsRoot) --database=$(db) <$<
216 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
217 255 aaronmk
# ignore errors in db import so that GRANT will still be run
218 250 aaronmk
219 383 aaronmk
rm_db: _always
220 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
221
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
222 365 aaronmk
# ignore errors if grant not defined
223 250 aaronmk
224 624 aaronmk
###
225 368 aaronmk
226
else
227 635 aaronmk
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
228 368 aaronmk
endif
229
230 622 aaronmk
# Must come after dbEngine is set
231
mapEnv := in_engine=$(dbEngine) in_database=$(db)
232
233 624 aaronmk
####
234
235 256 aaronmk
else
236 624 aaronmk
mapEnv :=
237
install: _always ;
238
uninstall: _always ;
239 256 aaronmk
endif
240 368 aaronmk
241
#####
242
243
# Must come after mapEnv is set
244 628 aaronmk
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
245 718 aaronmk
map2db := env out_database=vegbien $(map)