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
root := $(selfDir_uZPPqC)..
25
psqlAsBien := $(root)/bin/psql_vegbien
26

    
27
#####
28

    
29
all: _always maps ;
30

    
31
.SUFFIXES:
32

    
33
_always:
34
.PHONY: _always
35

    
36
clean: _always
37
	$(RM) $(all)
38

    
39
remake: _always clean all ;
40

    
41
%.out: %.make _always
42
	./$* >$@
43
.PRECIOUS: %.out
44

    
45
$(root)/%: _always
46
	+$(subMake)
47

    
48
#####
49

    
50
svn_props: _always
51
	svn propset svn:ignore $$'.~*\n*.log\n*.out\nsrc*' .
52
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
53

    
54
#####
55

    
56
reinstall: _always uninstall install ;
57

    
58
#####
59

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

    
71
maps: $(directMaps) _always ;
72

    
73
.PRECIOUS: $(allViaMaps) $(directMaps)
74

    
75
all += $(autogenMaps)
76

    
77
maps/VegBIEN.%.csv: maps/$(via).%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
78
	$(root)/bin/join_sort <$+ >$@
79
.PRECIOUS: maps/VegBIEN.%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
80

    
81
#####
82

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

    
86
+maps = $(filter maps/%,$(+_))
87
<in = $(firstword $(wildcard $(patsubst %,src/*.$*.%,csv xml)) \
88
$(filter-out $(+maps),$(+_)))
89
map = $(if $(mapEnv),env $(mapEnv) ,)$(root)/map $(if $(<in),<$(<in)) $(+maps)
90
map2db = env out_database=vegbien $(map)
91

    
92
#####
93

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

    
96
import: _always import-all verify ;
97

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

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

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

    
115
else
116
import: _always ;
117
endif
118

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

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

    
124
#####
125

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

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

    
131
all += verify.out
132

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

    
139
else
140
verify: _always ;
141
endif
142

    
143
#####
144

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

    
147
tests :=
148

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

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

    
160
test/$(via).xml: $(viaMaps) _always
161
	$(call test,$(map))
162
tests += test/$(via).xml
163

    
164
test/VegBIEN.xml: $(directMaps) _always
165
	$(call test,$(map))
166
tests += test/VegBIEN.xml
167

    
168
test/VegBIEN.2-step.xml: test/$(via).xml $(coreMap) _always
169
	-$(call test,$(root)/map)
170
# Don't abort tester if only 2-step test fails, as it's often finicky
171
tests += test/VegBIEN.2-step.xml
172

    
173
test/import.out: $(directMaps) _always
174
	$(call test,$(map2db))
175
tests += test/import.out
176

    
177
test: _always $(tests) ;
178

    
179
.PRECIOUS: $(tests)
180

    
181
all += $(tests)
182

    
183
# Accepts a test output: make <test_output_path>-ok
184
%-ok: _always
185
	$(CP) $* $(call testRef,$*)
186

    
187
else
188
test: _always ;
189
endif
190

    
191
#####
192

    
193
# Each input type needs var $(mapEnv) and targets install, uninstall
194

    
195
ifneq ($(dbFile),)
196

    
197
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
198
db := $(notdir $(realpath .))
199

    
200
%.ref: %.ref.sql
201
	$(inputDbAsBien) $(db) <$< >$@
202
.PRECIOUS: %.ref
203

    
204
####
205

    
206
install: _always db ;
207

    
208
uninstall: _always rm_db ;
209

    
210
####
211

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

    
214
ifeq ($(dbEngineExt),my)
215

    
216
dbEngine := MySQL
217

    
218
bienPassword := $(shell cat $(root)/config/bien_password)
219
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
220
mysqlAsRoot := $(call mysqlAs,root)
221
dbAsBien := $(call mysqlAs,bien)
222

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

    
229
rm_db: _always
230
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
231
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
232
# ignore errors if grant not defined
233

    
234
###
235

    
236
else
237
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
238
endif
239

    
240
# Must come after dbEngine is set
241
mapEnv := in_engine=$(dbEngine) in_database=$(db)
242

    
243
####
244

    
245
else
246
install: _always ;
247
uninstall: _always ;
248
endif
(2-2/2)