其他分享
首页 > 其他分享> > 一个通用的Makefile

一个通用的Makefile

作者:互联网

一个通用的Makefile

功能

可build任意目录结构的C/C++代码,此处在linux下以llvm+clang工具链为例

原理

make程序在读取makefile阶段时通过$(shell)间接使用shell脚本递归扫描所有的源码目录,在输出/build目录下生成与源码目录结构一一对应的目录结构,并且在每个目录下生成subdir.mk文件,该文件是子makefile文件,其中描述了对应目录下要编译的资源。生成所有的子subdir.mk后,把这些子subdir.mk再include到主makefile中

代码

主makefile

################################################################################
################################################################################
RM := rm -rf *.txt *.a *.hex *.tmp *.dat
BH := build	##build home,指定编译或输出目录
APP_NAME := $(APP_NAME)
OBJS := 	##记录所有要输出的obj文件列表
SUBMKS :=    ##所有subdir.mk文件列表
C_INCS :=	##记录所有inc目录,把所有存放源码文件的目录都当做头文件搜索目录
C_DEPS :=	##记录源码文件的依赖文件,主要用于.h被修改后的增量编译

TOOLCHAIN := $(TOOLCHAIN) ##工具链
ifeq ($(strip $(TOOLCHAIN)),)
TOOLCHAIN := /home/user/desktop/toolchain
endif

CFLAGS := $(CFLAGS)
AFLAGS := $(AFLAGS)
debug := $(debug)
ifeq ($(strip $(debug)),1)
CFLAGS += -O0 -g 
AFLAGS += -g
else
CFLAGS += -O2 
endif

-include ./Makefile.def

define gen_submkf 
	$(shell \
		SRC_DIR="$(1)";\
		BUD_DIR="$(2)";\
		SUBMKF="$$BUD_DIR/subdir.mk";\
		OBJS=;\
		C_DEPS=;\
		SPC_C=;\
		SPU_ASM=;\
		MPU_ASM=;\
		TMP_OBJ=;\
		TMP_BUD=;\
		for file in `ls $(1)`;\
		do \
			if [ -d '$$file' ]; \
				then \
					echo '';\
				else \
					if [ $${file#*.} = "s.c" ];\
						then \
							TMP_OBJ="$$BUD_DIR/$${file%%.*}.s.o";\
							OBJS="$$OBJS $$TMP_OBJ";\
							C_DEPS="$$C_DEPS $$BUD_DIR/$${file%%.*}.s.d";\
					fi;\
					if [ $${file#*.} = "s.asm" ];\
						then \
							TMP_OBJ="$$BUD_DIR/$${file%.*}.o";\
							TMP_BUD="$$TMP_OBJ: $$SRC_DIR/$$file\n\t\$$(TOOLCHAIN)/bin/llvm-mc -arch=ucps $(AFLAGS) -filetype=obj -o  \"\$$@\" \"\$$<\"\n\t@echo ' '";\
							OBJS="$$OBJS $$TMP_OBJ";\
							SPU_ASM="$$SPU_ASM \n\n$$TMP_BUD";\
					fi;\
					if [ $${file#*.} == "m0.asm" ] || [ $${file#*.} == "m1.asm" ];\
						then \
							TMP_OBJ="$$BUD_DIR/$${file%.*}.o";\
							TMP_BUD="$$TMP_OBJ: $$SRC_DIR/$$file\n\t\$$(TOOLCHAIN)/bin/llvm-mc -arch=ucpm $(AFLAGS) -filetype=obj -o  \"\$$@\" \"\$$<\"\n\t@echo ' '";\
							OBJS="$$OBJS $$TMP_OBJ";\
							MPU_ASM="$$MPU_ASM \n\n$$TMP_BUD";\
					fi;\
			fi; \
		done;\
		SPU_C="$$BUD_DIR/%.s.o: $$SRC_DIR/%.s.c\n\t\$$(TOOLCHAIN)/bin/clang --target=ucps \$$(C_INCS) $(CFLAGS) -MMD -MP -o \"\$$@\" -c \"\$$<\"\n\t@echo ' '";\
		echo -e "#generate automatically\n" > $$SUBMKF;\
		echo "C_INCS += -I$$SRC_DIR" >> $$SUBMKF;\
		echo "C_DEPS += $$C_DEPS" >> $$SUBMKF;\
		echo "OBJS += $$OBJS" >> $$SUBMKF;\
		echo -e "\n" >> $$SUBMKF;\
		echo -e $$SPU_C >> $$SUBMKF;\
		echo -e $$SPU_ASM >> $$SUBMKF;\
		echo -e $$MPU_ASM >> $$SUBMKF;\
	)
endef

define gen_submk
	$(if $(subst $(realpath $(1)),,$(realpath $(BH))),\
		$(shell mkdir -p $(2);rm -f $(2)/subdir.mk $(2)/*.o $(2)/*.d;touch $(2)/subdir.mk)$(2)/subdir.mk\
		$(call gen_submkf,$(1),$(2))
		$(foreach item,$(shell ls $(1)),$(if $(shell if [ -d '$(1)/$(item)' ];then echo 'isDir';fi;),\
			$(call gen_submk,$(1)/$(item),$(2)/$(item)),\
		)),\
	)
endef

$(shell mkdir -p $(BH))
SUB_MKS := $(strip $(call gen_submk,.,$(BH)))

# All of the sources participating in the build are defined here

ifneq ($(strip $(SUB_MKS)),)
-include $(SUB_MKS)
endif

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif

OBJS := $(strip $(OBJS))
C_DEPS := $(strip $(C_DEPS))
# Add inputs and outputs from these tool invocations to the build variables 

# All Target
APP: $(BH)/$(APP_NAME).out
LIB: $(BH)/$(APP_NAME).a

# Tool invocations
$(BH)/$(APP_NAME).out: $(OBJS)
	@echo 'Building target: $@'
	@echo 'Invoking: lld linker'
	$(MaPU_TC_HOME)/bin/ld.lld -o  "$(BH)/$(APP_NAME).out" $(USER_OBJS) $(OBJS) $(LIBS) -L$(MaPU_TC_HOME)/lib/ucp/release -T$(MaPU_TC_HOME)/include/ucp/c.ld
	@echo 'Finished building target: $@'
	@echo ' '

$(BH)/$(APP_NAME).a: $(OBJS)
	@echo 'Building target: $@'
	@echo 'Invoking: GNU archiver'
	ar rcs $@ $(OBJS)
	@echo 'Finished building target: $@'
	@echo ' '

# Other Targets
clean:
	-$(RM) $(EXECUTABLES) $(OBJS) $(C_DEPS) $(BH)/$(APP_NAME).out $(BH)/*
	-@echo ' '
    
.PHONY: all clean dependents

makefile.def

makefile.def文件,用于自定义一些编译参数、环境变量等,如CFLAGS等

标签:TMP,OBJS,通用,一个,BH,Makefile,echo,DEPS,DIR
来源: https://blog.csdn.net/mzjmzjmzjmzj/article/details/122421994