.PHONY: help
help:
	@echo
	@echo "Virtual Boy Emulator - October 10, 2024"
	@echo
	@echo "Target build environment is any Debian with the following packages:"
	@echo "  emscripten"
	@echo "  gcc"
	@echo "  openjdk-17-jdk"
	@echo 
	@echo "Available make targets:"
	@echo "  build   Perform all build commands"
	@echo "  bundle  Package the repository for HTML distribution"
	@echo "  clean   Remove output files"
	@echo "  core    Check the C core source for compiler warnings"
	@echo "  wasm    Build the WebAssembly core module"
	@echo

.PHONY: build
build:
	@echo "  Check C core library for style issues"
	@make -s core
	@echo "  Build WebAssembly core module"
	@make -s wasm
	@echo "  Bundle directory tree into HTML file"
	@make -s bundle

.PHONY: bundle
bundle:
	@java web/Bundle.java vbemu

.PHONY: clean
clean:
	@rm -f vbemu_*.html web/core/core.wasm

.PHONY: core
core:
#	GCC generic
	@gcc core/vb.c -I core -c -o /dev/null \
        -Werror -std=c90 -Wall -Wextra -Wpedantic
#	GCC compilation control
	@gcc core/vb.c -I core -c -o /dev/null \
        -Werror -std=c90 -Wall -Wextra -Wpedantic \
        -D VB_LITTLE_ENDIAN -D VB_SIGNED_PROPAGATE -D VB_DIV_GENERIC
#	Clang generic
	@emcc core/vb.c -I core -c -o /dev/null \
        -Werror -std=c90 -Wall -Wextra -Wpedantic
#	Clang compilation control
	@emcc core/vb.c -I core -c -o /dev/null \
        -Werror -std=c90 -Wall -Wextra -Wpedantic \
        -D VB_LITTLE_ENDIAN -D VB_SIGNED_PROPAGATE -D VB_DIV_GENERIC

.PHONY: wasm
wasm:
	@emcc -o web/core/core.wasm web/core/wasm.c core/vb.c -Icore \
        -D VB_LITTLE_ENDIAN -D VB_SIGNED_PROPAGATE \
        -D "VBAPI=__attribute__((used))" \
        --no-entry -O2 -flto -s WASM=1 \
        -s EXPORTED_RUNTIME_METHODS=[] -s ALLOW_MEMORY_GROWTH \
        -s MAXIMUM_MEMORY=4GB -fno-strict-aliasing
	@rm -f web/core/*.wasm.tmp*