# 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 -Werror -Wno-type-limits -Wno-sign-compare -Wno-format-truncation -march=i486 -mtune=i586 -I../libs/kpunch/libdvx -I../libs/kpunch/libdvx/platform -I../libs/kpunch/libtasks -I../libs/kpunch/libdvx/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 $(POBJDIR)/dvxPlatformUtil.o $(POBJDIR)/dvxPrefs.o $(OBJDIR)/dvxhlpc.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: ../libs/kpunch/libdvx/platform/dvxPlatformDos.c | $(POBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(POBJDIR)/dvxPlatformUtil.o: ../libs/kpunch/libdvx/platform/dvxPlatformUtil.c | $(POBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(POBJDIR)/dvxPrefs.o: ../libs/kpunch/libdvx/dvxPrefs.c | $(POBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR)/dvxhlpc.o: ../tools/dvxhlpc.c ../tools/hlpcCompile.h ../apps/kpunch/dvxhelp/hlpformat.h | $(OBJDIR)
	$(CC) $(CFLAGS) -DHLPC_NO_MAIN -c -o $@ ../tools/dvxhlpc.c

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

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

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

# Dependencies
$(OBJDIR)/loaderMain.o: loaderMain.c ../libs/kpunch/libdvx/platform/dvxPlat.h ../libs/kpunch/libdvx/dvxTypes.h
$(POBJDIR)/dvxPlatformDos.o: ../libs/kpunch/libdvx/platform/dvxPlatformDos.c ../libs/kpunch/libdvx/platform/dvxPlat.h ../libs/kpunch/libdvx/dvxTypes.h ../libs/kpunch/libdvx/dvxPal.h
$(POBJDIR)/dvxPlatformUtil.o: ../libs/kpunch/libdvx/platform/dvxPlatformUtil.c ../libs/kpunch/libdvx/platform/dvxPlat.h ../libs/kpunch/libdvx/dvxTypes.h
$(POBJDIR)/dvxPrefs.o: ../libs/kpunch/libdvx/dvxPrefs.c ../libs/kpunch/libdvx/dvxPrefs.h

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