chiark / gitweb /
jstest: wip lower test, currently just a skeleton
[otter.git] / Makefile
1 # Copyright 2020-2021 Ian Jackson and contributors to Otter
2 # SPDX-License-Identifier: AGPL-3.0-or-later
3 # There is NO WARRANTY.
4
5 # make -j8
6 # make -j8 release
7 # make -j8 shapelib
8
9 SHELL=/bin/bash
10 src=.
11
12 default: all check
13 all: debug
14 full-check: all check cargo-syntaxcheck-release shapelib doc-sphinx
15 full-check: for-deploy release
16 everything: debug doc release check bundled-sources
17
18 shapelib: templates/shapelib.html docs/html/index.html
19         @echo 'Shape library preview and docs can now be found here:'
20         @for f in $^; do echo '  file://$(PWD)/'$$f; done
21
22 MAKEFILE_DEP ?= Makefile
23 MAKEFILE_FIND_X ?=
24 # ^ set this to "x" to debug the $rsrcs rune
25
26 #---------- funky macros etc. ----------
27
28 cr = $(addprefix --,$(filter-out debug,$1))
29 rsrcs = $(shell $(foreach x,$(MAKEFILE_FIND_X),set -$x;)\
30     find -H $1 \( -name Cargo.toml -o -name Cargo.lock -o -name Cargo.lock.example -o -name \*.rs \) \! -path '*/build/*' )
31 stamp=@mkdir -p stamp; touch $@
32
33 BUNDLED_SOURCES_LIT = README.md LICENCE
34 BUNDLED_SOURCES_FILES = index.html $(BUNDLED_SOURCES_LIT)
35 BUNDLED_SOURCES_LINKS += $(BUNDLED_SOURCES_LIT) otter/
36 BUNDLED_SOURCES += $(BUNDLED_SOURCES_FILES)
37
38 #---------- programs and config variables ----------
39
40 CARGO ?= cargo
41 TARGET_DIR ?= target
42
43 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
44
45 WASM_BINDGEN = $(TARGET_DIR)/debug/wasm-bindgen
46 WASM_BINDGEN_OPTIONS =                                          \
47         --remove-name-section --remove-producers-section        \
48         --typescript
49
50 BUNDLE_SOURCES ?= bundle-rust-sources
51
52 SPHINXBUILD   = sphinx-build
53
54 ifndef INKSCAPE_EXTENSIONS
55 INKSCAPE ?= inkscape
56 INKSCAPE_SDD_QUIETEN ?= 2>/dev/null
57 # inkscape 0.92.4: --extension-directory works, no --system-data-directory
58 # inkscape 1.0.2: --system-data-directory, no --extension-directory
59 INKSCAPE_EXTENSIONS := $(shell set -e; { sdd=$$( $(INKSCAPE) --system-data-directory $(INKSCAPE_SDD_QUIETEN) ) && echo "$$sdd/extensions"; } || $(INKSCAPE) --extension-directory )
60 endif
61 RECOLOUR_SVG ?= ./run-inkscape-extension $(INKSCAPE_EXTENSIONS)/color_replace.py
62
63 DEPLOY_ARCH=x86_64-unknown-linux-musl
64 DEPLOY_RELEASE=debug
65 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
66 DEPLOYED_BRANCH=deployed
67 PUBLISHED_BRANCH=published
68
69 #---------- nailing-cargo ----------
70
71 ifneq (,$(wildcard ../Cargo.nail))
72
73 NAILING_CARGO = nailing-cargo
74 CARGO = $(NAILING_CARGO)
75 BUILD_SUBDIR ?= ../Build
76 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
77
78 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
79 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
80 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
81 WASM_BINDGEN_CLI_CARGO_OPTS ?= --subcommand-props=!manifest-path
82
83 clean-nailing:
84         $(NAILING_CARGO_JUST_RUN) \
85  sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
86                 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
87
88 else
89 clean-nailing:
90 endif # Cargo.nail
91
92 BUILD_SUBDIR ?= ../Build
93 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
94 USVG_CMD ?= $(USVG)
95
96 WASM_PACKED=$(TARGET_DIR)/packed-wasm
97
98 #---------- local programs ----------
99
100 define lp
101 stamp/cargo.$2: $(call rsrcs, ! -name \*.rs)
102         $$(CARGO) build $(call cr,$3) -p $2
103         $$(stamp)
104 $1 := $(abspath $(TARGET_DIR)/$3/$4)
105 endef
106
107 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
108 $(eval $(call lp,USVG,usvg,release,usvg))
109
110 #---------- variables defining bits of source etc. ----------
111
112 PROGRAMS=daemon-otter otter
113
114 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
115 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
116
117 TS_SRCS= script
118 TS_SRC_FILES= \
119         $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
120         webassembly-types/webassembly.d.ts \
121         $(WASM_PACKED)/otter_wasm.d.ts
122
123 LITFILES= LICENCE AGPLv3
124 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
125
126 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
127                         $(LITFILES) $(TXTFILES)) \
128                 $(wildcard templates/*.tera)
129
130 WASM := wasm32-unknown-unknown
131 # ^ todo: Is this still right after
132 #     Use correct ABI for wasm32 by default
133 #     https://github.com/rust-lang/rust/pull/79998
134 # ?  But maybe it doesn't matter since we're very conservative and
135 # only pass JsValue and a few strings across the WASM ABI.
136
137 #---------- toplevel aggregate targets ----------
138
139 check: stamp/cargo.debug-check at wdt jstest
140         @echo 'Tests passed.'
141
142 full-check: stamp/cargo.release-check
143 full-check: stamp/cargo.release-miri stamp/cargo.debug-miri
144
145 full-check:
146         @echo 'Full tests passed.'
147
148 doc: cargo-doc doc-sphinx examples
149
150 debug release:: %: stamp/cargo.% assets libraries extra-%
151
152 cargo: cargo-debug cargo-wasm-release
153
154 cargo-debug cargo-release cargo-check cargo-doc \
155 cargo-wasm-debug cargo-wasm-release:: \
156 cargo-%: stamp/cargo.%
157
158 EXAMPLE_BUNDLES = test-bundle big-bundle
159 EXAMPLE_BUNDLE_FILES = $(foreach f, $(EXAMPLE_BUNDLES), examples/$f.zip)
160
161 examples: $(EXAMPLE_BUNDLE_FILES)
162 .PHONY: examples
163
164 cargo-wasm: cargo-wasm-release
165
166 wasm: stamp/wasm-bindgen
167
168 assets: js stamp/wasm-bindgen $(FILEASSETS)
169
170 js: templates/script.js
171
172 extra-debug:
173 extra-release: bundled-sources
174
175 cargo-syntaxcheck: cargo-syntaxcheck-host cargo-syntaxcheck-wasm
176 cargo-syntaxcheck-host:
177         $(CARGO) check --workspace
178 cargo-syntaxcheck-wasm:
179         $(CARGO) check --target $(WASM) -p otter-wasm --release
180 cargo-syntaxcheck-release:
181         $(CARGO) check --workspace --release
182
183 #---------- cargo ----------
184
185 DR=debug release
186 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
187
188 $(addprefix stamp/cargo.,$(DR)):: \
189 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
190         $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon -p otter-cli
191         $(NAILING_CARGO_JUST_RUN) \
192         ln -sf otter $(abspath $(TARGET_DIR))/$*/otter-ssh-proxy
193         $(stamp)
194
195 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
196         $(CARGO) build --workspace -p otter-cli
197         $(NAILING_CARGO_JUST_RUN) touch $(abspath $@)
198
199 stamp/cargo.wasm-bindgen: $(call rsrcs, ! -name \*.rs)
200         $(CARGO) $(WASM_BINDGEN_CLI_CARGO_OPTS) build --target-dir=target \
201                 --manifest-path=$(abspath wasm/Cargo.toml) -p wasm-bindgen-cli
202         $(stamp)
203
204 stamp/cargo.%-check: $(call rsrcs,.)
205         $(CARGO) test --workspace $(call cr,$*)
206         $(stamp)
207
208 stamp/cargo.%-miri: $(call rsrcs,.)
209         $(CARGO) miri test --workspace $(call cr,$*)
210         $(stamp)
211
212 stamp/cargo-at.debug: $(call rsrcs,.)
213         $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
214         $(stamp)
215
216 stamp/cargo-wdt.debug: $(call rsrcs,.)
217         $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
218         $(stamp)
219
220 stamp/cargo.doc: $(call rsrcs,.)
221         set -o pipefail -e; \
222         $(CARGO) doc $(CARGO_DOC_OPTS) --workspace 2>&1 |egrep -vf .cargo-doc-suppress-errors
223         $(stamp)
224
225 $(addprefix stamp/cargo.wasm-,$(DR)):: \
226 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
227         $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
228         $(stamp)
229
230 stamp/cargo.deploy-build: $(call rsrcs,.)
231         $(CARGO) build --target $(DEPLOY_ARCH) $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-cli -p otter-daemon
232         $(NAILING_CARGO_JUST_RUN) \
233         ln -sf otter $(abspath $(TARGET_DIR)/$(DEPLOY_ARCH))/$(DEPLOY_RELEASE)/otter-ssh-proxy
234         $(stamp)
235
236 #---------- sphnix ----------
237
238 doc-sphinx:     docs/html/index.html \
239         $(foreach f, $(EXAMPLE_BUNDLES), docs/html/examples/$f.zip) \
240         $(addprefix docs/html/examples/, $(notdir $(wildcard specs/*.toml)))
241         @echo 'Documentation can now be found here:'
242         @echo '  file://$(PWD)/$<'
243
244 docs/html/index.html: docs/conf.py $(wildcard docs/*.md docs/*.rst docs/*.png)
245         $(SPHINXBUILD) -M html docs docs $(SPHINXOPTS)
246
247 docs/html/examples/%.zip: examples/%.zip
248         mkdir -p docs/html/examples
249         rm -f $@ && ln $< $@
250
251 docs/html/examples/%.toml: specs/%.toml
252         mkdir -p docs/html/examples
253         rm -f $@ && ln $< $@
254
255 #---------- jstest ----------
256
257 JSTESTS= basic lower
258
259 .PHONY: jstest
260 jstest jstests: $(foreach t,$(JSTESTS),stamp/$t.jstest)
261
262 stamp/%.jstest: jstest/run1 jstest/%.nodejs templates/script.js \
263                 stamp/wasm-bindgen-jstest
264         $(NAILING_CARGO_JUST_RUN) $(abspath $(filter-out stamp/%,$^))
265         $(stamp)
266
267 #---------- wasm ----------
268
269 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-bindgen
270 stamp/wasm-bindgen: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
271         $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
272                 $(WASM_BINDGEN_OPTIONS) --no-modules \
273                 --out-dir target/packed-wasm \
274                 target/$(WASM)/release/otter_wasm.wasm
275         $(stamp)
276
277 stamp/wasm-bindgen-jstest: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
278         $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
279                 $(WASM_BINDGEN_OPTIONS) --nodejs \
280                 --out-dir target/jstest \
281                 target/$(WASM)/release/otter_wasm.wasm
282         $(stamp)
283
284 #---------- bundle-sources ----------
285
286 BUNDLED_SOURCES_DIRS += otter
287
288 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
289
290 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
291
292 $(TARGET_BUNDLED):
293         $(NAILING_CARGO_JUST_RUN) mkdir -p $(abspath $@)
294
295 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
296 bundled-sources/%: stamp/cargo.bundle-sources $(TARGET_BUNDLED)
297         set -e; d=$(abspath $(TARGET_BUNDLED)); \
298         $(if $(filter-out otter,$*), cd ../$*;) \
299         $(BUNDLE_SOURCES_CMD) --output $$d/$*
300
301 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
302
303 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): \
304 $(TARGET_BUNDLED)/%: % $(TARGET_BUNDLED)
305         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
306
307 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index \
308                 $(MAKEFILE_DEP) $(TARGET_BUNDLED)
309         $(NAILING_CARGO_JUST_RUN) sh -ec '                      \
310                 cd $(abspath $(src)); mkdir -p $(dir $@);       \
311                 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS);          \
312                 mv -f $@.tmp $@;                                \
313         '
314
315 bundled-sources::
316         @echo Bundled sources.
317
318 #---------- svg processing ----------
319
320 LIBRARIES ?= $(basename $(wildcard library/*.toml))
321 USVG_DEP = stamp/cargo.usvg
322
323 include $(addsuffix /files.make, $(LIBRARIES))
324
325 USVG_PROCESSOR = usvg-processor
326 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
327 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_DEP) $(MAKEFILE_DEP)
328
329 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
330
331 library/%/files.make: media-scraper library/%.toml
332         ./$< --offline library/$*.toml
333
334 #---------- typescript ----------
335
336 templates/%.js: tsc-wrap tsconfig.json
337         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
338
339 templates/script.js: $(TS_SRC_FILES) stamp/wasm-bindgen
340
341 #---------- other templates ----------
342
343 $(addprefix templates/,$(LITFILES)): templates/%: %
344         cp $< $@.new && mv -f $@.new $@
345
346 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
347         cp $< $@.new && mv -f $@.new $@
348
349 libraries: $(LIBRARY_FILES)
350
351 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
352         $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
353         --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
354                 preview >$@.tmp && mv -f $@.tmp $@
355
356 #---------- examples ----------
357
358 EXAMPLE_BUNDLE_INPUT_DEPS := $(shell                                    \
359         cd examples/test-bundle/ && find \! \( -name '*~' -o -name '.*' \) \
360 )
361
362 examples/%.zip: $(MAKEFILE_DEP)
363         set -e; rm -f $@.tmp; cd examples/$*/; \
364         zip $(ZIPFLAGS) -DX ../$(notdir $@).tmp $(ZIP_INPUTS)
365         mv -f $@.tmp $@
366
367 examples/test-bundle.zip: ZIP_INPUTS=$(EXAMPLE_BUNDLE_INPUT_DEPS)
368 examples/test-bundle.zip: \
369  $(addprefix examples/test-bundle/, $(EXAMPLE_BUNDLE_INPUT_DEPS))
370
371 examples/big-bundle.zip: examples/big-bundle/otter.toml
372 examples/big-bundle.zip: ZIPFLAGS+= -0 -r -q
373 examples/big-bundle.zip: ZIP_INPUTS=.
374 examples/big-bundle/otter.toml: $(LIBRARY_FILES) $(MAKEFILE_DEP)
375         rm -rf examples/big-bundle
376         mkdir examples/big-bundle examples/big-bundle/library
377         @set -e; echo 'MKDIR for $@'; \
378         for l in $(LIBRARIES); do \
379                 mkdir examples/big-bundle/$$l; \
380                 perl -p \
381  -e 'BEGIN { print "# -- AUTOGENERATED FROM COPY IN OTTER SOURCE --\n" }' \
382  -e 'if (m/^\[scraper]/..0) { unless (m/^\[(?!scraper)/..0) { s/^/\#/ } }' \
383  -e 's/(?<=\s)\w\S*(?=\s)/-/ if m/^files = """/..m/^"""/;' \
384                         <$$l.toml >examples/big-bundle/$$l.toml; done
385         @set -e; echo 'LN for $@'; \
386         for e in $(LIBRARY_FILE_INPUTS); do \
387                 ln $${e#*:} examples/big-bundle/$${e%%.usvg:*}.svg; done;
388         @set -e; echo 'MV for $@'; \
389         cd examples/big-bundle/library/; for x in '' .toml; do \
390                 mv wikimedia$$x duped-example$$x; done
391         @set -e; mkdir examples/big-bundle/specs/; \
392         perl -pe <specs/demo.game.toml \
393                 >examples/big-bundle/specs/Modded-spec.game.toml \
394                 's/chess-b-/chess-purple-/; s/chess-w-/chess-yellow-/'
395         @set -e; echo 'ECHO for $@'; \
396         echo 'title = "Autogenerated test bundle - do not distribute"' \
397                 >$@.tmp
398         mv -f $@.tmp $@
399
400 #---------- webdriver tests (wdt) ----------
401
402 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
403 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
404
405 WDT_LANDSCAPE_TESTS = wdt-altergame
406
407 at:     $(foreach f, $(AT_TESTS), stamp/$f.check)
408
409 wdt:    $(foreach f, $(WDT_TESTS), stamp/$f.check) \
410         $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
411
412 RUNTEST_DEPS =  apitest/run1 stamp/cargo.debug $(FILEASSETS) \
413                 $(wildcard specs/*.toml) examples/test-bundle.zip \
414                 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
415
416 AT_DEPS =       $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
417                 examples/big-bundle.zip \
418                 stamp/cargo-at.debug
419
420 WDT_DEPS =      $(RUNTEST_DEPS) \
421                 stamp/cargo-wdt.debug
422
423 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
424
425 AT_RUN = $(AT_WDT_RUN) apitest --test=$(basename $(notdir $@))
426 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
427
428 for-at:                 $(AT_DEPS)
429 stamp/at-%.check:       $(AT_DEPS)
430         $(AT_RUN)
431         $(stamp)
432
433 for-wdt:                $(WDT_DEPS)
434 stamp/wdt-%.check:      $(WDT_DEPS)
435         $(WDT_RUN)
436         $(stamp)
437
438 stamp/wdt-%.lcheck:     $(WDT_DEPS)
439         $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
440         $(stamp)
441
442 #---------- docs publication ----------
443
444 PUBLISH_USER=ianmdlvl@login.chiark.greenend.org.uk
445 PUBLISH_DOC_SPHINX=$(PUBLISH_USER):public-html/otter/docs
446
447 publish: doc-sphinx
448         rsync -r --delete-delay docs/html/. $(PUBLISH_DOC_SPHINX)/.
449         git branch -f $(PUBLISHED_BRANCH)
450
451 #---------- deployment ----------
452
453 DEPLOY_USER=ian@login.chiark.greenend.org.uk
454 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
455 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
456
457 for-deploy: stamp/cargo.deploy-build
458 deploy: for-deploy bundled-sources assets libraries
459         rsync -zvl --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS) otter-ssh-proxy) $(DEPLOY_BASE)/bin/
460         rsync -zv --progress $(TARGET_DIR)/release/usvg $(DEPLOY_BASE)/libexec/
461         rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
462         rsync -r README.md $(DEPLOY_BASE)/.
463         rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
464         rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
465                 $(DEPLOY_BASE)/assets/
466         rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
467         ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
468         git branch -f $(DEPLOYED_BRANCH)
469         -git push origin main
470         -git push chiark main
471
472 #$(DEPLOY_BASE)/bundled-sources
473
474 #---------- clean ----------
475
476 clean-for-retest:
477         rm -f templates/script.js library/*/*.usvg stamp/*
478         rm -rf $(LIBRARY_CLEAN)
479         rm -rf examples/big-bundle examples/big-bundle.zip
480         rm -f examples/test-bundle.zip
481         find * \( -name '*~' -o -name '*.tmp' \) -print0 | xargs -0r rm --
482
483 clean: clean-nailing clean-for-retest
484         rm -rf target
485         $(NAILING_CARGO_JUST_RUN) rm -rf target