Project

General

Profile

1 640 aaronmk
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
2 250 aaronmk
3 368 aaronmk
# Make
4 640 aaronmk
SHELL := /bin/bash
5 415 aaronmk
subMake = $(MAKE) $(@F) --directory=$(@D)
6 404 aaronmk
+_ = $(+:_%=)
7 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
8
9 640 aaronmk
# Terminal
10
esc := '['
11
reset := $(esc)'0m'
12
emph := $(esc)'7m '
13
endEmph := ' '$(reset)
14
15 368 aaronmk
# Commands
16 395 aaronmk
CP = cp -p
17
DIFF = diff --unified=2
18 368 aaronmk
19 724 aaronmk
# Config
20
test_n ?= 2
21
tablesSort ?= plots organisms stems
22 760 aaronmk
coreMapTry ?= organisms specimens
23 724 aaronmk
24 630 aaronmk
+maps = $(filter maps/%,$(+_))
25
+in = $(filter-out maps/%,$(+_))
26
27 640 aaronmk
root := $(selfDir_uZPPqC)..
28 368 aaronmk
psqlAsBien := $(root)/bin/psql_vegbien
29 353 aaronmk
30 250 aaronmk
#####
31
32 418 aaronmk
all: _always maps ;
33 247 aaronmk
34
.SUFFIXES:
35
36 383 aaronmk
_always:
37
.PHONY: _always
38 250 aaronmk
39 383 aaronmk
clean: _always
40 256 aaronmk
	$(RM) $(all)
41
42 423 aaronmk
remake: _always clean all ;
43
44 515 aaronmk
%.out: %.make _always
45 368 aaronmk
	./$* >$@
46
.PRECIOUS: %.out
47
48 415 aaronmk
$(root)/%: _always
49
	+$(subMake)
50
51 250 aaronmk
#####
52
53 418 aaronmk
reinstall: _always uninstall install ;
54 264 aaronmk
55
#####
56
57 728 aaronmk
allViaMaps := $(filter-out maps/VegBIEN.%.csv,$(wildcard maps/*.csv))
58
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
59 760 aaronmk
coreMap := $(firstword $(wildcard\
60
$(coreMapTry:%=$(root)/mappings/$(via)-VegBIEN.%.csv)))
61 749 aaronmk
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
62
viaMaps += $(filter-out $(viaMaps),$(wildcard maps/$(via).*.csv))
63 728 aaronmk
tables := $(viaMaps:maps/$(via).%.csv=%)
64
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
65
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
66
$(wildcard maps/VegBIEN.*.csv))
67
68 724 aaronmk
maps: $(directMaps) _always ;
69 368 aaronmk
70 728 aaronmk
.PRECIOUS: $(allViaMaps) $(directMaps)
71 368 aaronmk
72 728 aaronmk
all += $(autogenMaps)
73
74 724 aaronmk
maps/VegBIEN.%.csv: maps/$(via).%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
75 415 aaronmk
	$(root)/bin/join_sort <$+ >$@
76 728 aaronmk
.PRECIOUS: maps/VegBIEN.%.csv $(root)/mappings/$(via)-VegBIEN.%.csv
77 264 aaronmk
78 368 aaronmk
#####
79
80 728 aaronmk
dbFile := $(firstword $(wildcard src/db.*.sql))
81
82 718 aaronmk
ifneq ($(wildcard $(dbFile)),)
83 368 aaronmk
84 718 aaronmk
import: _always import-all verify ;
85 368 aaronmk
86 718 aaronmk
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 636 aaronmk
# ignore import errors, which are often benign (e.g. invalid date format)
90 624 aaronmk
91 724 aaronmk
import-all: $(directMaps) _always
92 718 aaronmk
	$(import)
93
94
import-%: maps/VegBIEN.%.csv _always
95
	$(import)
96
97 339 aaronmk
logs := $(wildcard *.log)
98
99 383 aaronmk
rm_logs: _always
100 339 aaronmk
	$(RM) $(logs)
101
102 718 aaronmk
else
103
import: _always ;
104
import-%: _always ;
105
rm_logs: _always ;
106
endif
107
108 264 aaronmk
#####
109
110 624 aaronmk
ifneq ($(wildcard verify.ref*),)
111
112 514 aaronmk
verify: verify.ref verify.out _always
113 404 aaronmk
	$(DIFF) $(+_)
114 368 aaronmk
115 514 aaronmk
all += verify.out
116
117 383 aaronmk
%.out: %.sql _always
118 429 aaronmk
	$(out_cmd)
119 370 aaronmk
.PRECIOUS: %.out
120 429 aaronmk
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
121
--pset=null=NULL <$< >$@
122 369 aaronmk
123 624 aaronmk
else
124
verify: _always ;
125
endif
126
127 368 aaronmk
#####
128
129 624 aaronmk
ifneq ($(wildcard test/),)
130
131 724 aaronmk
tests :=
132 368 aaronmk
133 723 aaronmk
hasOwnRef = $(filter-out %.2-step.xml,$@)
134 630 aaronmk
testRef = $(1:.2-step.xml=.xml).ref
135
136 629 aaronmk
define test
137 664 aaronmk
env test=1 verbose=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) >$@
138 653 aaronmk
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
139 723 aaronmk
$(if $(hasOwnRef),echo $(emph)"To accept new test output:"$(endEmph);\
140
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";)\
141 652 aaronmk
exit $$e;}
142 628 aaronmk
endef
143
144 724 aaronmk
test/$(via).xml: $(viaMaps) _always
145 760 aaronmk
	$(call test,$(map))
146 724 aaronmk
tests += test/$(via).xml
147 629 aaronmk
148 724 aaronmk
test/VegBIEN.xml: $(directMaps) _always
149 656 aaronmk
	$(call test,$(map))
150 724 aaronmk
tests += test/VegBIEN.xml
151 628 aaronmk
152 760 aaronmk
test/VegBIEN.2-step.xml: test/$(via).xml $(coreMap) _always
153 722 aaronmk
	-$(call test,$(root)/map)
154
# Don't abort tester if only 2-step test fails, as it's often finicky
155 724 aaronmk
tests += test/VegBIEN.2-step.xml
156 630 aaronmk
157 724 aaronmk
test/import.out: $(directMaps) _always
158 718 aaronmk
	$(call test,$(map2db))
159 724 aaronmk
tests += test/import.out
160 627 aaronmk
161 724 aaronmk
test: _always $(tests) ;
162
163
.PRECIOUS: $(tests)
164
165
all += $(tests)
166
167 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
168 626 aaronmk
%-ok: _always
169 630 aaronmk
	$(CP) $* $(call testRef,$*)
170 502 aaronmk
171 624 aaronmk
else
172
test: _always ;
173
endif
174
175 368 aaronmk
#####
176
177 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
178
179 622 aaronmk
ifneq ($(dbFile),)
180 256 aaronmk
181 635 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
182
db := $(notdir $(realpath .))
183 622 aaronmk
184 621 aaronmk
%.ref: %.ref.sql
185
	$(inputDbAsBien) $(db) <$< >$@
186
.PRECIOUS: %.ref
187
188 368 aaronmk
####
189 256 aaronmk
190 621 aaronmk
install: _always db ;
191 339 aaronmk
192 621 aaronmk
uninstall: _always rm_db ;
193 368 aaronmk
194 621 aaronmk
####
195
196 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
197 256 aaronmk
198 622 aaronmk
ifeq ($(dbEngineExt),my)
199 621 aaronmk
200 622 aaronmk
dbEngine := MySQL
201
202 621 aaronmk
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 622 aaronmk
db: $(dbFile) _always
208 635 aaronmk
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
209
	-$(mysqlAsRoot) --database=$(db) <$<
210 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
211 255 aaronmk
# ignore errors in db import so that GRANT will still be run
212 250 aaronmk
213 383 aaronmk
rm_db: _always
214 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
215
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
216 365 aaronmk
# ignore errors if grant not defined
217 250 aaronmk
218 624 aaronmk
###
219 368 aaronmk
220
else
221 635 aaronmk
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
222 368 aaronmk
endif
223
224 622 aaronmk
# Must come after dbEngine is set
225
mapEnv := in_engine=$(dbEngine) in_database=$(db)
226
227 624 aaronmk
####
228
229 256 aaronmk
else
230 624 aaronmk
mapEnv :=
231
install: _always ;
232
uninstall: _always ;
233 256 aaronmk
endif
234 368 aaronmk
235
#####
236
237
# Must come after mapEnv is set
238 628 aaronmk
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
239 718 aaronmk
map2db := env out_database=vegbien $(map)