# mac/env/makefile.env — single cross-platform build environment for Klammertext.
#
# Included identically by every component Makefile:
#     include $(KLAMMERTEXT_HOME)/mac/env/makefile.env
#
# The platform is auto-detected with uname; the compiler is chosen with
#     make COMPILER=gcc      (default)
#     make COMPILER=clang
# There is no per-host/per-OS file and no DESKTOP_SESSION/HOST/SITE selector.

ifndef KLAMMERTEXT_HOME
$(error KLAMMERTEXT_HOME is not set -- source mac/env/runtime.env first)
endif

include $(KLAMMERTEXT_HOME)/mac/env/optimize.env

UNAME_S     := $(shell uname -s)
CPP_VERSION := c++20
# Default compiler: clang on macOS, gcc elsewhere. gcc-built Klammertext
# binaries crash at runtime on macOS (a documented gcc/macOS codegen issue), so
# gcc there is only a compile-time conformance check (via `./dbg/rebuild.sh
# gcc`, which compiles under g++ then rebuilds with clang). Override with
# `make COMPILER=...`.
ifeq ($(UNAME_S),Darwin)
COMPILER    ?= clang               # gcc | clang
else
COMPILER    ?= gcc                 # gcc | clang
endif

# Python: version- and platform-agnostic, no hardcoded paths.
PYTHON_INC := $(shell python3-config --includes)
PYTHON_LIB := $(shell python3-config --ldflags --embed)

# Flags common to all platforms.
CPPFLAGS = -I$(KLAMMERTEXT_HOME)/mac $(PYTHON_INC)
CXXFLAGS = -Wall -Wextra -Weffc++ -Wshadow -std=$(CPP_VERSION) -fPIC $(OPTIMIZE) $(SANITIZE)

ifeq ($(UNAME_S),Darwin)
# ---------- macOS (Apple Silicon) ----------
# Package-manager prefix, auto-detected: Homebrew (/opt/homebrew) or MacPorts
# (/opt/local). Override with `make MACOS_PREFIX=...`. Only the prefix differs
# between the two: the compiler is Apple Clang either way, and all Python flags
# come from python3-config (prefix-agnostic), so nothing else is PM-specific.
MACOS_PREFIX   ?= $(shell test -d /opt/homebrew && echo /opt/homebrew || (test -d /opt/local && echo /opt/local || echo /opt/homebrew))
# Newest optional gcc (conformance check only): Homebrew g++-NN or MacPorts g++-mp-NN.
GCC            := $(shell g=$$(ls $(MACOS_PREFIX)/bin/g++-[0-9]* $(MACOS_PREFIX)/bin/g++-mp-[0-9]* 2>/dev/null | sort -V | tail -1); echo $${g:-g++})
CLANG          := clang++                         # Apple clang (or brew/port llvm)
CPPFLAGS       += -I$(MACOS_PREFIX)/include
LDFLAGS         = -L$(MACOS_PREFIX)/lib
LDLIBS          = $(if $(NOPYTHON),,$(PYTHON_LIB))
SHARED          = -dynamiclib
SONAME          = -install_name @rpath/$(notdir $@)
ORIGIN         := @loader_path
EXPORT_DYNAMIC  = -Wl,-export_dynamic
else
# ---------- Linux ----------
GCC            := /usr/bin/g++
CLANG          := $(shell command -v clang++-18 2>/dev/null || command -v clang++ 2>/dev/null)
LDFLAGS         = -L/usr/lib/x86_64-linux-gnu
LDLIBS          = -ldl $(if $(NOPYTHON),,$(PYTHON_LIB))
SHARED          = -shared
SONAME          = -Wl,-soname,$(notdir $@)
ORIGIN         := $$ORIGIN
EXPORT_DYNAMIC  = -rdynamic
endif

# Resolve the compiler choice (gcc by default; clang with COMPILER=clang).
CXX := $(if $(filter clang,$(COMPILER)),$(CLANG),$(GCC))
