# 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 Core Library Makefile for DJGPP cross-compilation
#
# Builds libdvx.lib -- core GUI infrastructure (draw, compositor,
# window manager, event dispatch, layout engine, widget infrastructure).
# Zero widget implementations -- those are in ../widgets/ as .wgt modules.

include ../../../common.mk

CFLAGS        = $(DOS_CFLAGS) -I. -Iplatform -I../libtasks -Ithirdparty

OBJDIR  = ../../../../obj/core
LIBSDIR = ../../../../bin/libs

# Core sources.  dvxPrefs.c is not among them on DOS: the loader needs it
# before libdvx loads, so dvx.exe links it and exports prefs* to every DXE.
SRCS = dvxVideo.c dvxDraw.c dvxComp.c dvxWm.c dvxImage.c dvxImageWrite.c dvxDriver.c \
       dvxApp.c dvxDialog.c dvxPrefsBas.c dvxResource.c dvxText.c dvxTextBas.c dvxFontFmt.c dvxFlow.c \
       widgetClass.c widgetCore.c widgetScrollbar.c \
       widgetLayout.c widgetEvent.c widgetOps.c widgetIface.c

# Resource writing is shared with the dvxres tool and lives beside it.
TOOLSDIR = ../../../tools
TOOL_SRCS = $(TOOLSDIR)/dvxResWrite.c

OBJS   = $(patsubst %.c,$(OBJDIR)/%.o,$(SRCS) $(notdir $(TOOL_SRCS)))
TARGETDIR = $(LIBSDIR)/kpunch/libdvx
TARGET    = $(TARGETDIR)/libdvx.lib

# Native host build (test harness): same core sources plus the host
# platform backend and the shared platform pieces the DOS loader normally
# supplies (memory tracking, util, stb_ds).  Produces a static archive the
# test binaries link with --whole-archive so widget .so files can resolve
# every core symbol through -rdynamic.  SAN/COV select the instrumentation
# and the object tree (see common.mk).
HOSTOBJDIR    = $(HOST_OBJROOT)
HOSTTARGET    = $(HOSTOBJDIR)/libdvx.a
HOSTCFLAGS    = $(HOST_CFLAGS) -I. -Iplatform -I../libtasks -Ithirdparty
HOST_SRCS     = $(SRCS) dvxPrefs.c platform/dvxPlatformHost.c platform/dvxPlatformIrq.c platform/dvxMemTrack.c platform/dvxPlatformUtil.c thirdparty/stb_ds_impl.c
HOST_OBJS     = $(patsubst %.c,$(HOSTOBJDIR)/libdvx/%.o,$(HOST_SRCS) $(notdir $(TOOL_SRCS)))

.PHONY: all clean host host-clean

all: $(TARGET) $(TARGETDIR)/libdvx.dep

host: $(HOSTTARGET)

$(HOSTTARGET): $(HOST_OBJS)
	rm -f $@
	ar rcs $@ $(HOST_OBJS)

$(HOSTOBJDIR)/libdvx/%.o: %.c $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	$(HOSTCC) $(HOSTCFLAGS) -c -o $@ $<

$(HOSTOBJDIR)/libdvx/%.o: $(TOOLSDIR)/%.c $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	$(HOSTCC) $(HOSTCFLAGS) -c -o $@ $<

host-clean:
	rm -rf $(HOSTOBJDIR)/libdvx $(HOSTTARGET)

$(TARGETDIR)/libdvx.dep: libdvx.dep | $(TARGETDIR)
	sed 's/$$/\r/' $< > $@

$(TARGET): $(OBJS) | $(TARGETDIR)
	$(DXE3GEN) -o $(TARGETDIR)/libdvx.dxe -U $(OBJS)
	mv $(TARGETDIR)/libdvx.dxe $@

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

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

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

$(TARGETDIR):
	mkdir -p $(TARGETDIR)

# Header dependencies: written by the compiler (DEPFLAGS in common.mk).
-include $(OBJS:.o=.d) $(HOST_OBJS:.o=.d)

clean:
	rm -rf $(OBJS) $(OBJS:.o=.d) $(TARGET) $(TARGETDIR)/libdvx.dep $(TARGETDIR)
