# 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.

DJGPP_PREFIX  = $(HOME)/djgpp/djgpp
CC            = $(DJGPP_PREFIX)/bin/i586-pc-msdosdjgpp-gcc
DXE3GEN       = PATH=$(DJGPP_PREFIX)/bin:$(PATH) DJDIR=$(DJGPP_PREFIX)/i586-pc-msdosdjgpp $(DJGPP_PREFIX)/i586-pc-msdosdjgpp/bin/dxe3gen
CFLAGS        = -O2 -Wall -Wextra -Werror -Wno-type-limits -Wno-sign-compare -Wno-format-truncation -march=i486 -mtune=i586 -I. -Iplatform -I../libtasks -Ithirdparty

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

# Core sources
SRCS = dvxVideo.c dvxDraw.c dvxComp.c dvxWm.c dvxImage.c dvxImageWrite.c \
       dvxApp.c dvxDialog.c dvxPrefs.c dvxResource.c \
       widgetClass.c widgetCore.c widgetScrollbar.c \
       widgetLayout.c widgetEvent.c widgetOps.c

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

.PHONY: all clean

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

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

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

$(LIBSDIR):
	mkdir -p $(LIBSDIR)

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

# Dependencies
CORE_HDRS = dvxTypes.h dvxApp.h dvxDraw.h dvxWm.h dvxVideo.h dvxWgt.h platform/dvxPlat.h
$(OBJDIR)/dvxVideo.o:      dvxVideo.c dvxVideo.h platform/dvxPlat.h dvxTypes.h dvxPal.h
$(OBJDIR)/dvxDraw.o:       dvxDraw.c dvxDraw.h platform/dvxPlat.h dvxTypes.h
$(OBJDIR)/dvxComp.o:       dvxComp.c dvxComp.h platform/dvxPlat.h dvxTypes.h
$(OBJDIR)/dvxWm.o:         dvxWm.c dvxWm.h dvxTypes.h dvxDraw.h dvxComp.h dvxVideo.h dvxWgt.h platform/dvxPlat.h thirdparty/stb_image.h
$(OBJDIR)/dvxImage.o:       dvxImage.c thirdparty/stb_image.h
$(OBJDIR)/dvxImageWrite.o: dvxImageWrite.c thirdparty/stb_image_write.h
$(OBJDIR)/dvxApp.o:        dvxApp.c dvxApp.h platform/dvxPlat.h dvxTypes.h dvxVideo.h dvxDraw.h dvxComp.h dvxWm.h dvxFont.h dvxCur.h
$(OBJDIR)/dvxDialog.o:     dvxDialog.c dvxDlg.h platform/dvxPlat.h dvxApp.h dvxWgt.h dvxWgtP.h dvxTypes.h dvxDraw.h
$(OBJDIR)/dvxPrefs.o:      dvxPrefs.c dvxPrefs.h

WIDGET_DEPS = dvxWgtP.h dvxWgt.h dvxTypes.h dvxApp.h dvxDraw.h dvxWm.h dvxVideo.h platform/dvxPlat.h
$(OBJDIR)/widgetClass.o:     widgetClass.c $(WIDGET_DEPS)
$(OBJDIR)/widgetCore.o:      widgetCore.c $(WIDGET_DEPS)
$(OBJDIR)/widgetScrollbar.o: widgetScrollbar.c $(WIDGET_DEPS)
$(OBJDIR)/widgetLayout.o:    widgetLayout.c $(WIDGET_DEPS)
$(OBJDIR)/widgetEvent.o:     widgetEvent.c $(WIDGET_DEPS)
$(OBJDIR)/widgetOps.o:       widgetOps.c $(WIDGET_DEPS)

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