chiark / gitweb /
at: impl PieceSpecForOp for ()
[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 #---------- funky macros etc. ----------
23
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 $@
28
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)
33
34 #---------- programs and config variables ----------
35
36 CARGO ?= cargo
37 TARGET_DIR ?= target
38
39 USVG ?= usvg
40 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
41
42 WASM_PACK ?= wasm-pack
43 WASM_PACK_OPTIONS = --cargo-path=/bin/echo
44
45 BUNDLE_SOURCES ?= bundle-rust-sources
46
47 ifndef INKSCAPE_EXTENSIONS
48 INKSCAPE ?= inkscape
49 INKSCAPE_EXTENSIONS := $(shell $(INKSCAPE) -x)
50 endif
51 RECOLOUR_SVG ?= $(INKSCAPE_EXTENSIONS)/color_replace.py
52
53 DEPLOY_ARCH=x86_64-unknown-linux-musl
54 DEPLOY_RELEASE=debug
55 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
56 DEPLOYED_BRANCH=deployed
57
58 #---------- nailing-cargo ----------
59
60 ifneq (,$(wildcard ../Cargo.nail))
61
62 NAILING_CARGO = nailing-cargo
63 CARGO = $(NAILING_CARGO)
64 BUILD_SUBDIR ?= ../Build
65 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
66
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)
71
72 clean-nailing:
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)))
76
77 else
78 clean-nailing:
79 endif # Cargo.nail
80
81 BUILD_SUBDIR ?= ../Build
82 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
83 USVG_CMD ?= $(USVG)
84 WASM_PACK_CMD ?= $(WASM_PACK)
85
86 WASM_PACKED=$(TARGET_DIR)/packed-wasm
87
88 #---------- local programs ----------
89
90 define lp
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/
97 )
98 endef
99
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))
103
104 #---------- variables defining bits of source etc. ----------
105
106 PROGRAMS=daemon-otter otter
107
108 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
109 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
110
111 TS_SRCS= script
112 TS_SRC_FILES= \
113         $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
114         webassembly-types/webassembly.d.ts \
115         templates/otter_wasm.ns.d.ts
116
117 LITFILES= LICENCE AGPLv3
118 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
119
120 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
121                         $(LITFILES) $(TXTFILES)) \
122                 $(wildcard templates/*.tera)
123
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.
130
131 #---------- toplevel aggregate targets ----------
132
133 check: stamp/cargo.check at wdt
134         @echo 'All tests passed.'
135
136 doc: cargo-doc
137
138 debug release:: %: stamp/cargo.% assets libraries extra-%
139
140 cargo: cargo-debug cargo-wasm-release
141
142 cargo-debug cargo-release cargo-check cargo-doc \
143 cargo-wasm-debug cargo-wasm-release:: \
144 cargo-%: stamp/cargo.%
145
146 cargo-wasm: cargo-wasm-release
147
148 wasm-pack: stamp/wasm-pack
149
150 assets: js stamp/wasm-pack $(FILEASSETS)
151
152 js: templates/script.js
153
154 extra-debug:
155 extra-release: bundled-sources
156
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
162
163 #---------- cargo ----------
164
165 DR=debug release
166 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
167
168 $(addprefix stamp/cargo.,$(DR)):: \
169 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
170         $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
171         $(stamp)
172
173 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
174         $(CARGO) build --workspace -p otter --bin $*
175
176 stamp/cargo.check: $(call rsrcs,.)
177         $(CARGO) test --workspace
178         $(stamp)
179
180 stamp/cargo-at.debug: $(call rsrcs,.)
181         $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
182         $(stamp)
183
184 stamp/cargo-wdt.debug: $(call rsrcs,.)
185         $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
186         $(stamp)
187
188 stamp/cargo.doc: $(call rsrcs,.)
189         $(CARGO) doc --workspace 2>&1 | egrep -vf .cargo-doc-suppress-errors
190         $(stamp)
191
192 stamp/cargo.doc-otter-only: $(call rsrcs,.)
193         $(CARGO) doc --workspace -p otter --no-deps
194         $(stamp)
195
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,$*)
199         $(stamp)
200
201 stamp/cargo.deploy-build: $(call rsrcs,.)
202         $(CARGO) -T$(DEPLOY_ARCH) build --workspace $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
203         $(stamp)
204
205 #---------- wasm ----------
206
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
211         $(stamp)
212
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.)
217 # See
218 #    https://github.com/WebAssembly/binaryen/issues/3006
219
220 #---------- bundle-sources ----------
221
222 BUNDLED_SOURCES_DIRS += otter
223
224 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
225
226 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
227
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/$*
234
235 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
236
237 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
238         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
239
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);          \
244                 mv -f $@.tmp $@;                                \
245         '
246
247 bundled-sources::
248         @echo Bundled sources.
249
250 #---------- svg processing ----------
251
252 LIBRARIES ?= $(basename $(wildcard library/*.toml))
253
254 include $(addsuffix /files.make, $(LIBRARIES))
255
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
259
260 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
261
262 library/%/files.make: media-scraper library/%.toml
263         ./$< --offline library/$*.toml
264
265 #---------- typescript ----------
266
267 templates/%.js: tsc-wrap tsconfig.json
268         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
269
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
273
274 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
275 #       ./$^ >$@.tmp && mv -f $@.tmp $@
276
277 #js-check: templates/bigfloat-tests.js
278 #       nodejs <$<
279 #       @echo 'nodejs check $< ok'
280
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/' <$<; \
286         echo '}'
287         mv -v $@.tmp $@
288
289 #---------- other templates ----------
290
291 $(addprefix templates/,$(LITFILES)): templates/%: %
292         cp $< $@.new && mv -f $@.new $@
293
294 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
295         cp $< $@.new && mv -f $@.new $@
296
297 libraries: $(LIBRARY_FILES)
298
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 $@
303
304 #---------- webdriver tests (wdt) ----------
305
306 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
307 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
308
309 WDT_LANDSCAPE_TESTS = wdt-altergame
310
311 at:     $(foreach f, $(AT_TESTS), stamp/$f.check)
312
313 wdt:    $(foreach f, $(WDT_TESTS), stamp/$f.check) \
314         $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
315
316 RUNTEST_DEPS =  apitest/run1 stamp/cargo.debug $(FILEASSETS) \
317                 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
318
319 AT_DEPS =       $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
320                 stamp/cargo-at.debug
321
322 WDT_DEPS =      $(RUNTEST_DEPS) \
323                 stamp/cargo-wdt.debug
324
325 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
326
327 AT_RUN = $(AT_WDT_RUN) $(basename $(notdir $@))
328 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
329
330 stamp/at-%.check:       $(AT_DEPS)
331         $(AT_RUN)
332         $(stamp)
333
334 stamp/wdt-%.check:      $(WDT_DEPS)
335         $(WDT_RUN)
336         $(stamp)
337
338 stamp/wdt-%.lcheck:     $(WDT_DEPS)
339         $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
340         $(stamp)
341
342 #---------- deployment ----------
343
344 DEPLOY_USER=ian@login.chiark.greenend.org.uk
345 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
346 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
347
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)
358         -git push origin
359         -git push chiark
360
361 #$(DEPLOY_BASE)/bundled-sources
362
363 #---------- clean ----------
364
365 clean-for-retest:
366         rm -f templates/script.js library/*/*.usvg stamp/*
367         rm -rf $(LIBRARY_CLEAN)
368         find * -name '*~' -print0 | xargs -0r rm --
369
370 clean: clean-nailing clean-for-retest
371         rm -rf target
372         $(NAILING_CARGO_JUST_RUN) rm -rf target