views:

159

answers:

3

I'm looking for a way to bail out of a makefile if a certain string is not found when checking the version of a tool.

The grep expression I'm looking to match is:

dplus -VV | grep 'build date and time: Nov  1 2009 19:31:28'

which returns a matching line if the proper version of dplus is installed.

How do I work a conditional into my makefile based upon this expression?

+2  A: 

Here is one way:

.PHONY: check_dplus

check_dplus:
    dplus -VV | grep -q "build date and time: Nov  1 2009 19:31:28"

If grep finds no match, it should give

make: *** [check_dplus] Error 1

Then have your other targets depend on the check_dplus target.

retracile
Upvoted... but what is the purpose of the `|| exit 1` at the end?
Dave Hinton
@Dave: Uhhh.... excellent question, it's a wee bit redundant, isn't it. Don't know what I was thinking; fixed, and thanks.
retracile
A: 

If this is gnu make, you can do

 your-target: $(objects)
     ifeq (your-condition)
         do-something
     else
         do-something-else
     endif

See here for Makefile contionals

If your make doesn't support conditionals, you can always do

 your-target:
     dplus -VV | grep -q "build date and time: Nov  1 2009 19:31:28" || $(MAKE) -s another-target; exit 0
     do-something

 another-target:
     do-something-else
Davide
+2  A: 

Here's another way that works in GNU Make:

DPLUSVERSION = $(shell dplus -VV | grep 'build date and time: Nov  1 2009 19:31:28')

target_of_interest:
    do_things
ifeq ($(DPLUSVERSION),)
    $(error proper version of dplus not installed)
endif
    do_more_things

This target can be something real, or just a PHONY target on which the real ones depend.

Beta
Worked a charm, and the ifeq...$(error...) let me spew an error message letting the developer know why their build was killed.
mLewisLogic