Mike Lewis
Mike Lewis

Reputation: 734

How to conditionalize a makefile based upon grep results?

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?

Upvotes: 15

Views: 18659

Answers (3)

Beta
Beta

Reputation: 99104

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 do_things_that_uses_dplus

do_things:
    ...


do_things_that_uses_dplus:
ifeq ($(DPLUSVERSION),)
    $(error proper version of dplus not installed)
endif
    ...

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

Upvotes: 17

retracile
retracile

Reputation: 12339

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.

Upvotes: 3

Davide
Davide

Reputation: 17818

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

Upvotes: 2

Related Questions