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 #---------- funky macros etc. ----------
24 cr = $(addprefix --,$(filter-out debug,$1))
25 rsrcs = $(shell set -x;\
26 find -H $1 \( -name Cargo.toml -o -name Cargo.lock -o -name Cargo.lock.example -o -name \*.rs \) \! -path '*/build/*' )
27 stamp=@mkdir -p stamp; touch $@
29 BUNDLED_SOURCES_LIT = README.md LICENCE
30 BUNDLED_SOURCES_FILES = index.html $(BUNDLED_SOURCES_LIT)
31 BUNDLED_SOURCES_LINKS += $(BUNDLED_SOURCES_LIT) otter/
32 BUNDLED_SOURCES += $(BUNDLED_SOURCES_FILES)
34 #---------- programs and config variables ----------
40 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
42 WASM_PACK ?= wasm-pack
43 WASM_PACK_OPTIONS = --cargo-path=/bin/echo
45 BUNDLE_SOURCES ?= bundle-rust-sources
47 ifndef INKSCAPE_EXTENSIONS
49 INKSCAPE_EXTENSIONS := $(shell $(INKSCAPE) -x)
51 RECOLOUR_SVG ?= $(INKSCAPE_EXTENSIONS)/color_replace.py
53 DEPLOY_ARCH=x86_64-unknown-linux-musl
55 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
56 DEPLOYED_BRANCH=deployed
58 #---------- nailing-cargo ----------
60 ifneq (,$(wildcard ../Cargo.nail))
62 NAILING_CARGO = nailing-cargo
63 CARGO = $(NAILING_CARGO)
64 BUILD_SUBDIR ?= ../Build
65 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
67 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
68 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
69 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
70 WASM_PACK_CMD ?= $(NAILING_CARGO) --linkfarm=git --- $(WASM_PACK)
73 $(NAILING_CARGO_JUST_RUN) \
74 sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
75 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
81 BUILD_SUBDIR ?= ../Build
82 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
84 WASM_PACK_CMD ?= $(WASM_PACK)
86 WASM_PACKED=$(TARGET_DIR)/packed-wasm
88 #---------- local programs ----------
91 $(if $(wildcard $(BUILD_SUBDIR)/$2),
92 $(shell echo >&2 'Makefile: lp: Using program $4 from $(BUILD_SUBDIR)/$2')
93 $1 := $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4)
94 $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4):; cd ../$2 && $$(CARGO) build $(call cr,$3)
95 BUNDLED_SOURCES_DIRS += $2
96 BUNDLED_SOURCES_LINKS += $2/
100 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
101 $(eval $(call lp,USVG,resvg,release,usvg))
102 $(eval $(call lp,WASM_PACK,wasm-pack,debug,wasm-pack))
104 #---------- variables defining bits of source etc. ----------
106 PROGRAMS=daemon-otter otter
108 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
109 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
113 $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
114 webassembly-types/webassembly.d.ts \
115 templates/otter_wasm.ns.d.ts
117 LITFILES= LICENCE AGPLv3
118 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
120 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
121 $(LITFILES) $(TXTFILES)) \
122 $(wildcard templates/*.tera)
124 WASM := wasm32-unknown-unknown
125 # ^ todo: Is this still right after
126 # Use correct ABI for wasm32 by default
127 # https://github.com/rust-lang/rust/pull/79998
128 # ? But maybe it doesn't matter since we're very conservative and
129 # only pass JsValue and a few strings across the WASM ABI.
131 #---------- toplevel aggregate targets ----------
133 check: stamp/cargo.check at wdt
134 @echo 'All tests passed.'
138 debug release:: %: stamp/cargo.% assets libraries extra-%
140 cargo: cargo-debug cargo-wasm-release
142 cargo-debug cargo-release cargo-check cargo-doc \
143 cargo-wasm-debug cargo-wasm-release:: \
144 cargo-%: stamp/cargo.%
146 cargo-wasm: cargo-wasm-release
148 wasm-pack: stamp/wasm-pack
150 assets: js stamp/wasm-pack $(FILEASSETS)
152 js: templates/script.js
155 extra-release: bundled-sources
157 cargo-syntaxcheck: cargo-syntaxcheck-host cargo-syntaxcheck-wasm
158 cargo-syntaxcheck-host:
159 $(CARGO) check --workspace
160 cargo-syntaxcheck-wasm:
161 $(CARGO) check --target $(WASM) -p otter-wasm
163 #---------- cargo ----------
166 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
168 $(addprefix stamp/cargo.,$(DR)):: \
169 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
170 $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
173 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
174 $(CARGO) build --workspace -p otter --bin $*
176 stamp/cargo.check: $(call rsrcs,.)
177 $(CARGO) test --workspace
180 stamp/cargo-at.debug: $(call rsrcs,.)
181 $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
184 stamp/cargo-wdt.debug: $(call rsrcs,.)
185 $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
188 stamp/cargo.doc: $(call rsrcs,.)
189 $(CARGO) doc --workspace 2>&1 | egrep -vf .cargo-doc-suppress-errors
192 stamp/cargo.doc-otter-only: $(call rsrcs,.)
193 $(CARGO) doc --workspace -p otter --no-deps
196 $(addprefix stamp/cargo.wasm-,$(DR)):: \
197 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
198 $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
201 stamp/cargo.deploy-build: $(call rsrcs,.)
202 $(CARGO) -T$(DEPLOY_ARCH) build --workspace $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
205 #---------- wasm ----------
207 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-pack
208 stamp/wasm-pack: stamp/cargo.wasm-release
209 $(WASM_PACK_CMD) $(WASM_PACK_OPTIONS) build \
210 --out-dir=../target/packed-wasm wasm -t no-modules --release
213 # There is some kind of bug in wasm-pack's version of wasm-opt, which
214 # can be avoided by running --dev instead (but also then using the
215 # debug rust built). IME this happened when trying to return a u64,
216 # so maybe bigints are affected? (Which I don't want to use anyway.)
218 # https://github.com/WebAssembly/binaryen/issues/3006
220 #---------- bundle-sources ----------
222 BUNDLED_SOURCES_DIRS += otter
224 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
226 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
228 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
229 bundled-sources/%: $(wildcard $(BUNDLE_SOURCES))
230 set -e; d=$(abspath $(TARGET_BUNDLED)); \
231 $(NAILING_CARGO_JUST_RUN) mkdir -p $$d; \
232 $(if $(filter-out otter,$*), cd ../$*;) \
233 $(BUNDLE_SOURCES_CMD) --output $$d/$*
235 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
237 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
238 $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
240 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index Makefile
241 $(NAILING_CARGO_JUST_RUN) sh -ec ' \
242 cd $(abspath $(src)); mkdir -p $(dir $@); \
243 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS); \
248 @echo Bundled sources.
250 #---------- svg processing ----------
252 LIBRARIES ?= $(basename $(wildcard library/*.toml))
254 include $(addsuffix /files.make, $(LIBRARIES))
256 USVG_PROCESSOR = usvg-processor
257 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
258 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_BINARY) Makefile
260 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
262 library/%/files.make: media-scraper library/%.toml
263 ./$< --offline library/$*.toml
265 #---------- typescript ----------
267 templates/%.js: tsc-wrap tsconfig.json
268 ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
270 templates/script.js: $(TS_SRC_FILES)
271 #templates/bigfloat-tests.js: templates/bigfloat.ts \
272 # templates/bigfloat-tests.ts templates/bigfloat-tests-auto.ts
274 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
275 # ./$^ >$@.tmp && mv -f $@.tmp $@
277 #js-check: templates/bigfloat-tests.js
279 # @echo 'nodejs check $< ok'
281 templates/otter_wasm.ns.d.ts: $(WASM_PACKED)/otter_wasm.d.ts \
282 stamp/wasm-pack Makefile
283 set -e; exec >$@.tmp; \
284 echo 'declare namespace wasm_bindgen {'; \
285 sed 's/^export default function init/export function init/' <$<; \
289 #---------- other templates ----------
291 $(addprefix templates/,$(LITFILES)): templates/%: %
292 cp $< $@.new && mv -f $@.new $@
294 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
295 cp $< $@.new && mv -f $@.new $@
297 libraries: $(LIBRARY_FILES)
299 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
300 $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
301 --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
302 preview >$@.tmp && mv -f $@.tmp $@
304 #---------- webdriver tests (wdt) ----------
306 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
307 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
309 WDT_LANDSCAPE_TESTS = wdt-altergame
311 at: $(foreach f, $(AT_TESTS), stamp/$f.check)
313 wdt: $(foreach f, $(WDT_TESTS), stamp/$f.check) \
314 $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
316 RUNTEST_DEPS = apitest/run1 stamp/cargo.debug $(FILEASSETS) \
317 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
319 AT_DEPS = $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
322 WDT_DEPS = $(RUNTEST_DEPS) \
323 stamp/cargo-wdt.debug
325 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
327 AT_RUN = $(AT_WDT_RUN) $(basename $(notdir $@))
328 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
330 stamp/at-%.check: $(AT_DEPS)
334 stamp/wdt-%.check: $(WDT_DEPS)
338 stamp/wdt-%.lcheck: $(WDT_DEPS)
339 $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
342 #---------- deployment ----------
344 DEPLOY_USER=ian@login.chiark.greenend.org.uk
345 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
346 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
348 deploy: stamp/cargo.deploy-build bundled-sources assets libraries
349 rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
350 rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
351 rsync -r README.md $(DEPLOY_BASE)/.
352 rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
353 rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
354 $(DEPLOY_BASE)/assets/
355 rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
356 ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
357 git branch -f $(DEPLOYED_BRANCH)
361 #$(DEPLOY_BASE)/bundled-sources
363 #---------- clean ----------
366 rm -f templates/script.js library/*/*.usvg stamp/*
367 rm -rf $(LIBRARY_CLEAN)
368 find * -name '*~' -print0 | xargs -0r rm --
370 clean: clean-nailing clean-for-retest
372 $(NAILING_CARGO_JUST_RUN) rm -rf target