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 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)'
23 MAKEFILE_DEP ?= Makefile
25 # ^ set this to "x" to debug the $rsrcs rune
27 #---------- funky macros etc. ----------
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 $@
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)
39 #---------- programs and config variables ----------
44 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
46 WASM_BINDGEN = $(TARGET_DIR)/debug/wasm-bindgen
47 WASM_BINDGEN_OPTIONS = \
48 --remove-name-section --remove-producers-section \
49 --typescript --no-modules \
51 BUNDLE_SOURCES ?= bundle-rust-sources
53 SPHINXBUILD = sphinx-build
55 ifndef INKSCAPE_EXTENSIONS
57 INKSCAPE_SDD_QUIETEN ?= 2>/dev/null
58 # inkscape 0.92.4: --extension-directory works, no --system-data-directory
59 # inkscape 1.0.2: --system-data-directory, no --extension-directory
60 INKSCAPE_EXTENSIONS := $(shell set -e; { sdd=$$( $(INKSCAPE) --system-data-directory $(INKSCAPE_SDD_QUIETEN) ) && echo "$$sdd/extensions"; } || $(INKSCAPE) --extension-directory )
62 RECOLOUR_SVG ?= ./run-inkscape-extension $(INKSCAPE_EXTENSIONS)/color_replace.py
64 DEPLOY_ARCH=x86_64-unknown-linux-musl
66 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
67 DEPLOYED_BRANCH=deployed
68 PUBLISHED_BRANCH=published
70 #---------- nailing-cargo ----------
72 ifneq (,$(wildcard ../Cargo.nail))
74 NAILING_CARGO = nailing-cargo
75 CARGO = $(NAILING_CARGO)
76 BUILD_SUBDIR ?= ../Build
77 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
79 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
80 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
81 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
82 WASM_BINDGEN_CLI_CARGO_OPTS ?= --subcommand-props=!manifest-path
85 $(NAILING_CARGO_JUST_RUN) \
86 sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
87 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
93 BUILD_SUBDIR ?= ../Build
94 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
97 WASM_PACKED=$(TARGET_DIR)/packed-wasm
99 #---------- local programs ----------
102 stamp/cargo.$2: $(call rsrcs, ! -name \*.rs)
103 $$(CARGO) build $(call cr,$3) -p $2
105 $1 := $(abspath $(TARGET_DIR)/$3/$4)
108 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
109 $(eval $(call lp,USVG,usvg,release,usvg))
111 #---------- variables defining bits of source etc. ----------
113 PROGRAMS=daemon-otter otter
115 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
116 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
120 $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
121 webassembly-types/webassembly.d.ts \
122 $(WASM_PACKED)/otter_wasm.d.ts
124 LITFILES= LICENCE AGPLv3
125 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
127 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
128 $(LITFILES) $(TXTFILES)) \
129 $(wildcard templates/*.tera)
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.
138 #---------- toplevel aggregate targets ----------
140 check: stamp/cargo.debug-check at wdt
141 @echo 'Tests passed.'
143 full-check: stamp/cargo.release-check
144 full-check: stamp/cargo.release-miri stamp/cargo.debug-miri
147 @echo 'Full tests passed.'
149 doc: cargo-doc doc-sphinx examples
151 debug release:: %: stamp/cargo.% assets libraries extra-%
153 cargo: cargo-debug cargo-wasm-release
155 cargo-debug cargo-release cargo-check cargo-doc \
156 cargo-wasm-debug cargo-wasm-release:: \
157 cargo-%: stamp/cargo.%
159 EXAMPLE_BUNDLES = test-bundle big-bundle
160 EXAMPLE_BUNDLE_FILES = $(foreach f, $(EXAMPLE_BUNDLES), examples/$f.zip)
162 examples: $(EXAMPLE_BUNDLE_FILES)
165 cargo-wasm: cargo-wasm-release
167 wasm: stamp/wasm-bindgen
169 assets: js stamp/wasm-bindgen $(FILEASSETS)
171 js: templates/script.js
174 extra-release: bundled-sources
176 cargo-syntaxcheck: cargo-syntaxcheck-host cargo-syntaxcheck-wasm
177 cargo-syntaxcheck-host:
178 $(CARGO) check --workspace
179 cargo-syntaxcheck-wasm:
180 $(CARGO) check --target $(WASM) -p otter-wasm --release
181 cargo-syntaxcheck-release:
182 $(CARGO) check --workspace --release
184 #---------- cargo ----------
187 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
189 $(addprefix stamp/cargo.,$(DR)):: \
190 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
191 $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon -p otter-cli
192 $(NAILING_CARGO_JUST_RUN) \
193 ln -sf otter $(abspath $(TARGET_DIR))/$*/otter-ssh-proxy
196 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
197 $(CARGO) build --workspace -p otter-cli
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.doc: $(call rsrcs,.)
221 $(CARGO) doc $(CARGO_DOC_OPTS) --workspace 2>&1 |egrep -vf .cargo-doc-suppress-errors
224 stamp/cargo.doc-otter-only: $(call rsrcs,.)
225 $(CARGO) doc $(CARGO_DOC_OPTS) --workspace -p otter --no-deps
228 $(addprefix stamp/cargo.wasm-,$(DR)):: \
229 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
230 $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
233 stamp/cargo.deploy-build: $(call rsrcs,.)
234 $(CARGO) build --target $(DEPLOY_ARCH) $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-cli -p otter-daemon
235 $(NAILING_CARGO_JUST_RUN) \
236 ln -sf otter $(abspath $(TARGET_DIR)/$(DEPLOY_ARCH))/$(DEPLOY_RELEASE)/otter-ssh-proxy
239 #---------- sphnix ----------
241 doc-sphinx: docs/html/index.html \
242 $(foreach f, $(EXAMPLE_BUNDLES), docs/html/examples/$f.zip) \
243 $(addprefix docs/html/examples/, $(notdir $(wildcard specs/*.toml)))
244 @echo 'Documentation can now be found here:'
245 @echo ' file://$(PWD)/$<'
247 docs/html/index.html: docs/conf.py $(wildcard docs/*.md docs/*.rst docs/*.png)
248 $(SPHINXBUILD) -M html docs docs $(SPHINXOPTS)
250 docs/html/examples/%.zip: examples/%.zip
251 mkdir -p docs/html/examples
254 docs/html/examples/%.toml: specs/%.toml
255 mkdir -p docs/html/examples
258 #---------- wasm ----------
260 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-bindgen
261 stamp/wasm-bindgen: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
262 $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
263 $(WASM_BINDGEN_OPTIONS) --out-dir target/packed-wasm \
264 target/$(WASM)/release/otter_wasm.wasm
267 #---------- bundle-sources ----------
269 BUNDLED_SOURCES_DIRS += otter
271 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
273 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
276 $(NAILING_CARGO_JUST_RUN) mkdir -p $(abspath $@)
278 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
279 bundled-sources/%: stamp/cargo.bundle-sources $(TARGET_BUNDLED)
280 set -e; d=$(abspath $(TARGET_BUNDLED)); \
281 $(if $(filter-out otter,$*), cd ../$*;) \
282 $(BUNDLE_SOURCES_CMD) --output $$d/$*
284 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
286 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): \
287 $(TARGET_BUNDLED)/%: % $(TARGET_BUNDLED)
288 $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
290 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index \
291 $(MAKEFILE_DEP) $(TARGET_BUNDLED)
292 $(NAILING_CARGO_JUST_RUN) sh -ec ' \
293 cd $(abspath $(src)); mkdir -p $(dir $@); \
294 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS); \
299 @echo Bundled sources.
301 #---------- svg processing ----------
303 LIBRARIES ?= $(basename $(wildcard library/*.toml))
304 USVG_DEP = stamp/cargo.usvg
306 include $(addsuffix /files.make, $(LIBRARIES))
308 USVG_PROCESSOR = usvg-processor
309 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
310 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_DEP) $(MAKEFILE_DEP)
312 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
314 library/%/files.make: media-scraper library/%.toml
315 ./$< --offline library/$*.toml
317 #---------- typescript ----------
319 templates/%.js: tsc-wrap tsconfig.json
320 ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
322 templates/script.js: $(TS_SRC_FILES) stamp/wasm-bindgen
324 #---------- other templates ----------
326 $(addprefix templates/,$(LITFILES)): templates/%: %
327 cp $< $@.new && mv -f $@.new $@
329 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
330 cp $< $@.new && mv -f $@.new $@
332 libraries: $(LIBRARY_FILES)
334 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
335 $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
336 --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
337 preview >$@.tmp && mv -f $@.tmp $@
339 #---------- examples ----------
341 EXAMPLE_BUNDLE_INPUT_DEPS := $(shell \
342 cd examples/test-bundle/ && find \! \( -name '*~' -o -name '.*' \) \
345 examples/%.zip: $(MAKEFILE_DEP)
346 set -e; rm -f $@.tmp; cd examples/$*/; \
347 zip $(ZIPFLAGS) -DX ../$(notdir $@).tmp $(ZIP_INPUTS)
350 examples/test-bundle.zip: ZIP_INPUTS=$(EXAMPLE_BUNDLE_INPUT_DEPS)
351 examples/test-bundle.zip: \
352 $(addprefix examples/test-bundle/, $(EXAMPLE_BUNDLE_INPUT_DEPS))
354 examples/big-bundle.zip: examples/big-bundle/otter.toml
355 examples/big-bundle.zip: ZIPFLAGS+= -0 -r -q
356 examples/big-bundle.zip: ZIP_INPUTS=.
357 examples/big-bundle/otter.toml: $(LIBRARY_FILES) $(MAKEFILE_DEP)
358 rm -rf examples/big-bundle
359 mkdir examples/big-bundle examples/big-bundle/library
360 @set -e; echo 'MKDIR for $@'; \
361 for l in $(LIBRARIES); do \
362 mkdir examples/big-bundle/$$l; \
364 -e 'BEGIN { print "# -- AUTOGENERATED FROM COPY IN OTTER SOURCE --\n" }' \
365 -e 'if (m/^\[scraper]/..0) { unless (m/^\[(?!scraper)/..0) { s/^/\#/ } }' \
366 -e 's/(?<=\s)\w\S*(?=\s)/-/ if m/^files = """/..m/^"""/;' \
367 <$$l.toml >examples/big-bundle/$$l.toml; done
368 @set -e; echo 'LN for $@'; \
369 for e in $(LIBRARY_FILE_INPUTS); do \
370 ln $${e#*:} examples/big-bundle/$${e%%.usvg:*}.svg; done;
371 @set -e; echo 'MV for $@'; \
372 cd examples/big-bundle/library/; for x in '' .toml; do \
373 mv wikimedia$$x duped-example$$x; done
374 @set -e; mkdir examples/big-bundle/specs/; \
375 perl -pe <specs/demo.game.toml \
376 >examples/big-bundle/specs/Modded-spec.game.toml \
377 's/chess-b-/chess-purple-/; s/chess-w-/chess-yellow-/'
378 @set -e; echo 'ECHO for $@'; \
379 echo 'title = "Autogenerated test bundle - do not distribute"' \
383 #---------- webdriver tests (wdt) ----------
385 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
386 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
388 WDT_LANDSCAPE_TESTS = wdt-altergame
390 at: $(foreach f, $(AT_TESTS), stamp/$f.check)
392 wdt: $(foreach f, $(WDT_TESTS), stamp/$f.check) \
393 $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
395 RUNTEST_DEPS = apitest/run1 stamp/cargo.debug $(FILEASSETS) \
396 $(wildcard specs/*.toml) examples/test-bundle.zip \
397 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
399 AT_DEPS = $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
400 examples/big-bundle.zip \
403 WDT_DEPS = $(RUNTEST_DEPS) \
404 stamp/cargo-wdt.debug
406 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
408 AT_RUN = $(AT_WDT_RUN) apitest --test=$(basename $(notdir $@))
409 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
412 stamp/at-%.check: $(AT_DEPS)
417 stamp/wdt-%.check: $(WDT_DEPS)
421 stamp/wdt-%.lcheck: $(WDT_DEPS)
422 $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
425 #---------- docs publication ----------
427 PUBLISH_USER=ianmdlvl@login.chiark.greenend.org.uk
428 PUBLISH_DOC_SPHINX=$(PUBLISH_USER):public-html/otter/docs
431 rsync -r --delete-delay docs/html/. $(PUBLISH_DOC_SPHINX)/.
432 git branch -f $(PUBLISHED_BRANCH)
434 #---------- deployment ----------
436 DEPLOY_USER=ian@login.chiark.greenend.org.uk
437 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
438 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
440 for-deploy: stamp/cargo.deploy-build
441 deploy: for-deploy bundled-sources assets libraries
442 rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
443 rsync -zv --progress $(TARGET_DIR)/release/usvg $(DEPLOY_BASE)/libexec/
444 rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
445 rsync -r README.md $(DEPLOY_BASE)/.
446 rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
447 rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
448 $(DEPLOY_BASE)/assets/
449 rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
450 ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
451 git branch -f $(DEPLOYED_BRANCH)
452 -git push origin main
453 -git push chiark main
455 #$(DEPLOY_BASE)/bundled-sources
457 #---------- clean ----------
460 rm -f templates/script.js library/*/*.usvg stamp/*
461 rm -rf $(LIBRARY_CLEAN)
462 rm -rf examples/big-bundle examples/big-bundle.zip
463 rm -f examples/test-bundle.zip
464 find * \( -name '*~' -o -name '*.tmp' \) -print0 | xargs -0r rm --
466 clean: clean-nailing clean-for-retest
468 $(NAILING_CARGO_JUST_RUN) rm -rf target