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