# ==============================================================================
#									common makefile
#FileName：		makefile.common
#Author:		 xianfeng.du
#Data：			2022-08-10
#Description:	 common
# ==============================================================================
VERSION = 1.00

ifneq ($(findstring ape,$(build_variable)),)
	DEFINES += APE_INT
else
	DEFINES += RFM_INT
endif
#DEFINES         += APE_CORE_MASK=\"$(ape_core_mask)\"
DEFINES         += APE_CORE_MASK=$(ape_core_mask)
DEFINES         += PLATFORM_BUILD_DATA=$(platform_build_data)
ifeq ($(backhaul_option), pcie)
	DEFINES     += PCIE_BACKHAUL
endif

CC_FLAGS += $(foreach d,$(DEFINES),-D$(d))
$(info "DEFINES=" $(DEFINES))
$(info "LINK_FILE=" $(LINK_FILE))

# ==============================================================================
# files and dirs definition
# ==============================================================================
LIB_DIRS += $(DIRS_LIB_FOLDER)
SRC_DIRS += $(addsuffix /src, $(DIRS_SRC_FOLDER))
INC_DIRS += $(patsubst %/src,%/inc,$(sort $(SRC_DIRS)))

#Flatten files 	
#main.c files
MAIN_SRC_FILE := $(shell find $(DIRS_SRC_FOLDER) -name "*main.s.c")
TOP_SRC_FILE  += $(MAIN_SRC_FILE)
TOP_DIR := $(sort $(dir $(TOP_SRC_FILE)))
ABS_TOP_DIR := $(patsubst %/,%,$(abspath $(TOP_DIR)))

#src files 
ABS_SRC_DIRS := $(foreach d,$(SRC_DIRS),$(abspath $(d)))
SRC_FILES := $(foreach d,$(ABS_SRC_DIRS),$(wildcard $(d)/*.c))

# Allow certain files to be excluded from the build
EXCL_SRCS_FILES += $(TOP_SRC_FILE)
EXCL_SRCS_FILES += $(DIR_COMMON)/driver/src/ucp_wdt.s.c
EXCL_SRCS_FILES += $(DIR_COMMON)/utility/src/ucp_mem_dump.s.c

# Filter source files (allow files to be excluded)
FINAL_SRCS_FILES := $(foreach f,$(SRC_FILES),$(if $(findstring $(abspath $(f)),$(abspath $(EXCL_SRCS_FILES))),,$(f)))

# ==============================================================================
# Variables: Output Files
# ==============================================================================
TARGET_DIR := $(BUILD_DIR)/$(build_variable)
OBJ_TARGET := $(TARGET_DIR)/obj
OBJ_LIB    := $(TARGET_DIR)/lib
OBJ_TOP    := $(TARGET_DIR)/top

TARGET_LIB := $(TARGET_DIR)/lib$(build_variable).a
ifneq ($(findstring ape,$(build_variable)),)
	TARGET_BIN := $(TARGET_DIR)/$(build_variable)$(ape_id).out
else
	TARGET_BIN := $(TARGET_DIR)/$(build_variable).out
endif

OBJ_FILES      := $(foreach f,$(FINAL_SRCS_FILES),$(OBJ_TARGET)/$(patsubst %.c,%.o,$(notdir $(f))))
OBJ_FILES_TOP  := $(foreach f,$(TOP_SRC_FILE),$(OBJ_TOP)/$(patsubst %.c,%.o,$(notdir $(f))))
OBJ_FILES_LIB  := $(wildcard $(OBJ_LIB)/*.o)

OBJ_FILES_MAIN := $(foreach f,$(MAIN_SRC_FILE),$(OBJ_TOP)/$(patsubst %.c,%.o,$(notdir $(f))))
#$(info "OBJ_FILES_MAIN=" $(OBJ_FILES_MAIN))

# ==============================================================================
# Rules: Compilation
# ==============================================================================
define DO_BUILD_OBJ
$(2)/%.o: $(1)/%.c | $(2)
	@echo "# Compiling $(DIA_FLAGS) $$< -> $$@"
	$(PREFIX)$(UCP_CC)/clang $(DIA_FLAGS) $(TAR_FLAGS) $(CC_FLAGS) -o $$@ -c $$< $(patsubst %,-I %,$(INC_DIRS))
endef

define DO_BUILD_DIR
$(1):
#	@echo "# Creating directory $$@"
	mkdir -p $$@
endef

define DO_BUILD
	$(eval $(call DO_BUILD_DIR,$(1)))
	$(foreach d,$(sort $(2)),$(eval $(call DO_BUILD_OBJ,$(d),$(1))))
endef

$(eval $(call DO_BUILD,$(OBJ_TARGET),$(ABS_SRC_DIRS)))
$(eval $(call DO_BUILD,$(OBJ_TOP),$(ABS_TOP_DIR)))

$(TARGET_BIN):$(OBJ_FILES) $(OBJ_FILES_TOP)
	@echo "# Linking objects to form $@"
	$(PREFIX)$(UCP_CC)/ld.lld $(GC_FLAGS) -o $@ $^ $(USER_OBJS) $(patsubst %,-L %,$(LIB_DIRS)) $(LD_FLAGS) -T$(LINK_FILE)

$(TARGET_LIB):$(OBJ_FILES) $(OBJ_FILES_LIB) $(OBJ_FILES_MAIN)
	@echo "# Linking objects to form $@"
	$(PREFIX)ar rcs -o $@ $^
	
# ==============================================================================
# Rules: Targets
# ==============================================================================
.DEFAULT_GOAL := all
.PHONY: bin lib clean
all: bin lib

bin: $(TARGET_BIN) 
	
lib: $(TARGET_LIB) 

clean:
	@echo "deleted all files"
	rm -rf $(BUILD_DIR) 
