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

# T1 pure unit tests for libdvx.
#
# One binary, one section per libdvx area, no dvxInit.  Links the host
# libdvx.a with --whole-archive and -rdynamic so the box widget .so (needed
# by the layout section) resolves core symbols from the executable.
# Sanitizers are on by default (SAN=1); pass SAN=0 for a plain build.

include ../../common.mk

ROOT      = ../../..
OBJDIR    = $(HOST_OBJROOT)
BINDIR    = $(OBJDIR)/test/unit
LIBDVX    = $(ROOT)/src/libs/kpunch/libdvx
LIBTASKS  = $(ROOT)/src/libs/kpunch/libtasks
WIDGETS   = $(ROOT)/src/widgets/kpunch
WIDGETDIR = $(OBJDIR)/widgets
SCRATCH   = $(BINDIR)/scratch
LIBDVX_A  = $(OBJDIR)/libdvx.a

CFLAGS    = $(HOST_CFLAGS) -I$(LIBDVX) -I$(LIBDVX)/platform -I$(LIBDVX)/thirdparty -I$(LIBTASKS) -I$(WIDGETS)
LDFLAGS   = -rdynamic $(HOST_LDFLAGS)
LDLIBS    = -ldl -lm

SRCS      = unitMain.c unitDisplay.c testClass.c testClock.c testComp.c testDraw.c testEvent.c testLayout.c testMem.c testPlatform.c testPrefs.c testResource.c testUtil.c testVideo.c testWm.c
OBJS      = $(patsubst %.c,$(BINDIR)/%.o,$(SRCS))
TARGET    = $(BINDIR)/unit

# stb_ds allocation-failure test: its own binary, since it compiles its own
# stb_ds implementation (failing allocator) and so cannot link libdvx.a.
OOM_SRCS   = testStbDsOom.c $(LIBDVX)/platform/dvxPlatformUtil.c
OOM_OBJS   = $(patsubst %.c,$(BINDIR)/oom/%.o,$(notdir $(OOM_SRCS)))
OOM_TARGET = $(BINDIR)/stbDsOom

.PHONY: all run widgets clean force

all: run

# libdvx.a is linked, so its sub-make always runs and make re-reads the
# archive's time stamp: the unit binary relinks exactly when it changed.
$(LIBDVX_A): force
	$(MAKE) -C $(LIBDVX) host SAN=$(SAN)

widgets:
	$(MAKE) -C $(WIDGETS) host SAN=$(SAN)

$(BINDIR)/%.o: %.c $(DVX_MAKEFILES)
	@mkdir -p $(BINDIR)
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

$(TARGET): $(OBJS) $(LIBDVX_A)
	$(HOSTCC) $(LDFLAGS) -o $@ $(OBJS) -Wl,--whole-archive $(LIBDVX_A) -Wl,--no-whole-archive $(LDLIBS)

$(BINDIR)/oom/%.o: %.c $(DVX_MAKEFILES)
	@mkdir -p $(BINDIR)/oom
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

$(BINDIR)/oom/%.o: $(LIBDVX)/platform/%.c $(DVX_MAKEFILES)
	@mkdir -p $(BINDIR)/oom
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

$(OOM_TARGET): $(OOM_OBJS)
	$(HOSTCC) $(HOST_LDFLAGS) -o $@ $(OOM_OBJS) -lm

run: $(TARGET) $(OOM_TARGET) | widgets
	@mkdir -p $(SCRATCH)
	ASAN_OPTIONS=detect_leaks=1:allocator_may_return_null=1 $(TARGET) $(abspath $(WIDGETDIR)) $(abspath $(SCRATCH))
	ASAN_OPTIONS=detect_leaks=1 $(OOM_TARGET) $(abspath $(SCRATCH))

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

clean:
	rm -rf $(BINDIR)
