Eric J Ma's Website

Phony targets in Makefiles

written by Eric J. Ma on 2021-03-04 | tags: automation computation reproducibility


Makefiles can be used to create a lightweight CLI entry point for your project. By writing a Makefile, you can automate repetitive shell commands, such as building a Dockerfile (which might require you to remember certain incantations):

.PHONY: container

container:
    docker build -t mycontainer -f Dockerfile .

Doing so allows us to build a Docker container using the command make container when at the command line.

Now, you'll probably notice the .PHONY line at the top. What's that all about?

By convention, the command container would have referred to a file target on disk. (This is how Makefiles were originally designed.) However, if we want the command to be independent of any files on disk, we declare it to be a phony target that doesn't actually exist on disk.


Cite this blog post:
@article{
    ericmjl-2021-phony-makefiles,
    author = {Eric J. Ma},
    title = {Phony targets in Makefiles},
    year = {2021},
    month = {03},
    day = {04},
    howpublished = {\url{https://ericmjl.github.io}},
    journal = {Eric J. Ma's Blog},
    url = {https://ericmjl.github.io/blog/2021/3/4/phony-targets-in-makefiles},
}
  

I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.

If you would like to sponsor the coffee that goes into making my posts, please consider GitHub Sponsors!

Finally, I do free 30-minute GenAI strategy calls for teams that are looking to leverage GenAI for maximum impact. Consider booking a call on Calendly if you're interested!