# =========================
# Makefile for UnetPhex
# =========================
#
# UNetInterface.h is a C++ header (class CUNETInterface, std::string, bool).
# All translation units that include it must be compiled as C++.
# The final link is always done with g++ so libstdc++ and libgcc_s are pulled in.

CC      := gcc
CXX     := g++

# Library search paths
# IMPORTANT: -L. intentionally removed — stale .a files in the local dir would
# shadow the canonical libs in ../lib and cause hard-to-diagnose linker errors.
# ../4000 is kept for libExc4000.a; ../lib holds all stable release libs.
LDFLAGS := -fPIC -L../4000 -L../lib

# Libraries
# -lexchost2dpr / -lExc4000 may have circular references — wrap in --start/end-group
LIBS_GROUP := -lexchost2dpr -lExc4000
LIBS_SYS   := -ldl -lrt -lpthread -lftd2 -lftd3 

# =========================
# Build configuration
# =========================
# Default: RELEASE build. Use `make RELEASE=0` for a debug build.
# NOTE: -O2 is intentionally excluded — it causes a segfault in UnetPhex
#       due to unprotected memory-mapped register accesses in the driver libs.
RELEASE ?= 1

ifeq ($(RELEASE),1)
  # -D_GNU_SOURCE exposes: clock_gettime, CLOCK_MONOTONIC, usleep,
  #   pthread_tryjoin_np, and other POSIX/GNU extensions that are hidden
  #   under strict -std=c11 / -std=c++11 mode.
  # UNetInterface.h uses std::string (Linux path) so -std=c++11 minimum is needed.
  # IMPORTANT: -I. removed — ../4000 is the canonical include dir.
  # Local copies of headers shadow ../4000 and cause silent version mismatches.
  CFLAGS   := -std=c11   -D_GNU_SOURCE -O2 -g -DNDEBUG -fPIC -Wall -I.. -I../4000
  # NOTE: -O2 causes a segfault in UnetPhex — the driver libraries use
  # memory-mapped register accesses that the optimiser reorders/eliminates.
  # -O0 is intentional here. -DNDEBUG, -fPIC and all other flags are safe.
  CXXFLAGS := -D_GNU_SOURCE -O0 -g -DNDEBUG -fPIC -Wall -I.. -I../4000
else
  CFLAGS   := -std=c11   -D_GNU_SOURCE -O0 -g -fPIC -Wall -Wextra -I.. -I../4000
  CXXFLAGS := -D_GNU_SOURCE -O0 -g -fPIC -Wall -Wextra -I.. -I../4000
endif

$(info )
$(info ======================================)
$(info   UnetPhex build: $(if $(filter 1,$(RELEASE)),RELEASE,DEBUG))
$(info ======================================)
$(info )

# =========================
# Sources, objects, targets
# =========================
# FIX: initialise OBJS and PROGS before using +=
OBJS  := UnetPhex.o
PROGS := UnetPhex

# =========================
# Default target
# =========================
.PHONY: all clean

all: $(PROGS)
	@echo "=== $(if $(filter 1,$(RELEASE)),RELEASE,DEBUG) build completed successfully ==="

# =========================
# Compile rules
# =========================
%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# FIX: pass CXXFLAGS (not just -Wall -g) so -std=c++11 and -D_GNU_SOURCE
#      are applied when compiling UNetInterface.h-dependent sources.
%.o: %.cpp
	$(CXX) $(CXXFLAGS) -c $< -o $@

# =========================
# Link rule
# =========================
#
# ../lib/libexchost2dpr.a is listed as a make dependency so make rebuilds
# UnetPhex whenever the library changes — but it is NOT passed as a direct
# linker input.  It is already covered by -lexchost2dpr inside LIBS_GROUP.
# Passing it both ways (via a bare .a path AND via -l) linked it twice,
# which corrupted C++ static initialisers and caused the startup segfault.
#
# --start-group/--end-group: libexchost2dpr and libExc4000 have circular
# references (iniOptions, HeaderStrings) that require it at link time.
# Currently not triggered but kept for correctness.
UnetPhex: UnetPhex.o ../lib/libexchost2dpr.a
	$(CXX) -o $@ UnetPhex.o $(LDFLAGS) \
		-Wl,--start-group $(LIBS_GROUP) -Wl,--end-group \
		$(LIBS_SYS)

# =========================
# Clean
# =========================
clean:
	rm -f $(OBJS) $(PROGS)
