# 包含外部Makefile
include ./../../../linux.mk

ProjectName        := libweb_server

DIR := $(realpath $(CURDIR)/..)
LAST_DIR := $(notdir $(DIR))

	
# 静态库文件目标
OutputFile         := $(LIB_REL)/$(ProjectName).a
ArchiveCommand     := ar rcs  # 静态库打包命令

MakeDirCommand     := mkdir -p

OBJ := $(DIR)/$(ProjectName)/obj
SRC := $(SRC_ROOT_DIR)/$(LAST_DIR)/$(ProjectName)/src
INC := $(SRC_ROOT_DIR)/$(LAST_DIR)/$(ProjectName)/inc

# print_dir:
# 	@echo "Shell层面打印DIR: $(SRC_ROOT_DIR) $(OBJ) $(SRC) $(INC) "

# 定义源文件和对象文件
SOURCES     := $(wildcard $(SRC)/*.c) $(wildcard $(SRC)/*.cpp)
OBJECTS     := $(patsubst $(SRC)/%.c, $(OBJ)/%.o,$(filter %.c,$(SOURCES))) 
OBJECTSCPP  := $(patsubst $(SRC)/%.cpp, $(OBJ)/%.o,$(filter %.cpp,$(SOURCES))) 

ALL_OBJECTS := $(OBJECTS) $(OBJECTSCPP)

# 编译选项
APP_C_FLAGS := $(C_FLAGS)
APP_C_FLAGS += -I $(INC) -g

APP_C_FLAGS += -O0

# 伪目标
.PHONY : all clean veryclean rebuild

# 默认目标
all: $(OutputFile)

# 构建静态库
$(OutputFile): $(ALL_OBJECTS)
	@$(MakeDirCommand) $(dir $@)
	$(ArchiveCommand) $@ $^
	@echo "Static library built: $@"

# 重新构建
rebuild: veryclean all

# 清理
clean:
	rm -f $(OBJ)/*.o
	rm -f $(OBJ)/*.d
	rm -f $(OutputFile)
	rm -f ./out/*.*

# 非常彻底的清理
veryclean: clean
	rm -f ./out/$(OutputFile)
	rm -f $(SRC)/*.bak
	rm -f $(INC)/*.bak

# C对象文件规则
$(OBJECTS): $(OBJ)/%.o: $(SRC)/%.c 
	@mkdir -p $(dir $@)
	$(CC) $(APP_C_FLAGS) -o $@ -c $<

# C++对象文件规则
$(OBJECTSCPP): $(OBJ)/%.o: $(SRC)/%.cpp 
	@mkdir -p $(dir $@)
	$(CPP) $(APP_C_FLAGS) -o $@ -c $<

# 可选：生成依赖文件（用于自动重新编译）
# -include $(ALL_OBJECTS:.o=.d)

# C依赖文件规则
$(OBJ)/%.d: $(SRC)/%.c
	@mkdir -p $(dir $@)
	$(CC) $(APP_C_FLAGS) -MM -MT '$(OBJ)/$*.o' $< > $@

# C++依赖文件规则  
$(OBJ)/%.d: $(SRC)/%.cpp
	@mkdir -p $(dir $@)
	$(CPP) $(APP_C_FLAGS) -MM -MT '$(OBJ)/$*.o' $< > $@