chiark / gitweb /
Makefile: Remove spurious #!
[otter.git] / Makefile
1 # Copyright 2020-2021 Ian Jackson and contributors to Otter
2 # SPDX-License-Identifier: AGPL-3.0-or-later
3 # There is NO WARRANTY.
4
5 # make -j8
6 # make -j8 release
7 # make -j8 shapelib
8
9 SHELL=/bin/bash
10 src=.
11
12 default: all check
13 all: debug
14 full-check: all check cargo-syntaxcheck-release shapelib doc-sphinx
15 full-check: for-deploy release
16 everything: debug doc release check bundled-sources
17
18 shapelib: templates/shapelib.html stamp/cargo.doc-otter-only
19         @echo 'Shape library preview and docs can now be found here:'
20         @echo '  file://$(PWD)/$<'
21         @echo '  file://$(abspath $(TARGET_DIR)/doc/otter/shapelib_toml/index.html)'
22
23 MAKEFILE_DEP ?= Makefile
24 MAKEFILE_FIND_X ?=
25 # ^ set this to "x" to debug the $rsrcs rune
26
27 #---------- funky macros etc. ----------
28
29 cr = $(addprefix --,$(filter-out debug,$1))
30 rsrcs = $(shell $(foreach x,$(MAKEFILE_FIND_X),set -$x;)\
31     find -H $1 \( -name Cargo.toml -o -name Cargo.lock -o -name Cargo.lock.example -o -name \*.rs \) \! -path '*/build/*' )
32 stamp=@mkdir -p stamp; touch $@
33
34 BUNDLED_SOURCES_LIT = README.md LICENCE
35 BUNDLED_SOURCES_FILES = index.html $(BUNDLED_SOURCES_LIT)
36 BUNDLED_SOURCES_LINKS += $(BUNDLED_SOURCES_LIT) otter/
37 BUNDLED_SOURCES += $(BUNDLED_SOURCES_FILES)
38
39 #---------- programs and config variables ----------
40
41 CARGO ?= cargo
42 TARGET_DIR ?= target
43
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 SPHINXBUILD   = sphinx-build
54
55 ifndef INKSCAPE_EXTENSIONS
56 INKSCAPE ?= inkscape
57 # inkscape 0.92.4: --extension-directory works, no --system-data-directory
58 # inkscape 1.0.2: --system-data-directory, no --extension-directory
59 INKSCAPE_EXTENSIONS := $(shell set -e; { sdd=$$( $(INKSCAPE) --system-data-directory ) && echo "$$sdd/extensions"; } || $(INKSCAPE) --extension-directory )
60 endif
61 RECOLOUR_SVG ?= $(INKSCAPE_EXTENSIONS)/color_replace.py
62
63 DEPLOY_ARCH=x86_64-unknown-linux-musl
64 DEPLOY_RELEASE=debug
65 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
66 DEPLOYED_BRANCH=deployed
67 PUBLISHED_BRANCH=published
68
69 #---------- nailing-cargo ----------
70
71 ifneq (,$(wildcard ../Cargo.nail))
72
73 NAILING_CARGO = nailing-cargo
74 CARGO = $(NAILING_CARGO)
75 BUILD_SUBDIR ?= ../Build
76 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
77
78 NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q ---
79 BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES)
80 USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG)
81 WASM_BINDGEN_CLI_CARGO_OPTS ?= --subcommand-props=!manifest-path
82
83 clean-nailing:
84         $(NAILING_CARGO_JUST_RUN) \
85  sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \
86                 $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD)))
87
88 else
89 clean-nailing:
90 endif # Cargo.nail
91
92 BUILD_SUBDIR ?= ../Build
93 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
94 USVG_CMD ?= $(USVG)
95
96 WASM_PACKED=$(TARGET_DIR)/packed-wasm
97
98 #---------- local programs ----------
99
100 define lp
101 stamp/cargo.$2: $(call rsrcs, ! -name \*.rs)
102         $$(CARGO) build $(call cr,$3) -p $2
103         $$(stamp)
104 $1 := $(abspath $(TARGET_DIR)/$3/$4)
105 endef
106
107 $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources))
108 $(eval $(call lp,USVG,usvg,release,usvg))
109
110 #---------- variables defining bits of source etc. ----------
111
112 PROGRAMS=daemon-otter otter
113
114 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
115 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
116
117 TS_SRCS= script
118 TS_SRC_FILES= \
119         $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
120         webassembly-types/webassembly.d.ts \
121         $(WASM_PACKED)/otter_wasm.d.ts
122
123 LITFILES= LICENCE AGPLv3
124 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
125
126 FILEASSETS = $(addprefix templates/, libre shapelib.html script.js \
127                         $(LITFILES) $(TXTFILES)) \
128                 $(wildcard templates/*.tera)
129
130 WASM := wasm32-unknown-unknown
131 # ^ todo: Is this still right after
132 #     Use correct ABI for wasm32 by default
133 #     https://github.com/rust-lang/rust/pull/79998
134 # ?  But maybe it doesn't matter since we're very conservative and
135 # only pass JsValue and a few strings across the WASM ABI.
136
137 #---------- toplevel aggregate targets ----------
138
139 check: stamp/cargo.debug-check at wdt
140         @echo 'Tests passed.'
141
142 full-check: stamp/cargo.release-check
143 full-check: stamp/cargo.release-miri stamp/cargo.debug-miri
144
145 full-check:
146         @echo 'Full tests passed.'
147
148 doc: cargo-doc doc-sphinx
149
150 debug release:: %: stamp/cargo.% assets libraries extra-%
151
152 cargo: cargo-debug cargo-wasm-release
153
154 cargo-debug cargo-release cargo-check cargo-doc \
155 cargo-wasm-debug cargo-wasm-release:: \
156 cargo-%: stamp/cargo.%
157
158 cargo-wasm: cargo-wasm-release
159
160 wasm: stamp/wasm-bindgen
161
162 assets: js stamp/wasm-bindgen $(FILEASSETS)
163
164 js: templates/script.js
165
166 extra-debug:
167 extra-release: bundled-sources
168
169 cargo-syntaxcheck: cargo-syntaxcheck-host cargo-syntaxcheck-wasm
170 cargo-syntaxcheck-host:
171         $(CARGO) check --workspace
172 cargo-syntaxcheck-wasm:
173         $(CARGO) check --target $(WASM) -p otter-wasm --release
174 cargo-syntaxcheck-release:
175         $(CARGO) check --workspace --release
176
177 #---------- cargo ----------
178
179 DR=debug release
180 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
181
182 $(addprefix stamp/cargo.,$(DR)):: \
183 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
184         $(CARGO) build --workspace $(call cr,$*) -p otter -p otter-daemon
185         $(stamp)
186
187 $(TARGET_DIR)/debug/%: $(call rsrcs, ! -path './wasm/*')
188         $(CARGO) build --workspace -p otter --bin $*
189
190 stamp/cargo.wasm-bindgen: $(call rsrcs, ! -name \*.rs)
191         $(CARGO) $(WASM_BINDGEN_CLI_CARGO_OPTS) build --target-dir=target \
192                 --manifest-path=$(abspath wasm/Cargo.toml) -p wasm-bindgen-cli
193         $(stamp)
194
195 stamp/cargo.%-check: $(call rsrcs,.)
196         $(CARGO) test --workspace $(call cr,$*)
197         $(stamp)
198
199 stamp/cargo.%-miri: $(call rsrcs,.)
200         $(CARGO) miri test --workspace $(call cr,$*)
201         $(stamp)
202
203 stamp/cargo-at.debug: $(call rsrcs,.)
204         $(CARGO) build --workspace $(call cr,$*) -p otter-api-tests
205         $(stamp)
206
207 stamp/cargo-wdt.debug: $(call rsrcs,.)
208         $(CARGO) build --workspace $(call cr,$*) -p otter-webdriver-tests
209         $(stamp)
210
211 stamp/cargo.doc: $(call rsrcs,.)
212         $(CARGO) doc $(CARGO_DOC_OPTS) --workspace 2>&1 |egrep -vf .cargo-doc-suppress-errors
213         $(stamp)
214
215 stamp/cargo.doc-otter-only: $(call rsrcs,.)
216         $(CARGO) doc $(CARGO_DOC_OPTS) --workspace -p otter --no-deps
217         $(stamp)
218
219 $(addprefix stamp/cargo.wasm-,$(DR)):: \
220 stamp/cargo.wasm-%: $(call rsrcs, base wasm Cargo.*)
221         $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
222         $(stamp)
223
224 stamp/cargo.deploy-build: $(call rsrcs,.)
225         $(CARGO) build --target $(DEPLOY_ARCH) $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon
226         $(stamp)
227
228 #---------- sphnix ----------
229
230 doc-sphinx: docs/html/index.html
231         @echo 'Documentation can now be found here:'
232         @echo '  file://$(PWD)/$<'
233
234 docs/html/index.html: docs/conf.py $(wildcard docs/*.md docs/*.rst)
235         $(SPHINXBUILD) -M html docs docs $(SPHINXOPTS)
236
237 #---------- wasm ----------
238
239 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-bindgen
240 stamp/wasm-bindgen: stamp/cargo.wasm-bindgen stamp/cargo.wasm-release
241         $(NAILING_CARGO_JUST_RUN) $(abspath $(WASM_BINDGEN)) \
242                 $(WASM_BINDGEN_OPTIONS) --out-dir target/packed-wasm \
243                 target/$(WASM)/release/otter_wasm.wasm
244         $(stamp)
245
246 #---------- bundle-sources ----------
247
248 BUNDLED_SOURCES_DIRS += otter
249
250 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
251
252 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
253
254 $(TARGET_BUNDLED):
255         $(NAILING_CARGO_JUST_RUN) mkdir -p $@
256
257 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
258 bundled-sources/%: stamp/cargo.bundle-sources $(TARGET_BUNDLED)
259         set -e; d=$(abspath $(TARGET_BUNDLED)); \
260         $(if $(filter-out otter,$*), cd ../$*;) \
261         $(BUNDLE_SOURCES_CMD) --output $$d/$*
262
263 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
264
265 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): \
266 $(TARGET_BUNDLED)/%: % $(TARGET_BUNDLED)
267         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
268
269 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index \
270                 $(MAKEFILE_DEP) $(TARGET_BUNDLED)
271         $(NAILING_CARGO_JUST_RUN) sh -ec '                      \
272                 cd $(abspath $(src)); mkdir -p $(dir $@);       \
273                 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS);          \
274                 mv -f $@.tmp $@;                                \
275         '
276
277 bundled-sources::
278         @echo Bundled sources.
279
280 #---------- svg processing ----------
281
282 LIBRARIES ?= $(basename $(wildcard library/*.toml))
283 USVG_DEP = stamp/cargo.usvg
284
285 include $(addsuffix /files.make, $(LIBRARIES))
286
287 USVG_PROCESSOR = usvg-processor
288 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
289 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_DEP) $(MAKEFILE_DEP)
290
291 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
292
293 library/%/files.make: media-scraper library/%.toml
294         ./$< --offline library/$*.toml
295
296 #---------- typescript ----------
297
298 templates/%.js: tsc-wrap tsconfig.json
299         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
300
301 templates/script.js: $(TS_SRC_FILES) stamp/wasm-bindgen
302
303 #---------- other templates ----------
304
305 $(addprefix templates/,$(LITFILES)): templates/%: %
306         cp $< $@.new && mv -f $@.new $@
307
308 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
309         cp $< $@.new && mv -f $@.new $@
310
311 libraries: $(LIBRARY_FILES)
312
313 templates/shapelib.html: $(TARGET_DIR)/debug/otterlib $(LIBRARY_FILES)
314         $(NAILING_CARGO_JUST_RUN) $(abspath $<) \
315         --libs '$(addprefix $(PWD)/, $(addsuffix .toml, $(LIBRARIES)))' \
316                 preview >$@.tmp && mv -f $@.tmp $@
317
318 #---------- webdriver tests (wdt) ----------
319
320 AT_TESTS := $(basename $(notdir $(wildcard apitest/at-*.rs)))
321 WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs)))
322
323 WDT_LANDSCAPE_TESTS = wdt-altergame
324
325 at:     $(foreach f, $(AT_TESTS), stamp/$f.check)
326
327 wdt:    $(foreach f, $(WDT_TESTS), stamp/$f.check) \
328         $(foreach f, $(WDT_LANDSCAPE_TESTS), stamp/$f.lcheck) \
329
330 RUNTEST_DEPS =  apitest/run1 stamp/cargo.debug $(FILEASSETS) \
331                 $(wildcard libraries/*.toml) $(LIBRARY_FILES)
332
333 AT_DEPS =       $(filter-out templates/script.js, $(RUNTEST_DEPS)) \
334                 stamp/cargo-at.debug
335
336 WDT_DEPS =      $(RUNTEST_DEPS) \
337                 stamp/cargo-wdt.debug
338
339 AT_WDT_RUN = $(NAILING_CARGO_JUST_RUN) $(abspath $<)
340
341 AT_RUN = $(AT_WDT_RUN) $(basename $(notdir $@))
342 WDT_RUN = $(AT_WDT_RUN) wdriver --test=$(basename $(notdir $@))
343
344 for-at:                 $(AT_DEPS)
345 stamp/at-%.check:       $(AT_DEPS)
346         $(AT_RUN)
347         $(stamp)
348
349 for-wdt:                $(WDT_DEPS)
350 stamp/wdt-%.check:      $(WDT_DEPS)
351         $(WDT_RUN)
352         $(stamp)
353
354 stamp/wdt-%.lcheck:     $(WDT_DEPS)
355         $(WDT_RUN) --as-if=lwdt-$* --layout=Landscape
356         $(stamp)
357
358 #---------- docs publication ----------
359
360 PUBLISH_USER=ianmdlvl@login.chiark.greenend.org.uk
361 PUBLISH_DOC_SPHINX=$(PUBLISH_USER):public-html/otter/docs
362
363 publish: doc-sphinx
364         rsync -r --delete-delay docs/html/. $(PUBLISH_DOC_SPHINX)/.
365         git branch -f $(PUBLISHED_BRANCH)
366
367 #---------- deployment ----------
368
369 DEPLOY_USER=ian@login.chiark.greenend.org.uk
370 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
371 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
372
373 for-deploy: stamp/cargo.deploy-build
374 deploy: for-deploy bundled-sources assets libraries
375         rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
376         rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
377         rsync -r README.md $(DEPLOY_BASE)/.
378         rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
379         rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
380                 $(DEPLOY_BASE)/assets/
381         rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
382         ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
383         git branch -f $(DEPLOYED_BRANCH)
384         -git push origin master
385         -git push chiark master
386
387 #$(DEPLOY_BASE)/bundled-sources
388
389 #---------- clean ----------
390
391 clean-for-retest:
392         rm -f templates/script.js library/*/*.usvg stamp/*
393         rm -rf $(LIBRARY_CLEAN)
394         find * -name '*~' -print0 | xargs -0r rm --
395
396 clean: clean-nailing clean-for-retest
397         rm -rf target
398         $(NAILING_CARGO_JUST_RUN) rm -rf target