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

OPTION ?= 
ifeq ($(OPTION),GCC)
	CROSS_CC ?=
else
#	TOOLS ?= /opt/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin
#	CROSS_CC ?= $(TOOLS)/aarch64-linux-gnu-
	CROSS_CC ?= aarch64-linux-gnu-
endif

AR = ar
CC = $(CROSS_CC)gcc
#$(info "CC=" $(CC))

DEFINES ?= 
#DEFINES ?= UBLOX_ENABLE

DEFINES         += ARM_VERSION=\"${arm_version}\"
DEFINES         += ARM_TAG=\"${arm_tag}\"
DEFINES         += ARM_BUILD_DATE=\"${arm_build_date}\"

ifeq ($(fronthaul_option),jesd)
DEFINES += ENABLE_JESD_TEST
endif

#optimization level -O2
C_OPT_FLAGS ?= -O2

CC_FLAGS ?= $(C_OPT_FLAGS) -Wall -g 
CC_FLAGS += -Werror -Wno-unused-function 
CC_FLAGS += $(foreach d,$(DEFINES),-D$(d))
CC_FLAGS += -fPIC

LIB_DIRS ?= ./lib

SRC_DIRS ?= ./app/src
SRC_DIRS += ./osp/src
SRC_DIRS += ./driver/arm_csu/src
SRC_DIRS += ./driver/init/src

INCLUDE_DIRS ?= ./app/inc
INCLUDE_DIRS += ./osp/inc
INCLUDE_DIRS += ./driver/arm_csu/inc
INCLUDE_DIRS += ./driver/init/inc
INCLUDE_DIRS += ./driver/tfu/stc/inc
test ?= $(test_option)
case ?= $(test_id)
ifeq ($(test),yes)
	DEFINES += PALLADIUM_TEST
#testbench
	CASE_ID := $(case)
	TEST_DIR := ./test/case$(CASE_ID)
	SRC_DIRS += $(TEST_DIR)/src
	INCLUDE_DIRS += $(TEST_DIR)/inc
endif

cache ?= $(cache_option)
ifeq ($(cache),yes)
	DEFINES += CACHE_ENABLE
endif

LD_FLAGS ?= -lgcc -lc 
LD_FLAGS += -lm -ldl -Wl,-rpath=./
LD_FLAGS += -lpthread
LD_FLAGS += -rdynamic -funwind-tables -ffunction-sections
ifeq ($(fronthaul_option),jesd)
LD_FLAGS += -lrfic -lstc
endif
LD_FLAGS += -lstc
$(info "DEFINES=" $(DEFINES))

#Flatten files and remove the duplicate by sort
ABS_SRC_DIRS := $(foreach d,$(SRC_DIRS),$(abspath $(d)))

SRC_FILES := $(foreach d,$(ABS_SRC_DIRS),$(wildcard $(d)/*.c))

#main.c
MAIN_DIR := ./app/src
MAIN_FILE := ./app/src/main.c

# Allow certain files to be excluded from the build
EXCL_SRCS ?= $(MAIN_FILE)
#EXCL_SRCS += ./app/src/main.c

# Filter source files (allow files to be excluded)
FINAL_SRCS_FILES += $(foreach f,$(SRC_FILES),$(if $(findstring $(abspath $(f)),$(abspath $(EXCL_SRCS))),,$(f)))
#$(info "FINAL_SRCS_FILES=" $(FINAL_SRCS_FILES))

# ==============================================================================
# Variables: Output Files
# ==============================================================================
BUILD_DIR := ./build
OBJ_MAIN_DIR   := $(BUILD_DIR)/main
OBJ_DIR   := $(BUILD_DIR)/obj
#BIN_DIR   := $(BUILD_DIR)/bin
#LIB_DIR   := $(BUILD_DIR)/lib
#ELF_FILE  := $(BIN_DIR)/test.elf
BIN_FILE := $(BUILD_DIR)/msgtransfer.out
LIB_FILE := $(BUILD_DIR)/libmsgtransfer.a

OBJ_FILES += $(foreach f,$(FINAL_SRCS_FILES),$(OBJ_DIR)/$(patsubst %.c,%.o,$(notdir $(f))))
OBJ_MAIN_FILES += $(foreach f,$(MAIN_FILE),$(OBJ_MAIN_DIR)/$(patsubst %.c,%.o,$(notdir $(f))))

#ifeq ($(fronthaul_option),jesd)
#OBJ_FILES += $(wildcard $(RFIC_DIR)/*.o)
#RFIC_OBJ_DIR := $(BUILD_DIR)/rfic_obj
#RFIC_OBJ_FILES := $(wildcard $(RFIC_OBJ_DIR)/*.o)
#else
#RFIC_OBJ_FILES := 
#endif

# ==============================================================================
# Rules: Compilation
# ==============================================================================
define DO_BUILD_OBJ
$(2)/%.o: $(1)/%.c | $(2)
	@echo "# Compiling $$< -> $$@"
	$(CC) $(CC_FLAGS) -o $$@ -c $$< $(patsubst %,-I %,$(INCLUDE_DIRS))
#OBJ_TGTS += $(foreach f,$(wildcard $(1)/*.c),$(OBJ_DIR)/$(notdir $(basename $(1)))/$(patsubst %.c,%.o,$(notdir $(f))))
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_DIR),$(ABS_SRC_DIRS)))
$(eval $(call DO_BUILD,$(OBJ_MAIN_DIR), $(abspath $(MAIN_DIR))))

$(BIN_FILE):$(OBJ_FILES) $(OBJ_MAIN_FILES) 
#	@echo "# Creating bin directory $(BIN_DIR)"
#	mkdir -p $(BIN_DIR)
	
	@echo "# Linking objects to form $@"
	$(CC) -o $@ $^ $(LD_FLAGS) $(patsubst %,-L %,$(LIB_DIRS))
#	$(CC) -o $@ $^ $(LD_FLAGS) -T$(LINK_FILE)	

#$(LIB_FILE):$(OBJ_FILES) $(RFIC_OBJ_FILES)
$(LIB_FILE):$(OBJ_FILES)
#	@echo "# Creating lib directory $(LIB_DIR)"
#	mkdir -p $(LIB_DIR)
	
	@echo "# Linking objects to form $@"
	$(AR) -rcs $@ $^ 	
#	$(CC) -shared -o $@ $^ $(LD_FLAGS)

# ==============================================================================
# Rules: Targets
# ==============================================================================
.DEFAULT_GOAL := all
.PHONY: build lib clean
all: build lib

build: $(BIN_FILE)
	
lib: $(LIB_FILE)

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