MOXIE    ?= $(CURDIR)/../moxie/moxie
MOXIEJS  ?= $(CURDIR)/../moxie/moxiejs
# An inherited MOXIEROOT is ignored on purpose: `?=` cannot tell an exported
# stale root from an intended one, and a stale root silently compiles a
# different tree's src/ and pkg/ (that is how `make build-app-wasm` failed
# with "'err' shadows declaration in enclosing scope" from an old checkout,
# while the same build with the repo root succeeds). Pass it on the command
# line to build against another tree: make MOXIEROOT=/path/to/moxie ...
ifeq ($(origin MOXIEROOT),environment)
MOXIEROOT := $(CURDIR)/../moxie
endif
MOXIEROOT ?= $(CURDIR)/../moxie
JSRUNTIME ?= $(CURDIR)/../moxie/jsruntime
ISKRA    ?= $(HOME)/.local/bin/moxie-iskra
# relay-proxy builds from source with the tree compiler. The earlier workaround
# of using an installed moxie (its cached bitcode "avoided too much recursion
# during _start()") was a symptom of a compiler bug: taking the address of a
# frame local stored a stack pointer into the receiver's arena. Fixed in
# _mxc_stage4 (markAddrEscaping mirrors the legacy compiler's Alloc.Heap), and
# a fresh build now produces the same 430KB module the installed compiler did.
MOXIE_RELAY_PROXY ?= $(MOXIE)

# The relay is its OWN repo. This tree is the front end: nothing under web/
# imports a relay package, and the relay's source lives in RELAY (../morly by
# default). The suite still drives a relay binary - the app has to be served
# from somewhere - so `relay-bin` builds it from that repo and the fixtures
# take it through SMESH_RELAY_BIN.
RELAY     ?= $(CURDIR)/../morly
RELAY_BIN ?= $(CURDIR)/tmp/morly

# Moxie closures do not capture: a closure receives what it needs through its
# parameters, and the legacy compiler rejects a capturing one. stage4 enforces
# the same rule by default. This app predates that enforcement and registers
# hundreds of handlers that close over the enclosing frame (286 sites in
# web/wasm/app alone), so every build here opts out explicitly. Porting them to
# explicit parameters or to holder types with the callback as an interface is
# tracked with the app, not the compiler; until it lands, the debt is declared
# here rather than silently relying on stage4's old acceptance.
CAPTURES ?= -allow-closure-captures

# === Relay (a different repo) ===
# Built only so the app has something to be served from; there is no relay
# source here to build.
relay-bin:
	@mkdir -p $(dir $(RELAY_BIN))
	cd $(RELAY) && MOXIEROOT=$(MOXIEROOT) $(MOXIE) build -o $(RELAY_BIN) .

# === Frontend (WASM) ===
build-app-wasm:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/app.wasm ./web/wasm/app/

build-relay-proxy-wasm:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE_RELAY_PROXY) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/relay-proxy.wasm ./web/wasm/relay-proxy/

build-store-wasm:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/store.wasm ./web/wasm/store/

build-verify-wasm:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/verify.wasm ./web/wasm/verify/

build-profile-wasm:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/profile.wasm ./web/wasm/profile/

build-feed-wasm:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/feed.wasm ./web/wasm/feed/

build-mls-wasm:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/mls.wasm ./web/wasm/mls/

build-notif-wasm:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/notif.wasm ./web/wasm/notif/

build-sw:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/\$$sw/sw.wasm ./web/wasm/sw/

# === Signer WASM (page-embedded worker) ===
# The web app uses signer.wasm directly via signer-wasm-host.mjs.
# No browser extension is required for normal operation.
build-signer:
	MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -o $(CURDIR)/web/static/signer.wasm ./web/wasm/signer/

# Extension build (optional, not part of default build).
build-ext-firefox: build-signer
	cp $(CURDIR)/web/static/signer.wasm $(CURDIR)/web/ext/bg/signer.wasm
	bash scripts/pack-ext.sh firefox
	cp dist/smesh-signer-firefox.xpi web/static/smesh-signer.xpi

build-ext: build-ext-firefox

# === All ===
# Direct messaging (MLS/Marmot) is parked again: the wasm worker panics in
# Moxie's codec while generating its first KeyPackage (an invalid type
# descriptor, kind=0), so its worker is not built or served and the app has no
# /msg route. The mls code, host plumbing and worker all stay in the tree;
# re-enable by restoring the sidebar entry, the router cases, the mlsWorkerTypes
# branch and build-mls-wasm in `build`/`dev`.
build: build-app-wasm build-relay-proxy-wasm build-store-wasm build-verify-wasm build-profile-wasm build-feed-wasm build-notif-wasm build-signer build-sw

# === Test ===
# Unit tests: every package under web/ carrying a *_test.mx, run by the
# compiler's own test harness. The relay's own unit tests (and its coverage
# run) live with the relay, in $(RELAY).
test-unit:
	@set -e ; for f in $$( find web -name '*_test.mx' | sort ) ; do \
		d=$$(dirname $$f) ; \
		echo "=== unit: $$d" ; \
		MOXIEROOT=$(MOXIEROOT) $(MOXIE) test $(CAPTURES) ./$$d || exit 1 ; \
	done

test-signer: build-signer
	node web/_test/signertest/run.mjs web/ext/bg/signer.wasm

test: test-unit relay-bin test-signer
	SMESH_RELAY_BIN=$(RELAY_BIN) ORLY_STATIC_DIR=$(CURDIR)/web/static \
	 python3 -m pytest test/ -v --tb=short -x

test-smoke: relay-bin
	SMESH_RELAY_BIN=$(RELAY_BIN) ORLY_STATIC_DIR=$(CURDIR)/web/static \
	 python3 -m pytest test/test_smoke.py -v --tb=short

# Allocation instrumentation: the app wasm is rebuilt with -tags alloc_trace
# and -print-allocs, which makes src/runtime publish the __moxie_* counters the
# worker host reads in its __mem_query reply. The pattern is anchored to the
# entry package: the compiler names a function by its package path, and the app
# is the root package of its own module, so it is "main.<func>" - the same name
# the legacy compiler reports.
#
# The instrumented module is scratch: the stock app.wasm is put back whether
# the build or the tests fail, so this never leaves an instrumented artifact or
# a stray side file in the tree. An instrumented build recompiles everything
# serially (site ids are numbered in emit order), so it is slower than
# build-app-wasm.
APP_WASM := $(CURDIR)/web/static/app.wasm
test-memory: relay-bin
	@if [ -f "$(APP_WASM)" ]; then cp "$(APP_WASM)" "$(APP_WASM).stock"; fi ; \
	 MOXIEROOT=$(MOXIEROOT) GOWORK=off $(MOXIE) build $(CAPTURES) -target wasm -tags alloc_trace \
		-print-allocs '^main\.' -o $(APP_WASM) ./web/wasm/app/ ; \
	 rc=$$? ; \
	 rm -f "$(APP_WASM).alloc_sites.json" ; \
	 if [ $$rc -eq 0 ]; then \
		SMESH_RELAY_BIN=$(RELAY_BIN) ORLY_STATIC_DIR=$(CURDIR)/web/static \
		 python3 -m pytest test/test_memory_profile.py -v --tb=short -m memory ; rc=$$? ; \
	 fi ; \
	 if [ -f "$(APP_WASM).stock" ]; then mv "$(APP_WASM).stock" "$(APP_WASM)"; fi ; \
	 exit $$rc

# === Dev (frontend only, relay already running) ===
dev: build-app-wasm build-relay-proxy-wasm build-store-wasm build-verify-wasm build-profile-wasm build-feed-wasm build-notif-wasm build-sw

.PHONY: relay-bin build-app-wasm build-relay-proxy-wasm build-store-wasm build-verify-wasm build-profile-wasm build-feed-wasm build-mls-wasm build-notif-wasm build-sw build-signer build-ext-firefox build-ext build dev test-signer test-unit test test-smoke test-memory
