1
|
##### Configuration
|
2
|
|
3
|
log ?= $(if $(test),,1)
|
4
|
profile ?=
|
5
|
exts ?= csv tsv txt xml
|
6
|
test_n ?= 2
|
7
|
tablesSort ?= plots organisms stems
|
8
|
|
9
|
##### Vars/functions
|
10
|
|
11
|
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
|
12
|
|
13
|
# Make
|
14
|
SHELL := /bin/bash
|
15
|
subMake = $(MAKE) $(@F) --directory=$(@D)
|
16
|
+_ = $(+:_%=)
|
17
|
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
|
18
|
|
19
|
# System
|
20
|
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
|
21
|
|
22
|
# Terminal
|
23
|
termCols := $(shell tput cols)
|
24
|
esc := '['
|
25
|
reset := $(esc)'0m'
|
26
|
emph := $(esc)'7m '
|
27
|
endEmph := ' '$(reset)
|
28
|
|
29
|
# Commands
|
30
|
MKDIR = mkdir -p
|
31
|
mkdir = $(MKDIR) $(@D)
|
32
|
CP = cp -p
|
33
|
diff = diff --unified=2
|
34
|
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
|
35
|
--width=$(termCols),$(diff))
|
36
|
|
37
|
# Paths
|
38
|
datasrc := $(notdir $(realpath .))
|
39
|
root := $(selfDir_uZPPqC)..
|
40
|
bin := $(root)/bin
|
41
|
mappings := $(root)/mappings
|
42
|
|
43
|
# Commands
|
44
|
join = $(bin)/join_union_sort
|
45
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
46
|
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
|
47
|
|
48
|
##### General targets
|
49
|
|
50
|
all: _always maps ;
|
51
|
|
52
|
.SUFFIXES: # turn off built-in suffix rules
|
53
|
.SECONDARY: # don't automatically delete intermediate files
|
54
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
55
|
|
56
|
_always:
|
57
|
.PHONY: _always
|
58
|
|
59
|
clean: _always
|
60
|
$(RM) $(all)
|
61
|
|
62
|
%.out: %.make _always
|
63
|
./$* >$@
|
64
|
|
65
|
##### SVN
|
66
|
|
67
|
svn_props: _always
|
68
|
svn propset svn:ignore $$'*.log\n*.trace\nsrc*' .
|
69
|
$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
|
70
|
$(if $(wildcard src/),svn propset svn:ignore $$'*' src)
|
71
|
$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
|
72
|
$(if $(wildcard verify/),svn propset svn:ignore $$'*.out' verify)
|
73
|
|
74
|
##### Installation
|
75
|
|
76
|
reinstall: _always uninstall install ;
|
77
|
|
78
|
##### Maps
|
79
|
|
80
|
srcMap := maps/src.%.csv
|
81
|
fullViaMap := maps/%.full.csv
|
82
|
directMap := maps/VegBIEN.%.csv
|
83
|
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
|
84
|
$(wildcard maps/*.csv))
|
85
|
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
|
86
|
|
87
|
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
|
88
|
selfMap := $(mappings)/$(via).self.%.csv
|
89
|
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
|
90
|
|
91
|
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
|
92
|
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
|
93
|
|
94
|
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
|
95
|
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
|
96
|
$(wildcard maps/VegBIEN.*.csv))
|
97
|
tables := $(directMaps:maps/VegBIEN.%.csv=%)
|
98
|
|
99
|
srcMaps := $(filter-out maps/src.join.%.csv,$(wildcard maps/src.*.csv))
|
100
|
srcJoinMaps := $(srcMaps:maps/src.%.csv=maps/src.join.%.csv)
|
101
|
|
102
|
# Must come before $(root)/% to override it
|
103
|
$(selfMap): _always
|
104
|
-+$(subMake)
|
105
|
# ignore errors if $(selfMap) does not exist
|
106
|
|
107
|
$(root)/%: _always
|
108
|
+$(subMake)
|
109
|
|
110
|
maps :=
|
111
|
|
112
|
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
|
113
|
$(join) <$+ >$@
|
114
|
maps += $(autogenMaps)
|
115
|
|
116
|
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
|
117
|
env ignore=1 $(bin)/union <$+|$(bin)/sort_map >$@,$(CP) $< $@)
|
118
|
# can't use $(wildcard) because it won't recheck file after $(selfMap) is run
|
119
|
|
120
|
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
|
121
|
$(makeFullCsv)
|
122
|
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
|
123
|
|
124
|
maps/src.join.%.csv: maps/src.%.csv maps/$(via).%.full.csv $(noEmptyMap)
|
125
|
$(bin)/cols 0 0 <$<|$(bin)/join $(word 2,$+)|$(join) $(word 3,$+) >$@
|
126
|
maps += $(srcJoinMaps)
|
127
|
|
128
|
maps: $(maps) _always ;
|
129
|
|
130
|
all += $(maps)
|
131
|
|
132
|
##### Mapping
|
133
|
|
134
|
dbExport := $(firstword $(wildcard src/db.*.sql))
|
135
|
inputFiles := $(wildcard $(exts:%=src/*.%))
|
136
|
|
137
|
+maps = $(filter maps/% $(mappings)/%,$(+_))
|
138
|
<in = $(firstword $(filter-out $(+maps),$(+_)))
|
139
|
srcs = $(sort $(wildcard $(exts:%=src/*.$*.%)))
|
140
|
map = $(if $(<in),<$(<in),\
|
141
|
$(if $(srcs),$(bin)/with_cat_csv $(srcs) --,\
|
142
|
$(if $(mapEnv),env $(mapEnv),\
|
143
|
$(error No input file src/*.$*.{$(exts)}))))\
|
144
|
$(root)/map $(+maps)
|
145
|
map2db = env out_database=vegbien $(map)
|
146
|
|
147
|
##### Import to VegBIEN
|
148
|
|
149
|
ifneq ($(dbExport)$(inputFiles),)
|
150
|
|
151
|
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
|
152
|
trace = $(log_:.log=.trace)
|
153
|
import = -(set -x; "time" env commit=1 verbose=1\
|
154
|
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
|
155
|
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
|
156
|
# don't abort on import errors, which often relate to invalid input data
|
157
|
|
158
|
import: $(addprefix import-,$(tables)) _always ;
|
159
|
|
160
|
import-%: maps/VegBIEN.%.csv _always
|
161
|
$(import)
|
162
|
# default:
|
163
|
import-%: _always ;
|
164
|
|
165
|
else
|
166
|
import: _always ;
|
167
|
endif
|
168
|
|
169
|
##### Log files from import
|
170
|
|
171
|
logs := $(wildcard *.log *.trace)
|
172
|
|
173
|
rm_logs: _always
|
174
|
$(RM) $(logs)
|
175
|
|
176
|
##### Verification of import
|
177
|
|
178
|
verify: $(addprefix verify-,$(tables)) _always ;
|
179
|
|
180
|
verify-%: verify/%.ref verify/%.out _always
|
181
|
-$(diffVerbose) $(+_)
|
182
|
# don't abort on verification errors, which are expected during development
|
183
|
# default:
|
184
|
verify-%: verify/%.out _always
|
185
|
$(if $(shell test -e $< && echo t),cat $<)
|
186
|
# don't run if verify/%.out's default do-nothing action was used
|
187
|
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
|
188
|
|
189
|
define verify
|
190
|
$(mkdir)
|
191
|
"time" $(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
|
192
|
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
|
193
|
endef
|
194
|
|
195
|
verify/%.out: $(mappings)/verify.%.sql _always
|
196
|
$(verify)
|
197
|
# default:
|
198
|
verify/%.out: _always ;
|
199
|
|
200
|
all += $(wildcard verify/*.out)
|
201
|
|
202
|
ifneq ($(dbExport),)
|
203
|
%.ref: %.ref.sql
|
204
|
$(dbAsBien) $(db) <$< >$@
|
205
|
endif
|
206
|
|
207
|
##### Testing
|
208
|
|
209
|
ifneq ($(wildcard test/),)
|
210
|
|
211
|
hasOwnRef = $(filter-out %.2-step.xml,$@)
|
212
|
testRef = $(1:.2-step.xml=.xml).ref
|
213
|
|
214
|
define runTest
|
215
|
@echo "Testing $(abspath $@)..."
|
216
|
>$@ env test=1 n=$(test_n) $(1)
|
217
|
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
|
218
|
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
219
|
$(if $(hasOwnRef),\
|
220
|
echo $(emph)"To accept new test output:"$(endEmph);\
|
221
|
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
|
222
|
,\
|
223
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
224
|
output"$(endEmph);\
|
225
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
226
|
)\
|
227
|
exit $$e;}
|
228
|
endef
|
229
|
|
230
|
test2File = $(call runTest,$(map))
|
231
|
|
232
|
tests :=
|
233
|
|
234
|
test/$(via).%.xml: maps/$(via).%.full.csv _always
|
235
|
$(test2File)
|
236
|
tests += test/$(via).%.xml
|
237
|
|
238
|
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
|
239
|
$(test2File)
|
240
|
tests += test/VegBIEN.%.xml
|
241
|
|
242
|
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
|
243
|
-$(test2File)
|
244
|
# Don't abort tester if only 2-step test fails, as it's often finicky
|
245
|
tests += test/VegBIEN.%.2-step.xml
|
246
|
|
247
|
test/import.%.out: maps/VegBIEN.%.csv _always
|
248
|
$(call runTest,$(map2db))
|
249
|
tests += test/import.%.out
|
250
|
|
251
|
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
|
252
|
|
253
|
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
|
254
|
|
255
|
test: _always $(testOutputs) ;
|
256
|
|
257
|
all += $(testOutputs)
|
258
|
|
259
|
# Accepts a test output: make <test_output_path>-ok
|
260
|
%-ok: _always
|
261
|
$(CP) $* $(call testRef,$*)
|
262
|
|
263
|
else
|
264
|
test: _always ;
|
265
|
endif
|
266
|
|
267
|
##### Input-type-specific
|
268
|
|
269
|
# Each input type needs var $(mapEnv) and targets install, uninstall
|
270
|
|
271
|
#### DB export
|
272
|
|
273
|
ifneq ($(dbExport),)
|
274
|
|
275
|
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
|
276
|
db := $(datasrc)
|
277
|
|
278
|
### Installation
|
279
|
|
280
|
install: _always db ;
|
281
|
|
282
|
uninstall: _always rm_db ;
|
283
|
|
284
|
### DB-engine-specific
|
285
|
|
286
|
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
|
287
|
|
288
|
ifeq ($(dbEngineExt),my)
|
289
|
|
290
|
dbEngine := MySQL
|
291
|
|
292
|
bienPassword := $(shell cat $(root)/config/bien_password)
|
293
|
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
|
294
|
mysqlAsRoot := $(call mysqlAs,root)
|
295
|
dbAsBien := $(call mysqlAs,bien)
|
296
|
|
297
|
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
|
298
|
|
299
|
define createDb
|
300
|
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
|
301
|
-$(mysqlAsRoot) --database=$(db) <$<
|
302
|
endef
|
303
|
# ignore errors in db import so that GRANT will still be run
|
304
|
|
305
|
db: $(dbExport) _always
|
306
|
$(if $(dbExists),,$(createDb))
|
307
|
echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
|
308
|
|
309
|
rm_db: _always
|
310
|
-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
|
311
|
echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
|
312
|
# ignore errors if grant not defined
|
313
|
|
314
|
## Unrecognized DB engine
|
315
|
|
316
|
else
|
317
|
$(error The DB filename $(dbExport) must be db.my.sql)
|
318
|
endif
|
319
|
|
320
|
### Other input types
|
321
|
|
322
|
else
|
323
|
|
324
|
install: _always ;
|
325
|
uninstall: _always ;
|
326
|
|
327
|
endif
|
328
|
|
329
|
#### DB connection info
|
330
|
|
331
|
ifneq ($(dbEngine),)
|
332
|
# Must come after dbEngine is set
|
333
|
mapEnv := in_engine=$(dbEngine) in_database=$(db)
|
334
|
endif
|