If really all targets are PHONY, this is a bit pointless. make
is meant for "do what is necessary to update my output", and if the answer to what is necessary? is always the same, simple scripts would do the same job with less overhead.
That being said, I could imagine a situation where all targets intended to be given at the commandline should be PHONY -- Let's assume you want to build some documents like this:
all: doc1.pdf doc2.pdf doc3.pdf doc4.pdf doc5.pdf
manuals: doc3.pdf doc4.pdf
faqs: doc1.pdf
designdocs: doc2.pdf
apidocs: doc5.pdf
developerdocs: doc2.pdf doc5.pdf
userdocs: doc1.pdf doc3.pdf doc4.pdf
%.pdf: %.md
$(somedocgenerator) -o $@ $<
Then you could do the following:
.PHONY: all $(MAKECMDGOALS)
This would dynamically make any target given at the command line PHONY. Note you have to include your default target here as this could be invoked without giving it explicitly, by simply typing make
.
I would not recommend this approach because it has two drawbacks:
- It's less portable to different flavors of
make
.
- It will misbehave if someone decides to
make
a specific output file like here make doc3.pdf
.
- It will also misbehave if you decide to have one of your PHONY targets depend on another PHONY one.
So, better go with the approach to have one line declaring all the PHONY targets. If your Makefile
is really huge, you could split it in several files and use include
-- and have one .PHONY:
line in each file.
make
is not the correct tool because it has no advantage whatsoever over a plain script....PHONY: $(MAKECMDGOALS)
would make exactly the targets given at the command-line phony, which would work for you if none of your targets depend on other targets. – Opulentcase
statement and functions in scripts, do you? The whole idea aboutmake
is that it decides based on timestamps, so if all your targets are PHONY,make
is pointless. – Opulent.PHONY
for exactly what it was intended?-which is to trigger a target every time, regardless of whether or not it's out of date. Regarding the "whole idea" of Make, you forgot to mention the other part of the "whole idea"… dependency management. Which, in my case, is why I prefer to use Make than rewriting (and debugging) dependency logic in a shell script. – Frizzellmake
. Sure it works, it's just like using pincers to pull a screw. – Opulentmake
has all sorts of bells and whistles to support a real building process based on file timestamps. This goes to great lengths withGNU make
. It just doesn't make sense to even depend onmake
here. – Opulentmake
to setup an application that relies on some dozen microservices running on docker containers. I want to be able to start each service individually, so during development, I can just start one of them, develop something and test, without running the whole application. But the services have some common dependencies, and declaring them asmake
targets make it very convenient to start any number of services, being sure that all the dependencies are ran in the right order, and only once. – Kidd.PHONY: %
work in this situation? IIRC%
matches all targets when used as a dependency. You could put that at the bottom of each Makefile. – Phalansterianjust
. It's similar tomake
but all the rules are PHONY by default. github.com/casey/just – Partial.PHONY: %
does not work for me, neither at the top nor at the bottom of a file. – Inflight.PHONY
right before each target is the best way. Wildcards like.PHONY: *
are good for DRY but will become useless if one day you will need non-phony target. And between DRY and flexibility I would choose flexibility. – Mesenchyme