#
# Linux Makefile for Slow Chess 2.96
#
# The source code (originally Windows/MSVC) already contains portability
# guards (#if defined(_WIN32)...) and a POSIX implementation of Windows
# "Events" (pevents.cpp, based on pthreads): only the compiler and flags
# change, no code modification is needed to build on Linux.
#
# Files intentionally excluded from separate compilation because they are
# #included directly by other .cpp files:
#   - movegennc.cpp    (included by moves.cpp)
#   - hashnc.cpp       (included by searchextra.cpp)
#   - searchmainnc.cpp (included by searchextra.cpp)
#
# Usage:
#   make            -> portable "release" build (recommended, default)
#   make native     -> build optimized for THIS machine (mtune=native), not redistributable
#   make debug      -> build with debug symbols, no optimization
#   make static     -> "release" build with static linking (standalone binary)
#   make clean      -> removes object files and the executable
#
# One-off overrides without editing the file:
#   make CXX=clang++
#

CXX      ?= g++
TARGET   := slowchess

SOURCES := pevents.cpp \
           bitbases.cpp \
           board.cpp \
           endgameeval.cpp \
           evaluate.cpp \
           gui.cpp \
           moves.cpp \
           obook.cpp \
           perft.cpp \
           searchextra.cpp \
           transcript.cpp \
           winboard.cpp

OBJECTS := $(SOURCES:.cpp=.o)

LIBS     := -lpthread
DEFINES  := -DHAVE_STRUCT_TIMESPEC -DNDEBUG

COMMON_FLAGS := -std=c++14 -w

# ---- Default profile: portable release ---------------------------------
CXXFLAGS ?= $(COMMON_FLAGS) -O2 $(DEFINES)
LDFLAGS  ?=

.PHONY: default native debug static clean

default: $(TARGET)

# ---- Native profile: fastest on THIS machine, not redistributable
native: CXXFLAGS := $(COMMON_FLAGS) -O3 -march=native -mtune=native -flto -funroll-loops $(DEFINES)
native: LDFLAGS  := -flto
native: $(TARGET)

# ---- Debug profile: symbols, no optimization, asserts enabled ----------
debug: CXXFLAGS := $(COMMON_FLAGS) -O0 -g3 -DHAVE_STRUCT_TIMESPEC
debug: LDFLAGS  :=
debug: $(TARGET)

# ---- Static profile: standalone binary (useful for deploying without libc++)
static: CXXFLAGS := $(COMMON_FLAGS) -O2 $(DEFINES)
static: LDFLAGS  := -static
static: $(TARGET)

$(TARGET): $(OBJECTS)
	$(CXX) -o $@ $(OBJECTS) $(LDFLAGS) $(LIBS)

%.o: %.cpp
	$(CXX) -c $(CXXFLAGS) $< -o $@

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