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

SAN      ?= 1
HOSTCC    = gcc
ROOT      = ../../..
OBJDIR    = $(ROOT)/obj/host
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

CFLAGS    = -O1 -g -Wall -Wextra -Werror -Wno-type-limits -Wno-sign-compare -Wno-format-truncation -D_GNU_SOURCE -I$(LIBDVX) -I$(LIBDVX)/platform -I$(LIBDVX)/thirdparty -I$(LIBTASKS) -I$(WIDGETS)
LDFLAGS   = -rdynamic
LDLIBS    = -ldl -lm
ifeq ($(SAN),1)
CFLAGS   += -fsanitize=address,undefined -fno-omit-frame-pointer
LDFLAGS  += -fsanitize=address,undefined
endif
# COV=1 (make coverage): gcov instrumentation under obj/hostcov.
ifeq ($(COV),1)
OBJDIR    = $(ROOT)/obj/hostcov
CFLAGS   += --coverage
LDFLAGS  += --coverage
endif

SRCS      = unitMain.c unitDisplay.c testClock.c testComp.c testDraw.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

.PHONY: all run libs clean

all: run

libs:
	$(MAKE) -C $(LIBDVX) host SAN=$(SAN)
	$(MAKE) -C $(WIDGETS) host SAN=$(SAN)

$(BINDIR)/%.o: %.c unitTest.h | libs
	@mkdir -p $(BINDIR)
	$(HOSTCC) $(CFLAGS) -c -o $@ $<

$(TARGET): $(OBJS) $(OBJDIR)/libdvx.a
	$(HOSTCC) $(LDFLAGS) -o $@ $(OBJS) -Wl,--whole-archive $(OBJDIR)/libdvx.a -Wl,--no-whole-archive $(LDLIBS)

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

clean:
	rm -rf $(BINDIR)
