#======================================================================================================== # 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 += ../lib/librfic.a LD_FLAGS += -rdynamic -funwind-tables -ffunction-sections $(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 := ./src MAIN_FILE := ./src/main.c # Allow certain files to be excluded from the build EXCL_SRCS ?= $(MAIN_FILE) # 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_FILE := $(BUILD_DIR)/rfic.out LIB_FILE := $(BUILD_DIR)/librfic_main.a BJ_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)))) # ============================================================================== # 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) # @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) #rm -rf ./lib/librfic.a #rm -rf ./inc/ucp_api_rfic.h #rm -rf ./inc/xzJSON.h