>_ IzzyLab

Hands-on task

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

Order-only prerequisite: a directory for artifacts

hard~10 min

Build artifacts should live in an out/ subdirectory that doesn't exist yet. Write a Makefile with an out/app target that builds app from main.c (cp) into out/. Create the out directory automatically, but declare it as an order-only prerequisite (after the | symbol) — so that changes to the directory's timestamp don't trigger needless rebuilds of out/app. The Makefile is handy to write in the Editor tab above the terminal: in a Makefile it puts a real TAB at the start of a recipe line.

What you'll practise:
  • make
  • order-only prerequisites
  • mtime
  • artifact directory

Topic breakdown

A regular prerequisite makes make look at the modification time of the prerequisite, but a directory's mtime changes on any edit to its contents — which would make the target constantly appear out of date. For such cases there are order-only prerequisites: everything after the | symbol is only guaranteed by make to exist, but its timestamp is not considered when deciding whether to rebuild. This is the standard trick for automatically creating an artifact directory without provoking needless rebuilds.

What's checked

  1. out/app is built inside the out directory
  2. changing the directory's timestamp doesn't rebuild out/app
  3. the out directory is declared order-only

What it looks like