#CXX	= distcc i386-obrien-freebsd5-g++

PLATFORM = $(shell file /bin/ls | cut -d' ' -f3 | cut -d'-' -f1)

BSD_VERSION = $(shell uname -v 2>&1 | cut -d' ' -f2 | cut -d'.' -f1)
SVN_VERSION = $(shell svnversion -n -c . | cut -d':' -f2)

BIN     = ./libsql.a

# ---------------------------------------------------------------------------
# ### LINUX-BLOCK-BEGIN (Makefile platform detection)
# FreeBSD takes the else branch, which is verbatim the original: CXX,
# GCC_VERSION, IFLAGS = -I/usr/local/include (where the FreeBSD MySQL port puts
# its headers) and the original CFLAGS.
# ---------------------------------------------------------------------------
UNAME_S := $(shell uname -s)

# 32-bit third-party tree; same relative convention as game/src/Makefile's
# "-I../../../extern/include", with the Linux subdirectory appended.
EXTERN_DIR ?= ../../../extern/Linux40250

ifeq ($(UNAME_S),Linux)

CXX	= g++

GCC_VERSION = $(shell $(CC) --version 2>&1 | grep "(GCC)" | cut -d' ' -f3  | cut -d'.' -f1)

# AsyncSQL.h includes <mysql/mysql.h>, <mysql/errmsg.h> and
# <mysql/mysqld_error.h>.  On FreeBSD those live under /usr/local/include; here
# they are the MariaDB Connector/C headers shipped with the 32-bit extern tree,
# so the include root is $(EXTERN_DIR)/include and the "mysql/" prefix in the
# sources resolves unchanged.  MariaDB Connector/C still defines my_bool and
# still accepts MYSQL_OPT_RECONNECT, so AsyncSQL.cpp needs no source change.
IFLAGS = -I$(EXTERN_DIR)/include
CFLAGS  = $(IFLAGS) -Wall -O2 -pipe -mtune=i686 -m32 -std=c++23 -D_GLIBCXX_USE_CXX11_ABI=1 -D_THREAD_SAFE -fno-exceptions

ARFLAGS_SQL = cr

# The checked-in Depend was generated on FreeBSD and names
# /usr/local/include/mysql/*.h by absolute path.  Those files do not exist on
# Linux, so "sinclude Depend" makes gmake abort with
#   "No rule to make target '/usr/local/include/mysql/mysql.h'".
# Use a separate, Linux-generated dependency file and leave the FreeBSD one
# exactly as it is.  ("make dep" writes whichever file this platform uses.)
DEPFILE = Depend.linux

else

CXX	= clang++-devel

GCC_VERSION = $(shell $(CC) --version 2>&1 | grep "(GCC)" | cut -d' ' -f3  | cut -d'.' -f1)

IFLAGS = -I/usr/local/include
CFLAGS  = $(IFLAGS) -Wall -O2 -pipe -mtune=i686 -D_GLIBCXX_USE_CXX11_ABI=1 -D_THREAD_SAFE -fno-exceptions

ARFLAGS_SQL = cru

DEPFILE = Depend

endif
# ### LINUX-BLOCK-END (Makefile platform detection)

LIBS =

OBJFILES = AsyncSQL.o Semaphore.o Tellwait.o Statement.o

default:
	$(MAKE) $(BIN)

$(BIN): $(OBJFILES)
	ar $(ARFLAGS_SQL) $(BIN) $(OBJFILES) $(LIBS)
	ranlib $(BIN)
	chmod 700 $(BIN)

clean:
	rm -f *.o
	rm -f $(BIN)

dep:
	touch $(DEPFILE)
	$(CXX) $(CFLAGS) -MM *.cpp > $(DEPFILE)

$(OBJFILES):
	$(CXX) $(CFLAGS) -c $<

sinclude $(DEPFILE)
