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),\
158
echo $(emph)"To accept new test output:"$(endEmph);\
159
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
160
,\
161
echo $(emph)"Note: The preceding failed test is compared to another test's\
162
output"$(endEmph);\
163
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
164
)\
165
exit $$e;}
166
endef
167

    
168
test2File = $(call test,$(map))
169

    
170
test/$(via).xml: $(viaMaps) _always
171
	$(test2File)
172
tests += test/$(via).xml
173

    
174
test/VegBIEN.xml: $(directMaps) _always
175
	$(test2File)
176
tests += test/VegBIEN.xml
177

    
178
test/VegBIEN.2-step.xml: test/$(via).xml $(mainCoreMap) _always
179
	-$(test2File)
180
# Don't abort tester if only 2-step test fails, as it's often finicky
181
tests += test/VegBIEN.2-step.xml
182

    
183
test/import.out: $(directMaps) _always
184
	$(call test,$(map2db))
185
tests += test/import.out
186

    
187
test: _always $(tests) ;
188

    
189
.PRECIOUS: $(tests)
190

    
191
all += $(tests)
192

    
193
# Accepts a test output: make <test_output_path>-ok
194
%-ok: _always
195
	$(CP) $* $(call testRef,$*)
196

    
197
else
198
test: _always ;
199
endif
200

    
201
#####
202

    
203
# Each input type needs var $(mapEnv) and targets install, uninstall
204

    
205
ifneq ($(dbFile),)
206

    
207
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
208
db := $(notdir $(realpath .))
209

    
210
%.ref: %.ref.sql
211
	$(inputDbAsBien) $(db) <$< >$@
212
.PRECIOUS: %.ref
213

    
214
####
215

    
216
install: _always db ;
217

    
218
uninstall: _always rm_db ;
219

    
220
####
221

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

    
224
ifeq ($(dbEngineExt),my)
225

    
226
dbEngine := MySQL
227

    
228
bienPassword := $(shell cat $(root)/config/bien_password)
229
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
230
mysqlAsRoot := $(call mysqlAs,root)
231
dbAsBien := $(call mysqlAs,bien)
232

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

    
239
rm_db: _always
240
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
241
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
242
# ignore errors if grant not defined
243

    
244
###
245

    
246
else
247
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
248
endif
249

    
250
# Must come after dbEngine is set
251
mapEnv := in_engine=$(dbEngine) in_database=$(db)
252

    
253
####
254

    
255
else
256
install: _always ;
257
uninstall: _always ;
258
endif
(2-2/2)