paint-brush
What Does .PHONY Do in a Makefile?by@alexandrunastase
7,205 reads
7,205 reads

What Does .PHONY Do in a Makefile?

by Alexandru Nastase
Alexandru Nastase HackerNoon profile picture

Alexandru Nastase

@alexandrunastase

Sr. Software Engineer ∙ Open Source Enthusiast ∙ Lifelong Learner

February 9th, 2023
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The.PHONY directive is used to mark a target as phony. That means a target that doesn't take into consideration for execution any file that matches its name. Let's say we have a target called `clear-cache', which removes the cache of an application.
featured image - What Does .PHONY Do in a Makefile?
1x
Read by Dr. One voice-avatar

Listen to this story

Alexandru Nastase HackerNoon profile picture
Alexandru Nastase

Alexandru Nastase

@alexandrunastase

Sr. Software Engineer ∙ Open Source Enthusiast ∙ Lifelong Learner

Learn More
LEARN MORE ABOUT @ALEXANDRUNASTASE'S
EXPERTISE AND PLACE ON THE INTERNET.


What does .PHONY actually do?


.PHONY is used to mark a target as phony. That means a target that doesn't take into consideration for execution any file that matches its name.


Let's say we have a target called clear-cache, which removes the cache of an application

clear-cache:
    rm -rf cache/*


Which results in the following command:

command line result 1

command line result 1

The cache removal cli command will run fine most of the time, but let's check what happens if we add a file with the same name as the target at the same level as the Makefile


command line result 2

command line result 2


Now when running make clear-cache again we get:


command line result 3

command line result 3


The Makefile says basically that it has the target file already and doesn't need to execute.

This default behavior is not what we expect in this case and we want to override it. This is when the .PHONY directive comes to the rescue.


Let's update our example to use it:

.PHONY: clear-cache
clear-cache:
    rm -rf cache/*


After marking the target as phony, the command behaves as expected

command line result 4

command line result 4


Pro tip: Another way to mark the targets as phony is to have them all in a list rather than above each of them like:


clear-cache:
    rm -rf cache/*
clear-build:
    rm -rf build/*

.PHONY: clear-cache clear-build


Conclusion

In order to override the default behavior in Makefile and use some targets like command runners you can use .PHONY.


References:



Also published here.

L O A D I N G
. . . comments & more!

About Author

Alexandru Nastase HackerNoon profile picture
Alexandru Nastase@alexandrunastase
Sr. Software Engineer ∙ Open Source Enthusiast ∙ Lifelong Learner

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Also published here
Coffee-web
Unni
Hashnode
Learnrepo
Coffee-web
Hashnode
X REMOVE AD