andywineio
andywineio

Reputation: 610

How to add `--flag value` conditionally based on a parameter in a Makefile target command?

Consider 2 makefile target triggers:

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

Now, inside Makefile target, the filter functionality is realised via some command's flag, like this:

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

In some cases, the command doesn't handle an empty --filter very well, so the question is..

Question

How to add/remove --filter <filter conditionally inside Makefile target based on whether the param has been passed to the make itself (make print filter=topic-a)?

Upvotes: 2

Views: 1524

Answers (1)

andywineio
andywineio

Reputation: 610

This can be achieved with make functions for conditionals (https://www.gnu.org/software/make/manual/html_node/Conditional-Functions.html):

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

An inline conditional expression works like this:

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

Note how the condition is evaluated:

If it expands to any non-empty string, then the condition is considered to be true. If it expands to an empty string, the condition is considered to be false.

Upvotes: 2

Related Questions