>_ IzzyLab

Hands-on task

This task is step 5 of the course “Make: builds and automation”

Make clean work via .PHONY

medium~7 min

In /root/proj there is a build artifact build.out and — as it happens — a regular file named clean. Write a Makefile with a clean target that removes build.out. The catch: because a file named clean sits right there, a naive clean target will be considered "already built" and the recipe won't run. Make it so that make clean always runs and removes build.out, despite the same-named file.

What you'll practise:
  • make
  • .PHONY
  • phony targets
  • makefile
  • target name as file

Topic breakdown

make treats a target name as a filename: if a same-named file sits next to it and isn't "out of date" relative to its dependencies, make considers the target built, says up to date, and doesn't run the recipe. Utility targets that don't produce a same-named file (clean, all, test) may therefore silently fail to run. The cure is to declare them as a special pseudo-target that marks the target "always run the recipe" regardless of any same-named file.

What's checked

  1. make clean removes build.out regardless of file dates
  2. the clean file is still there

What it looks like