ILD

Makefile调用shell函数,并支持多行shell语句
作者:Herbert Yuan 邮箱:yuanjp@hust.edu.cn
发布时间:2018-1-17 站点:Inside Linux Development

GNU Make的shell函数可以调用shell命令。如果shell很长,可以:

1
2
3
4
5
6
define SAVE_MODE
if [ ! -f .oldmodel ] || [ "$(MODEL)" != `cat .oldmodel` ] ; then \
    echo $(MODEL) > .oldmodel ; \
fi
endef
$(shell $(SAVE_MODE))


或者:

1
2
$(shell if [ ! -f .oldmodel ] || [ "$(MODEL)" != `cat .oldmodel` ] ; then \
    echo $(MODEL) > .oldmodel ; fi)


注意:

函数调用的输出可以赋值给变量,如果没有任何赋值或使用,如上,则输出到Makefile作为Makefile内容的一部分。所以如果上述shell有任何输出,将可能导致输出变成Makefile的一部分,这些输出内容可能不是合法的Make语法,此时将报错,解决方法是:

1
2
3
$(info $(shell xxx))
_x := $(shell xxx)
$(shell xxx > /dev/null)

等等。


https://stackoverflow.com/questions/48284432/how-to-break-gnu-make-shell-function-call-to-multiple-line


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.