Cyan
Cyan

Reputation: 13948

Makefile : .PHONY on multiple targets

In a well refined Makefile, it's recommended to mark non-generated targets as .PHONY.

Now, the process is pretty simple : suppose there is target1 that must be run any time it's invoked, irrespective of any previous build, then just :

.PHONY : target1
target1 :
<TAB> ...recipe...

Ok, what about 2 targets? simple enough:

.PHONY : target1
target1 :
<TAB> ...recipe...

.PHONY : target2
target2 :
<TAB> ...recipe...

etc.

What about 100 targets ? Well, just repeat the process.

So, yeah, it works, but it's annoying. It adds one line in front of each target declaration. That's fine for a few lines, but when there is a large section of the Makefile which consist solely of .PHONY targets, one has to wonder if it could be done in a more efficient way, both reducing the risk of forgetting a .PHONY statement, and improving Makefile readability.

So, for example, let's say the bottom half of the Makefile consists solely of .PHONY targets, all of the form : test-something.

Attempted : .PHONY : test-% but it did not work.

Looking around for documentation, and there is simply no example anywhere of such kind of "mass .PHONYfication". All targets are always expressly named. I vaguely remember a statement about .PHONY being incompatible with wildcard characters, though make doesn't flag that as an error.

The question is : is it possible to mass .PHONY the bottom half of the Makefile, by taking advantage either of the stable name scheme (test-%), or the position of targets in the file ?

Upvotes: 2

Views: 1592

Answers (1)

John Bollinger
John Bollinger

Reputation: 180181

The question is : is it possible to mass .PHONY the bottom half of the Makefile, by taking advantage either of the stable name scheme (test-%), or the position of targets in the file ?

No.

make does not have general-purpose wildcards. % has a special interpretation in GNU-make pattern rules (among other places), but a rule for .PHONY cannot be a pattern rule, because those require the target name to contain a %.

Also make has no mechanism for distinguishing between or selecting targets by their position in the file, except with respect to choosing the default target.

But if you are willing to depend specifically on GNU make (which your references to .PHONY and % suggest) then you can do at least a little better than what is described in the question. For example, define a variable containing the varying stems of your target names, and expand that to a list of prerequisites for .PHONY:

TESTS = something1 something2 something3

# Declares targets test-something1 test-something2 test-something3
# to be phony
.PHONY: $(patsubst %,test-%,$(TESTS))

If it happens that the recipes for those targets all have the same form, and it is computable from the selected target names, then you could also write a single rule for all of them. As a (probably too) simple example, maybe something like this:

$(patsubst %,test-%,$(TESTS)):
        ./runtest $@

But in the latter case, you can also do away with the $(patsubst) altogether, and just specify the target names you want in full in the controlling variable, since that is the only place they appear in the makefile.

Upvotes: 3

Related Questions