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
exts ?= csv xml
21
test_n ?= 2
22
tablesSort ?= plots organisms stems
23
mainCoreMapTry ?= organisms specimens
24

    
25
root := $(selfDir_uZPPqC)..
26
mappings := $(root)/mappings
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
svn_props: _always
53
	svn propset svn:ignore $$'.~*\n*.log\n*.out\nsrc*' .
54
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
55

    
56
#####
57

    
58
reinstall: _always uninstall install ;
59

    
60
#####
61

    
62
allViaMaps := $(filter-out maps/VegBIEN.%.csv,$(wildcard maps/*.csv))
63
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
64
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
65
mainCoreMap := $(firstword $(wildcard $(mainCoreMapTry:%=$(coreMap))))
66
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
67
viaMaps += $(filter-out $(viaMaps),$(wildcard maps/$(via).*.csv))
68
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
69
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
70
$(wildcard maps/VegBIEN.*.csv))
71
tables := $(directMaps:maps/VegBIEN.%.csv=%)
72

    
73
maps: $(directMaps) _always ;
74

    
75
.PRECIOUS: $(allViaMaps) $(directMaps)
76

    
77
all += $(autogenMaps)
78

    
79
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
80
	$(root)/bin/join_sort <$+ >$@
81
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
82

    
83
#####
84

    
85
dbFile := $(firstword $(wildcard src/db.*.sql))
86
inputFiles := $(wildcard src/*.csv src/*.xml)
87

    
88
+maps = $(filter maps/% $(mappings)/%,$(+_))
89
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
90
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ))$(root)/map $(+maps)
91
map2db = env out_database=vegbien $(map)
92

    
93
#####
94

    
95
ifneq ($(dbFile)$(inputFiles),)
96

    
97
import: _always import-all verify ;
98

    
99
log = $(@:-all=)$(if $(n),.n=$(n),).log
100
import = -(set -x; "time" env commit=1 $(map2db)) \
101
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
102
# ignore import errors, which are often benign (e.g. invalid date format)
103

    
104
ifneq ($(inputFiles),)
105
import-all: $(addprefix import-,$(tables)) _always ;
106
else
107
import-all: $(directMaps) _always
108
	$(import)
109
endif
110

    
111
import-%: maps/VegBIEN.%.csv _always
112
	$(import)
113
# default:
114
import-%: _always ;
115

    
116
else
117
import: _always ;
118
endif
119

    
120
logs := $(wildcard *.log)
121

    
122
rm_logs: _always
123
	$(RM) $(logs)
124

    
125
#####
126

    
127
ifneq ($(wildcard verify.ref*),)
128

    
129
verify: verify.ref verify.out _always
130
	$(DIFF) $(+_)
131

    
132
all += verify.out
133

    
134
%.out: %.sql _always
135
	$(out_cmd)
136
.PRECIOUS: %.out
137
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off\
138
--pset=null=NULL <$< >$@
139

    
140
else
141
verify: _always ;
142
endif
143

    
144
#####
145

    
146
ifneq ($(wildcard test/),)
147

    
148
tests :=
149

    
150
hasOwnRef = $(filter-out %.2-step.xml,$@)
151
testRef = $(1:.2-step.xml=.xml).ref
152

    
153
define test
154
@echo "Testing $(abspath $@)..."
155
>$@ env test=1 verbose=1 n=$(test_n) $(1)
156
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
157
$(if $(hasOwnRef),echo $(emph)"To accept new test output:"$(endEmph);\
158
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";,\
159
echo $(emph)"Note: This test is compared to another test's output"$(endEmph);)\
160
exit $$e;}
161
endef
162

    
163
test2File = $(call test,$(map))
164

    
165
test/$(via).xml: $(viaMaps) _always
166
	$(test2File)
167
tests += test/$(via).xml
168

    
169
test/VegBIEN.xml: $(directMaps) _always
170
	$(test2File)
171
tests += test/VegBIEN.xml
172

    
173
test/VegBIEN.2-step.xml: test/$(via).xml $(mainCoreMap) _always
174
	-$(test2File)
175
# Don't abort tester if only 2-step test fails, as it's often finicky
176
tests += test/VegBIEN.2-step.xml
177

    
178
test/import.out: $(directMaps) _always
179
	$(call test,$(map2db))
180
tests += test/import.out
181

    
182
test: _always $(tests) ;
183

    
184
.PRECIOUS: $(tests)
185

    
186
all += $(tests)
187

    
188
# Accepts a test output: make <test_output_path>-ok
189
%-ok: _always
190
	$(CP) $* $(call testRef,$*)
191

    
192
else
193
test: _always ;
194
endif
195

    
196
#####
197

    
198
# Each input type needs var $(mapEnv) and targets install, uninstall
199

    
200
ifneq ($(dbFile),)
201

    
202
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
203
db := $(notdir $(realpath .))
204

    
205
%.ref: %.ref.sql
206
	$(inputDbAsBien) $(db) <$< >$@
207
.PRECIOUS: %.ref
208

    
209
####
210

    
211
install: _always db ;
212

    
213
uninstall: _always rm_db ;
214

    
215
####
216

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

    
219
ifeq ($(dbEngineExt),my)
220

    
221
dbEngine := MySQL
222

    
223
bienPassword := $(shell cat $(root)/config/bien_password)
224
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
225
mysqlAsRoot := $(call mysqlAs,root)
226
dbAsBien := $(call mysqlAs,bien)
227

    
228
db: $(dbFile) _always
229
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
230
	-$(mysqlAsRoot) --database=$(db) <$<
231
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
232
# ignore errors in db import so that GRANT will still be run
233

    
234
rm_db: _always
235
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
236
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
237
# ignore errors if grant not defined
238

    
239
###
240

    
241
else
242
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
243
endif
244

    
245
# Must come after dbEngine is set
246
mapEnv := in_engine=$(dbEngine) in_database=$(db)
247

    
248
####
249

    
250
else
251
install: _always ;
252
uninstall: _always ;
253
endif
(2-2/2)