Inside the Makefile of a project you can find a .PHONY element followed by a label and then the same label used as a target for the Makefile.
For example:
.PHONY: build
build:
go build
.PHONY is actually itself a target for the make commandand and it denotes labels that do not represent files of the project. What does it mean?
In the general case, assuming that there are no files called build in the project, when not using the .PHONY target, like so
build:
go build
everytime the make build command is invoked it will execute the specified command (in this case go build). However, if there was a file named build, the make build would be bound by the presence of this file and executed only if that file was updated.
The .PHONY target overrides this last behaviour and let's the make build execute no matter of the presence of a file named build.
For a more in-depth overview head to the make command reference.
Top comments (4)
Actualy the better and simplier explanation about .PHONY directive.
Simple and easier to understand.
Why would you ever use
makein modern project.... anything seems like a better choice?Common build tool against multiple languages in a polyglot development environment?
Anything else would likely come with extra baggage / installation required. Being able to know you download any codebase, without even knowing the language or framework it's written in, and run
maketo get the codebase into a runnable state ormake cleanto remove all the transitory files and get back to a clean copy, can make that first engagement much easier.If you have experience with the underlying language / framework, you can always sidestep make and run the underlying commands it's calling (rake, npm, yarn etc...) but providing a top level file that works for everyone, because everyone has make installed, is pretty powerful.
Plus if used for the right category of task, I'm not sure if other tools offer anything make doesn't already do effectively, with very little extra baggage than the commands (shell script or lang-specific tools) it wraps.