chiark / gitweb /
wip await gen
[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
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,$*)
148         $(stamp)
149
150 stamp/cargo.check: $(call rsrcs,.)
151         $(CARGO) test --workspace
152         $(stamp)
153
154 stamp/cargo.doc: $(call rsrcs,.)
155         $(CARGO) doc --workspace
156         $(stamp)
157
158 $(addprefix stamp/cargo.wasm-,$(DR)):: \
159 stamp/cargo.wasm-%: $(call rsrcs, zcoord wasm Cargo.*)
160         $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*)
161         $(stamp)
162
163 stamp/cargo.deploy-build: $(call rsrcs,.)
164         $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE))
165         $(stamp)
166
167 #---------- wasm ----------
168
169 $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-pack
170 stamp/wasm-pack: stamp/cargo.wasm-release
171         $(WASM_PACK_CMD) $(WASM_PACK_OPTIONS) build \
172                 --out-dir=../target/packed-wasm wasm -t no-modules --release
173         $(stamp)
174
175 # There is some kind of bug in wasm-pack's version of wasm-opt, which
176 # can be avoided by running --dev instead (but also then using the
177 # debug rust built).  IME this happened when trying to return a u64,
178 # so maybe bigints are affected?  (Which I don't want to use anyway.)
179 # See
180 #    https://github.com/WebAssembly/binaryen/issues/3006
181
182 #---------- bundle-sources ----------
183
184 BUNDLED_SOURCES_DIRS += otter
185
186 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
187
188 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
189
190 $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \
191 bundled-sources/%: $(wildcard $(BUNDLE_SOURCES))
192         set -e; d=$(abspath $(TARGET_BUNDLED)); \
193         $(NAILING_CARGO_JUST_RUN) mkdir -p $$d; \
194         $(if $(filter-out otter,$*), cd ../$*;) \
195         $(BUNDLE_SOURCES_CMD) --output $$d/$*
196
197 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
198
199 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
200         $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
201
202 $(TARGET_BUNDLED)/index.html: bundled-sources-make-index Makefile
203         $(NAILING_CARGO_JUST_RUN) sh -ec '                      \
204                 cd $(abspath $(src)); mkdir -p $(dir $@);       \
205                 ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS);          \
206                 mv -f $@.tmp $@;                                \
207         '
208
209 bundled-sources::
210         @echo Bundled sources.
211
212 #---------- svg processing ----------
213
214 include $(wildcard library/*/files.make)
215
216 USVG_PROCESSOR = usvg-processor
217 LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)'
218 $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_BINARY) Makefile
219
220 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
221
222 #---------- typescript ----------
223
224 templates/%.js: tsc-wrap tsconfig.json
225         ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
226
227 templates/script.js: $(TS_SRC_FILES)
228 #templates/bigfloat-tests.js: templates/bigfloat.ts \
229 #       templates/bigfloat-tests.ts templates/bigfloat-tests-auto.ts
230
231 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
232 #       ./$^ >$@.tmp && mv -f $@.tmp $@
233
234 #js-check: templates/bigfloat-tests.js
235 #       nodejs <$<
236 #       @echo 'nodejs check $< ok'
237
238 templates/otter_wasm.ns.d.ts: $(WASM_PACKED)/otter_wasm.d.ts \
239                                 stamp/wasm-pack Makefile
240         set -e; exec >$@.tmp;                           \
241         echo 'declare namespace wasm_bindgen {';        \
242         sed 's/^export default function init/export function init/' <$<; \
243         echo '}'
244         mv -v $@.tmp $@
245
246 #---------- other templates ----------
247
248 $(addprefix templates/,$(LITFILES)): templates/%: %
249         cp $< $@.new && mv -f $@.new $@
250
251 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
252         cp $< $@.new && mv -f $@.new $@
253
254 libraries: $(LIBRARY_FILES)
255
256 #---------- deployment ----------
257
258 DEPLOY_USER=ian@login.chiark.greenend.org.uk
259 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
260 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
261
262 deploy: stamp/cargo.deploy-build bundled-sources assets libraries
263         rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/
264         rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources
265         rsync -r README.md $(DEPLOY_BASE)/.
266         rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/.
267         rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \
268                 $(DEPLOY_BASE)/assets/
269         rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/
270         ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH)
271         git branch -f $(DEPLOYED_BRANCH)
272         -git push origin
273         -git push chiark
274
275 #$(DEPLOY_BASE)/bundled-sources
276
277 #---------- clean ----------
278
279 clean: clean-nailing
280         rm -f templates/script.js library/*/*.usvg stamp/*
281         rm -rf target
282         $(NAILING_CARGO_JUST_RUN) rm -rf target