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
viaMapsSort := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
59
viaMaps := $(viaMapsSort) $(filter-out $(viaMapsSort),$(viaMaps))
60
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 656 aaronmk
	$(call test,$(map))
143 724 aaronmk
tests += test/$(via).xml
144 629 aaronmk
145 724 aaronmk
test/VegBIEN.xml: $(directMaps) _always
146 656 aaronmk
	$(call test,$(map))
147 724 aaronmk
tests += test/VegBIEN.xml
148 628 aaronmk
149 724 aaronmk
test/VegBIEN.2-step.xml: test/$(via).xml\
150
$(root)/mappings/$(via)-VegBIEN.organisms.csv _always
151 722 aaronmk
	-$(call test,$(root)/map)
152
# Don't abort tester if only 2-step test fails, as it's often finicky
153 724 aaronmk
tests += test/VegBIEN.2-step.xml
154 630 aaronmk
155 724 aaronmk
test/import.out: $(directMaps) _always
156 718 aaronmk
	$(call test,$(map2db))
157 724 aaronmk
tests += test/import.out
158 627 aaronmk
159 724 aaronmk
test: _always $(tests) ;
160
161
.PRECIOUS: $(tests)
162
163
all += $(tests)
164
165 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
166 626 aaronmk
%-ok: _always
167 630 aaronmk
	$(CP) $* $(call testRef,$*)
168 502 aaronmk
169 624 aaronmk
else
170
test: _always ;
171
endif
172
173 368 aaronmk
#####
174
175 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
176
177 622 aaronmk
ifneq ($(dbFile),)
178 256 aaronmk
179 635 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
180
db := $(notdir $(realpath .))
181 622 aaronmk
182 621 aaronmk
%.ref: %.ref.sql
183
	$(inputDbAsBien) $(db) <$< >$@
184
.PRECIOUS: %.ref
185
186 368 aaronmk
####
187 256 aaronmk
188 621 aaronmk
install: _always db ;
189 339 aaronmk
190 621 aaronmk
uninstall: _always rm_db ;
191 368 aaronmk
192 621 aaronmk
####
193
194 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
195 256 aaronmk
196 622 aaronmk
ifeq ($(dbEngineExt),my)
197 621 aaronmk
198 622 aaronmk
dbEngine := MySQL
199
200 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
201
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
202
mysqlAsRoot := $(call mysqlAs,root)
203
dbAsBien := $(call mysqlAs,bien)
204
205 622 aaronmk
db: $(dbFile) _always
206 635 aaronmk
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
207
	-$(mysqlAsRoot) --database=$(db) <$<
208 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
209 255 aaronmk
# ignore errors in db import so that GRANT will still be run
210 250 aaronmk
211 383 aaronmk
rm_db: _always
212 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
213
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
214 365 aaronmk
# ignore errors if grant not defined
215 250 aaronmk
216 624 aaronmk
###
217 368 aaronmk
218
else
219 635 aaronmk
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
220 368 aaronmk
endif
221
222 622 aaronmk
# Must come after dbEngine is set
223
mapEnv := in_engine=$(dbEngine) in_database=$(db)
224
225 624 aaronmk
####
226
227 256 aaronmk
else
228 624 aaronmk
mapEnv :=
229
install: _always ;
230
uninstall: _always ;
231 256 aaronmk
endif
232 368 aaronmk
233
#####
234
235
# Must come after mapEnv is set
236 628 aaronmk
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
237 718 aaronmk
map2db := env out_database=vegbien $(map)