# The MIT License (MIT)
#
# Copyright (C) 2026 Scott Duensing
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# 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().  dvxPlatformUtil.c
# (logging, glob, path helpers), dvxPlatformIrq.c (held-key table, timer
# service) and dvxMemTrack.c (per-app allocation tracking) are the
# backend-independent parts of the platform layer.

include ../common.mk

LIBDVX    = ../libs/kpunch/libdvx
CFLAGS    = $(DOS_CFLAGS) -I$(LIBDVX) -I$(LIBDVX)/platform -I../libs/kpunch/libtasks -I$(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)/dvxPlatformIrq.o $(POBJDIR)/dvxPlatformUtil.o $(POBJDIR)/dvxMemTrack.o
TARGET = $(BINDIR)/dvx.exe

# The help compiler as a module of its own (tools/hlpcModule.h), loaded
# only while help compiles: the boot rebuild and BASIC's HelpCompile
HLPC   = $(BINDIR)/system/HLPC.DXE

.PHONY: all clean

all: $(TARGET) $(HLPC)

$(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 $(DVX_MAKEFILES) | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(POBJDIR)/%.o: $(LIBDVX)/platform/%.c $(DVX_MAKEFILES) | $(POBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

# The help compiler's library entry point (no main) for help rebuilds.
$(HLPC): $(OBJDIR)/dvxhlpc.o | $(BINDIR)
	@mkdir -p $(dir $@)
	$(DXE3GEN) -o $@ -U $<

$(OBJDIR)/dvxhlpc.o: ../tools/dvxhlpc.c $(DVX_MAKEFILES) | $(OBJDIR)
	$(CC) $(CFLAGS) -DHLPC_NO_MAIN -c -o $@ $<

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

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

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

-include $(OBJS:.o=.d) $(POBJS:.o=.d)

clean:
	rm -f $(OBJS) $(POBJS) $(OBJS:.o=.d) $(POBJS:.o=.d) $(OBJDIR)/dvxhlpc.o $(OBJDIR)/dvxhlpc.d $(TARGET) $(HLPC) $(BINDIR)/dvx.map
