2 # Copyright 2020-2021 Ian Jackson and contributors to Otter
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # There is NO WARRANTY.
15 everything: debug doc release check bundled-sources
17 shapelib: templates/shapelib.html stamp/cargo.doc-otter-only
18 @echo 'Now you can visit these in your web browser:'
19 @echo ' file://$(PWD)/$<'
20 @echo ' file://$(abspath $(TARGET_DIR)/doc/otter/shapelib_toml/index.html)'
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 ----------
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 ifndef INKSCAPE_EXTENSIONS
55 INKSCAPE_EXTENSIONS := $(shell $(INKSCAPE) -x)
57 RECOLOUR_SVG ?= $(INKSCAPE_EXTENSIONS)/color_replace.py
59 DEPLOY_ARCH=x86_64-unknown-linux-musl
61 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
62 DEPLOYED_BRANCH=deployed
64 #---------- nailing-cargo ----------
66 ifneq (,$(wildcard ../Cargo.nail))
68 NAILING_CARGO = nailing-cargo
69 CARGO = $(NAILING_CARGO)
70 BUILD_SUBDIR ?= ../Build
71 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
73 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
74 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
75 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
76 WASM_BINDGEN_CLI_CARGO_OPTS ?= --subcommand-props=!manifest-path
79 $(NAILING_CARGO_JUST_RUN) \
80 sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
81 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
87 BUILD_SUBDIR ?= ../Build
88 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
91 WASM_PACKED=$(TARGET_DIR)/packed-wasm
93 #---------- local programs ----------
96 $(if $(wildcard $(BUILD_SUBDIR)/$2),
97 $(shell echo >&2 'Makefile: lp: Using program $4 from $(BUILD_SUBDIR)/$2')
98 $1 := $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4)
99 $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4):; cd ../$2 && $$(CARGO) build $(call cr,$3)
100 BUNDLED_SOURCES_DIRS += $2
101 BUNDLED_SOURCES_LINKS += $2/
105 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
106 $(eval $(call lp,USVG,resvg,release,usvg))
108 #---------- variables defining bits of source etc. ----------
110 PROGRAMS=daemon-otter otter
112 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
113 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
117 $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
118 webassembly-types/webassembly.d.ts \
119 templates/otter_wasm.ns.d.ts
121 LITFILES= LICENCE AGPLv3
122 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
124 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
125 $(LITFILES) $(TXTFILES)) \
126 $(wildcard templates/*.tera)
128 WASM := wasm32-unknown-unknown
129 # ^ todo: Is this still right after
130 # Use correct ABI for wasm32 by default
131 # https://github.com/rust-lang/rust/pull/79998
132 # ? But maybe it doesn't matter since we're very conservative and
133 # only pass JsValue and a few strings across the WASM ABI.
135 #---------- toplevel aggregate targets ----------
137 check: stamp/cargo.check at wdt
138 @echo 'All tests passed.'
142 debug release:: %: stamp/cargo.% assets libraries extra-%
144 cargo: cargo-debug cargo-wasm-release
146 cargo-debug cargo-release cargo-check cargo-doc \
147 cargo-wasm-debug cargo-wasm-release:: \
148 cargo-%: stamp/cargo.%
150 cargo-wasm: cargo-wasm-release
152 wasm: stamp/wasm-bindgen
154 assets: js stamp/wasm-bindgen $(FILEASSETS)
156 js: templates/script.js
159 extra-release: bundled-sources
161 cargo-syntaxcheck: cargo-syntaxcheck-host cargo-syntaxcheck-wasm
162 cargo-syntaxcheck-host:
163 $(CARGO) check --workspace
164 cargo-syntaxcheck-wasm:
165 $(CARGO) check --target $(WASM) -p otter-wasm
167 #---------- cargo ----------
170 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
172 $(addprefix stamp/cargo.,$(DR)):: \
173 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
174 $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
177 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
178 $(CARGO) build --workspace -p otter --bin $*
180 $(WASM_BINDGEN): $(call rsrcs, ! -name \*.rs)
181 $(CARGO) $(WASM_BINDGEN_CLI_CARGO_OPTS) build --target-dir=target \
182 --manifest-path=$(abspath wasm/Cargo.toml) -p wasm-bindgen-cli
184 stamp/cargo.check: $(call rsrcs,.)
185 $(CARGO) test --workspace
188 stamp/cargo-at.debug: $(call rsrcs,.)
189 $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
192 stamp/cargo-wdt.debug: $(call rsrcs,.)
193 $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
196 stamp/cargo.doc: $(call rsrcs,.)
197 $(CARGO) doc --workspace 2>&1 | egrep -vf .cargo-doc-suppress-errors
200 stamp/cargo.doc-otter-only: $(call rsrcs,.)
201 $(CARGO) doc --workspace -p otter --no-deps
204 $(addprefix stamp/cargo.wasm-,$(DR)):: \
205 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
206 $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
209 stamp/cargo.deploy-build: $(call rsrcs,.)
210 $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
213 #---------- wasm ----------
215 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-bindgen
216 stamp/wasm-bindgen: $(WASM_BINDGEN) stamp/cargo.wasm-release
217 $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
218 $(WASM_BINDGEN_OPTIONS) --out-dir target/packed-wasm \
219 target/$(WASM)/release/otter_wasm.wasm
222 #---------- bundle-sources ----------
224 BUNDLED_SOURCES_DIRS += otter
226 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
228 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
230 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
231 bundled-sources/%: $(wildcard $(BUNDLE_SOURCES))
232 set -e; d=$(abspath $(TARGET_BUNDLED)); \
233 $(NAILING_CARGO_JUST_RUN) mkdir -p $$d; \
234 $(if $(filter-out otter,$*), cd ../$*;) \
235 $(BUNDLE_SOURCES_CMD) --output $$d/$*
237 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
239 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
240 $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
242 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index $(MAKEFILE_DEP)
243 $(NAILING_CARGO_JUST_RUN) sh -ec ' \
244 cd $(abspath $(src)); mkdir -p $(dir $@); \
245 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS); \
250 @echo Bundled sources.
252 #---------- svg processing ----------
254 LIBRARIES ?= $(basename $(wildcard library/*.toml))
256 include $(addsuffix /files.make, $(LIBRARIES))
258 USVG_PROCESSOR = usvg-processor
259 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
260 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_BINARY) $(MAKEFILE_DEP)
262 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
264 library/%/files.make: media-scraper library/%.toml
265 ./$< --offline library/$*.toml
267 #---------- typescript ----------
269 templates/%.js: tsc-wrap tsconfig.json
270 ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
272 templates/script.js: $(TS_SRC_FILES)
274 templates/otter_wasm.ns.d.ts: $(WASM_PACKED)/otter_wasm.d.ts \
275 stamp/wasm-bindgen $(MAKEFILE_DEP)
276 set -e; exec >$@.tmp; \
277 echo 'declare namespace wasm_bindgen {'; \
278 sed 's/^export default function init/export function init/' <$<; \
282 #---------- other templates ----------
284 $(addprefix templates/,$(LITFILES)): templates/%: %
285 cp $< $@.new && mv -f $@.new $@
287 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
288 cp $< $@.new && mv -f $@.new $@
290 libraries: $(LIBRARY_FILES)
292 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
293 $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
294 --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
295 preview >$@.tmp && mv -f $@.tmp $@
297 #---------- webdriver tests (wdt) ----------
299 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
300 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
302 WDT_LANDSCAPE_TESTS = wdt-altergame
304 at: $(foreach f, $(AT_TESTS), stamp/$f.check)
306 wdt: $(foreach f, $(WDT_TESTS), stamp/$f.check) \
307 $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
309 RUNTEST_DEPS = apitest/run1 stamp/cargo.debug $(FILEASSETS) \
310 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
312 AT_DEPS = $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
315 WDT_DEPS = $(RUNTEST_DEPS) \
316 stamp/cargo-wdt.debug
318 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
320 AT_RUN = $(AT_WDT_RUN) $(basename $(notdir $@))
321 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
323 stamp/at-%.check: $(AT_DEPS)
327 stamp/wdt-%.check: $(WDT_DEPS)
331 stamp/wdt-%.lcheck: $(WDT_DEPS)
332 $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
335 #---------- deployment ----------
337 DEPLOY_USER=ian@login.chiark.greenend.org.uk
338 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
339 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
341 deploy: stamp/cargo.deploy-build bundled-sources assets libraries
342 rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
343 rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
344 rsync -r README.md $(DEPLOY_BASE)/.
345 rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
346 rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
347 $(DEPLOY_BASE)/assets/
348 rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
349 ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
350 git branch -f $(DEPLOYED_BRANCH)
354 #$(DEPLOY_BASE)/bundled-sources
356 #---------- clean ----------
359 rm -f templates/script.js library/*/*.usvg stamp/*
360 rm -rf $(LIBRARY_CLEAN)
361 find * -name '*~' -print0 | xargs -0r rm --
363 clean: clean-nailing clean-for-retest
365 $(NAILING_CARGO_JUST_RUN) rm -rf target