一、makefile执行shell脚本
FILE=test
A:=$(shell ls ../)
B:=$(shell pwd)
C:=$(shell if [ ! -f $(FILE) ];then touch $(FILE);fi;)
a:
echo $(A)
echo $(B)
echo $(C)
clean:
$(RM) $(FILE)
二、makefile中的条件判断
ifeq 判断是否相等;相等返回true,不相等返回false
ifneq 判断是否不相等;相等返回true,不相等返回false
ifdef 判断变量是否存在;存在返回true,不存在返回false
ifndef 判断变量是否不存在;不存在返回true,存在返回false
ifeq/ifneq与条件之间要有空格,不然会报错;
不存在elseif的写法,如果需要实现,那么需要进行if else的嵌套;
对某个路径下的makefile传参
make -f xxx/makefile 参数名=参数值
当前路径下
make 参数名=参数值
示例 ifeq
ifeq ($(B),test)
lg=hello make
else
lg=hello no make
endif
show:
@echo $(lg)
示例 ifdef
ifeq ($(A),123)
test:=yes
else
test:=no
endif
ifdef C
B=$(C)
else
B=empty
endif
show:
echo $(test) $(A) $(B)
三、makefile中的循环
#循环
#makefile 中只有一个循环foreach,只支持GUN Make。其它平台的make,可以借助shell中的循环来实现
TARGET=x y z
show:
for v in $(TARGET); do echo $$v; done; #shell的方式编写foreach
echo $(TARGET)
echo $(foreach v, $(TARGET), $v_pdf)
四、makefile中自定义函数
#自定义函数,不是真正的函数;本质上是多行命令,放在外面了
#这里自定义函数,没有返回值
#$(0)表示方法名,从$(1)开始表示入参
.PHONY:show
define FUN
echo $(0) $(1) $(2)
endef
funalias:=$(call FUN)
show:
$(funalias) 9 19
$(call FUN,67,90)
五、makefile嵌套调用
示例1
#-C 指定工作目录
all:
make -C ./cdir
make -C ./cppdir
clean:
make -C ./cdir -f makefile clean
make -C ./cppdir -f makefile clean
示例2
#-C 指定工作目录
.PHONY:cdir cppdir clean
DIR=cdir cppdir
all:$(DIR)
$(DIR):
make -C $@
clean:
$(shell for dir in $(DIR);do make -C $$dir clean;done)