Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running grep from within GNU make

I need to find the text 'ifeq ($(Param1)' using grep. I try to assign search result to make variable. The problem is that single quotes don't escape text in make so when I try:

GrepResult:= $(shell grep 'ifeq ($$(Param1)' TextFile)

I get:

Makefile:214: *** unterminated call to function `shell': missing `)'.  Stop.

The $ can be escaped with $$ but how do I escape parentheses in make? Thanks.

NB: $GrepResult is used in $(error) function, not in a rule command.

like image 484
jackhab Avatar asked Oct 29 '09 14:10

jackhab


1 Answers

The trick is to smuggle the special characters past Make and grep.

GrepResult := ${shell grep 'ifeq (\$$(Param1)' TextFile}

Make turns $$ into $, then grep turns \$ into $. Also note that this assignment uses curly braces "{}", not parentheses "()", so as not to be confused by the results of the match. (There may be a more robust way to handle the string, but never mind.)

When you use the result, use single quotes:

all:
    @echo '$(GrepResult)'

This too was tested with GNUMake 3.81.

EDIT: This also works with $(error...):

    $(error '$(GrepResult)')
like image 154
Beta Avatar answered Oct 06 '22 13:10

Beta