# ==========================================================================
# acquire-core-full / TxCore  Makefile  (생성기 산출물)
#
#   .pgc --ecpg--> .c --gcc--> build/*.o --ar--> libtxcore.a --ld--> bin/*
#
#   ~1000개 .c/.pgc 를 build/*.o 로 컴파일하는 것이 빌드 검증의 핵심.
#   와일드카드로 전체 소스를 수집하고, 대표 데모 바이너리 몇 개를 링크한다.
# ==========================================================================
CC        := gcc
ECPG      := ecpg
AR        := ar
PG_INCDIR := $(shell pg_config --includedir 2>/dev/null)

INCLUDES  := -Iframework/txcore/include \
             -Iframework/txcore/dbio \
             -Iapp/include \
             -Iapp/shared/include \
             -I$(PG_INCDIR)

# 고정길이 관용구 경고만 억제 (-Werror 없음)
CFLAGS    := -g -O2 -Wall -Wextra -Wno-unused-parameter \
             -Wno-stringop-truncation -Wno-format-truncation \
             -Wno-unused-variable -Wno-unused-but-set-variable $(INCLUDES)
ECPGFLAGS := -Iframework/txcore/include -Iframework/txcore/dbio \
             -Iapp/include -Iapp/shared/include
LDLIBS    := -lecpg -lpq

BUILD := build
BIN   := bin

# ---- 소스 수집 (와일드카드) ----------------------------------------------
ONLINE_PGC := $(wildcard app/online/*.pgc)
BATCH_PGC  := $(wildcard app/batch/*.pgc)
DBIO_PGC   := $(wildcard app/dbio/*.pgc)
COMMON_C   := $(wildcard app/common/*.c)
SHARED_C   := $(wildcard app/shared/common/*.c)

PGC_ALL := $(ONLINE_PGC) $(BATCH_PGC) $(DBIO_PGC)
GEN_C   := $(PGC_ALL:.pgc=.c)

PGC_OBJ    := $(patsubst %,$(BUILD)/%.o,$(basename $(notdir $(PGC_ALL))))
COMMON_OBJ := $(patsubst %,$(BUILD)/%.o,$(basename $(notdir $(COMMON_C))))
SHARED_OBJ := $(patsubst %,$(BUILD)/%.o,$(basename $(notdir $(SHARED_C))))
ALL_OBJ    := $(PGC_OBJ) $(COMMON_OBJ) $(SHARED_OBJ)

LIBTXCORE  := $(BUILD)/libtxcore.a

DEMO_BINS := $(BIN)/demo_mg_server $(BIN)/demo_ac_server $(BIN)/demo_rc_server $(BIN)/demo_st_server $(BIN)/demo_rc_batch $(BIN)/demo_cl_batch

.SECONDARY:
.PHONY: all clean dirs gen objs demos

all: dirs $(LIBTXCORE) objs demos
	@echo "==> 빌드 완료"
	@echo "오브젝트: $(words $(ALL_OBJ)) 개"

dirs:
	@mkdir -p $(BUILD) $(BIN)

gen: $(GEN_C)
objs: $(ALL_OBJ)
demos: $(DEMO_BINS)

# ---- ecpg: .pgc -> .c (디렉토리별) ---------------------------------------
app/online/%.c: app/online/%.pgc
	$(ECPG) $(ECPGFLAGS) -o $@ $<
app/batch/%.c: app/batch/%.pgc
	$(ECPG) $(ECPGFLAGS) -o $@ $<
app/dbio/%.c: app/dbio/%.pgc
	$(ECPG) $(ECPGFLAGS) -o $@ $<

# ---- 컴파일: build/<basename>.o (gen 선행 보장) --------------------------
$(BUILD)/%.o: app/online/%.c | gen
	$(CC) $(CFLAGS) -c $< -o $@
$(BUILD)/%.o: app/batch/%.c | gen
	$(CC) $(CFLAGS) -c $< -o $@
$(BUILD)/%.o: app/dbio/%.c | gen
	$(CC) $(CFLAGS) -c $< -o $@
$(BUILD)/%.o: app/common/%.c
	$(CC) $(CFLAGS) -c $< -o $@
$(BUILD)/%.o: app/shared/common/%.c
	$(CC) $(CFLAGS) -c $< -o $@

# ---- TxCore 정적 라이브러리 ----------------------------------------------
$(BUILD)/txcore.o: framework/txcore/src/txcore.c
	$(CC) $(CFLAGS) -c $< -o $@
$(LIBTXCORE): $(BUILD)/txcore.o
	$(AR) rcs $@ $^

# ---- 대표 데모 바이너리 (링크 검증) --------------------------------------
$(BIN)/demo_mg_server: $(BUILD)/demo_mg_main.o $(BUILD)/mg_ol_0001.o $(BUILD)/mg_dbio_main.o $(SHARED_OBJ) $(LIBTXCORE)
	$(CC) $(CFLAGS) -o $@ $(BUILD)/demo_mg_main.o $(BUILD)/mg_ol_0001.o $(BUILD)/mg_dbio_main.o $(SHARED_OBJ) -L$(BUILD) -ltxcore $(LDLIBS)

$(BIN)/demo_ac_server: $(BUILD)/demo_ac_main.o $(BUILD)/ac_ol_0001.o $(BUILD)/ac_dbio_main.o $(SHARED_OBJ) $(LIBTXCORE)
	$(CC) $(CFLAGS) -o $@ $(BUILD)/demo_ac_main.o $(BUILD)/ac_ol_0001.o $(BUILD)/ac_dbio_main.o $(SHARED_OBJ) -L$(BUILD) -ltxcore $(LDLIBS)

$(BIN)/demo_rc_server: $(BUILD)/demo_rc_main.o $(BUILD)/rc_ol_0001.o $(BUILD)/rc_dbio_main.o $(SHARED_OBJ) $(LIBTXCORE)
	$(CC) $(CFLAGS) -o $@ $(BUILD)/demo_rc_main.o $(BUILD)/rc_ol_0001.o $(BUILD)/rc_dbio_main.o $(SHARED_OBJ) -L$(BUILD) -ltxcore $(LDLIBS)

$(BIN)/demo_st_server: $(BUILD)/demo_st_main.o $(BUILD)/st_ol_0001.o $(BUILD)/st_dbio_main.o $(SHARED_OBJ) $(LIBTXCORE)
	$(CC) $(CFLAGS) -o $@ $(BUILD)/demo_st_main.o $(BUILD)/st_ol_0001.o $(BUILD)/st_dbio_main.o $(SHARED_OBJ) -L$(BUILD) -ltxcore $(LDLIBS)

$(BIN)/demo_rc_batch: $(BUILD)/rc_bt_0001.o $(BUILD)/rc_dbio_main.o $(SHARED_OBJ) $(LIBTXCORE)
	$(CC) $(CFLAGS) -o $@ $(BUILD)/rc_bt_0001.o $(BUILD)/rc_dbio_main.o $(SHARED_OBJ) -L$(BUILD) -ltxcore $(LDLIBS)

$(BIN)/demo_cl_batch: $(BUILD)/cl_bt_0001.o $(BUILD)/cl_dbio_main.o $(SHARED_OBJ) $(LIBTXCORE)
	$(CC) $(CFLAGS) -o $@ $(BUILD)/cl_bt_0001.o $(BUILD)/cl_dbio_main.o $(SHARED_OBJ) -L$(BUILD) -ltxcore $(LDLIBS)

# ==========================================================================
clean:
	rm -rf $(BUILD) $(BIN)
	rm -f $(GEN_C)

