itaysk
itaysk

Reputation: 6226

makefile rules precedence for similar file names

I have a makefile similar to the following:

dist/myapp_darwin.tar.gz: release

myapp_%: $(SRC)
  recipe1

release:
  recipe2

sometarget: dist/myapp_darwin.tar.gz
  recipe3

I'm running make sometarget, which requires dist/myapp_darwin.tar.gz. I expect this requirement to be satisfied using recipe2 (release target) thanks to the specific target in the first line, but it is being built using recipe1. what am I doing wrong, and how can I fix this?

(just for context the myapp_% target is meant to build a binary in the root directory called myapp_darwin, and the dist/myapp_darwin.tar.gz is meant to build a different artifact, an archive for distribution.)

Upvotes: 0

Views: 152

Answers (1)

MadScientist
MadScientist

Reputation: 100781

The problem is make always wants a recipe for each target. If you write this with no recipe associated with it:

dist/myapp_darwin.tar.gz: release

that's not defining a rule for the target dist/myapp_darwin.tar.gz, all that's doing is adding a new prerequisite release to that target. Make will still have to search its rules database to find a rule to build that target with, and it will match the pattern rule.

So, it will run both recipe2 then recipe1.

If you want to declare an explicit rule for the target then you have to give it a recipe, even if it's a no-op:

dist/myapp_darwin.tar.gz: release
         @:

Now make has a recipe for this target and it won't need to go looking for one via implicit rule search.

Upvotes: 1

Related Questions