chiark / gitweb /
Makefile: provide new clena-for-retest target
[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 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 ----------
158
159 DR=debug release
160 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
161
162 $(addprefix stamp/cargo.,$(DR)):: \
163 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
164         $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
165         $(stamp)
166
167 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
168         $(CARGO) build --workspace -p otter --bin $*
169
170 stamp/cargo.check: $(call rsrcs,.)
171         $(CARGO) test --workspace
172         $(stamp)
173
174 stamp/cargo-wdt.debug: $(call rsrcs,.)
175         $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
176         $(stamp)
177
178 stamp/cargo.doc: $(call rsrcs,.)
179         $(CARGO) doc --workspace 2>&1 | egrep -vf .cargo-doc-suppress-errors
180         $(stamp)
181
182 stamp/cargo.doc-otter-only: $(call rsrcs,.)
183         $(CARGO) doc --workspace -p otter --no-deps
184         $(stamp)
185
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,$*)
189         $(stamp)
190
191 stamp/cargo.deploy-build: $(call rsrcs,.)
192         $(CARGO) -T$(DEPLOY_ARCH) build --workspace $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
193         $(stamp)
194
195 #---------- wasm ----------
196
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
201         $(stamp)
202
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.)
207 # See
208 #    https://github.com/WebAssembly/binaryen/issues/3006
209
210 #---------- bundle-sources ----------
211
212 BUNDLED_SOURCES_DIRS += otter
213
214 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
215
216 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
217
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/$*
224
225 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
226
227 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
228         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
229
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);          \
234                 mv -f $@.tmp $@;                                \
235         '
236
237 bundled-sources::
238         @echo Bundled sources.
239
240 #---------- svg processing ----------
241
242 LIBRARIES ?= $(basename $(wildcard library/*.toml))
243
244 include $(addsuffix /files.make, $(LIBRARIES))
245
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
249
250 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
251
252 library/%/files.make: media-scraper library/%.toml
253         ./$< --offline library/$*.toml
254
255 #---------- typescript ----------
256
257 templates/%.js: tsc-wrap tsconfig.json
258         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
259
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
263
264 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
265 #       ./$^ >$@.tmp && mv -f $@.tmp $@
266
267 #js-check: templates/bigfloat-tests.js
268 #       nodejs <$<
269 #       @echo 'nodejs check $< ok'
270
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/' <$<; \
276         echo '}'
277         mv -v $@.tmp $@
278
279 #---------- other templates ----------
280
281 $(addprefix templates/,$(LITFILES)): templates/%: %
282         cp $< $@.new && mv -f $@.new $@
283
284 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
285         cp $< $@.new && mv -f $@.new $@
286
287 libraries: $(LIBRARY_FILES)
288
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 $@
293
294 #---------- webdriver tests (wdt) ----------
295
296 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
297
298 WDT_LANDSCAPE_TESTS = wdt-altergame
299
300 wdt:    $(foreach f, $(WDT_TESTS), stamp/$f.check) \
301         $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
302
303 WDT_DEPS =      wdriver/run1 stamp/cargo.debug stamp/cargo-wdt.debug \
304                 $(FILEASSETS) templates/script.js \
305                 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
306
307 stamp/wdt-%.check:      $(WDT_DEPS)
308         $(NAILING_CARGO_JUST_RUN) $(abspath $<) $(basename $(notdir $@))
309         $(stamp)
310
311 stamp/wdt-%.lcheck:     $(WDT_DEPS)
312         $(NAILING_CARGO_JUST_RUN) $(abspath $<) $(basename $(notdir $@)) --as-if=lwdt-$* --layout=Landscape
313         $(stamp)
314
315 #---------- deployment ----------
316
317 DEPLOY_USER=ian@login.chiark.greenend.org.uk
318 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
319 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
320
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)
331         -git push origin
332         -git push chiark
333
334 #$(DEPLOY_BASE)/bundled-sources
335
336 #---------- clean ----------
337
338 clean-for-retest:
339         rm -f templates/script.js library/*/*.usvg stamp/*
340         rm -rf $(LIBRARY_CLEAN)
341         find * -name '*~' -print0 | xargs -0r rm --
342
343 clean: clean-nailing clean-for-retest
344         rm -rf target
345         $(NAILING_CARGO_JUST_RUN) rm -rf target