Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 915
TheNumber23 Avatar asked Mar 03 '23 21:03

TheNumber23


2 Answers

This was my eventual answer to have an inline solution.

everything: foo bar $(if $(EXTRA), biz baz)
    recipe_to_do_stuff 
like image 170
TheNumber23 Avatar answered Mar 05 '23 18:03

TheNumber23


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
like image 29
LeoRochael Avatar answered Mar 05 '23 17:03

LeoRochael