From 698e123a26825852a1783182feba2045c1ac6b97 Mon Sep 17 00:00:00 2001 From: Gatsby Date: Fri, 9 May 2025 22:22:40 +0800 Subject: [PATCH] chore: refine common commands --- generic.mk | 69 ++++++++++++++++++--------------------- platform/xilinx/xilinx.mk | 9 +++-- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/generic.mk b/generic.mk index 798df5b..f304fc5 100644 --- a/generic.mk +++ b/generic.mk @@ -5,48 +5,42 @@ HIDE = > /dev/zero endif TIMESTAMP = $(shell date +"%T") -copy_files = cp $(1) $(2) -copy_dir = cp -r $(1) $(2) -remove_file = rm -rf $(1) -remove_dir = rm -rf $(1) -mk_dir = mkdir -p $(1) -read_file = cat $(1) 2>/dev/null -print_lines = echo $(1) | tr ' ' '\n' -green = \\e[32m$(1)\\e[39m -print = printf "$(call green,[$(TIMESTAMP)]) $(1)\n" -make_dir_link = ln -sfr $(1) $(2) -make_link = ln -sfr $(1) $(2) +cmd_copy = cp -a $(1) $(2) +cmd_link = ln -sfr $(1) $(2) +cmd_remove = rm -rf $(1) +cmd_mkdir = mkdir -p $(1) +cmd_cat = cat $(1) 2>/dev/null +cmd_print_lines = echo $(1) | tr ' ' '\n' + +color_green = \033[32m$(1)\033[0m +log = @printf "$(call color_green,[$(TIMESTAMP)]) %s\n" "$(1)" +show_build_type = $(call log,Building for $(call green,$(1))) + +check-var = $(if $(strip $($(1))),,$(error [ERROR] Missing required variable: $(1))) check_command = $(shell which $(1) > /dev/null 2>&1 && echo y || echo n) -print_build_type = $(call print,Building for $(call green,$(1))) -ifeq ('y', '$(strip $(LINK_SRCS))') - update_files = $(call make_link,$(1),$(2)) - update_dir = $(call make_dir_link,$(1),$(2)) - ACTION = Linking -else - update_files = $(call copy_files,$(1),$(dir $(2))) - update_dir = $(call copy_dir,$(1),$(2)) - ACTION = Copying -endif +$(call check-var,PROJECT) +$(call check-var,PLATFORM) +$(call check-var,BUILD_DIR) +$(call check-var,CONFIG_FILE) +$(call check-var,LINK_SRCS) -ifndef PROJECT - @$(error "PROJECT variable needs to be set") -endif -ifndef PLATFORM - @$(error "PLATFORM variable needs to be set") -endif -ifndef BUILD_DIR - @$(error "BUILD_DIR variable needs to be set") -endif -ifndef CONFIG_FILE - @$(error "CONFIG_FILE variable needs to be set") -endif ifndef SCRIPT_DIR CURRENT_FILE := $(lastword $(MAKEFILE_LIST)) CURRENT_DIR := $(patsubst %/,%,$(dir $(CURRENT_FILE))) SCRIPT_DIR := $(CURRENT_DIR) endif +ifeq ($(strip $(LINK_SRCS)),y) + update_files = $(call cmd_link,$(1),$(2)) + update_dir = $(call cmd_link,$(1),$(2)) + ACTION = Linking +else + update_files = $(call cmd_copy,$(1),$(dir $(2))) + update_dir = $(call cmd_copy,$(1),$(2)) + ACTION = Copying +endif + #------------------------------------------------------------------------------ # COMMON INITIALIZATION #------------------------------------------------------------------------------ @@ -93,18 +87,17 @@ sdkopen: $(PLATFORM)_sdkopen PHONY += clean ifneq ($(wildcard $(BUILD_LOCK)),) clean: - @$(call print,Cleaning sdk build uploaded to board) + @$(call log,Cleaning sdk build uploaded to board) $(MAKE) --no-print-directory $(PLATFORM)_sdkclean else clean: - @$(call print,No project found, cleaning dummy files) + @$(call log,No project found, cleaning dummy files) $(MAKE) --no-print-directory reset endif # Remove the whole build directory -PHONY += reset reset: $(PLATFORM)_reset - @$(call print,[Delete] $(BUILD_DIR)) - @$(call remove_dir,$(BUILD_DIR)) + @$(call log,[Delete] $(BUILD_DIR)) + @$(call cmd_remove,$(BUILD_DIR)) .PHONY: $(PHONY) diff --git a/platform/xilinx/xilinx.mk b/platform/xilinx/xilinx.mk index 0981081..15fc5e1 100644 --- a/platform/xilinx/xilinx.mk +++ b/platform/xilinx/xilinx.mk @@ -8,7 +8,7 @@ define tcl_util endef xilinx_sdkopen: - @$(call print,Launching IDE...) + @$(call log,Launching IDE...) @if [ "$(call check_command,vitis)" = "y" ]; then \ if [ "$(XILINX_VITIS_CLASSIC)" = "y" ]; then \ vitis -classic -workspace=$(BUILD_DIR); \ @@ -23,12 +23,11 @@ xilinx_sdkopen: PHONY += xilinx_sdkbuild xilinx_sdkbuild: - @$(call print,[Build] SDK projects in $(BUILD_DIR)) + @$(call log,[Build] SDK projects in $(BUILD_DIR)) $(call tcl_util, build) $(HIDE) -PHONY += xilinx_sdkclean xilinx_sdkclean: - @$(call print,[Delete] SDK artefacts from $(BUILD_DIR)) + @$(call log,[Delete] SDK artefacts from $(BUILD_DIR)) $(call tcl_util, clean_project) $(HIDE) xilinx_project: @@ -37,4 +36,4 @@ xilinx_project: $(call tcl_util, create_project) $(HIDE) xilinx_reset: - $(call remove_dir,.Xil) $(HIDE) \ No newline at end of file + $(call cmd_remove,.Xil) $(HIDE) \ No newline at end of file