# engines/c/Makefile
# 跨平台编译：make         → 本地系统编译
#              make linux  → Linux 交叉编译（需 x86_64-linux-gnu-gcc）
#              make windows → Windows 交叉编译（需 x86_64-w64-mingw32-gcc）
#              make macos  → macOS 交叉编译（需 osxcross 工具链）

CC        = gcc
CFLAGS    = -O3 -march=native -Wall -Wextra
LDFLAGS   = -lm
SRCS      = main.c

# 自动检测系统
UNAME_S  := $(shell uname -s 2>/dev/null || echo Windows)

# 目标文件名：统一使用 .exe 后缀（方便 Python 跨平台调用）
TARGET    = build/dynamics_c.exe

# ── 本地编译 ─────────────────────────────────
.PHONY: all clean linux windows macos

all: $(TARGET)

$(TARGET): $(SRCS) | build
	$(CC) $(CFLAGS) -o $@ $(SRCS) $(LDFLAGS)
	@echo "  === C engine built: $@ ==="

build:
	mkdir -p build

# ── 交叉编译 ─────────────────────────────────
# Linux → Linux (x86_64)
linux: CROSS_PREFIX = x86_64-linux-gnu-
linux: CC = $(CROSS_PREFIX)gcc
linux: CFLAGS = -O3 -march=x86-64 -Wall -Wextra
linux: $(SRCS) | build
	$(CC) $(CFLAGS) -o build/dynamics_c_linux.exe $(SRCS) $(LDFLAGS)
	@echo "  === Linux binary: build/dynamics_c_linux.exe ==="

# 任意平台 → Windows (x86_64)
# 需要安装 MinGW 交叉编译器：
#   apt install mingw-w64 (Debian/Ubuntu)
#   brew install mingw-w64 (macOS)
windows: CROSS_PREFIX = x86_64-w64-mingw32-
windows: CC = $(CROSS_PREFIX)gcc
windows: CFLAGS = -O3 -march=x86-64 -Wall -Wextra
windows: $(SRCS) | build
	$(CC) $(CFLAGS) -o build/dynamics_c_win.exe $(SRCS) $(LDFLAGS)
	@echo "  === Windows binary: build/dynamics_c_win.exe ==="

# 任意平台 → macOS (x86_64)
# 需要安装 osxcross 工具链
macos: CROSS_PREFIX = x86_64-apple-darwin-
macos: CC = $(CROSS_PREFIX)gcc
macos: CFLAGS = -O3 -march=x86-64 -Wall -Wextra
macos: $(SRCS) | build
	$(CC) $(CFLAGS) -o build/dynamics_c_mac.exe $(SRCS) $(LDFLAGS)
	@echo "  === macOS binary: build/dynamics_c_mac.exe ==="

# ── 编译所有平台 ──────────────────────────────
all-platforms: linux windows macos

clean:
	rm -rf build *.o
