chiark / gitweb /
Makefile: Add more tests to full-check
[otter.git] / Makefile
1 #!/bin/sh
2 # Copyright 2020-2021 Ian Jackson and contributors to Otter
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # There is NO WARRANTY.
5
6 # make -j8
7 # make -j8 release
8 # make -j8 shapelib
9
10 SHELL=/bin/bash
11 src=.
12
13 default: all check
14 all: debug
15 full-check: all check cargo-syntaxcheck-release shapelib sphinx-doc
16 everything: debug doc release check bundled-sources
17
18 shapelib: templates/shapelib.html stamp/cargo.doc-otter-only
19         @echo 'Shape library preview and docs can now be found here:'
20         @echo '  file://$(PWD)/$<'
21         @echo '  file://$(abspath $(TARGET_DIR)/doc/otter/shapelib_toml/index.html)'
22
23 MAKEFILE_DEP ?= Makefile
24 MAKEFILE_FIND_X ?=
25 # ^ set this to "x" to debug the $rsrcs rune
26
27 #---------- funky macros etc. ----------
28
29 cr = $(addprefix --,$(filter-out debug,$1))
30 rsrcs = $(shell $(foreach x,$(MAKEFILE_FIND_X),set -$x;)\
31     find -H $1 \( -name Cargo.toml -o -name Cargo.lock -o -name Cargo.lock.example -o -name \*.rs \) \! -path '*/build/*' )
32 stamp=@mkdir -p stamp; touch $@
33
34 BUNDLED_SOURCES_LIT = README.md LICENCE
35 BUNDLED_SOURCES_FILES = index.html $(BUNDLED_SOURCES_LIT)
36 BUNDLED_SOURCES_LINKS += $(BUNDLED_SOURCES_LIT) otter/
37 BUNDLED_SOURCES += $(BUNDLED_SOURCES_FILES)
38
39 #---------- programs and config variables ----------
40
41 CARGO ?= cargo
42 TARGET_DIR ?= target
43
44 USVG ?= usvg
45 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
46
47 WASM_BINDGEN = $(TARGET_DIR)/debug/wasm-bindgen
48 WASM_BINDGEN_OPTIONS =                                          \
49         --remove-name-section --remove-producers-section        \
50         --typescript --no-modules                               \
51
52 BUNDLE_SOURCES ?= bundle-rust-sources
53
54 SPHINXBUILD   = sphinx-build
55
56 ifndef INKSCAPE_EXTENSIONS
57 INKSCAPE ?= inkscape
58 INKSCAPE_EXTENSIONS := $(shell $(INKSCAPE) -x)
59 endif
60 RECOLOUR_SVG ?= $(INKSCAPE_EXTENSIONS)/color_replace.py
61
62 DEPLOY_ARCH=x86_64-unknown-linux-musl
63 DEPLOY_RELEASE=debug
64 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
65 DEPLOYED_BRANCH=deployed
66
67 #---------- nailing-cargo ----------
68
69 ifneq (,$(wildcard ../Cargo.nail))
70
71 NAILING_CARGO = nailing-cargo
72 CARGO = $(NAILING_CARGO)
73 BUILD_SUBDIR ?= ../Build
74 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
75
76 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
77 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
78 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
79 WASM_BINDGEN_CLI_CARGO_OPTS ?= --subcommand-props=!manifest-path
80
81 clean-nailing:
82         $(NAILING_CARGO_JUST_RUN) \
83  sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
84                 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
85
86 else
87 clean-nailing:
88 endif # Cargo.nail
89
90 BUILD_SUBDIR ?= ../Build
91 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
92 USVG_CMD ?= $(USVG)
93
94 WASM_PACKED=$(TARGET_DIR)/packed-wasm
95
96 #---------- local programs ----------
97
98 define lp
99 $(if $(wildcard $(BUILD_SUBDIR)/$2),
100 $(shell echo >&2 'Makefile: lp: Using program $4 from $(BUILD_SUBDIR)/$2')
101 $1 := $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4)
102 $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4):; cd ../$2 && $$(CARGO) build $(call cr,$3)
103 BUNDLED_SOURCES_DIRS += $2
104 BUNDLED_SOURCES_LINKS += $2/
105 )
106 endef
107
108 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
109 $(eval $(call lp,USVG,resvg,release,usvg))
110
111 #---------- variables defining bits of source etc. ----------
112
113 PROGRAMS=daemon-otter otter
114
115 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
116 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
117
118 TS_SRCS= script
119 TS_SRC_FILES= \
120         $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
121         webassembly-types/webassembly.d.ts \
122         $(WASM_PACKED)/otter_wasm.d.ts
123
124 LITFILES= LICENCE AGPLv3
125 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
126
127 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
128                         $(LITFILES) $(TXTFILES)) \
129                 $(wildcard templates/*.tera)
130
131 WASM := wasm32-unknown-unknown
132 # ^ todo: Is this still right after
133 #     Use correct ABI for wasm32 by default
134 #     https://github.com/rust-lang/rust/pull/79998
135 # ?  But maybe it doesn't matter since we're very conservative and
136 # only pass JsValue and a few strings across the WASM ABI.
137
138 #---------- toplevel aggregate targets ----------
139
140 check: stamp/cargo.debug-check at wdt
141         @echo 'All tests passed.'
142
143 full-check: stamp/cargo.release-check
144 full-check: stamp/cargo.release-miri stamp/cargo.debug-miri
145
146 doc: cargo-doc sphinx-doc
147
148 debug release:: %: stamp/cargo.% assets libraries extra-%
149
150 cargo: cargo-debug cargo-wasm-release
151
152 cargo-debug cargo-release cargo-check cargo-doc \
153 cargo-wasm-debug cargo-wasm-release:: \
154 cargo-%: stamp/cargo.%
155
156 cargo-wasm: cargo-wasm-release
157
158 wasm: stamp/wasm-bindgen
159
160 assets: js stamp/wasm-bindgen $(FILEASSETS)
161
162 js: templates/script.js
163
164 extra-debug:
165 extra-release: bundled-sources
166
167 cargo-syntaxcheck: cargo-syntaxcheck-host cargo-syntaxcheck-wasm
168 cargo-syntaxcheck-host:
169         $(CARGO) check --workspace
170 cargo-syntaxcheck-wasm:
171         $(CARGO) check --target $(WASM) -p otter-wasm --release
172 cargo-syntaxcheck-release:
173         $(CARGO) check --workspace --release
174
175 #---------- cargo ----------
176
177 DR=debug release
178 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
179
180 $(addprefix stamp/cargo.,$(DR)):: \
181 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
182         $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
183         $(stamp)
184
185 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
186         $(CARGO) build --workspace -p otter --bin $*
187
188 stamp/cargo.wasm-bindgen: $(call rsrcs, ! -name \*.rs)
189         $(CARGO) $(WASM_BINDGEN_CLI_CARGO_OPTS) build --target-dir=target \
190                 --manifest-path=$(abspath wasm/Cargo.toml) -p wasm-bindgen-cli
191         $(stamp)
192
193 stamp/cargo.%-check: $(call rsrcs,.)
194         $(CARGO) test --workspace $(call cr,$*)
195         $(stamp)
196
197 stamp/cargo.%-miri: $(call rsrcs,.)
198         $(CARGO) miri test --workspace $(call cr,$*)
199         $(stamp)
200
201 stamp/cargo-at.debug: $(call rsrcs,.)
202         $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
203         $(stamp)
204
205 stamp/cargo-wdt.debug: $(call rsrcs,.)
206         $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
207         $(stamp)
208
209 stamp/cargo.doc: $(call rsrcs,.)
210         $(CARGO) doc $(CARGO_DOC_OPTS) --workspace 2>&1 |egrep -vf .cargo-doc-suppress-errors
211         $(stamp)
212
213 stamp/cargo.doc-otter-only: $(call rsrcs,.)
214         $(CARGO) doc $(CARGO_DOC_OPTS) --workspace -p otter --no-deps
215         $(stamp)
216
217 $(addprefix stamp/cargo.wasm-,$(DR)):: \
218 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
219         $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
220         $(stamp)
221
222 stamp/cargo.deploy-build: $(call rsrcs,.)
223         $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
224         $(stamp)
225
226 #---------- sphnix ----------
227
228 sphinx-doc: docs/html/index.html
229         @echo 'Documentation can now be found here:'
230         @echo '  file://$(PWD)/$<'
231
232 docs/html/index.html: docs/conf.py $(wildcard docs/*.md docs/*.rst)
233         $(SPHINXBUILD) -M html docs docs $(SPHINXOPTS)
234
235 #---------- wasm ----------
236
237 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-bindgen
238 stamp/wasm-bindgen: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
239         $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
240                 $(WASM_BINDGEN_OPTIONS) --out-dir target/packed-wasm \
241                 target/$(WASM)/release/otter_wasm.wasm
242         $(stamp)
243
244 #---------- bundle-sources ----------
245
246 BUNDLED_SOURCES_DIRS += otter
247
248 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
249
250 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
251
252 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
253 bundled-sources/%: $(wildcard $(BUNDLE_SOURCES))
254         set -e; d=$(abspath $(TARGET_BUNDLED)); \
255         $(NAILING_CARGO_JUST_RUN) mkdir -p $$d; \
256         $(if $(filter-out otter,$*), cd ../$*;) \
257         $(BUNDLE_SOURCES_CMD) --output $$d/$*
258
259 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
260
261 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
262         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
263
264 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index $(MAKEFILE_DEP)
265         $(NAILING_CARGO_JUST_RUN) sh -ec '                      \
266                 cd $(abspath $(src)); mkdir -p $(dir $@);       \
267                 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS);          \
268                 mv -f $@.tmp $@;                                \
269         '
270
271 bundled-sources::
272         @echo Bundled sources.
273
274 #---------- svg processing ----------
275
276 LIBRARIES ?= $(basename $(wildcard library/*.toml))
277
278 include $(addsuffix /files.make, $(LIBRARIES))
279
280 USVG_PROCESSOR = usvg-processor
281 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
282 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_BINARY) $(MAKEFILE_DEP)
283
284 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
285
286 library/%/files.make: media-scraper library/%.toml
287         ./$< --offline library/$*.toml
288
289 #---------- typescript ----------
290
291 templates/%.js: tsc-wrap tsconfig.json
292         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
293
294 templates/script.js: $(TS_SRC_FILES) stamp/wasm-bindgen
295
296 #---------- other templates ----------
297
298 $(addprefix templates/,$(LITFILES)): templates/%: %
299         cp $< $@.new && mv -f $@.new $@
300
301 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
302         cp $< $@.new && mv -f $@.new $@
303
304 libraries: $(LIBRARY_FILES)
305
306 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
307         $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
308         --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
309                 preview >$@.tmp && mv -f $@.tmp $@
310
311 #---------- webdriver tests (wdt) ----------
312
313 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
314 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
315
316 WDT_LANDSCAPE_TESTS = wdt-altergame
317
318 at:     $(foreach f, $(AT_TESTS), stamp/$f.check)
319
320 wdt:    $(foreach f, $(WDT_TESTS), stamp/$f.check) \
321         $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
322
323 RUNTEST_DEPS =  apitest/run1 stamp/cargo.debug $(FILEASSETS) \
324                 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
325
326 AT_DEPS =       $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
327                 stamp/cargo-at.debug
328
329 WDT_DEPS =      $(RUNTEST_DEPS) \
330                 stamp/cargo-wdt.debug
331
332 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
333
334 AT_RUN = $(AT_WDT_RUN) $(basename $(notdir $@))
335 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
336
337 for-at:                 $(AT_DEPS)
338 stamp/at-%.check:       $(AT_DEPS)
339         $(AT_RUN)
340         $(stamp)
341
342 for-wdt:                $(WDT_DEPS)
343 stamp/wdt-%.check:      $(WDT_DEPS)
344         $(WDT_RUN)
345         $(stamp)
346
347 stamp/wdt-%.lcheck:     $(WDT_DEPS)
348         $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
349         $(stamp)
350
351 #---------- deployment ----------
352
353 DEPLOY_USER=ian@login.chiark.greenend.org.uk
354 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
355 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
356
357 deploy: stamp/cargo.deploy-build bundled-sources assets libraries
358         rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
359         rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
360         rsync -r README.md $(DEPLOY_BASE)/.
361         rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
362         rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
363                 $(DEPLOY_BASE)/assets/
364         rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
365         ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
366         git branch -f $(DEPLOYED_BRANCH)
367         -git push origin master
368         -git push chiark master
369
370 #$(DEPLOY_BASE)/bundled-sources
371
372 #---------- clean ----------
373
374 clean-for-retest:
375         rm -f templates/script.js library/*/*.usvg stamp/*
376         rm -rf $(LIBRARY_CLEAN)
377         find * -name '*~' -print0 | xargs -0r rm --
378
379 clean: clean-nailing clean-for-retest
380         rm -rf target
381         $(NAILING_CARGO_JUST_RUN) rm -rf target