# DVX Loader Makefile for DJGPP cross-compilation
#
# Builds the bootstrap loader (dvx.exe) that loads DXE modules.
# Links dvxPlatformDos.c directly -- the platform layer provides
# the DXE export table via platformRegisterDxeExports().

DJGPP_PREFIX  = $(HOME)/djgpp/djgpp
DJGPP_LIBPATH = $(HOME)/claude/windriver/tools/lib
CC            = $(DJGPP_PREFIX)/bin/i586-pc-msdosdjgpp-gcc
EXE2COFF      = $(DJGPP_PREFIX)/i586-pc-msdosdjgpp/bin/exe2coff
CWSDSTUB      = $(DJGPP_PREFIX)/i586-pc-msdosdjgpp/bin/CWSDSTUB.EXE
CFLAGS        = -O2 -Wall -Wextra -Wno-type-limits -Wno-sign-compare -march=i486 -mtune=i586 -I../core -I../core/platform -I../tasks -I../core/thirdparty
LDFLAGS       = -lm

OBJDIR    = ../obj/loader
POBJDIR   = ../obj/loader/platform
BINDIR    = ../bin

SRCS   = loaderMain.c
OBJS   = $(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
POBJS  = $(POBJDIR)/dvxPlatformDos.o
TARGET = $(BINDIR)/dvx.exe

.PHONY: all clean

all: $(TARGET)

$(TARGET): $(OBJS) $(POBJS) | $(BINDIR)
	$(CC) $(CFLAGS) -o $@ $(OBJS) $(POBJS) $(LDFLAGS) -Wl,-Map=$(BINDIR)/dvx.map
	$(EXE2COFF) $@
	cat $(CWSDSTUB) $(BINDIR)/dvx > $@
	rm -f $(BINDIR)/dvx

$(OBJDIR)/%.o: %.c | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(POBJDIR)/dvxPlatformDos.o: ../core/platform/dvxPlatformDos.c | $(POBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR):
	mkdir -p $(OBJDIR)

$(POBJDIR):
	mkdir -p $(POBJDIR)

$(BINDIR):
	mkdir -p $(BINDIR)

# Dependencies
$(OBJDIR)/loaderMain.o: loaderMain.c ../core/platform/dvxPlatform.h ../core/dvxTypes.h
$(POBJDIR)/dvxPlatformDos.o: ../core/platform/dvxPlatformDos.c ../core/platform/dvxPlatform.h ../core/dvxTypes.h ../core/dvxPalette.h

clean:
	rm -f $(OBJS) $(POBJS) $(TARGET) $(BINDIR)/dvx.map
