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)
273 #templates/bigfloat-tests.js: templates/bigfloat.ts \
274 # templates/bigfloat-tests.ts templates/bigfloat-tests-auto.ts
276 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
277 # ./$^ >$@.tmp && mv -f $@.tmp $@
279 #js-check: templates/bigfloat-tests.js
281 # @echo 'nodejs check $< ok'
283 templates/otter_wasm.ns.d.ts: $(WASM_PACKED)/otter_wasm.d.ts \
284 stamp/wasm-bindgen $(MAKEFILE_DEP)
285 set -e; exec >$@.tmp; \
286 echo 'declare namespace wasm_bindgen {'; \
287 sed 's/^export default function init/export function init/' <$<; \
291 #---------- other templates ----------
293 $(addprefix templates/,$(LITFILES)): templates/%: %
294 cp $< $@.new && mv -f $@.new $@
296 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
297 cp $< $@.new && mv -f $@.new $@
299 libraries: $(LIBRARY_FILES)
301 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
302 $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
303 --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
304 preview >$@.tmp && mv -f $@.tmp $@
306 #---------- webdriver tests (wdt) ----------
308 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
309 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
311 WDT_LANDSCAPE_TESTS = wdt-altergame
313 at: $(foreach f, $(AT_TESTS), stamp/$f.check)
315 wdt: $(foreach f, $(WDT_TESTS), stamp/$f.check) \
316 $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
318 RUNTEST_DEPS = apitest/run1 stamp/cargo.debug $(FILEASSETS) \
319 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
321 AT_DEPS = $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
324 WDT_DEPS = $(RUNTEST_DEPS) \
325 stamp/cargo-wdt.debug
327 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
329 AT_RUN = $(AT_WDT_RUN) $(basename $(notdir $@))
330 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
332 stamp/at-%.check: $(AT_DEPS)
336 stamp/wdt-%.check: $(WDT_DEPS)
340 stamp/wdt-%.lcheck: $(WDT_DEPS)
341 $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
344 #---------- deployment ----------
346 DEPLOY_USER=ian@login.chiark.greenend.org.uk
347 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
348 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
350 deploy: stamp/cargo.deploy-build bundled-sources assets libraries
351 rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
352 rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
353 rsync -r README.md $(DEPLOY_BASE)/.
354 rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
355 rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
356 $(DEPLOY_BASE)/assets/
357 rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
358 ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
359 git branch -f $(DEPLOYED_BRANCH)
363 #$(DEPLOY_BASE)/bundled-sources
365 #---------- clean ----------
368 rm -f templates/script.js library/*/*.usvg stamp/*
369 rm -rf $(LIBRARY_CLEAN)
370 find * -name '*~' -print0 | xargs -0r rm --
372 clean: clean-nailing clean-for-retest
374 $(NAILING_CARGO_JUST_RUN) rm -rf target