如何根据Makefile目标命令中的参数有条件地添加"--flag value"



考虑2个makefile目标触发器:

make print  # Print everything
make print filter=topic-a  # Print only topic-a

现在,在Makefile目标中,filter功能是通过一些命令的标志实现的,比如:

print:
some_command --arg --anotherarg 
--filter <filter>

在某些情况下,该命令不能很好地处理--filter,因此问题是.

问题

如何根据参数是否已传递给make本身(make print filter=topic-a(,在Makefile目标内有条件地添加/删除--filter <filter

这可以通过条件语句的make函数来实现(https://www.gnu.org/software/make/manual/html_node/Conditional-Functions.html):

print:
some_command --arg --anotherarg 
$(if $(filter),--filter $(filter),)

内联条件表达式的工作方式如下:

$(if condition,then-part[,else-part])

注意如何评估condition

如果它扩展到任何非空字符串,则条件被认为是true。如果它扩展为一个空字符串,则该条件被认为是false。

最新更新