petrelharp
petrelharp

Reputation: 5197

execution within make rule

Is it possible to write a make rule that does not evaluate e.g. $(shell ) expressions until previous operations have been executed?

An artificial example:

all : clean
    echo 'hey' > test
    echo $(shell cat test)

clean :
    rm test

then running make will result in the following error:

cat: test: No such file or directory
echo 

The problem seems to be that make evaluates the shell command before going through the rules, so that test doesn't exist yet. Is there a good way around this?

As background, what I am trying to do is write a make rule that checks out a different branch of a git repository and does things there in a way that requires evaluating e.g. $(shell git ls-files); but git ls-files is evaluated before switching branches.

Upvotes: 0

Views: 33

Answers (1)

tripleee
tripleee

Reputation: 189467

The trivial workaround is to use the shell's own facilities. When executing a rule, you are already running shell commands.

all:
    echo 'hey' >test
    echo $$(cat test)

(Dollar signs meant for the shell need to be doubled, which is an escape to produce a literal dollar sign in Make.)

Obviously that's possible to refactor further, because the echo is useless; but let's consider it as a placeholder for something useful.

Upvotes: 1

Related Questions