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 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 ----------
160 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
162 $(addprefix stamp/cargo.,$(DR)):: \
163 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
164 $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
167 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
168 $(CARGO) build --workspace -p otter --bin $*
170 stamp/cargo.check: $(call rsrcs,.)
171 $(CARGO) test --workspace
174 stamp/cargo-wdt.debug: $(call rsrcs,.)
175 $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
178 stamp/cargo.doc: $(call rsrcs,.)
179 $(CARGO) doc --workspace 2>&1 | egrep -vf .cargo-doc-suppress-errors
182 stamp/cargo.doc-otter-only: $(call rsrcs,.)
183 $(CARGO) doc --workspace -p otter --no-deps
186 $(addprefix stamp/cargo.wasm-,$(DR)):: \
187 stamp/cargo.wasm-%: $(call rsrcs, zcoord wasm Cargo.*)
188 $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
191 stamp/cargo.deploy-build: $(call rsrcs,.)
192 $(CARGO) -T$(DEPLOY_ARCH) build --workspace $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
195 #---------- wasm ----------
197 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-pack
198 stamp/wasm-pack: stamp/cargo.wasm-release
199 $(WASM_PACK_CMD) $(WASM_PACK_OPTIONS) build \
200 --out-dir=../target/packed-wasm wasm -t no-modules --release
203 # There is some kind of bug in wasm-pack's version of wasm-opt, which
204 # can be avoided by running --dev instead (but also then using the
205 # debug rust built). IME this happened when trying to return a u64,
206 # so maybe bigints are affected? (Which I don't want to use anyway.)
208 # https://github.com/WebAssembly/binaryen/issues/3006
210 #---------- bundle-sources ----------
212 BUNDLED_SOURCES_DIRS += otter
214 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
216 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
218 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
219 bundled-sources/%: $(wildcard $(BUNDLE_SOURCES))
220 set -e; d=$(abspath $(TARGET_BUNDLED)); \
221 $(NAILING_CARGO_JUST_RUN) mkdir -p $$d; \
222 $(if $(filter-out otter,$*), cd ../$*;) \
223 $(BUNDLE_SOURCES_CMD) --output $$d/$*
225 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
227 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
228 $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
230 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index Makefile
231 $(NAILING_CARGO_JUST_RUN) sh -ec ' \
232 cd $(abspath $(src)); mkdir -p $(dir $@); \
233 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS); \
238 @echo Bundled sources.
240 #---------- svg processing ----------
242 LIBRARIES ?= $(basename $(wildcard library/*.toml))
244 include $(addsuffix /files.make, $(LIBRARIES))
246 USVG_PROCESSOR = usvg-processor
247 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
248 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_BINARY) Makefile
250 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
252 library/%/files.make: media-scraper library/%.toml
253 ./$< --offline library/$*.toml
255 #---------- typescript ----------
257 templates/%.js: tsc-wrap tsconfig.json
258 ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
260 templates/script.js: $(TS_SRC_FILES)
261 #templates/bigfloat-tests.js: templates/bigfloat.ts \
262 # templates/bigfloat-tests.ts templates/bigfloat-tests-auto.ts
264 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
265 # ./$^ >$@.tmp && mv -f $@.tmp $@
267 #js-check: templates/bigfloat-tests.js
269 # @echo 'nodejs check $< ok'
271 templates/otter_wasm.ns.d.ts: $(WASM_PACKED)/otter_wasm.d.ts \
272 stamp/wasm-pack Makefile
273 set -e; exec >$@.tmp; \
274 echo 'declare namespace wasm_bindgen {'; \
275 sed 's/^export default function init/export function init/' <$<; \
279 #---------- other templates ----------
281 $(addprefix templates/,$(LITFILES)): templates/%: %
282 cp $< $@.new && mv -f $@.new $@
284 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
285 cp $< $@.new && mv -f $@.new $@
287 libraries: $(LIBRARY_FILES)
289 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
290 $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
291 --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
292 preview >$@.tmp && mv -f $@.tmp $@
294 #---------- webdriver tests (wdt) ----------
296 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
298 WDT_LANDSCAPE_TESTS = wdt-altergame
300 wdt: $(foreach f, $(WDT_TESTS), stamp/$f.check) \
301 $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
303 WDT_DEPS = wdriver/run1 stamp/cargo.debug stamp/cargo-wdt.debug \
304 $(FILEASSETS) templates/script.js \
305 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
307 stamp/wdt-%.check: $(WDT_DEPS)
308 $(NAILING_CARGO_JUST_RUN) $(abspath $<) $(basename $(notdir $@))
311 stamp/wdt-%.lcheck: $(WDT_DEPS)
312 $(NAILING_CARGO_JUST_RUN) $(abspath $<) $(basename $(notdir $@)) --as-if=lwdt-$* --layout=Landscape
315 #---------- deployment ----------
317 DEPLOY_USER=ian@login.chiark.greenend.org.uk
318 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
319 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
321 deploy: stamp/cargo.deploy-build bundled-sources assets libraries
322 rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
323 rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
324 rsync -r README.md $(DEPLOY_BASE)/.
325 rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
326 rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
327 $(DEPLOY_BASE)/assets/
328 rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
329 ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
330 git branch -f $(DEPLOYED_BRANCH)
334 #$(DEPLOY_BASE)/bundled-sources
336 #---------- clean ----------
339 rm -f templates/script.js library/*/*.usvg stamp/*
340 rm -rf $(LIBRARY_CLEAN)
341 find * -name '*~' -print0 | xargs -0r rm --
343 clean: clean-nailing clean-for-retest
345 $(NAILING_CARGO_JUST_RUN) rm -rf target