功能
可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
APP_NAME := $(APP_NAME)
OBJS :=
SUBMKS :=
C_INCS :=
C_DEPS :=
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
then \
TMP_OBJ="$$BUD_DIR/$${file%%.*}.s.o";\
OBJS="$$OBJS $$TMP_OBJ";\
C_DEPS="$$C_DEPS $$BUD_DIR/$${file%%.*}.s.d";\
fi;\
if [ $${file
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
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)))
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))
APP: $(BH)/$(APP_NAME).out
LIB: $(BH)/$(APP_NAME).a
$(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 ' '
clean:
-$(RM) $(EXECUTABLES) $(OBJS) $(C_DEPS) $(BH)/$(APP_NAME).out $(BH)/*
-@echo ' '
.PHONY: all clean dependents
makefile.def
makefile.def文件,用于自定义一些编译参数、环境变量等,如CFLAGS等
|