# DVX SQL Library Makefile for DJGPP cross-compilation
#
# Builds SQLite3 from source and wraps it with a high-level
# handle-based API for the BASIC runtime.
#
# Two-phase build:
#   1. Native tools (lemon, mkkeywordhash) built with host GCC
#      to generate parse.c, opcodes.c, opcodes.h, keywordhash.h
#   2. SQLite + wrapper cross-compiled with DJGPP into dvxsql.lib DXE

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
HOSTCC        = gcc

SQLITE_DIR    = thirdparty/sqlite
SQLITE_SRC    = $(SQLITE_DIR)/src
SQLITE_TOOL   = $(SQLITE_DIR)/tool

CFLAGS        = -O2 -Wall -Werror -Wno-type-limits -Wno-sign-compare -Wno-format-truncation -Wno-address -march=i486 -mtune=i586 \
                -I../libdvx -I../libdvx/platform -I../libtasks \
                -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
                -I$(SQLITE_SRC) -I$(SQLITE_DIR)/examples -I$(OBJDIR)

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

# SQLite library source files (no test, shell, tcl, server, md5, os_test)
SQLITE_SRCS = \
	alter analyze attach auth btmutex btree build callback complete \
	date delete expr fault func hash insert journal \
	legacy loadext main malloc mem1 mem2 mem3 mem4 \
	mutex mutex_os2 mutex_unix mutex_w32 \
	os os_os2 os_unix os_win \
	pager pragma prepare printf random select \
	table tokenize trigger update utf util vacuum \
	vdbeapi vdbeaux vdbeblob vdbe vdbefifo vdbemem vtab where

SQLITE_OBJS = $(SQLITE_SRCS:%=$(OBJDIR)/sqlite_%.o)

# Generated files
GEN_PARSE_C    = $(OBJDIR)/parse.c
GEN_PARSE_H    = $(OBJDIR)/parse.h
GEN_OPCODES_H  = $(OBJDIR)/opcodes.h
GEN_OPCODES_C  = $(OBJDIR)/opcodes.c
GEN_KEYWORDHASH = $(OBJDIR)/keywordhash.h

# Native tools
LEMON          = $(OBJDIR)/lemon
MKKEYWORDHASH  = $(OBJDIR)/mkkeywordhash

OBJS = $(OBJDIR)/dvxSql.o $(SQLITE_OBJS) $(OBJDIR)/sqlite_parse.o $(OBJDIR)/sqlite_opcodes.o

.PHONY: all clean

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

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

# ---- Phase 1: Native tools and generated files ----

$(LEMON): $(SQLITE_TOOL)/lemon.c | $(OBJDIR)
	$(HOSTCC) -O2 -o $@ $<

$(MKKEYWORDHASH): $(SQLITE_TOOL)/mkkeywordhash.c | $(OBJDIR)
	$(HOSTCC) -O2 -o $@ $<

$(GEN_PARSE_C) $(GEN_PARSE_H): $(SQLITE_SRC)/parse.y $(LEMON) $(SQLITE_DIR)/addopcodes.awk | $(OBJDIR)
	cp $(SQLITE_SRC)/parse.y $(OBJDIR)/parse.y
	cp $(SQLITE_TOOL)/lempar.c $(OBJDIR)/lempar.c
	cd $(OBJDIR) && $(abspath $(LEMON)) parse.y
	mv $(OBJDIR)/parse.h $(OBJDIR)/parse.h.temp
	gawk -f $(SQLITE_DIR)/addopcodes.awk $(OBJDIR)/parse.h.temp > $(OBJDIR)/parse.h

$(GEN_OPCODES_H): $(GEN_PARSE_H) $(SQLITE_SRC)/vdbe.c $(SQLITE_DIR)/mkopcodeh.awk
	cat $(GEN_PARSE_H) $(SQLITE_SRC)/vdbe.c | gawk -f $(SQLITE_DIR)/mkopcodeh.awk > $@

$(GEN_OPCODES_C): $(GEN_OPCODES_H) $(SQLITE_DIR)/mkopcodec.awk
	sort -n -b -k 3 $(GEN_OPCODES_H) | gawk -f $(SQLITE_DIR)/mkopcodec.awk > $@

$(GEN_KEYWORDHASH): $(MKKEYWORDHASH)
	$(MKKEYWORDHASH) > $@

# ---- Phase 2: Cross-compile SQLite + wrapper ----

$(OBJDIR)/dvxSql.o: dvxSql.c dvxSql.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR)/sqlite_%.o: $(SQLITE_SRC)/%.c $(GEN_OPCODES_H) $(GEN_KEYWORDHASH) $(GEN_PARSE_H) | $(OBJDIR)
	$(CC) $(CFLAGS) -Wno-unused-parameter -Wno-unused-variable -Wno-implicit-fallthrough -c -o $@ $<

$(OBJDIR)/sqlite_parse.o: $(GEN_PARSE_C) $(GEN_OPCODES_H) | $(OBJDIR)
	$(CC) $(CFLAGS) -Wno-unused-parameter -Wno-unused-variable -Wno-implicit-fallthrough -c -o $@ $<

$(OBJDIR)/sqlite_opcodes.o: $(GEN_OPCODES_C) | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

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

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

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

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

clean:
	rm -rf $(OBJS) $(TARGET) $(TARGETDIR)/dvxsql.dep $(TARGETDIR)
	rm -f $(LEMON) $(MKKEYWORDHASH)
	rm -f $(GEN_PARSE_C) $(GEN_PARSE_H) $(GEN_OPCODES_H) $(GEN_OPCODES_C) $(GEN_KEYWORDHASH)
	rm -f $(OBJDIR)/parse.y $(OBJDIR)/parse.out
