#!/bin/sh # Copyright 2020 Ian Jackson # SPDX-License-Identifier: AGPL-3.0-or-later # There is NO WARRANTY. # make -j8 # make -j8 release SHELL=/bin/bash src=. default: all check all: debug everything: debug doc release check bundled-sources #---------- funky macros etc. ---------- cr = $(addprefix --,$(filter-out debug,$1)) rsrcs = $(shell \ find $1 \( -name Cargo.toml -o -name Cargo.lock -o -name Cargo.lock.example -o -name \*.rs \) ) stamp=@mkdir -p stamp; touch $@ BUNDLED_SOURCES_LIT = README.md LICENCE BUNDLED_SOURCES_FILES = index.html $(BUNDLED_SOURCES_LIT) BUNDLED_SOURCES_LINKS += $(BUNDLED_SOURCES_LIT) otter/ BUNDLED_SOURCES += $(BUNDLED_SOURCES_FILES) #---------- programs and config variables ---------- CARGO ?= cargo TARGET_DIR ?= target USVG ?= usvg USVG_OPTIONS = "--sans-serif-family=DejaVu Sans" WASM_PACK ?= wasm-pack WASM_PACK_OPTIONS = --cargo-path=/bin/echo BUNDLE_SOURCES ?= bundle-rust-sources DEPLOY_ARCH=x86_64-unknown-linux-musl DEPLOY_RELEASE=debug DEPLOY_TARGET_DIR=$(TARGET_DIR)/$(addsuffix /,$(DEPLOY_ARCH))$(DEPLOY_RELEASE) DEPLOYED_BRANCH=deployed #---------- nailing-cargo ---------- ifneq (,$(wildcard ../Cargo.nail)) NAILING_CARGO = nailing-cargo CARGO = $(NAILING_CARGO) BUILD_SUBDIR ?= ../Build TARGET_DIR = $(BUILD_SUBDIR)/$(notdir $(PWD))/target NAILING_CARGO_JUST_RUN ?= $(NAILING_CARGO) --just-run -q --- BUNDLE_SOURCES_CMD ?= $(NAILING_CARGO) --- $(BUNDLE_SOURCES) USVG_CMD ?= $(NAILING_CARGO_JUST_RUN) $(USVG) WASM_PACK_CMD ?= $(NAILING_CARGO) --linkfarm=git --- $(WASM_PACK) clean-nailing: $(NAILING_CARGO_JUST_RUN) \ sh -c 'cd "$1"; find -mindepth 1 -maxdepth 1 -print0 | xargs -0r rm -rf --' \ $(abspath $(BUILD_SUBDIR)/$(notdir $(PWD))) else clean-nailing: endif # Cargo.nail BUILD_SUBDIR ?= ../Build BUNDLE_SOURCES_CMD ?= $(BUNDLE_SOURCES) USVG_CMD ?= $(USVG) WASM_PACK_CMD ?= $(WASM_PACK) WASM_PACKED=$(TARGET_DIR)/packed-wasm #---------- local programs ---------- define lp $(if $(wildcard $(BUILD_SUBDIR)/$2), $(shell echo >&2 'Makefile: lp: Using program $4 from $(BUILD_SUBDIR)/$2') $1 := $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4) $(abspath $(BUILD_SUBDIR)/$2/target/$3/$4):; cd ../$2 && $$(CARGO) build $(call cr,$3) BUNDLED_SOURCES_DIRS += $2 BUNDLED_SOURCES_LINKS += $2/ ) endef $(eval $(call lp,BUNDLE_SOURCES,bundle-sources,debug,bundle-rust-sources)) $(eval $(call lp,USVG,resvg,release,usvg)) $(eval $(call lp,WASM_PACK,wasm-pack,debug,wasm-pack)) #---------- variables defining bits of source etc. ---------- PROGRAMS=daemon-otter otter WASM_ASSETS := $(addprefix otter_wasm,.js _bg.wasm) WASM_OUTPUTS := $(addprefix otter_wasm,.d.ts) TS_SRCS= script TS_SRC_FILES= \ $(addprefix templates/,$(addsuffix .ts,$(TS_SRCS))) \ webassembly-types/webassembly.d.ts \ templates/otter_wasm.ns.d.ts LITFILES= LICENCE AGPLv3 TXTFILES= CC-BY-SA-3.0 CC-BY-SA-4.0 FILEASSETS = $(addprefix templates/, libre script.js \ $(LITFILES) $(TXTFILES)) \ $(wildcard templates/*.tera) WASM := wasm32-unknown-unknown #---------- toplevel aggregate targets ---------- check: stamp/cargo.check wdt @echo 'All tests passed.' doc: cargo-doc debug release:: %: stamp/cargo.% assets libraries extra-% cargo: cargo-debug cargo-wasm-release cargo-debug cargo-release cargo-check cargo-doc \ cargo-wasm-debug cargo-wasm-release:: \ cargo-%: stamp/cargo.% cargo-wasm: cargo-wasm-release wasm-pack: stamp/wasm-pack assets: js stamp/wasm-pack $(FILEASSETS) js: templates/script.js extra-debug: extra-release: bundled-sources #---------- cargo ---------- DR=debug release CARGOES=$(foreach t, wasm-,$(addprefix $t,check $(DR))) $(addprefix stamp/cargo.,$(DR)):: \ stamp/cargo.%: $(call rsrcs,. ! -path './wasm/*') $(CARGO) build $(call cr,$*) -p otter -p otter-daemon $(stamp) stamp/cargo.check: $(call rsrcs,.) $(CARGO) test --workspace $(stamp) stamp/cargo-wdt.debug: $(call rsrcs,.) $(CARGO) build $(call cr,$*) -p otter-webdriver-tests $(stamp) stamp/cargo.doc: $(call rsrcs,.) $(CARGO) doc --workspace $(stamp) $(addprefix stamp/cargo.wasm-,$(DR)):: \ stamp/cargo.wasm-%: $(call rsrcs, zcoord wasm Cargo.*) $(CARGO) build --target $(WASM) -p otter-wasm $(call cr,$*) $(stamp) stamp/cargo.deploy-build: $(call rsrcs,.) $(CARGO) -T$(DEPLOY_ARCH) build $(call cr,$(DEPLOY_RELEASE)) -p otter -p otter-daemon $(stamp) #---------- wasm ---------- $(addprefix $(WASM_PACKED)/,$(WASM_ASSETS) $(WASM_OUTPUTS)): stamp/wasm-pack stamp/wasm-pack: stamp/cargo.wasm-release $(WASM_PACK_CMD) $(WASM_PACK_OPTIONS) build \ --out-dir=../target/packed-wasm wasm -t no-modules --release $(stamp) # There is some kind of bug in wasm-pack's version of wasm-opt, which # can be avoided by running --dev instead (but also then using the # debug rust built). IME this happened when trying to return a u64, # so maybe bigints are affected? (Which I don't want to use anyway.) # See # https://github.com/WebAssembly/binaryen/issues/3006 #---------- bundle-sources ---------- BUNDLED_SOURCES_DIRS += otter bundled-sources:: $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)) TARGET_BUNDLED=$(TARGET_DIR)/bundled-sources $(addprefix bundled-sources/, $(BUNDLED_SOURCES_DIRS)): \ bundled-sources/%: $(wildcard $(BUNDLE_SOURCES)) set -e; d=$(abspath $(TARGET_BUNDLED)); \ $(NAILING_CARGO_JUST_RUN) mkdir -p $$d; \ $(if $(filter-out otter,$*), cd ../$*;) \ $(BUNDLE_SOURCES_CMD) --output $$d/$* bundled-sources:: $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_FILES)) $(addprefix $(TARGET_BUNDLED)/, $(BUNDLED_SOURCES_LIT)): $(TARGET_BUNDLED)/%: % $(NAILING_CARGO_JUST_RUN) cp $(abspath $(src))/$< $(abspath $@) $(TARGET_BUNDLED)/index.html: bundled-sources-make-index Makefile $(NAILING_CARGO_JUST_RUN) sh -ec ' \ cd $(abspath $(src)); mkdir -p $(dir $@); \ ./$< >$@.tmp $(BUNDLED_SOURCES_LINKS); \ mv -f $@.tmp $@; \ ' bundled-sources:: @echo Bundled sources. #---------- svg processing ---------- include $(wildcard library/*/files.make) USVG_PROCESSOR = usvg-processor LIBRARY_PROCESS_SVG = ./$(USVG_PROCESSOR) $@ $(wordlist 1,2,$^) '$(USVG_CMD) $(USVG_OPTIONS)' $(LIBRARY_FILES): $(USVG_PROCESSOR) $(USVG_BINARY) Makefile # actual command for each of $(LIBRARY_FILES) is in one of the files.make #---------- typescript ---------- templates/%.js: tsc-wrap tsconfig.json ./tsc-wrap $@ tsconfig.json $(filter %.ts,$^) templates/script.js: $(TS_SRC_FILES) #templates/bigfloat-tests.js: templates/bigfloat.ts \ # templates/bigfloat-tests.ts templates/bigfloat-tests-auto.ts #templates/bigfloat-tests-auto.ts: extract-bf-tests src/bigfloat.rs # ./$^ >$@.tmp && mv -f $@.tmp $@ #js-check: templates/bigfloat-tests.js # nodejs <$< # @echo 'nodejs check $< ok' templates/otter_wasm.ns.d.ts: $(WASM_PACKED)/otter_wasm.d.ts \ stamp/wasm-pack Makefile set -e; exec >$@.tmp; \ echo 'declare namespace wasm_bindgen {'; \ sed 's/^export default function init/export function init/' <$<; \ echo '}' mv -v $@.tmp $@ #---------- other templates ---------- $(addprefix templates/,$(LITFILES)): templates/%: % cp $< $@.new && mv -f $@.new $@ $(addprefix templates/,$(TXTFILES)): templates/%: %.txt cp $< $@.new && mv -f $@.new $@ libraries: $(LIBRARY_FILES) #---------- webdriver tests (wdt) ---------- WDT_TESTS := $(basename $(notdir $(wildcard wdriver/wdt-*.rs))) wdt: $(foreach f, $(WDT_TESTS), stamp/$f.check) stamp/wdt-%.check: wdriver/run1 stamp/cargo.debug stamp/cargo-wdt.debug \ $(FILEASSETS) templates/script.js $(LIBRARY_FILES) $(NAILING_CARGO_JUST_RUN) $(abspath $<) $(basename $(notdir $@)) $(stamp) #---------- deployment ---------- DEPLOY_USER=ian@login.chiark.greenend.org.uk DEPLOY_BASE=$(DEPLOY_USER):/volatile/Otter DEPLOY_FINISH=/home/Otter/etc/deploy-finish deploy: stamp/cargo.deploy-build bundled-sources assets libraries rsync -zv --progress $(addprefix $(DEPLOY_TARGET_DIR)/,$(PROGRAMS)) $(DEPLOY_BASE)/bin/ rsync -rv --progress $(TARGET_DIR)/bundled-sources/. $(DEPLOY_BASE)/bundled-sources rsync -r README.md $(DEPLOY_BASE)/. rsync -r --delete --exclude=\*~ library specs $(DEPLOY_BASE)/. rsync -r $(FILEASSETS) $(addprefix $(WASM_PACKED)/, $(WASM_ASSETS)) \ $(DEPLOY_BASE)/assets/ rsync -r nwtemplates/*.tera $(DEPLOY_BASE)/nwtemplates/ ssh -o BatchMode=true $(DEPLOY_USER) $(DEPLOY_FINISH) git branch -f $(DEPLOYED_BRANCH) -git push origin -git push chiark #$(DEPLOY_BASE)/bundled-sources #---------- clean ---------- clean: clean-nailing rm -f templates/script.js library/*/*.usvg stamp/* rm -rf target $(NAILING_CARGO_JUST_RUN) rm -rf target