#!/usr/bin/make -f

%:
	dh $@ --builddirectory=_build --buildsystem=golang

execute_before_dh_auto_test:
	cd _build/src/github.com/ncruces/wasm2go && \
	clang --target=wasm32-wasip1 -g -O0 \
		-ffreestanding -nostdlib \
		testdata/dwarfline/dwarfline.c \
		-o testdata/dwarfline/dwarfline.wasm \
		-ffile-prefix-map="$(pwd)/"= \
		-fdebug-compilation-dir=. \
		-Wl,--no-entry \
		-Wl,--import-undefined \
		-Wl,--export=simple_call \
		-Wl,--export=expr_call \
		-Wl,--export=chain_call \
		-Wl,--export=branch_call \
		-Wl,--export=loop_call \
		-Wl,--export=multi_call
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/fib/fib.wat -o testdata/fib/fib.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/loops/loops.wat -o testdata/loops/loops.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/memory/memory.wat -o testdata/memory/memory.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/primes/primes.wat -o testdata/primes/primes.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/recursion/recursion.wat -o testdata/recursion/recursion.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/stack/stack.wat -o testdata/stack/stack.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/table/table.wat -o testdata/table/table.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/trig/trig.wat -o testdata/trig/trig.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/regression/bulk_grow/bulk_grow.wat -o testdata/regression/bulk_grow/bulk_grow.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/regression/oob_trap/oob_trap.wat -o testdata/regression/oob_trap/oob_trap.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/regression/provided_helper/provided_helper.wat -o testdata/regression/provided_helper/provided_helper.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/regression/select_effect/select_effect.wat -o testdata/regression/select_effect/select_effect.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm --enable-threads testdata/regression/shared_export/shared_export.wat -o testdata/regression/shared_export/shared_export.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/regression/store_grow/store_grow.wat -o testdata/regression/store_grow/store_grow.wasm
	cd _build/src/github.com/ncruces/wasm2go && \
	wat2wasm testdata/regression/tee_self_loop/tee_self_loop.wat -o testdata/regression/tee_self_loop/tee_self_loop.wasm

# Upstream tool named libc-gen → wasm2go-libc-gen (avoid generic /usr/bin).
# chmod *.wasm only when the Arch:all -dev package is being built
# (skipped on binary-arch / test-build-any).
execute_after_dh_install:
	if [ -e debian/wasm2go/usr/bin/libc-gen ]; then \
		mv debian/wasm2go/usr/bin/libc-gen debian/wasm2go/usr/bin/wasm2go-libc-gen; \
	fi
	if [ -d debian/golang-github-ncruces-wasm2go-dev ]; then \
		find debian/golang-github-ncruces-wasm2go-dev -name '*.wasm' -exec chmod a-x {} +; \
	fi
