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
|
viaMapsSort := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
|
59
|
viaMaps := $(viaMapsSort) $(filter-out $(viaMapsSort),$(viaMaps))
|
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
|
tests += test/$(via).xml
|
144
|
|
145
|
test/VegBIEN.xml: $(directMaps) _always
|
146
|
$(call test,$(map))
|
147
|
tests += test/VegBIEN.xml
|
148
|
|
149
|
test/VegBIEN.2-step.xml: test/$(via).xml\
|
150
|
$(root)/mappings/$(via)-VegBIEN.organisms.csv _always
|
151
|
-$(call test,$(root)/map)
|
152
|
# Don't abort tester if only 2-step test fails, as it's often finicky
|
153
|
tests += test/VegBIEN.2-step.xml
|
154
|
|
155
|
test/import.out: $(directMaps) _always
|
156
|
$(call test,$(map2db))
|
157
|
tests += test/import.out
|
158
|
|
159
|
test: _always $(tests) ;
|
160
|
|
161
|
.PRECIOUS: $(tests)
|
162
|
|
163
|
all += $(tests)
|
164
|
|
165
|
# Accepts a test output: make <test_output_path>-ok
|
166
|
%-ok: _always
|
167
|
$(CP) $* $(call testRef,$*)
|
168
|
|
169
|
else
|
170
|
test: _always ;
|
171
|
endif
|
172
|
|
173
|
#####
|
174
|
|
175
|
# Each input type needs var $(mapEnv) and targets install, uninstall
|
176
|
|
177
|
ifneq ($(dbFile),)
|
178
|
|
179
|
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
|
180
|
db := $(notdir $(realpath .))
|
181
|
|
182
|
%.ref: %.ref.sql
|
183
|
$(inputDbAsBien) $(db) <$< >$@
|
184
|
.PRECIOUS: %.ref
|
185
|
|
186
|
####
|
187
|
|
188
|
install: _always db ;
|
189
|
|
190
|
uninstall: _always rm_db ;
|
191
|
|
192
|
####
|
193
|
|
194
|
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
|
195
|
|
196
|
ifeq ($(dbEngineExt),my)
|
197
|
|
198
|
dbEngine := MySQL
|
199
|
|
200
|
bienPassword := $(shell cat $(root)/config/bien_password)
|
201
|
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
|
202
|
mysqlAsRoot := $(call mysqlAs,root)
|
203
|
dbAsBien := $(call mysqlAs,bien)
|
204
|
|
205
|
db: $(dbFile) _always
|
206
|
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
|
207
|
-$(mysqlAsRoot) --database=$(db) <$<
|
208
|
echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
|
209
|
# ignore errors in db import so that GRANT will still be run
|
210
|
|
211
|
rm_db: _always
|
212
|
-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
|
213
|
echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
|
214
|
# ignore errors if grant not defined
|
215
|
|
216
|
###
|
217
|
|
218
|
else
|
219
|
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
|
220
|
endif
|
221
|
|
222
|
# Must come after dbEngine is set
|
223
|
mapEnv := in_engine=$(dbEngine) in_database=$(db)
|
224
|
|
225
|
####
|
226
|
|
227
|
else
|
228
|
mapEnv :=
|
229
|
install: _always ;
|
230
|
uninstall: _always ;
|
231
|
endif
|
232
|
|
233
|
#####
|
234
|
|
235
|
# Must come after mapEnv is set
|
236
|
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
|
237
|
map2db := env out_database=vegbien $(map)
|