# engines/src/c/Makefile
# 编译 DLL 到 engines/release/（主程序通过 ctypes 直接调用）

CC        = gcc
CFLAGS    = -O3 -march=native -Wall -Wextra
LDFLAGS   = -lm
LIB_SRC   = dynamics_lib.c

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

# Windows 检测：Msys2/MINGW 也视为 Windows
IS_WINDOWS := $(findstring MINGW,$(UNAME_S))
ifneq ($(IS_WINDOWS),)
  UNAME_S := Windows
endif
IS_WINDOWS := $(findstring MSYS,$(UNAME_S))
ifneq ($(IS_WINDOWS),)
  UNAME_S := Windows
endif

# DLL 输出到 engines/release/
DLL_DIR = ../../release

ifeq ($(UNAME_S),Linux)
  DLL_TARGET = $(DLL_DIR)/dynamics_c.so
  DLL_FLAGS  = -shared -fPIC
else ifeq ($(UNAME_S),Darwin)
  DLL_TARGET = $(DLL_DIR)/dynamics_c.dylib
  DLL_FLAGS  = -dynamiclib
else
  # Windows: 静态链接运行时，避免依赖 libgcc_s_seh-1.dll
  DLL_TARGET = $(DLL_DIR)/dynamics_c.dll
  DLL_FLAGS  = -shared -static
endif

.PHONY: all dll clean

all: dll

dll: $(DLL_TARGET)

$(DLL_TARGET): $(LIB_SRC) | $(DLL_DIR)
	$(CC) $(CFLAGS) $(DLL_FLAGS) -o $@ $(LIB_SRC) $(LDFLAGS)
	@echo "  === C DLL built: $@ ==="

$(DLL_DIR):
	mkdir -p $(DLL_DIR)

clean:
	rm -f $(DLL_TARGET)
