1 # Copyright 2020-2021 Ian Jackson and contributors to Otter
2 # SPDX-License-Identifier: AGPL-3.0-or-later
3 # There is NO WARRANTY.
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
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
22 MAKEFILE_DEP ?= Makefile
24 # ^ set this to "x" to debug the $rsrcs rune
26 #---------- funky macros etc. ----------
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 $@
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)
38 #---------- programs and config variables ----------
42 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
44 WASM_BINDGEN = $(TARGET_DIR)/debug/wasm-bindgen
45 WASM_BINDGEN_OPTIONS = \
46 --remove-name-section --remove-producers-section \
49 BUNDLE_SOURCES ?= bundle-rust-sources
51 SPHINXBUILD ?= sphinx-build
53 ifndef INKSCAPE_EXTENSIONS
55 INKSCAPE_SDD_QUIETEN ?= 2>/dev/null
56 # inkscape 0.92.4: --extension-directory works, no --system-data-directory
57 # inkscape 1.0.2: --system-data-directory, no --extension-directory
58 INKSCAPE_EXTENSIONS := $(shell set -e; { sdd=$$( $(INKSCAPE) --system-data-directory $(INKSCAPE_SDD_QUIETEN) ) && echo "$$sdd/extensions"; } || $(INKSCAPE) --extension-directory )
60 RECOLOUR_SVG ?= ./run-inkscape-extension $(INKSCAPE_EXTENSIONS)/color_replace.py
62 DEPLOY_ARCH=x86_64-unknown-linux-musl
63 DEPLOY_RELEASE=release
64 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
65 DEPLOYED_BRANCH=deployed
66 PUBLISHED_BRANCH=published
68 RUST_CLIPPY_OPTIONS ?= $(shell perl -pe 's/\#.*//; s/\n/ /' clippy-options)
70 RUST_CLIPPY_CMD := clippy $(RUST_CLIPPY_OPTIONS)
72 #---------- nailing-cargo ----------
74 ifneq (,$(wildcard ../Cargo.nail))
76 NAILING_CARGO ?= nailing-cargo
77 CARGO_CMD ?= $(NAILING_CARGO)
78 BUILD_SUBDIR ?= ../Build
79 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
81 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
82 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
83 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
84 WASM_BINDGEN_CLI_CARGO_OPTS ?= --subcommand-props=!manifest-path
86 CARGO_DOCUMENT_PRIVATE_ITEMS ?= --document-private-items
87 CARGO_DOC_OPTS += $(CARGO_DOCUMENT_PRIVATE_ITEMS)
90 $(NAILING_CARGO_JUST_RUN) \
91 sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
92 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
99 BUILD_SUBDIR ?= ../Build
100 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
103 WASM_PACKED=$(TARGET_DIR)/packed-wasm
105 #---------- local programs ----------
108 stamp/cargo.$2: $(call rsrcs, ! -name \*.rs)
109 $$(CARGO) build $(call cr,$3) -p $2
111 $1 := $(abspath $(TARGET_DIR)/$3/$4)
114 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
115 $(eval $(call lp,USVG,usvg,release,usvg))
117 #---------- variables defining bits of source etc. ----------
119 PROGRAMS=daemon-otter otter
121 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
122 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
126 $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
127 webassembly-types/webassembly.d.ts \
128 $(WASM_PACKED)/otter_wasm.d.ts
130 LITFILES= LICENCE AGPLv3
131 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
133 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
134 $(LITFILES) $(TXTFILES)) \
135 $(wildcard templates/*.tera) \
136 nwtemplates/die-overlay.tera
138 WASM := wasm32-unknown-unknown
139 # ^ todo: Is this still right after
140 # Use correct ABI for wasm32 by default
141 # https://github.com/rust-lang/rust/pull/79998
142 # ? But maybe it doesn't matter since we're very conservative and
143 # only pass JsValue and a few strings across the WASM ABI.
145 CARGO = $(CARGO_CMD) $(RUST_VERSION)
147 #---------- toplevel aggregate targets ----------
149 check: stamp/cargo.debug-check at wdt jstest
150 @echo 'Tests passed.'
152 full-check: stamp/cargo.release-check
153 full-check: stamp/cargo.release-miri stamp/cargo.debug-miri
156 @echo 'Full tests passed.'
158 doc: cargo-doc doc-sphinx examples
160 debug release:: %: stamp/cargo.% assets libraries extra-%
162 cargo: cargo-debug cargo-wasm-release
164 cargo-debug cargo-release cargo-check cargo-doc \
165 cargo-wasm-debug cargo-wasm-release:: \
166 cargo-%: stamp/cargo.%
168 EXAMPLE_BUNDLES = test-bundle big-bundle
169 EXAMPLE_BUNDLE_FILES = $(foreach f, $(EXAMPLE_BUNDLES), examples/$f.zip)
171 examples: $(EXAMPLE_BUNDLE_FILES)
174 cargo-wasm: cargo-wasm-release
176 wasm: stamp/wasm-bindgen
178 assets: js stamp/wasm-bindgen $(FILEASSETS)
180 js: templates/script.js
183 extra-release: bundled-sources
185 cargo-syntaxcheck: cargo-syntaxcheck-host cargo-syntaxcheck-wasm
186 cargo-syntaxcheck-host:
187 $(CARGO) check --workspace
188 cargo-syntaxcheck-wasm:
189 $(CARGO) check --target $(WASM) -p otter-wasm --release
190 cargo-syntaxcheck-release:
191 $(CARGO) check --workspace --release
193 cargo-clippy: cargo-clippy-host cargo-clippy-wasm
194 cargo-clippy-host: clippy-options
195 $(CARGO) $(RUST_CLIPPY) --workspace $(RUST_CLIPPY_OPTIONS)
196 cargo-clippy-wasm: clippy-options
197 $(CARGO) $(RUST_CLIPPY) --target $(WASM) -p otter-wasm $(RUST_CLIPPY_OPTIONS)
199 #---------- cargo ----------
202 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
204 $(addprefix stamp/cargo.,$(DR)):: \
205 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
206 $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon -p otter-cli
207 $(NAILING_CARGO_JUST_RUN) \
208 ln -sf otter $(abspath $(TARGET_DIR))/$*/otter-ssh-proxy
211 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
212 $(CARGO) build --workspace -p otter-cli
213 $(NAILING_CARGO_JUST_RUN) touch $(abspath $@)
215 stamp/cargo.wasm-bindgen: $(call rsrcs, ! -name \*.rs)
216 $(CARGO_CMD) $(WASM_BINDGEN_CLI_CARGO_OPTS) $(RUST_VERSION) \
217 build --target-dir=target \
218 --manifest-path=$(abspath wasm/Cargo.toml) -p wasm-bindgen-cli
221 stamp/cargo.%-check: $(call rsrcs,.)
222 $(CARGO) test --workspace $(call cr,$*)
225 stamp/cargo.%-miri: $(call rsrcs,.)
226 $(CARGO) miri test --workspace $(call cr,$*)
229 stamp/cargo-at.debug: $(call rsrcs,.)
230 $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
233 stamp/cargo-wdt.debug: $(call rsrcs,.)
234 $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
237 stamp/cargo-jstest.debug: $(call rsrcs,.)
238 $(CARGO) build --workspace $(call cr,$*) -p otter-nodejs-tests
241 stamp/cargo.doc: $(call rsrcs,.)
242 set -o pipefail -e; \
243 $(CARGO) doc $(CARGO_DOC_OPTS) --workspace 2>&1 \
244 |egrep -vf .cargo-doc-suppress-errors
247 $(addprefix stamp/cargo.wasm-,$(DR)):: \
248 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
249 $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
252 stamp/cargo.deploy-build: $(call rsrcs,.)
253 $(CARGO) build --target $(DEPLOY_ARCH) $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-cli -p otter-daemon
254 $(NAILING_CARGO_JUST_RUN) \
255 ln -sf otter $(abspath $(TARGET_DIR)/$(DEPLOY_ARCH))/$(DEPLOY_RELEASE)/otter-ssh-proxy
258 #---------- sphnix ----------
260 doc-sphinx: docs/html/index.html \
261 $(foreach f, $(EXAMPLE_BUNDLES), docs/html/examples/$f.zip) \
262 $(addprefix docs/html/examples/, $(notdir $(wildcard specs/*.toml)))
263 @echo 'Documentation can now be found here:'
264 @echo ' file://$(PWD)/$<'
266 docs/html/index.html: docs/conf.py $(wildcard docs/*.md docs/*.rst docs/*.png)
267 $(SPHINXBUILD) -M html docs docs $(SPHINXOPTS)
269 docs/html/examples/%.zip: examples/%.zip
270 mkdir -p docs/html/examples
273 docs/html/examples/%.toml: specs/%.toml
274 mkdir -p docs/html/examples
277 #---------- jstest ----------
282 jstest jstests: $(foreach t,$(JSTESTS),stamp/$t.jstest)
284 stamp/%.jstest: jstest/run1 jstest/%.nodejs templates/script.js \
285 stamp/wasm-bindgen-jstest stamp/cargo-jstest.debug
286 $(NAILING_CARGO_JUST_RUN) $(abspath $(filter-out stamp/%,$^))
289 #---------- wasm ----------
291 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-bindgen
292 stamp/wasm-bindgen: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
293 $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
294 $(WASM_BINDGEN_OPTIONS) --no-modules \
295 --out-dir target/packed-wasm \
296 target/$(WASM)/release/otter_wasm.wasm
299 stamp/wasm-bindgen-jstest: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
300 $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
301 $(WASM_BINDGEN_OPTIONS) --nodejs \
302 --out-dir target/jstest \
303 target/$(WASM)/release/otter_wasm.wasm
306 #---------- bundle-sources ----------
308 BUNDLED_SOURCES_DIRS += otter
310 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
312 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
315 $(NAILING_CARGO_JUST_RUN) mkdir -p $(abspath $@)
317 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
318 bundled-sources/%: stamp/cargo.bundle-sources $(TARGET_BUNDLED)
319 set -e; d=$(abspath $(TARGET_BUNDLED)); \
320 $(if $(filter-out otter,$*), cd ../$*;) \
321 $(BUNDLE_SOURCES_CMD) --output $$d/$*
323 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
325 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): \
326 $(TARGET_BUNDLED)/%: % $(TARGET_BUNDLED)
327 $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
329 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index \
330 $(MAKEFILE_DEP) $(TARGET_BUNDLED)
331 $(NAILING_CARGO_JUST_RUN) sh -ec ' \
332 cd $(abspath $(src)); mkdir -p $(dir $@); \
333 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS); \
338 @echo Bundled sources.
340 #---------- svg processing ----------
342 LIBRARIES ?= $(basename $(wildcard library/*.toml))
343 USVG_DEP = stamp/cargo.usvg
345 include $(addsuffix /files.make, $(LIBRARIES))
347 USVG_PROCESSOR = usvg-processor
348 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
349 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_DEP) $(MAKEFILE_DEP)
351 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
353 library/%/files.make: media-scraper library/%.toml
354 ./$< --offline library/$*.toml
356 #---------- typescript ----------
358 templates/%.js: tsc-wrap tsconfig.json
359 ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
361 templates/script.js: $(TS_SRC_FILES) stamp/wasm-bindgen
363 #---------- other templates ----------
365 $(addprefix templates/,$(LITFILES)): templates/%: %
366 cp $< $@.new && mv -f $@.new $@
368 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
369 cp $< $@.new && mv -f $@.new $@
371 libraries: $(LIBRARY_FILES)
373 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
374 $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
375 --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
376 preview >$@.tmp && mv -f $@.tmp $@
378 nwtemplates/die-overlay.tera: dice/overlay-template-extractor dice/die.svg
379 ./$< <dice/die.svg >$@.tmp && mv -f $@.tmp $@
381 #---------- examples ----------
383 EXAMPLE_BUNDLE_INPUT_DEPS := $(shell \
384 cd examples/test-bundle/ && find \! \( -name '*~' -o -name '.*' \) \
387 examples/%.zip: $(MAKEFILE_DEP)
388 set -e; rm -f $@.tmp; cd examples/$*/; \
389 zip $(ZIPFLAGS) -DX ../$(notdir $@).tmp $(ZIP_INPUTS)
392 examples/test-bundle.zip: ZIP_INPUTS=$(EXAMPLE_BUNDLE_INPUT_DEPS)
393 examples/test-bundle.zip: \
394 $(addprefix examples/test-bundle/, $(EXAMPLE_BUNDLE_INPUT_DEPS))
396 examples/big-bundle.zip: examples/big-bundle/otter.toml
397 examples/big-bundle.zip: ZIPFLAGS+= -0 -r -q
398 examples/big-bundle.zip: ZIP_INPUTS=.
399 examples/big-bundle/otter.toml: $(LIBRARY_FILES) $(MAKEFILE_DEP)
400 rm -rf examples/big-bundle
401 mkdir examples/big-bundle examples/big-bundle/library
402 @set -e; echo 'MKDIR for $@'; \
403 for l in $(LIBRARIES); do \
404 mkdir examples/big-bundle/$$l; \
406 -e 'BEGIN { print "# -- AUTOGENERATED FROM COPY IN OTTER SOURCE --\n" }' \
407 -e 'if (m/^\[scraper]/..0) { unless (m/^\[(?!scraper)/..0) { s/^/\#/ } }' \
408 -e 's/(?<=\s)\w\S*(?=\s)/-/ if m/^files = """/..m/^"""/;' \
409 <$$l.toml >examples/big-bundle/$$l.toml; done
410 @set -e; echo 'LN for $@'; \
411 for e in $(LIBRARY_FILE_INPUTS); do \
412 ln $${e#*:} examples/big-bundle/$${e%%.usvg:*}.svg; done;
413 @set -e; echo 'MV for $@'; \
414 cd examples/big-bundle/library/; for x in '' .toml; do \
415 mv wikimedia$$x duped-example$$x; done
416 @set -e; mkdir examples/big-bundle/specs/; \
417 perl -pe <specs/demo.game.toml \
418 >examples/big-bundle/specs/Modded-spec.game.toml \
419 's/chess-b-/chess-purple-/; s/chess-w-/chess-yellow-/'
420 @set -e; echo 'ECHO for $@'; \
421 echo 'title = "Autogenerated test bundle - do not distribute"' \
425 #---------- webdriver tests (wdt) ----------
427 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
428 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
430 WDT_LANDSCAPE_TESTS = wdt-altergame
432 at: $(foreach f, $(AT_TESTS), stamp/$f.check)
434 wdt: $(foreach f, $(WDT_TESTS), stamp/$f.check) \
435 $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
437 RUNTEST_DEPS = apitest/run1 stamp/cargo.debug $(FILEASSETS) \
438 $(wildcard specs/*.toml) examples/test-bundle.zip \
439 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
441 AT_DEPS = $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
442 examples/big-bundle.zip \
445 WDT_DEPS = $(RUNTEST_DEPS) wdriver/firefox-wrapper \
446 stamp/cargo-wdt.debug
448 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
450 AT_RUN = $(AT_WDT_RUN) apitest --test=$(basename $(notdir $@))
451 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
454 stamp/at-%.check: $(AT_DEPS)
459 stamp/wdt-%.check: $(WDT_DEPS)
463 stamp/wdt-%.lcheck: $(WDT_DEPS)
464 $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
467 #---------- docs publication ----------
469 PUBLISH_VERSION=unreleased
470 PUBLISH_USER=ianmdlvl@login.chiark.greenend.org.uk
471 PUBLISH_DOC_SPHINX_BASE=public-html/otter
472 PUBLISH_DOC_SPHINX_TAIL=$(PUBLISH_VERSION)/docs
473 PUBLISH_DOC_SPHINX=$(PUBLISH_USER):$(PUBLISH_DOC_SPHINX_BASE)/$(PUBLISH_DOC_SPHINX_TAIL)
476 ssh $(PUBLISH_USER) 'cd $(PUBLISH_DOC_SPHINX_BASE) && mkdir -p $(PUBLISH_DOC_SPHINX_TAIL)'
477 rsync -r --delete-delay docs/html/. $(PUBLISH_DOC_SPHINX)/.
478 git branch -f $(PUBLISHED_BRANCH)
480 #---------- deployment ----------
482 DEPLOY_USER=ian@login.chiark.greenend.org.uk
483 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
484 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
486 for-deploy: stamp/cargo.deploy-build
487 deploy: for-deploy bundled-sources assets libraries
488 rsync -zvl --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS) otter-ssh-proxy) $(DEPLOY_BASE)/bin/
489 rsync -zv --progress $(TARGET_DIR)/release/usvg $(DEPLOY_BASE)/libexec/
490 rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
491 rsync -r README.md $(DEPLOY_BASE)/.
492 rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
493 rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
494 $(DEPLOY_BASE)/assets/
495 rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
496 ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
497 git branch -f $(DEPLOYED_BRANCH)
498 -git push origin main
499 -git push chiark main
501 #$(DEPLOY_BASE)/bundled-sources
503 #---------- clean ----------
506 rm -f templates/script.js library/*/*.usvg stamp/*
507 rm -rf $(LIBRARY_CLEAN)
508 rm -rf examples/big-bundle examples/big-bundle.zip
509 rm -f examples/test-bundle.zip
510 find * \( -name '*~' -o -name '*.tmp' \) -print0 | xargs -0r rm --
512 clean: clean-nailing clean-for-retest
514 $(NAILING_CARGO_JUST_RUN) rm -rf target