2 # Copyright 2020 Ian Jackson
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 # There is NO WARRANTY.
14 everything: debug doc release check bundled-sources
16 #---------- funky macros etc. ----------
18 cr = $(addprefix --,$(filter-out debug,$1))
20 find $1 \( -name Cargo.toml -o -name Cargo.lock -o -name Cargo.lock.example -o -name \*.rs \) )
21 stamp=@mkdir -p stamp; touch $@
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)
28 #---------- programs and config variables ----------
34 USVG_OPTIONS = "--sans-serif-family=DejaVu Sans"
36 WASM_PACK ?= wasm-pack
37 WASM_PACK_OPTIONS = --cargo-path=/bin/echo
39 BUNDLE_SOURCES ?= bundle-rust-sources
41 DEPLOY_ARCH=x86_64-unknown-linux-musl
43 DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE)
44 DEPLOYED_BRANCH=deployed
46 #---------- nailing-cargo ----------
48 ifneq (,$(wildcard ../Cargo.nail))
50 NAILING_CARGO = nailing-cargo
51 CARGO = $(NAILING_CARGO)
52 BUILD_SUBDIR ?= ../Build
53 TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target
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)
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)))
69 BUILD_SUBDIR ?= ../Build
70 BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES)
72 WASM_PACK_CMD ?= $(WASM_PACK)
74 WASM_PACKED=$(TARGET_DIR)/packed-wasm
76 #---------- local programs ----------
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/
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))
92 #---------- variables defining bits of source etc. ----------
94 PROGRAMS=daemon-otter otter
96 WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm)
97 WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts)
101 $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \
102 webassembly-types/webassembly.d.ts \
103 templates/otter_wasm.ns.d.ts
105 LITFILES= LICENCE AGPLv3
106 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0
108 FILEASSETS = $(addprefix templates/, libre script.js \
109 $(LITFILES) $(TXTFILES)) \
110 $(wildcard templates/*.tera)
112 WASM := wasm32-unknown-unknown
114 #---------- toplevel aggregate targets ----------
116 check: stamp/cargo.check
117 @echo 'All tests passed.'
121 debug release:: %: stamp/cargo.% assets libraries extra-%
123 cargo: cargo-debug cargo-wasm-release
125 cargo-debug cargo-release cargo-check cargo-doc \
126 cargo-wasm-debug cargo-wasm-release:: \
127 cargo-%: stamp/cargo.%
129 cargo-wasm: cargo-wasm-release
131 wasm-pack: stamp/wasm-pack
133 assets: js stamp/wasm-pack $(FILEASSETS)
135 js: templates/script.js
138 extra-release: bundled-sources
140 #---------- cargo ----------
143 CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR)))
145 $(addprefix stamp/cargo.,$(DR)):: \
146 stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*')
147 $(CARGO) build $(call cr,$*)
150 stamp/cargo.check: $(call rsrcs,.)
151 $(CARGO) test --workspace
154 stamp/cargo.doc: $(call rsrcs,.)
155 $(CARGO) doc --workspace
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,$*)
163 stamp/cargo.deploy-build: $(call rsrcs,.)
164 $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE))
167 #---------- wasm ----------
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
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.)
180 # https://github.com/WebAssembly/binaryen/issues/3006
182 #---------- bundle-sources ----------
184 BUNDLED_SOURCES_DIRS += otter
186 bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS))
188 TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources
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/$*
197 bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES))
199 $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: %
200 $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@)
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); \
210 @echo Bundled sources.
212 #---------- svg processing ----------
214 include $(wildcard library/*/files.make)
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
220 # actual command for each of $(LIBRARY_FILES) is in one of the files.make
222 #---------- typescript ----------
224 templates/%.js: tsc-wrap tsconfig.json
225 ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^)
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
231 #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs
232 # ./$^ >$@.tmp && mv -f $@.tmp $@
234 #js-check: templates/bigfloat-tests.js
236 # @echo 'nodejs check $< ok'
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/' <$<; \
246 #---------- other templates ----------
248 $(addprefix templates/,$(LITFILES)): templates/%: %
249 cp $< $@.new && mv -f $@.new $@
251 $(addprefix templates/,$(TXTFILES)): templates/%: %.txt
252 cp $< $@.new && mv -f $@.new $@
254 libraries: $(LIBRARY_FILES)
256 #---------- deployment ----------
258 DEPLOY_USER=ian@login.chiark.greenend.org.uk
259 DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter
260 DEPLOY_FINISH=/home/Otter/etc/deploy-finish
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)
275 #$(DEPLOY_BASE)/bundled-sources
277 #---------- clean ----------
280 rm -f templates/script.js library/*/*.usvg stamp/*
282 $(NAILING_CARGO_JUST_RUN) rm -rf target