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

    
57
#####
58

    
59
reinstall: _always uninstall install ;
60

    
61
#####
62

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

    
74
maps: $(directMaps) _always ;
75

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

    
78
all += $(autogenMaps)
79

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

    
84
#####
85

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

    
88
ifneq ($(wildcard $(dbFile)),)
89

    
90
import: _always import-all verify ;
91

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

    
97
import-all: $(directMaps) _always
98
	$(import)
99

    
100
import-%: maps/VegBIEN.%.csv _always
101
	$(import)
102

    
103
logs := $(wildcard *.log)
104

    
105
rm_logs: _always
106
	$(RM) $(logs)
107

    
108
else
109
import: _always ;
110
import-%: _always ;
111
rm_logs: _always ;
112
endif
113

    
114
#####
115

    
116
ifneq ($(wildcard verify.ref*),)
117

    
118
verify: verify.ref verify.out _always
119
	$(DIFF) $(+_)
120

    
121
all += verify.out
122

    
123
%.out: %.sql _always
124
	$(out_cmd)
125
.PRECIOUS: %.out
126
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
127
--pset=null=NULL <$< >$@
128

    
129
else
130
verify: _always ;
131
endif
132

    
133
#####
134

    
135
ifneq ($(wildcard test/),)
136

    
137
tests :=
138

    
139
hasOwnRef = $(filter-out %.2-step.xml,$@)
140
testRef = $(1:.2-step.xml=.xml).ref
141

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

    
150
test/$(via).xml: $(viaMaps) _always
151
	$(call test,$(map))
152
tests += test/$(via).xml
153

    
154
test/VegBIEN.xml: $(directMaps) _always
155
	$(call test,$(map))
156
tests += test/VegBIEN.xml
157

    
158
test/VegBIEN.2-step.xml: test/$(via).xml $(coreMap) _always
159
	-$(call test,$(root)/map)
160
# Don't abort tester if only 2-step test fails, as it's often finicky
161
tests += test/VegBIEN.2-step.xml
162

    
163
test/import.out: $(directMaps) _always
164
	$(call test,$(map2db))
165
tests += test/import.out
166

    
167
test: _always $(tests) ;
168

    
169
.PRECIOUS: $(tests)
170

    
171
all += $(tests)
172

    
173
# Accepts a test output: make <test_output_path>-ok
174
%-ok: _always
175
	$(CP) $* $(call testRef,$*)
176

    
177
else
178
test: _always ;
179
endif
180

    
181
#####
182

    
183
# Each input type needs var $(mapEnv) and targets install, uninstall
184

    
185
ifneq ($(dbFile),)
186

    
187
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
188
db := $(notdir $(realpath .))
189

    
190
%.ref: %.ref.sql
191
	$(inputDbAsBien) $(db) <$< >$@
192
.PRECIOUS: %.ref
193

    
194
####
195

    
196
install: _always db ;
197

    
198
uninstall: _always rm_db ;
199

    
200
####
201

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

    
204
ifeq ($(dbEngineExt),my)
205

    
206
dbEngine := MySQL
207

    
208
bienPassword := $(shell cat $(root)/config/bien_password)
209
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
210
mysqlAsRoot := $(call mysqlAs,root)
211
dbAsBien := $(call mysqlAs,bien)
212

    
213
db: $(dbFile) _always
214
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
215
	-$(mysqlAsRoot) --database=$(db) <$<
216
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
217
# ignore errors in db import so that GRANT will still be run
218

    
219
rm_db: _always
220
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
221
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
222
# ignore errors if grant not defined
223

    
224
###
225

    
226
else
227
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
228
endif
229

    
230
# Must come after dbEngine is set
231
mapEnv := in_engine=$(dbEngine) in_database=$(db)
232

    
233
####
234

    
235
else
236
mapEnv :=
237
install: _always ;
238
uninstall: _always ;
239
endif
240

    
241
#####
242

    
243
# Must come after mapEnv is set
244
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
245
map2db := env out_database=vegbien $(map)
(2-2/2)