首页 > 解决方案 > 如何根据 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,所以问题是..

问题

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

标签: bashmakefile

解决方案


这可以通过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评估:

如果它扩展为任何非空字符串,则该条件被认为是真的。如果它扩展为空字符串,则该条件被认为是错误的。


推荐阅读