# 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 SQL Library Makefile for DJGPP cross-compilation
#
# Builds the SQLite amalgamation (thirdparty/sqlite3) with the DVX VFS
# and wraps it with a high-level handle-based API for the BASIC runtime.
# The compile-time options live in thirdparty/sqlite3/sqliteOpts.mk and
# are shared by the DOS and host builds.

include ../../../common.mk
include thirdparty/sqlite3/sqliteOpts.mk

SQLITE_DIR    = thirdparty/sqlite3
SQLITE_C      = $(SQLITE_DIR)/sqlite3.c

# The wrapper and the VFS get the project flags.  The amalgamation is
# third party code: size-optimised for DOS, compiled without warnings
# (-Wextra reports unused parameters in upstream stubs) and without
# -fanalyzer (upstream analyses it far more deeply than we can; the
# static gate's analyzer build would otherwise spend hours on it).
# Every libc / libgcc symbol it needs (__dj_huge_valf for INFINITY,
# __divmoddi4 and the other 64-bit helpers) is exported by dvx.exe.
CFLAGS        = $(DOS_CFLAGS) -I../libdvx -I../libdvx/platform -I../libtasks $(SQLITE_OPTS)
SQLITE_CFLAGS = $(filter-out -O2 -Wextra $(DEPFLAGS),$(DOS_CFLAGS)) -Os -w -fno-analyzer $(SQLITE_OPTS)

OBJDIR    = ../../../../obj/sql
LIBSDIR   = ../../../../bin/libs
TARGETDIR = $(LIBSDIR)/kpunch/dvxsql
TARGET    = $(TARGETDIR)/dvxsql.lib

WRAP_SRCS = dvxSql.c dvxSqlVfs.c
OBJS      = $(WRAP_SRCS:%.c=$(OBJDIR)/%.o) $(OBJDIR)/sqlite3.o

# ---- Native host build (test harness) ----
#
# Builds the same amalgamation, VFS and wrapper with the host gcc into a
# static archive the test executables link with -rdynamic so the
# datactrl widget .so resolves dvxSql* at load time.  SAN/COV per
# common.mk; the amalgamation keeps the build's instrumentation (the
# sanitizers, or MemorySanitizer for src/test/lint msan, whose reports
# are false without it) but not gcov.
HOSTOBJDIR      = $(HOST_OBJROOT)/sql
HOSTTARGET      = $(HOST_OBJROOT)/dvxsql.a
HOSTCFLAGS      = $(HOST_CFLAGS) -I../libdvx -I../libdvx/platform $(SQLITE_OPTS)
HOSTSQLITEFLAGS = -O1 -g -fPIC -w -D_GNU_SOURCE $(SQLITE_OPTS) $(if $(filter hostcov,$(HOST_SUB)),,$(HOST_INSTFLAGS))

HOST_OBJS = $(WRAP_SRCS:%.c=$(HOSTOBJDIR)/%.o) $(HOSTOBJDIR)/sqlite3.o

.PHONY: all clean host host-clean

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

host: $(HOSTTARGET)

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

$(HOSTOBJDIR)/%.o: %.c $(DVX_MAKEFILES) $(SQLITE_DIR)/sqliteOpts.mk | $(HOSTOBJDIR)
	$(HOSTCC) $(HOSTCFLAGS) -c -o $@ $<

$(HOSTOBJDIR)/sqlite3.o: $(SQLITE_C) $(SQLITE_DIR)/sqlite3.h $(SQLITE_DIR)/sqliteOpts.mk | $(HOSTOBJDIR)
	$(HOSTCC) $(HOSTSQLITEFLAGS) -c -o $@ $<

$(HOSTOBJDIR):
	mkdir -p $(HOSTOBJDIR)

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

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

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

$(OBJDIR)/sqlite3.o: $(SQLITE_C) $(SQLITE_DIR)/sqlite3.h $(SQLITE_DIR)/sqliteOpts.mk | $(OBJDIR)
	$(CC) $(SQLITE_CFLAGS) -c -o $@ $<

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

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

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

-include $(WRAP_SRCS:%.c=$(OBJDIR)/%.d) $(WRAP_SRCS:%.c=$(HOSTOBJDIR)/%.d)

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