Project

General

Profile

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
coreMapTry ?= organisms specimens
23

    
24
+maps = $(filter maps/%,$(+_))
25
+in = $(filter-out maps/%,$(+_))
26

    
27
root := $(selfDir_uZPPqC)..
28
psqlAsBien := $(root)/bin/psql_vegbien
29

    
30
#####
31

    
32
all: _always maps ;
33

    
34
.SUFFIXES:
35

    
36
_always:
37
.PHONY: _always
38

    
39
clean: _always
40
	$(RM) $(all)
41

    
42
remake: _always clean all ;
43

    
44
%.out: %.make _always
45
	./$* >$@
46
.PRECIOUS: %.out
47

    
48
$(root)/%: _always
49
	+$(subMake)
50

    
51
#####
52

    
53
reinstall: _always uninstall install ;
54

    
55
#####
56

    
57
allViaMaps := $(filter-out maps/VegBIEN.%.csv,$(wildcard maps/*.csv))
58
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
59
coreMap := $(firstword $(wildcard\
60
$(coreMapTry:%=$(root)/mappings/$(via)-VegBIEN.%.csv)))
61
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
62
viaMaps += $(filter-out $(viaMaps),$(wildcard maps/$(via).*.csv))
63
tables := $(viaMaps:maps/$(via).%.csv=%)
64
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
65
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
66
$(wildcard maps/VegBIEN.*.csv))
67

    
68
maps: $(directMaps) _always ;
69

    
70
.PRECIOUS: $(allViaMaps) $(directMaps)
71

    
72
all += $(autogenMaps)
73

    
74
maps/VegBIEN.%.csv: maps/$(via).%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
75
	$(root)/bin/join_sort <$+ >$@
76
.PRECIOUS: maps/VegBIEN.%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
77

    
78
#####
79

    
80
dbFile := $(firstword $(wildcard src/db.*.sql))
81

    
82
ifneq ($(wildcard $(dbFile)),)
83

    
84
import: _always import-all verify ;
85

    
86
log = $(@:-all=)$(if $(n),.n=$(n),).log
87
import = -(set -x; "time" env commit=1 $(map2db) $(+_)) \
88
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
89
# ignore import errors, which are often benign (e.g. invalid date format)
90

    
91
import-all: $(directMaps) _always
92
	$(import)
93

    
94
import-%: maps/VegBIEN.%.csv _always
95
	$(import)
96

    
97
logs := $(wildcard *.log)
98

    
99
rm_logs: _always
100
	$(RM) $(logs)
101

    
102
else
103
import: _always ;
104
import-%: _always ;
105
rm_logs: _always ;
106
endif
107

    
108
#####
109

    
110
ifneq ($(wildcard verify.ref*),)
111

    
112
verify: verify.ref verify.out _always
113
	$(DIFF) $(+_)
114

    
115
all += verify.out
116

    
117
%.out: %.sql _always
118
	$(out_cmd)
119
.PRECIOUS: %.out
120
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
121
--pset=null=NULL <$< >$@
122

    
123
else
124
verify: _always ;
125
endif
126

    
127
#####
128

    
129
ifneq ($(wildcard test/),)
130

    
131
tests :=
132

    
133
hasOwnRef = $(filter-out %.2-step.xml,$@)
134
testRef = $(1:.2-step.xml=.xml).ref
135

    
136
define test
137
env test=1 verbose=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) >$@
138
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
139
$(if $(hasOwnRef),echo $(emph)"To accept new test output:"$(endEmph);\
140
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";)\
141
exit $$e;}
142
endef
143

    
144
test/$(via).xml: $(viaMaps) _always
145
	$(call test,$(map))
146
tests += test/$(via).xml
147

    
148
test/VegBIEN.xml: $(directMaps) _always
149
	$(call test,$(map))
150
tests += test/VegBIEN.xml
151

    
152
test/VegBIEN.2-step.xml: test/$(via).xml $(coreMap) _always
153
	-$(call test,$(root)/map)
154
# Don't abort tester if only 2-step test fails, as it's often finicky
155
tests += test/VegBIEN.2-step.xml
156

    
157
test/import.out: $(directMaps) _always
158
	$(call test,$(map2db))
159
tests += test/import.out
160

    
161
test: _always $(tests) ;
162

    
163
.PRECIOUS: $(tests)
164

    
165
all += $(tests)
166

    
167
# Accepts a test output: make <test_output_path>-ok
168
%-ok: _always
169
	$(CP) $* $(call testRef,$*)
170

    
171
else
172
test: _always ;
173
endif
174

    
175
#####
176

    
177
# Each input type needs var $(mapEnv) and targets install, uninstall
178

    
179
ifneq ($(dbFile),)
180

    
181
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
182
db := $(notdir $(realpath .))
183

    
184
%.ref: %.ref.sql
185
	$(inputDbAsBien) $(db) <$< >$@
186
.PRECIOUS: %.ref
187

    
188
####
189

    
190
install: _always db ;
191

    
192
uninstall: _always rm_db ;
193

    
194
####
195

    
196
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
197

    
198
ifeq ($(dbEngineExt),my)
199

    
200
dbEngine := MySQL
201

    
202
bienPassword := $(shell cat $(root)/config/bien_password)
203
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
204
mysqlAsRoot := $(call mysqlAs,root)
205
dbAsBien := $(call mysqlAs,bien)
206

    
207
db: $(dbFile) _always
208
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
209
	-$(mysqlAsRoot) --database=$(db) <$<
210
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
211
# ignore errors in db import so that GRANT will still be run
212

    
213
rm_db: _always
214
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
215
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
216
# ignore errors if grant not defined
217

    
218
###
219

    
220
else
221
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
222
endif
223

    
224
# Must come after dbEngine is set
225
mapEnv := in_engine=$(dbEngine) in_database=$(db)
226

    
227
####
228

    
229
else
230
mapEnv :=
231
install: _always ;
232
uninstall: _always ;
233
endif
234

    
235
#####
236

    
237
# Must come after mapEnv is set
238
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
239
map2db := env out_database=vegbien $(map)
(2-2/2)