hlovdal
hlovdal

Reputation: 28180

automate rules in makefile from two variables at the same time

I have some search patterns that fits poorly as part of a file name for the result. I therefore split the regular expression and the corresponding file name part into two different variables like below. How can I automate the following so that I do not have to manually list files for ALL and also do not have to manually enter the rules running grep?

SEARCH_RE   = test  a/b/c  a.*b
SEARCH_FILE = test  abc    ab

ALL = result.test result.abc result.ab

all: $(ALL)

result.test:
        grep test  inputfile > result.test

result.abc:
        grep a/b/c inputfile > result.abc

result.ab
        grep a.*b  inputfile > result.ab

Upvotes: 1

Views: 276

Answers (2)

Beta
Beta

Reputation: 99084

I recommend

ALL = $(addprefix result.,$(SEARCH_FILE))

As for writing the rules, the kind of lookup I think you want can be done in Make, but really shouldn't be-- it would be a horrible kludge. I'd suggest doing it this way:

result.test: TARG = test
result.abc: TARG = a/b/c
result.ab: TARG = a.*b

$(ALL):
    grep $(TARG) inputfile > $@

Upvotes: 1

Douglas Leeder
Douglas Leeder

Reputation: 53310

I don't know how to create the rules, but the ALL target is easy enough:

ALL = $(patsubst %,result.%,$(SEARCH_FILE))

Upvotes: 1

Related Questions