BIN_DIR = ../lib
BIN     = $(BIN_DIR)/libthecore.a
INCLUDE = ../include

# ---------------------------------------------------------------------------
# ### LINUX-BLOCK-BEGIN (Makefile platform detection)
#
# The FreeBSD build is the production build and must keep working exactly as
# before: on FreeBSD "uname -s" reports "FreeBSD", the ifeq below is false and
# every variable is assigned by the else branch, which is verbatim the original
# Makefile (same CC, same GCC_VERSION probe, same CFLAGS in both arms of the
# original ifeq, same "ar cru").  Only the Linux branch is new.
# ---------------------------------------------------------------------------
UNAME_S := $(shell uname -s)

ifeq ($(UNAME_S),Linux)

# clang++-devel is the FreeBSD ports name for the development clang; it does
# not exist on Linux.  g++ with gcc-multilib is the Linux equivalent and, like
# clang++, compiles these .c files as C++ - which they rely on (they are
# declared "extern C" through the headers, so the emitted symbols are the same
# either way).  Override on the command line with "make CC=clang++" if wanted.
CC	= g++

# -m32: the whole server is a 32-bit build.  Do NOT add -D_TIME_BITS=64 here or
#       anywhere else - sizeof(time_t) must stay 4 under -m32 because several
#       packed wire/DB structures embed it.
# -fno-strict-aliasing: tea.c, des.c, gost.c and buffer.c type-pun through
#       char*/DWORD* casts.  That is undefined behaviour under the strict
#       aliasing rules GCC applies at -O2, and it is exactly the kind of code
#       GCC 13 miscompiles silently.  clang has historically been laxer here,
#       which is why the FreeBSD build never needed the flag.
# -std=c++23: matches game/src/Makefile and db/src/Makefile so the whole link
#       is built against one set of libstdc++ headers.
CFLAGS  = -Wall -O2 -pipe -mtune=i686 -g -m32 -std=c++23 -fno-strict-aliasing -I$(INCLUDE)

# GNU ar warns "`u' modifier ignored since `D' is the default" on every
# invocation; the archive is created from scratch anyway.
ARFLAGS_THECORE = cr

else

CC	= clang++-devel

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

ifeq ($(GCC_VERSION), 4)
CFLAGS  = -Wall -O2 -pipe -mtune=i686 -g -I$(INCLUDE)
else
CFLAGS  = -Wall -O2 -pipe -mtune=i686 -g -I$(INCLUDE)
endif

ARFLAGS_THECORE = cru

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

LIBS    =

OBJFILES = socket.o fdwatch.o buffer.o signal.o log.o utils.o \
	kstbl.o hangul.o heart.o main.o tea.o des.o gost.o memcpy.o

default:
	$(MAKE) $(BIN)

$(BIN): $(OBJFILES)
	if [ ! -d $(BIN_DIR) ]; then mkdir $(BIN_DIR); fi
	ar $(ARFLAGS_THECORE) $(BIN) $(OBJFILES) $(LIBS)
	ranlib $(BIN)
	chmod 700 $(BIN)

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

dep:
	$(CC) $(CFLAGS) -MM *.c > Depend

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

memcpy: memcpy.o utils.o log.o
	$(CC) $(CFLAGS) -c -D__MAIN__ memcpy.c
	$(CC) $(CFLAGS) -o memcpy memcpy.o utils.o log.o

include Depend
