TheNumber23
TheNumber23

Reputation: 383

Conditional Dependencies in GNU Make

I would like to have my target have conditional dependencies. Below is an example that doesn't work

everything: foo bar \
ifndef EXTRA
  biz baz
endif
    recipe_to_do_stuff 

So if I run make it will make everything with all the dependencies foo bar biz baz. But if I ran make EXTRA=true it would make everything with only foo bar.

Is this a possibility? I could have conditionals that run two separate commands but my target has lots of possible dependencies and I don't want to have two places to change if they need updates. Thanks in advance.

Upvotes: 8

Views: 5608

Answers (3)

LeoRochael
LeoRochael

Reputation: 15187

You can define dependencies in multiple lines by repeating the target name at the beginning of the line:

# Makefile
baz:
    @echo baz

bar:
    @echo bar

foo: bar
foo: baz

foo:
    @echo foo
$ make foo
bar
baz
foo

Hence, you can ifdef around one or more of these dependency lines:

# Makefile
baz:
    @echo baz

bar:
    @echo bar

foo: bar

ifdef DO_BAZ
foo: baz
endif

foo:
    @echo foo

And define the variable if you want the extra dependencies:

$ make foo
bar
foo
$ make foo DO_BAZ=1
bar
baz
foo

Upvotes: 5

TheNumber23
TheNumber23

Reputation: 383

This was my eventual answer to have an inline solution.

everything: foo bar $(if $(EXTRA), biz baz)
    recipe_to_do_stuff 

Upvotes: 11

uzsolt
uzsolt

Reputation: 6037

This should work.

ifndef EXTRA
  REQ=biz baz
endif

everything: foo bar ${REQ}
    recipe_to_do_stuff

Upvotes: 0

Related Questions