chiark / gitweb /
Makefile: Drop use of wasm-pack
[otter.git] / Makefile
1 #!/bin/sh
2 # Copyright 2020-2021 Ian Jackson and contributors to Otter
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # There is NO WARRANTY.
5
6 # make -j8
7 # make -j8 release
8 # make -j8 shapelib
9
10 SHELL=/bin/bash
11 src=.
12
13 default: all check
14 all: debug
15 everything: debug doc release check bundled-sources
16
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)'
21
22 MAKEFILE_DEP ?= Makefile
23 MAKEFILE_FIND_X ?=
24 # ^ set this to "x" to debug the $rsrcs rune
25
26 #---------- funky macros etc. ----------
27
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 $@
32
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)
37
38 #---------- programs and config variables ----------
39
40 CARGO ?= cargo
41 TARGET_DIR ?= target
42
43 USVG ?= usvg
44 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
45
46 WASM_BINDGEN = $(TARGET_DIR)/debug/wasm-bindgen
47 WASM_BINDGEN_OPTIONS =                                          \
48         --remove-name-section --remove-producers-section        \
49         --typescript --no-modules                               \
50
51 BUNDLE_SOURCES ?= bundle-rust-sources
52
53 ifndef INKSCAPE_EXTENSIONS
54 INKSCAPE ?= inkscape
55 INKSCAPE_EXTENSIONS := $(shell $(INKSCAPE) -x)
56 endif
57 RECOLOUR_SVG ?= $(INKSCAPE_EXTENSIONS)/color_replace.py
58
59 DEPLOY_ARCH=x86_64-unknown-linux-musl
60 DEPLOY_RELEASE=debug
61 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
62 DEPLOYED_BRANCH=deployed
63
64 #---------- nailing-cargo ----------
65
66 ifneq (,$(wildcard ../Cargo.nail))
67
68 NAILING_CARGO = nailing-cargo
69 CARGO = $(NAILING_CARGO)
70 BUILD_SUBDIR ?= ../Build
71 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
72
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
77
78 clean-nailing:
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)))
82
83 else
84 clean-nailing:
85 endif # Cargo.nail
86
87 BUILD_SUBDIR ?= ../Build
88 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
89 USVG_CMD ?= $(USVG)
90
91 WASM_PACKED=$(TARGET_DIR)/packed-wasm
92
93 #---------- local programs ----------
94
95 define lp
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/
102 )
103 endef
104
105 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
106 $(eval $(call lp,USVG,resvg,release,usvg))
107
108 #---------- variables defining bits of source etc. ----------
109
110 PROGRAMS=daemon-otter otter
111
112 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
113 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
114
115 TS_SRCS= script
116 TS_SRC_FILES= \
117         $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
118         webassembly-types/webassembly.d.ts \
119         templates/otter_wasm.ns.d.ts
120
121 LITFILES= LICENCE AGPLv3
122 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
123
124 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
125                         $(LITFILES) $(TXTFILES)) \
126                 $(wildcard templates/*.tera)
127
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.
134
135 #---------- toplevel aggregate targets ----------
136
137 check: stamp/cargo.check at wdt
138         @echo 'All tests passed.'
139
140 doc: cargo-doc
141
142 debug release:: %: stamp/cargo.% assets libraries extra-%
143
144 cargo: cargo-debug cargo-wasm-release
145
146 cargo-debug cargo-release cargo-check cargo-doc \
147 cargo-wasm-debug cargo-wasm-release:: \
148 cargo-%: stamp/cargo.%
149
150 cargo-wasm: cargo-wasm-release
151
152 wasm: stamp/wasm-bindgen
153
154 assets: js stamp/wasm-bindgen $(FILEASSETS)
155
156 js: templates/script.js
157
158 extra-debug:
159 extra-release: bundled-sources
160
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
166
167 #---------- cargo ----------
168
169 DR=debug release
170 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
171
172 $(addprefix stamp/cargo.,$(DR)):: \
173 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
174         $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
175         $(stamp)
176
177 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
178         $(CARGO) build --workspace -p otter --bin $*
179
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
183
184 stamp/cargo.check: $(call rsrcs,.)
185         $(CARGO) test --workspace
186         $(stamp)
187
188 stamp/cargo-at.debug: $(call rsrcs,.)
189         $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
190         $(stamp)
191
192 stamp/cargo-wdt.debug: $(call rsrcs,.)
193         $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
194         $(stamp)
195
196 stamp/cargo.doc: $(call rsrcs,.)
197         $(CARGO) doc --workspace 2>&1 | egrep -vf .cargo-doc-suppress-errors
198         $(stamp)
199
200 stamp/cargo.doc-otter-only: $(call rsrcs,.)
201         $(CARGO) doc --workspace -p otter --no-deps
202         $(stamp)
203
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,$*)
207         $(stamp)
208
209 stamp/cargo.deploy-build: $(call rsrcs,.)
210         $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
211         $(stamp)
212
213 #---------- wasm ----------
214
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
220         $(stamp)
221
222 #---------- bundle-sources ----------
223
224 BUNDLED_SOURCES_DIRS += otter
225
226 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
227
228 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
229
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/$*
236
237 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
238
239 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
240         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
241
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);          \
246                 mv -f $@.tmp $@;                                \
247         '
248
249 bundled-sources::
250         @echo Bundled sources.
251
252 #---------- svg processing ----------
253
254 LIBRARIES ?= $(basename $(wildcard library/*.toml))
255
256 include $(addsuffix /files.make, $(LIBRARIES))
257
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)
261
262 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
263
264 library/%/files.make: media-scraper library/%.toml
265         ./$< --offline library/$*.toml
266
267 #---------- typescript ----------
268
269 templates/%.js: tsc-wrap tsconfig.json
270         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
271
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
275
276 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
277 #       ./$^ >$@.tmp && mv -f $@.tmp $@
278
279 #js-check: templates/bigfloat-tests.js
280 #       nodejs <$<
281 #       @echo 'nodejs check $< ok'
282
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/' <$<; \
288         echo '}'
289         mv -v $@.tmp $@
290
291 #---------- other templates ----------
292
293 $(addprefix templates/,$(LITFILES)): templates/%: %
294         cp $< $@.new && mv -f $@.new $@
295
296 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
297         cp $< $@.new && mv -f $@.new $@
298
299 libraries: $(LIBRARY_FILES)
300
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 $@
305
306 #---------- webdriver tests (wdt) ----------
307
308 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
309 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
310
311 WDT_LANDSCAPE_TESTS = wdt-altergame
312
313 at:     $(foreach f, $(AT_TESTS), stamp/$f.check)
314
315 wdt:    $(foreach f, $(WDT_TESTS), stamp/$f.check) \
316         $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
317
318 RUNTEST_DEPS =  apitest/run1 stamp/cargo.debug $(FILEASSETS) \
319                 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
320
321 AT_DEPS =       $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
322                 stamp/cargo-at.debug
323
324 WDT_DEPS =      $(RUNTEST_DEPS) \
325                 stamp/cargo-wdt.debug
326
327 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
328
329 AT_RUN = $(AT_WDT_RUN) $(basename $(notdir $@))
330 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
331
332 stamp/at-%.check:       $(AT_DEPS)
333         $(AT_RUN)
334         $(stamp)
335
336 stamp/wdt-%.check:      $(WDT_DEPS)
337         $(WDT_RUN)
338         $(stamp)
339
340 stamp/wdt-%.lcheck:     $(WDT_DEPS)
341         $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
342         $(stamp)
343
344 #---------- deployment ----------
345
346 DEPLOY_USER=ian@login.chiark.greenend.org.uk
347 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
348 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
349
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)
360         -git push origin
361         -git push chiark
362
363 #$(DEPLOY_BASE)/bundled-sources
364
365 #---------- clean ----------
366
367 clean-for-retest:
368         rm -f templates/script.js library/*/*.usvg stamp/*
369         rm -rf $(LIBRARY_CLEAN)
370         find * -name '*~' -print0 | xargs -0r rm --
371
372 clean: clean-nailing clean-for-retest
373         rm -rf target
374         $(NAILING_CARGO_JUST_RUN) rm -rf target