chiark / gitweb /
Makefile: Use --target, not -T, for deploy arch
[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 full-check: all check cargo-syntaxcheck-release shapelib sphinx-doc
16 full-check: for-deploy release
17 everything: debug doc release check bundled-sources
18
19 shapelib: templates/shapelib.html stamp/cargo.doc-otter-only
20         @echo 'Shape library preview and docs can now be found here:'
21         @echo '  file://$(PWD)/$<'
22         @echo '  file://$(abspath $(TARGET_DIR)/doc/otter/shapelib_toml/index.html)'
23
24 MAKEFILE_DEP ?= Makefile
25 MAKEFILE_FIND_X ?=
26 # ^ set this to "x" to debug the $rsrcs rune
27
28 #---------- funky macros etc. ----------
29
30 cr = $(addprefix --,$(filter-out debug,$1))
31 rsrcs = $(shell $(foreach x,$(MAKEFILE_FIND_X),set -$x;)\
32     find -H $1 \( -name Cargo.toml -o -name Cargo.lock -o -name Cargo.lock.example -o -name \*.rs \) \! -path '*/build/*' )
33 stamp=@mkdir -p stamp; touch $@
34
35 BUNDLED_SOURCES_LIT = README.md LICENCE
36 BUNDLED_SOURCES_FILES = index.html $(BUNDLED_SOURCES_LIT)
37 BUNDLED_SOURCES_LINKS += $(BUNDLED_SOURCES_LIT) otter/
38 BUNDLED_SOURCES += $(BUNDLED_SOURCES_FILES)
39
40 #---------- programs and config variables ----------
41
42 CARGO ?= cargo
43 TARGET_DIR ?= target
44
45 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
46
47 WASM_BINDGEN = $(TARGET_DIR)/debug/wasm-bindgen
48 WASM_BINDGEN_OPTIONS =                                          \
49         --remove-name-section --remove-producers-section        \
50         --typescript --no-modules                               \
51
52 BUNDLE_SOURCES ?= bundle-rust-sources
53
54 SPHINXBUILD   = sphinx-build
55
56 ifndef INKSCAPE_EXTENSIONS
57 INKSCAPE ?= inkscape
58 INKSCAPE_EXTENSIONS := $(shell $(INKSCAPE) -x)
59 endif
60 RECOLOUR_SVG ?= $(INKSCAPE_EXTENSIONS)/color_replace.py
61
62 DEPLOY_ARCH=x86_64-unknown-linux-musl
63 DEPLOY_RELEASE=debug
64 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
65 DEPLOYED_BRANCH=deployed
66
67 #---------- nailing-cargo ----------
68
69 ifneq (,$(wildcard ../Cargo.nail))
70
71 NAILING_CARGO = nailing-cargo
72 CARGO = $(NAILING_CARGO)
73 BUILD_SUBDIR ?= ../Build
74 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
75
76 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
77 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
78 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
79 WASM_BINDGEN_CLI_CARGO_OPTS ?= --subcommand-props=!manifest-path
80
81 clean-nailing:
82         $(NAILING_CARGO_JUST_RUN) \
83  sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
84                 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
85
86 else
87 clean-nailing:
88 endif # Cargo.nail
89
90 BUILD_SUBDIR ?= ../Build
91 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
92 USVG_CMD ?= $(USVG)
93
94 WASM_PACKED=$(TARGET_DIR)/packed-wasm
95
96 #---------- local programs ----------
97
98 define lp
99 stamp/cargo.$2: $(call rsrcs, ! -name \*.rs)
100         $$(CARGO) build $(call cr,$3) -p $2
101         $$(stamp)
102 $1 := $(abspath $(TARGET_DIR)/$3/$4)
103 endef
104
105 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
106 $(eval $(call lp,USVG,usvg,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         $(WASM_PACKED)/otter_wasm.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.debug-check at wdt
138         @echo 'All tests passed.'
139
140 full-check: stamp/cargo.release-check
141 full-check: stamp/cargo.release-miri stamp/cargo.debug-miri
142
143 doc: cargo-doc sphinx-doc
144
145 debug release:: %: stamp/cargo.% assets libraries extra-%
146
147 cargo: cargo-debug cargo-wasm-release
148
149 cargo-debug cargo-release cargo-check cargo-doc \
150 cargo-wasm-debug cargo-wasm-release:: \
151 cargo-%: stamp/cargo.%
152
153 cargo-wasm: cargo-wasm-release
154
155 wasm: stamp/wasm-bindgen
156
157 assets: js stamp/wasm-bindgen $(FILEASSETS)
158
159 js: templates/script.js
160
161 extra-debug:
162 extra-release: bundled-sources
163
164 cargo-syntaxcheck: cargo-syntaxcheck-host cargo-syntaxcheck-wasm
165 cargo-syntaxcheck-host:
166         $(CARGO) check --workspace
167 cargo-syntaxcheck-wasm:
168         $(CARGO) check --target $(WASM) -p otter-wasm --release
169 cargo-syntaxcheck-release:
170         $(CARGO) check --workspace --release
171
172 #---------- cargo ----------
173
174 DR=debug release
175 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
176
177 $(addprefix stamp/cargo.,$(DR)):: \
178 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
179         $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
180         $(stamp)
181
182 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
183         $(CARGO) build --workspace -p otter --bin $*
184
185 stamp/cargo.wasm-bindgen: $(call rsrcs, ! -name \*.rs)
186         $(CARGO) $(WASM_BINDGEN_CLI_CARGO_OPTS) build --target-dir=target \
187                 --manifest-path=$(abspath wasm/Cargo.toml) -p wasm-bindgen-cli
188         $(stamp)
189
190 stamp/cargo.%-check: $(call rsrcs,.)
191         $(CARGO) test --workspace $(call cr,$*)
192         $(stamp)
193
194 stamp/cargo.%-miri: $(call rsrcs,.)
195         $(CARGO) miri test --workspace $(call cr,$*)
196         $(stamp)
197
198 stamp/cargo-at.debug: $(call rsrcs,.)
199         $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
200         $(stamp)
201
202 stamp/cargo-wdt.debug: $(call rsrcs,.)
203         $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
204         $(stamp)
205
206 stamp/cargo.doc: $(call rsrcs,.)
207         $(CARGO) doc $(CARGO_DOC_OPTS) --workspace 2>&1 |egrep -vf .cargo-doc-suppress-errors
208         $(stamp)
209
210 stamp/cargo.doc-otter-only: $(call rsrcs,.)
211         $(CARGO) doc $(CARGO_DOC_OPTS) --workspace -p otter --no-deps
212         $(stamp)
213
214 $(addprefix stamp/cargo.wasm-,$(DR)):: \
215 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
216         $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
217         $(stamp)
218
219 stamp/cargo.deploy-build: $(call rsrcs,.)
220         $(CARGO) build --target $(DEPLOY_ARCH) $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
221         $(stamp)
222
223 #---------- sphnix ----------
224
225 sphinx-doc: docs/html/index.html
226         @echo 'Documentation can now be found here:'
227         @echo '  file://$(PWD)/$<'
228
229 docs/html/index.html: docs/conf.py $(wildcard docs/*.md docs/*.rst)
230         $(SPHINXBUILD) -M html docs docs $(SPHINXOPTS)
231
232 #---------- wasm ----------
233
234 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-bindgen
235 stamp/wasm-bindgen: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
236         $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
237                 $(WASM_BINDGEN_OPTIONS) --out-dir target/packed-wasm \
238                 target/$(WASM)/release/otter_wasm.wasm
239         $(stamp)
240
241 #---------- bundle-sources ----------
242
243 BUNDLED_SOURCES_DIRS += otter
244
245 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
246
247 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
248
249 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
250 bundled-sources/%: stamp/cargo.bundle-sources
251         set -e; d=$(abspath $(TARGET_BUNDLED)); \
252         $(NAILING_CARGO_JUST_RUN) mkdir -p $$d; \
253         $(if $(filter-out otter,$*), cd ../$*;) \
254         $(BUNDLE_SOURCES_CMD) --output $$d/$*
255
256 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
257
258 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
259         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
260
261 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index $(MAKEFILE_DEP)
262         $(NAILING_CARGO_JUST_RUN) sh -ec '                      \
263                 cd $(abspath $(src)); mkdir -p $(dir $@);       \
264                 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS);          \
265                 mv -f $@.tmp $@;                                \
266         '
267
268 bundled-sources::
269         @echo Bundled sources.
270
271 #---------- svg processing ----------
272
273 LIBRARIES ?= $(basename $(wildcard library/*.toml))
274 USVG_DEP = stamp/cargo.usvg
275
276 include $(addsuffix /files.make, $(LIBRARIES))
277
278 USVG_PROCESSOR = usvg-processor
279 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
280 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_DEP) $(MAKEFILE_DEP)
281
282 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
283
284 library/%/files.make: media-scraper library/%.toml
285         ./$< --offline library/$*.toml
286
287 #---------- typescript ----------
288
289 templates/%.js: tsc-wrap tsconfig.json
290         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
291
292 templates/script.js: $(TS_SRC_FILES) stamp/wasm-bindgen
293
294 #---------- other templates ----------
295
296 $(addprefix templates/,$(LITFILES)): templates/%: %
297         cp $< $@.new && mv -f $@.new $@
298
299 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
300         cp $< $@.new && mv -f $@.new $@
301
302 libraries: $(LIBRARY_FILES)
303
304 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
305         $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
306         --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
307                 preview >$@.tmp && mv -f $@.tmp $@
308
309 #---------- webdriver tests (wdt) ----------
310
311 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
312 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
313
314 WDT_LANDSCAPE_TESTS = wdt-altergame
315
316 at:     $(foreach f, $(AT_TESTS), stamp/$f.check)
317
318 wdt:    $(foreach f, $(WDT_TESTS), stamp/$f.check) \
319         $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
320
321 RUNTEST_DEPS =  apitest/run1 stamp/cargo.debug $(FILEASSETS) \
322                 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
323
324 AT_DEPS =       $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
325                 stamp/cargo-at.debug
326
327 WDT_DEPS =      $(RUNTEST_DEPS) \
328                 stamp/cargo-wdt.debug
329
330 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
331
332 AT_RUN = $(AT_WDT_RUN) $(basename $(notdir $@))
333 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
334
335 for-at:                 $(AT_DEPS)
336 stamp/at-%.check:       $(AT_DEPS)
337         $(AT_RUN)
338         $(stamp)
339
340 for-wdt:                $(WDT_DEPS)
341 stamp/wdt-%.check:      $(WDT_DEPS)
342         $(WDT_RUN)
343         $(stamp)
344
345 stamp/wdt-%.lcheck:     $(WDT_DEPS)
346         $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
347         $(stamp)
348
349 #---------- deployment ----------
350
351 DEPLOY_USER=ian@login.chiark.greenend.org.uk
352 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
353 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
354
355 for-deploy: stamp/cargo.deploy-build
356 deploy: for-deploy bundled-sources assets libraries
357         rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
358         rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
359         rsync -r README.md $(DEPLOY_BASE)/.
360         rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
361         rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
362                 $(DEPLOY_BASE)/assets/
363         rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
364         ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
365         git branch -f $(DEPLOYED_BRANCH)
366         -git push origin master
367         -git push chiark master
368
369 #$(DEPLOY_BASE)/bundled-sources
370
371 #---------- clean ----------
372
373 clean-for-retest:
374         rm -f templates/script.js library/*/*.usvg stamp/*
375         rm -rf $(LIBRARY_CLEAN)
376         find * -name '*~' -print0 | xargs -0r rm --
377
378 clean: clean-nailing clean-for-retest
379         rm -rf target
380         $(NAILING_CARGO_JUST_RUN) rm -rf target