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

# Networking Library Makefile for DJGPP cross-compilation
#
# Builds dvxnet.lib -- TCP/IP (lwIP, unmodified, in thirdparty/lwip) with
# a packet-driver network interface and the standard BSD socket API (see
# dvxNet.h and the headers in include/, which ship in the SDK).
#
# lwIP is configured only by port/lwipopts.h and port/arch/cc.h.  The
# library's own sources and lwIP are compiled with the same flags; the
# SDK include directory comes first so <errno.h> has the network codes.

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

LWIP_DIR  = thirdparty/lwip/src
CFLAGS    = $(DOS_CFLAGS) -I. -Iinclude -Iport -I$(LWIP_DIR)/include -I../libdvx -I../libdvx/platform -I../libtasks -I../dvxshell

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

SRCS      = dvxNet.c netBas.c netDos.c netOrder.c netProto.c netResolve.c netSock.c netStack.c pktDrvDos.c pktRing.c
LWIP_SRCS = $(wildcard $(LWIP_DIR)/core/*.c) $(wildcard $(LWIP_DIR)/core/ipv4/*.c) $(LWIP_DIR)/netif/ethernet.c
OWN_OBJS  = $(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
OBJS      = $(OWN_OBJS) $(patsubst $(LWIP_DIR)/%.c,$(OBJDIR)/lwip/%.o,$(LWIP_SRCS))

# What the library's own objects (whole: the packet driver's interrupt
# code is laid out by file) reach of lwIP is all dvxnet.lib keeps, and it
# exports its own functions only (DXE_GC)
GC_OBJ    = $(OBJDIR)/dvxnetGc.o
TARGETDIR = $(LIBSDIR)/kpunch/dvxnet
TARGET    = $(TARGETDIR)/dvxnet.lib

.PHONY: all clean

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

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

$(GC_OBJ): $(OBJS)
	$(DXE_GC) $@ $(OWN_OBJS) -- $(OBJS)

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

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

$(OBJDIR)/lwip/%.o: $(LWIP_DIR)/%.c $(DVX_MAKEFILES)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(DXE_GC_CFLAGS) -c -o $@ $<

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

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

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

clean:
	rm -rf $(OBJDIR) $(TARGET) $(TARGETDIR)/dvxnet.dep $(TARGETDIR)
