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 ----------
43 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
45 WASM_BINDGEN = $(TARGET_DIR)/debug/wasm-bindgen
46 WASM_BINDGEN_OPTIONS = \
47 --remove-name-section --remove-producers-section \
50 BUNDLE_SOURCES ?= bundle-rust-sources
52 SPHINXBUILD = sphinx-build
54 ifndef INKSCAPE_EXTENSIONS
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 )
61 RECOLOUR_SVG ?= ./run-inkscape-extension $(INKSCAPE_EXTENSIONS)/color_replace.py
63 DEPLOY_ARCH=x86_64-unknown-linux-musl
65 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
66 DEPLOYED_BRANCH=deployed
67 PUBLISHED_BRANCH=published
69 #---------- nailing-cargo ----------
71 ifneq (,$(wildcard ../Cargo.nail))
73 NAILING_CARGO = nailing-cargo
74 CARGO = $(NAILING_CARGO)
75 BUILD_SUBDIR ?= ../Build
76 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
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
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)))
92 BUILD_SUBDIR ?= ../Build
93 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
96 WASM_PACKED=$(TARGET_DIR)/packed-wasm
98 #---------- local programs ----------
101 stamp/cargo.$2: $(call rsrcs, ! -name \*.rs)
102 $$(CARGO) build $(call cr,$3) -p $2
104 $1 := $(abspath $(TARGET_DIR)/$3/$4)
107 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
108 $(eval $(call lp,USVG,usvg,release,usvg))
110 #---------- variables defining bits of source etc. ----------
112 PROGRAMS=daemon-otter otter
114 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
115 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
119 $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
120 webassembly-types/webassembly.d.ts \
121 $(WASM_PACKED)/otter_wasm.d.ts
123 LITFILES= LICENCE AGPLv3
124 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
126 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
127 $(LITFILES) $(TXTFILES)) \
128 $(wildcard templates/*.tera)
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.
137 #---------- toplevel aggregate targets ----------
139 check: stamp/cargo.debug-check at wdt jstest
140 @echo 'Tests passed.'
142 full-check: stamp/cargo.release-check
143 full-check: stamp/cargo.release-miri stamp/cargo.debug-miri
146 @echo 'Full tests passed.'
148 doc: cargo-doc doc-sphinx examples
150 debug release:: %: stamp/cargo.% assets libraries extra-%
152 cargo: cargo-debug cargo-wasm-release
154 cargo-debug cargo-release cargo-check cargo-doc \
155 cargo-wasm-debug cargo-wasm-release:: \
156 cargo-%: stamp/cargo.%
158 EXAMPLE_BUNDLES = test-bundle big-bundle
159 EXAMPLE_BUNDLE_FILES = $(foreach f, $(EXAMPLE_BUNDLES), examples/$f.zip)
161 examples: $(EXAMPLE_BUNDLE_FILES)
164 cargo-wasm: cargo-wasm-release
166 wasm: stamp/wasm-bindgen
168 assets: js stamp/wasm-bindgen $(FILEASSETS)
170 js: templates/script.js
173 extra-release: bundled-sources
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
183 #---------- cargo ----------
186 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
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
195 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
196 $(CARGO) build --workspace -p otter-cli
197 $(NAILING_CARGO_JUST_RUN) touch $(abspath $@)
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
204 stamp/cargo.%-check: $(call rsrcs,.)
205 $(CARGO) test --workspace $(call cr,$*)
208 stamp/cargo.%-miri: $(call rsrcs,.)
209 $(CARGO) miri test --workspace $(call cr,$*)
212 stamp/cargo-at.debug: $(call rsrcs,.)
213 $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
216 stamp/cargo-wdt.debug: $(call rsrcs,.)
217 $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
220 stamp/cargo-jstest.debug: $(call rsrcs,.)
221 $(CARGO) build --workspace $(call cr,$*) -p otter-nodejs-tests
224 stamp/cargo.doc: $(call rsrcs,.)
225 set -o pipefail -e; \
226 $(CARGO) doc $(CARGO_DOC_OPTS) --workspace 2>&1 |egrep -vf .cargo-doc-suppress-errors
229 $(addprefix stamp/cargo.wasm-,$(DR)):: \
230 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
231 $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
234 stamp/cargo.deploy-build: $(call rsrcs,.)
235 $(CARGO) build --target $(DEPLOY_ARCH) $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-cli -p otter-daemon
236 $(NAILING_CARGO_JUST_RUN) \
237 ln -sf otter $(abspath $(TARGET_DIR)/$(DEPLOY_ARCH))/$(DEPLOY_RELEASE)/otter-ssh-proxy
240 #---------- sphnix ----------
242 doc-sphinx: docs/html/index.html \
243 $(foreach f, $(EXAMPLE_BUNDLES), docs/html/examples/$f.zip) \
244 $(addprefix docs/html/examples/, $(notdir $(wildcard specs/*.toml)))
245 @echo 'Documentation can now be found here:'
246 @echo ' file://$(PWD)/$<'
248 docs/html/index.html: docs/conf.py $(wildcard docs/*.md docs/*.rst docs/*.png)
249 $(SPHINXBUILD) -M html docs docs $(SPHINXOPTS)
251 docs/html/examples/%.zip: examples/%.zip
252 mkdir -p docs/html/examples
255 docs/html/examples/%.toml: specs/%.toml
256 mkdir -p docs/html/examples
259 #---------- jstest ----------
264 jstest jstests: $(foreach t,$(JSTESTS),stamp/$t.jstest)
266 stamp/%.jstest: jstest/run1 jstest/%.nodejs templates/script.js \
267 stamp/wasm-bindgen-jstest stamp/cargo-jstest.debug
268 $(NAILING_CARGO_JUST_RUN) $(abspath $(filter-out stamp/%,$^))
271 #---------- wasm ----------
273 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-bindgen
274 stamp/wasm-bindgen: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
275 $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
276 $(WASM_BINDGEN_OPTIONS) --no-modules \
277 --out-dir target/packed-wasm \
278 target/$(WASM)/release/otter_wasm.wasm
281 stamp/wasm-bindgen-jstest: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
282 $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
283 $(WASM_BINDGEN_OPTIONS) --nodejs \
284 --out-dir target/jstest \
285 target/$(WASM)/release/otter_wasm.wasm
288 #---------- bundle-sources ----------
290 BUNDLED_SOURCES_DIRS += otter
292 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
294 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
297 $(NAILING_CARGO_JUST_RUN) mkdir -p $(abspath $@)
299 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
300 bundled-sources/%: stamp/cargo.bundle-sources $(TARGET_BUNDLED)
301 set -e; d=$(abspath $(TARGET_BUNDLED)); \
302 $(if $(filter-out otter,$*), cd ../$*;) \
303 $(BUNDLE_SOURCES_CMD) --output $$d/$*
305 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
307 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): \
308 $(TARGET_BUNDLED)/%: % $(TARGET_BUNDLED)
309 $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
311 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index \
312 $(MAKEFILE_DEP) $(TARGET_BUNDLED)
313 $(NAILING_CARGO_JUST_RUN) sh -ec ' \
314 cd $(abspath $(src)); mkdir -p $(dir $@); \
315 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS); \
320 @echo Bundled sources.
322 #---------- svg processing ----------
324 LIBRARIES ?= $(basename $(wildcard library/*.toml))
325 USVG_DEP = stamp/cargo.usvg
327 include $(addsuffix /files.make, $(LIBRARIES))
329 USVG_PROCESSOR = usvg-processor
330 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
331 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_DEP) $(MAKEFILE_DEP)
333 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
335 library/%/files.make: media-scraper library/%.toml
336 ./$< --offline library/$*.toml
338 #---------- typescript ----------
340 templates/%.js: tsc-wrap tsconfig.json
341 ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
343 templates/script.js: $(TS_SRC_FILES) stamp/wasm-bindgen
345 #---------- other templates ----------
347 $(addprefix templates/,$(LITFILES)): templates/%: %
348 cp $< $@.new && mv -f $@.new $@
350 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
351 cp $< $@.new && mv -f $@.new $@
353 libraries: $(LIBRARY_FILES)
355 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
356 $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
357 --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
358 preview >$@.tmp && mv -f $@.tmp $@
360 #---------- examples ----------
362 EXAMPLE_BUNDLE_INPUT_DEPS := $(shell \
363 cd examples/test-bundle/ && find \! \( -name '*~' -o -name '.*' \) \
366 examples/%.zip: $(MAKEFILE_DEP)
367 set -e; rm -f $@.tmp; cd examples/$*/; \
368 zip $(ZIPFLAGS) -DX ../$(notdir $@).tmp $(ZIP_INPUTS)
371 examples/test-bundle.zip: ZIP_INPUTS=$(EXAMPLE_BUNDLE_INPUT_DEPS)
372 examples/test-bundle.zip: \
373 $(addprefix examples/test-bundle/, $(EXAMPLE_BUNDLE_INPUT_DEPS))
375 examples/big-bundle.zip: examples/big-bundle/otter.toml
376 examples/big-bundle.zip: ZIPFLAGS+= -0 -r -q
377 examples/big-bundle.zip: ZIP_INPUTS=.
378 examples/big-bundle/otter.toml: $(LIBRARY_FILES) $(MAKEFILE_DEP)
379 rm -rf examples/big-bundle
380 mkdir examples/big-bundle examples/big-bundle/library
381 @set -e; echo 'MKDIR for $@'; \
382 for l in $(LIBRARIES); do \
383 mkdir examples/big-bundle/$$l; \
385 -e 'BEGIN { print "# -- AUTOGENERATED FROM COPY IN OTTER SOURCE --\n" }' \
386 -e 'if (m/^\[scraper]/..0) { unless (m/^\[(?!scraper)/..0) { s/^/\#/ } }' \
387 -e 's/(?<=\s)\w\S*(?=\s)/-/ if m/^files = """/..m/^"""/;' \
388 <$$l.toml >examples/big-bundle/$$l.toml; done
389 @set -e; echo 'LN for $@'; \
390 for e in $(LIBRARY_FILE_INPUTS); do \
391 ln $${e#*:} examples/big-bundle/$${e%%.usvg:*}.svg; done;
392 @set -e; echo 'MV for $@'; \
393 cd examples/big-bundle/library/; for x in '' .toml; do \
394 mv wikimedia$$x duped-example$$x; done
395 @set -e; mkdir examples/big-bundle/specs/; \
396 perl -pe <specs/demo.game.toml \
397 >examples/big-bundle/specs/Modded-spec.game.toml \
398 's/chess-b-/chess-purple-/; s/chess-w-/chess-yellow-/'
399 @set -e; echo 'ECHO for $@'; \
400 echo 'title = "Autogenerated test bundle - do not distribute"' \
404 #---------- webdriver tests (wdt) ----------
406 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
407 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
409 WDT_LANDSCAPE_TESTS = wdt-altergame
411 at: $(foreach f, $(AT_TESTS), stamp/$f.check)
413 wdt: $(foreach f, $(WDT_TESTS), stamp/$f.check) \
414 $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
416 RUNTEST_DEPS = apitest/run1 stamp/cargo.debug $(FILEASSETS) \
417 $(wildcard specs/*.toml) examples/test-bundle.zip \
418 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
420 AT_DEPS = $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
421 examples/big-bundle.zip \
424 WDT_DEPS = $(RUNTEST_DEPS) \
425 stamp/cargo-wdt.debug
427 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
429 AT_RUN = $(AT_WDT_RUN) apitest --test=$(basename $(notdir $@))
430 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
433 stamp/at-%.check: $(AT_DEPS)
438 stamp/wdt-%.check: $(WDT_DEPS)
442 stamp/wdt-%.lcheck: $(WDT_DEPS)
443 $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
446 #---------- docs publication ----------
448 PUBLISH_USER=ianmdlvl@login.chiark.greenend.org.uk
449 PUBLISH_DOC_SPHINX=$(PUBLISH_USER):public-html/otter/docs
452 rsync -r --delete-delay docs/html/. $(PUBLISH_DOC_SPHINX)/.
453 git branch -f $(PUBLISHED_BRANCH)
455 #---------- deployment ----------
457 DEPLOY_USER=ian@login.chiark.greenend.org.uk
458 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
459 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
461 for-deploy: stamp/cargo.deploy-build
462 deploy: for-deploy bundled-sources assets libraries
463 rsync -zvl --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS) otter-ssh-proxy) $(DEPLOY_BASE)/bin/
464 rsync -zv --progress $(TARGET_DIR)/release/usvg $(DEPLOY_BASE)/libexec/
465 rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
466 rsync -r README.md $(DEPLOY_BASE)/.
467 rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
468 rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
469 $(DEPLOY_BASE)/assets/
470 rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
471 ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
472 git branch -f $(DEPLOYED_BRANCH)
473 -git push origin main
474 -git push chiark main
476 #$(DEPLOY_BASE)/bundled-sources
478 #---------- clean ----------
481 rm -f templates/script.js library/*/*.usvg stamp/*
482 rm -rf $(LIBRARY_CLEAN)
483 rm -rf examples/big-bundle examples/big-bundle.zip
484 rm -f examples/test-bundle.zip
485 find * \( -name '*~' -o -name '*.tmp' \) -print0 | xargs -0r rm --
487 clean: clean-nailing clean-for-retest
489 $(NAILING_CARGO_JUST_RUN) rm -rf target