Makefile 變數替換

Makefile

SUBDIRS = xxx aaa

BUILDSUBDIRS = $(SUBDIRS:%=build-%)
CLEANSUBDIRS = $(SUBDIRS:%=clean-%)

.PHONY: all clean

all: $(BUILDSUBDIRS)

$(BUILDSUBDIRS):
	@echo "<===" $@
	make -C $(@:build-%=%)
	@echo ">===" $@
	@echo "
"

clean: $(CLEANSUBDIRS)

$(CLEANSUBDIRS):
	@echo "<===" $@
	make -C $(@:clean-%=%) clean
	@echo "===>" $@
	@echo "
"

================================================
CLEANSUBDIRS = $(SUBDIRS:%=clean-%):
SUBDIRS 變數裡的元素,
若有符合 % pattern,(%是萬用字元,也可代表很多字)
則使用 clean-% 這個 pattern 送元素出去。
make -C $(@:clean-%=%) clean :
$@ 是代表 target,
$@ = $(@),
(@:clean-%=%) 是說 target 裡的元素,
若符合 clean-% 的 pattern,
則使用 % 這個 pattern 送元素出去

================================================
在 command line 執行

$ make

此指令是 build first target,
在 此例就是 all,
若 all 和 clean target 對換,

$ make

就是執行 make clean,
並不是 make 就是一定 build all

reference:
http://vincentlogistics.blogspot.com/2014/05/makefile-recursive-make.html
http://lackof.org/taggart/hacking/make-example/
https://blog.csdn.net/a827415225/article/details/73414696
https://www.cnblogs.com/wang_yb/p/3990952.html

原文地址:https://www.cnblogs.com/youchihwang/p/10573828.html