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