#Makefile for card-based application - development version
#Bryan E. Topp <betopp@betopp.com> 2024

CC     =cc
OBJCOPY=objcopy
DATE   =date
FIND   =find
MKDIR  =mkdir
RM     =rm

#Directories used locally
SRCDIR=./src
OBJDIR=./obj.dev
BINDIR=./bin.dev
IMGDIR=./img.dev
CARDROOT=./cardroot

#C source files and resulting objects
CSRC:=$(shell $(FIND) -L $(SRCDIR) -name "*.c")
COBJ:=$(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(CSRC))

#For development build - phony system call library
CSRC+= $(SRCDIR)/sdlsc.c.dev
COBJ+= $(OBJDIR)/sdlsc.o

#Flags to use when compiling
CFLAGS += -std=c99 -pedantic
CFLAGS += -Wall -Werror -Wextra
CFLAGS += -O2 -g
CFLAGS += -fstack-usage

#Phony system call library
CFLAGS += -I../../sdlsc/include
CFLAGS += -DUSE_SDLSC=1
CFLAGS += $(shell sdl2-config --cflags)

#Flags to use when linking
LINKFLAGS += 
LIBS += -lm $(shell sdl2-config --libs)

#Build information provided to compiler
BUILDDATE := $(shell $(DATE) -u +%Y-%m-%dT%H:%M:%SZ)
BUILDUSER := $(shell whoami)@$(shell hostname)
CFLAGS += -DBUILDDATE=\"$(BUILDDATE)\"
CFLAGS += -DBUILDUSER=\"$(BUILDUSER)\"

#Build directory with the game contents
IMGFILES_SRC=$(shell $(FIND) -H $(CARDROOT) -type f)
IMGFILES_DST=$(patsubst $(CARDROOT)%, $(IMGDIR)%, $(IMGFILES_SRC))
IMGFILES_DST+=$(IMGDIR)/boot.nne $(IMGDIR)/boot.elf

$(IMGDIR) : $(IMGFILES_DST)
	
#Image comes from "bin.dev" for executables, cardroot otherwise
$(IMGDIR)/%.elf : $(BINDIR)/%.elf
	$(MKDIR) -p $(@D)
	$(CP) $^ $@

$(IMGDIR)/%.nne : $(BINDIR)/%.nne
	$(MKDIR) -p $(@D)
	$(CP) $^ $@

$(IMGDIR)/% : $(CARDROOT)/%
	$(MKDIR) -p $(@D)
	$(CP) $^ $@

#Link post-process target - no-nonsense executable made by stripping the ELF
$(BINDIR)/boot.nne : $(BINDIR)/boot.elf
	$(MKDIR) -p $(@D)
	$(OBJCOPY) -O binary $< $@

#Link stage target - ELF made from linking all objects
$(BINDIR)/boot.elf : $(COBJ)
	$(MKDIR) -p $(@D)
	$(CC) $(LINKFLAGS) $^ -o $@ $(LIBS)

#Compile targets - object for each C file
$(OBJDIR)/%.o : $(SRCDIR)/%.c
	$(MKDIR) -p $(@D)
	$(CC) $(CFLAGS) -c $< -o $@
	
#Dev-only targets - object for each C file
$(OBJDIR)/%.o : $(SRCDIR)/%.c.dev
	$(MKDIR) -p $(@D)
	$(CC) $(CFLAGS) -c $< -o $@
	
#Cleanup target
clean:
	$(RM) -rf $(BINDIR) $(OBJDIR) $(IMGDIR)
