Error in Makefile with conditional use in the definition used in commands part of the rule

I have a makefile section that has this structure:

bob: ifdef DEBUG @echo running endif @echo chug chug chug ifdef DEBUG @echo done endif bobit: @echo "before" @make bob @echo "after" 

I greatly simplify here, all echoes are actually non-trivial blocks of commands, and there is more conditional material, but this reflects the essence of my problem.

For technical reasons, I donโ€™t want to go into it right now, I need to get rid of this fake, but since the echo is a non-trivial amount of code, I donโ€™t just want to copy and drive the beanโ€™s body to its place of feeding.

Ideally, what I would like to do is something like this

 define BOB_BODY ifdef DEBUG @echo running endif @echo chug chug chug ifdef DEBUG @echo done endif endef bob: $(BOB_BODY) bobit: @echo "before" $(BOB_BODY) @echo "after" 

Unfortunately, conditional expressions seem to throw me in, they produce "ifdef: Command not found" errors, I tried to get around this with various combinations of eval and call, but it can't seem to work.

How do I do this job? and is this even the right way to approach the problem?

+4
source share
2 answers

The way I fixed this is to use bash conventions instead, which actually makes some sense as we play with teams and not make rules.

So, my perfect solution from above becomes something like

 define BOB_BODY @if [[ -n "$(DEBUG)" ]]; then \ echo running; \ fi; @echo chug chug chug @if [[ -n "$(DEBUG)" ]]; then \ echo done; \ fi endef 

bob: $(BOB_BODY)

bobit: @echo "before" $(BOB_BODY) @echo "after"

+2
source

You can simply reorder the ifdef / define:

 ifdef DEBUG define BOB_BODY @echo running @echo chug chug chug @echo done endef else define BOB_BODY @echo chug chug chug endef endif 

UPDATE

 define CHUG @echo chug chug chug endef ifdef DEBUG define BOB_BODY @echo running $(CHUG) @echo done endef else define BOB_BODY $(CHUG) endef endif 
0
source

All Articles