#========================================================================================================
#                                            stc 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))

git_id ?= $(shell git log -1 --format="%H")
commit_ts ?= $(shell git log -1 --format="%ct")
commit_time ?= $(shell date -d@$(commit_ts) +"%Y-%m-%d-%H:%M:%S")
#git_cmit ?= $(shell date +"%Y-%m-%d::%H:%M:%S")

$(info "commit_ts=" $(commit_ts))
$(info "commit_time=" $(commit_time))

#DEFINES ?= RFIC_VER=\"commit_id:$(git_id),commit_time:$(commit_time)\"

#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 ?= ./src

INCLUDE_DIRS ?= ./inc

LD_FLAGS ?= -lgcc -lc 
LD_FLAGS += -lm -ldl -Wl,-rpath=./
LD_FLAGS += -lpthread
LD_FLAGS += -lrfic
LD_FLAGS += -rdynamic -funwind-tables -ffunction-sections

#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))

# Allow certain files to be excluded from the build
EXCL_SRCS ?=

# 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
#ifeq ($(BOARD_TYPE),EVB)
#	BIN_FILE_NAME := rfic_evb.out
#else ifeq ($(BOARD_TYPE),EVMY)
#	BIN_FILE_NAME := rfic_evmy.out
#else ifeq ($(BOARD_TYPE),EVMYF)
#	BIN_FILE_NAME := rfic_evmyf.out	
#else 
#	BIN_FILE_NAME := rfic_evmyt.out		
#endif

BIN_FILE_NAME := rfic.out		

#$(info "BUILD_DIR=" $(BUILD_DIR))
OBJ_DIR  := $(BUILD_DIR)/rfic
BIN_FILE := $(BUILD_DIR)/$(BIN_FILE_NAME)
OBJ_FILES += $(foreach f,$(FINAL_SRCS_FILES),$(OBJ_DIR)/$(patsubst %.c,%.o,$(notdir $(f))))

# ==============================================================================
# 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)))

$(BIN_FILE):$(OBJ_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)	

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

build: $(BIN_FILE)

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




