# SPDX-License-Identifier: GPL-3.0-or-later
# Build with GNU Make and the VCC Plus CMOC toolchain on PATH.
CMOC ?= cmoc
WRITECOCOFILE ?= writecocofile
PERL ?= perl
VCC_CONFIGURATION ?= Debug
ifeq ($(VCC_CONFIGURATION),Release)
OPTIM ?= -O2
else
OPTIM ?= -O0 -fno-omit-frame-pointer
endif
export VCC_CMOC_COMPAT = 1
# The IDE supplies its selected library bindings. At the command line the
# same archives are available by name from the bundled toolchain.
LIBRARIES = $(if $(strip $(GUEST_LIBS)),$(GUEST_LIBS),-lsdc -lvcccompat -ldecbfile)
DISKS := EXPED/EXPLORE.DSK EXPED/EARTH.DSK EXPED/MARS.DSK
.PHONY: all build disk clean
.DELETE_ON_ERROR:
all build disk: $(DISKS)

EXPLORE.BIN: expedition.c Makefile
	"$(CMOC)" --coco --intermediate --org=2800 --stack-space=2048 $(OPTIM) -Werror -o $@ $< $(LIBRARIES)

generated/%/LANDING.TXT: assets/%.txt Makefile
	@mkdir -p $(dir $@)
	"$(PERL)" -pe 's/\r?\n/\r/' $< > $@

EXPED/EXPLORE.DSK: EXPLORE.BIN
EXPED/EARTH.DSK: generated/earth/LANDING.TXT
EXPED/MARS.DSK: generated/mars/LANDING.TXT
$(DISKS): Makefile
	@mkdir -p $(dir $@)
	"$(PERL)" -e 'print chr(255) x (35*18*256)' > $@
	"$(WRITECOCOFILE)" $@ $(filter-out Makefile,$^)

clean:
	rm -f $(DISKS) EXPLORE.BIN expedition.o expedition.s expedition.lst EXPLORE.map EXPLORE.link
	rm -f generated/earth/LANDING.TXT generated/mars/LANDING.TXT
