# C -> WASM
WASI_ROOT     := /opt/wasi-sdk
WASI_CC       := ${WASI_ROOT}/bin/clang
WASI_CFLAGS   := --target=wasm32 \
				 --sysroot=${WASI_ROOT}/share/wasi-sysroot \
				 -z stack-size=4096 \
				 -nostdlib \
				 -O0 -g -Wall \
				 -Wl,--no-entry \
				 -Wl,--initial-memory=65536 \
				 -Wl,--export-all \
				 -Wl,--export=__heap_base \
				 -Wl,--export=__data_end

# WASM -> Baremetal
WAMR          := /opt/wamr
IWASM_LIB     := /opt/wamr-libiwasm
CC            := gcc
CFLAGS        := -I. -include arch/${ARCH}/lib.c -O0 -g -m32 -fomit-frame-pointer
LDFLAGS       := -Wl,-T linker.ld $^ -Wl,--build-id=none -static -nostdlib -m32 \
				 -Wl,-rpath,${IWASM_LIB} -L${IWASM_LIB} -liwasm
INCL          := -I${WAMR}/core/iwasm/include \
				 -I${WAMR}/core/iwasm/common \
				 -I${WAMR}/core/shared/platform/include \
				 -I${WAMR}/core/shared/platform/linux \
				 -I${WAMR}/core/shared/mem-alloc \
				 -I${WAMR}/core/shared/utils \
				 -I${WAMR}/core/shared/utils/uncommon
WAMRC         := /opt/wamr-wamrc/wamrc
WAMRCFLAGS    := --target=i386 --format=object
XXD           := busybox xxd
WASM2C        := wasm2c


################################################################
# C -> WASM
${BUILD_DIR}/%/module.wasm: %.c
	mkdir -p $(shell dirname $@)
	${WASI_CC} ${WASI_CFLAGS} $< -o $@


################################################################
# WASM -> Native Object File
# ${BUILD_DIR}/%/system.o: ${BUILD_DIR}/%/module.wasm
# 	${WAMRC} ${WAMRCFLAGS} -o ${BUILD_DIR}/$*/system.o ${BUILD_DIR}/$*/module.wasm
#
# ${BUILD_DIR}/startup.o: arch/bochs/startup.s
# 	${CC} $< ${CFLAGS} -c -ffunction-sections -o $@
#
# ${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
# 	${CC} ${LDFLAGS} -o $@


################################################################
# WASM -> AOT -> Loaded by Runtime
${BUILD_DIR}/%/module.aot: ${BUILD_DIR}/%/module.wasm
	${WAMRC} -o $@ $<

${BUILD_DIR}/%/module_wasm.c: ${BUILD_DIR}/%/module.aot
	${XXD} -i $< > $@

${BUILD_DIR}/%/system.o: ${BUILD_DIR}/%/module_wasm.c
	cp embed/host.c ${BUILD_DIR}/$*/module_host.c
	sed -i \
	    -e "s/__WASM_ARRAY_FILE__/module_wasm.c/g" \
	    -e "s/__WASM_ARRAY__/build_bochs_$*_module_aot/g" \
	    -e "s/__WASM_ARRAY_LEN__/build_bochs_$*_module_aot_len/g" \
        ${BUILD_DIR}/$*/module_host.c
	${CC} ${CFLAGS} ${INCL} -c -ffunction-sections ${BUILD_DIR}/$*/module_host.c -o $@

${BUILD_DIR}/startup.o: arch/bochs/startup.s
	${CC} $< ${CFLAGS} -c -ffunction-sections -o $@

${BUILD_DIR}/%/system.elf: ${BUILD_DIR}/%/system.o ${BUILD_DIR}/startup.o
	${CC} ${LDFLAGS} -o $@

################################################################
# Bochs
${BUILD_DIR}/%/system.iso: ${BUILD_DIR}/%/system.elf
	rm -rf $(shell dirname $<)/grub
	mkdir -p $(shell dirname $<)/grub/boot/grub
	cp arch/bochs/grub.cfg $(shell dirname $<)/grub/boot/grub
	cp $< $(shell dirname $<)/grub/boot/system.elf
	grub-mkrescue -o $@ $(shell dirname $<)/grub


BOCHS_RUNNER_ARGS = \
	-V arch/bochs/vgabios.bin                      \
	-b arch/bochs/BIOS-bochs-latest                \

${BUILD_DIR}/%/trace.pb: ${BUILD_DIR}/%/system.iso
	${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS} -1        \
	-f ${FAIL_TRACE}                               \
	-e $(shell dirname $<)/system.elf              \
	-i $(shell dirname $<)/system.iso              \
	-- \
	-Wf,--state-file=$(shell dirname $<)/state     \
	-Wf,--trace-file=$(shell dirname $<)/trace.pb  \
	-Wf,--start-symbol=start_trace                 \
	-Wf,--end-symbol=stop_trace                    \
	-Wf,--check-bounds


client-%:
	${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS}            \
	-f ${FAIL_INJECT}                               \
	-e ${BUILD_DIR}/$(subst client-,,$@)/system.elf \
	-i ${BUILD_DIR}/$(subst client-,,$@)/system.iso \
	-j $(shell getconf _NPROCESSORS_ONLN) \
	-- \
	-Wf,--state-dir=${BUILD_DIR}/$(subst client-,,$@)/state \
	-Wf,--trap                    \
	-Wf,--timeout=10              \
	-Wf,--ok-marker=ok_marker     \
	-Wf,--fail-marker=fail_marker \
	-Wf,--catch-write-textsegment \
	-Wf,--catch-outerspace        \
	2>/dev/null | grep -B 2 -A 8 'INJECT'

inject-%:
	${BOCHS_RUNNER} ${BOCHS_RUNNER_ARGS} -1         \
	-f ${FAIL_INJECT}                               \
	-e ${BUILD_DIR}/$(subst inject-,,$@)/system.elf \
	-i ${BUILD_DIR}/$(subst inject-,,$@)/system.iso \
	-j 1 -- \
	-Wf,--state-dir=${BUILD_DIR}/$(subst inject-,,$@)/state \
	-Wf,--trap                    \
	-Wf,--timeout=10              \
	-Wf,--ok-marker=ok_marker     \
	-Wf,--fail-marker=fail_marker \
	-Wf,--catch-write-textsegment \
	-Wf,--catch-outerspace

# TODO: Command line interface changed
import-arch-%: ${BUILD_DIR}/%/trace.pb ${HOME}/.my.cnf
	# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b mem  -t $< -e $(shell dirname $<)/system.elf -i mem --memory-type ram
	# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs-trace  -t $< -e $(shell dirname $<)/system.elf -i mem --memory-type register
	# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs -t $< -e $(shell dirname $<)/system.elf -i regs
	# ${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b ip   -t $< -e $(shell dirname $<)/system.elf -i regs --no-gp --ip
	# ${FAIL_PRUNE}  -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b %% --overwrite
	${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b mem  -t $< -e $(shell dirname $<)/system.elf -i MemoryImporter
	${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs-trace  -t $< -e $(shell dirname $<)/system.elf -i MemoryImporter
	${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b regs -t $< -e $(shell dirname $<)/system.elf -i RegisterImporter
	${FAIL_IMPORT} -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b ip   -t $< -e $(shell dirname $<)/system.elf -i RegisterImporter --no-gp --ip
	${FAIL_PRUNE}  -v ${ARCH}/$(patsubst import-arch-%,%,$@) -b %% --overwrite


define arch-make-targets

build-$1: ${BUILD_DIR}/$1/system.iso

trace-$1: ${BUILD_DIR}/$1/trace.pb

endef
