chiark / gitweb /
add missing dependency
[otter.git] / Makefile
1 #!/bin/sh
2 # Copyright 2020 Ian Jackson
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # There is NO WARRANTY.
5
6 # make -j8
7 # make -j8 release
8
9 SHELL=/bin/bash
10 src=.
11
12 default: all check
13 all: debug
14 everything: debug doc release check bundled-sources
15
16 #---------- funky macros etc. ----------
17
18 cr = $(addprefix --,$(filter-out debug,$1))
19 rsrcs = $(shell \
20     find $1 \( -name Cargo.toml -o -name Cargo.lock -o -name Cargo.lock.example -o -name \*.rs \) )
21 stamp=@mkdir -p stamp; touch $@
22
23 BUNDLED_SOURCES_LIT = README.md LICENCE
24 BUNDLED_SOURCES_FILES = index.html $(BUNDLED_SOURCES_LIT)
25 BUNDLED_SOURCES_LINKS += $(BUNDLED_SOURCES_LIT) otter/
26 BUNDLED_SOURCES += $(BUNDLED_SOURCES_FILES)
27
28 #---------- programs and config variables ----------
29
30 CARGO ?= cargo
31 TARGET_DIR ?= target
32
33 USVG ?= usvg
34 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
35
36 WASM_PACK ?= wasm-pack
37 WASM_PACK_OPTIONS = --cargo-path=/bin/echo
38
39 BUNDLE_SOURCES ?= bundle-rust-sources
40
41 DEPLOY_ARCH=x86_64-unknown-linux-musl
42 DEPLOY_RELEASE=debug
43 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
44 DEPLOYED_BRANCH=deployed
45
46 #---------- nailing-cargo ----------
47
48 ifneq (,$(wildcard ../Cargo.nail))
49
50 NAILING_CARGO = nailing-cargo
51 CARGO = $(NAILING_CARGO)
52 BUILD_SUBDIR ?= ../Build
53 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
54
55 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
56 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
57 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
58 WASM_PACK_CMD ?= $(NAILING_CARGO) --linkfarm=git --- $(WASM_PACK)
59
60 clean-nailing:
61         $(NAILING_CARGO_JUST_RUN) \
62  sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
63                 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
64
65 else
66 clean-nailing:
67 endif # Cargo.nail
68
69 BUILD_SUBDIR ?= ../Build
70 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
71 USVG_CMD ?= $(USVG)
72 WASM_PACK_CMD ?= $(WASM_PACK)
73
74 WASM_PACKED=$(TARGET_DIR)/packed-wasm
75
76 #---------- local programs ----------
77
78 define lp
79 $(if $(wildcard $(BUILD_SUBDIR)/$2),
80 $(shell echo >&2 'Makefile: lp: Using program $4 from $(BUILD_SUBDIR)/$2')
81 $1 := $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4)
82 $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4):; cd ../$2 && $$(CARGO) build $(call cr,$3)
83 BUNDLED_SOURCES_DIRS += $2
84 BUNDLED_SOURCES_LINKS += $2/
85 )
86 endef
87
88 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
89 $(eval $(call lp,USVG,resvg,release,usvg))
90 $(eval $(call lp,WASM_PACK,wasm-pack,debug,wasm-pack))
91
92 #---------- variables defining bits of source etc. ----------
93
94 PROGRAMS=daemon-otter otter
95
96 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
97 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
98
99 TS_SRCS= script
100 TS_SRC_FILES= \
101         $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
102         webassembly-types/webassembly.d.ts \
103         templates/otter_wasm.ns.d.ts
104
105 LITFILES= LICENCE AGPLv3
106 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
107
108 FILEASSETS = $(addprefix templates/, libre script.js \
109                         $(LITFILES) $(TXTFILES)) \
110                 $(wildcard templates/*.tera)
111
112 WASM := wasm32-unknown-unknown
113
114 #---------- toplevel aggregate targets ----------
115
116 check: stamp/cargo.check wdt
117         @echo 'All tests passed.'
118
119 doc: cargo-doc
120
121 debug release:: %: stamp/cargo.% assets libraries extra-%
122
123 cargo: cargo-debug cargo-wasm-release
124
125 cargo-debug cargo-release cargo-check cargo-doc \
126 cargo-wasm-debug cargo-wasm-release:: \
127 cargo-%: stamp/cargo.%
128
129 cargo-wasm: cargo-wasm-release
130
131 wasm-pack: stamp/wasm-pack
132
133 assets: js stamp/wasm-pack $(FILEASSETS)
134
135 js: templates/script.js
136
137 extra-debug:
138 extra-release: bundled-sources
139
140 #---------- cargo ----------
141
142 DR=debug release
143 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
144
145 $(addprefix stamp/cargo.,$(DR)):: \
146 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
147         $(CARGO) build $(call cr,$*) -p otter -p otter-daemon
148         $(stamp)
149
150 stamp/cargo.check: $(call rsrcs,.)
151         $(CARGO) test --workspace
152         $(stamp)
153
154 stamp/cargo-wdt.debug: $(call rsrcs,.)
155         $(CARGO) build $(call cr,$*) -p otter-webdriver-tests
156         $(stamp)
157
158 stamp/cargo.doc: $(call rsrcs,.)
159         $(CARGO) doc --workspace
160         $(stamp)
161
162 $(addprefix stamp/cargo.wasm-,$(DR)):: \
163 stamp/cargo.wasm-%: $(call rsrcs, zcoord wasm Cargo.*)
164         $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
165         $(stamp)
166
167 stamp/cargo.deploy-build: $(call rsrcs,.)
168         $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
169         $(stamp)
170
171 #---------- wasm ----------
172
173 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-pack
174 stamp/wasm-pack: stamp/cargo.wasm-release
175         $(WASM_PACK_CMD) $(WASM_PACK_OPTIONS) build \
176                 --out-dir=../target/packed-wasm wasm -t no-modules --release
177         $(stamp)
178
179 # There is some kind of bug in wasm-pack's version of wasm-opt, which
180 # can be avoided by running --dev instead (but also then using the
181 # debug rust built).  IME this happened when trying to return a u64,
182 # so maybe bigints are affected?  (Which I don't want to use anyway.)
183 # See
184 #    https://github.com/WebAssembly/binaryen/issues/3006
185
186 #---------- bundle-sources ----------
187
188 BUNDLED_SOURCES_DIRS += otter
189
190 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
191
192 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
193
194 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
195 bundled-sources/%: $(wildcard $(BUNDLE_SOURCES))
196         set -e; d=$(abspath $(TARGET_BUNDLED)); \
197         $(NAILING_CARGO_JUST_RUN) mkdir -p $$d; \
198         $(if $(filter-out otter,$*), cd ../$*;) \
199         $(BUNDLE_SOURCES_CMD) --output $$d/$*
200
201 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
202
203 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
204         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
205
206 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index Makefile
207         $(NAILING_CARGO_JUST_RUN) sh -ec '                      \
208                 cd $(abspath $(src)); mkdir -p $(dir $@);       \
209                 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS);          \
210                 mv -f $@.tmp $@;                                \
211         '
212
213 bundled-sources::
214         @echo Bundled sources.
215
216 #---------- svg processing ----------
217
218 include $(wildcard library/*/files.make)
219
220 USVG_PROCESSOR = usvg-processor
221 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
222 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_BINARY) Makefile
223
224 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
225
226 #---------- typescript ----------
227
228 templates/%.js: tsc-wrap tsconfig.json
229         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
230
231 templates/script.js: $(TS_SRC_FILES)
232 #templates/bigfloat-tests.js: templates/bigfloat.ts \
233 #       templates/bigfloat-tests.ts templates/bigfloat-tests-auto.ts
234
235 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
236 #       ./$^ >$@.tmp && mv -f $@.tmp $@
237
238 #js-check: templates/bigfloat-tests.js
239 #       nodejs <$<
240 #       @echo 'nodejs check $< ok'
241
242 templates/otter_wasm.ns.d.ts: $(WASM_PACKED)/otter_wasm.d.ts \
243                                 stamp/wasm-pack Makefile
244         set -e; exec >$@.tmp;                           \
245         echo 'declare namespace wasm_bindgen {';        \
246         sed 's/^export default function init/export function init/' <$<; \
247         echo '}'
248         mv -v $@.tmp $@
249
250 #---------- other templates ----------
251
252 $(addprefix templates/,$(LITFILES)): templates/%: %
253         cp $< $@.new && mv -f $@.new $@
254
255 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
256         cp $< $@.new && mv -f $@.new $@
257
258 libraries: $(LIBRARY_FILES)
259
260 #---------- webdriver tests (wdt) ----------
261
262 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
263
264 WDT_LANDSCAPE_TESTS = wdt-altergame
265
266 wdt:    $(foreach f, $(WDT_TESTS), stamp/$f.check) \
267         $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
268
269 WDT_DEPS =      wdriver/run1 stamp/cargo.debug stamp/cargo-wdt.debug \
270                 $(FILEASSETS) templates/script.js \
271                 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
272
273 stamp/wdt-%.check:      $(WDT_DEPS)
274         $(NAILING_CARGO_JUST_RUN) $(abspath $<) $(basename $(notdir $@))
275         $(stamp)
276
277 stamp/wdt-%.lcheck:     $(WDT_DEPS)
278         $(NAILING_CARGO_JUST_RUN) $(abspath $<) $(basename $(notdir $@)) --as-if=lwdt-$* --layout=Landscape
279         $(stamp)
280
281 #---------- deployment ----------
282
283 DEPLOY_USER=ian@login.chiark.greenend.org.uk
284 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
285 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
286
287 deploy: stamp/cargo.deploy-build bundled-sources assets libraries
288         rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
289         rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
290         rsync -r README.md $(DEPLOY_BASE)/.
291         rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
292         rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
293                 $(DEPLOY_BASE)/assets/
294         rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
295         ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
296         git branch -f $(DEPLOYED_BRANCH)
297         -git push origin
298         -git push chiark
299
300 #$(DEPLOY_BASE)/bundled-sources
301
302 #---------- clean ----------
303
304 clean: clean-nailing
305         rm -f templates/script.js library/*/*.usvg stamp/*
306         rm -rf target
307         $(NAILING_CARGO_JUST_RUN) rm -rf target