>_ IzzyLab

Hands-on task

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

Build a report from all logs via wildcard

medium~7 min

In /root/proj there are several *.log files (you don't know in advance how many or what they're called). Write a Makefile with a report.txt target that gathers all the lines from all the *.log files into one sorted file report.txt. Don't hardcode the list of logs — pick it up with the $(wildcard *.log) function, and make report.txt depend on those logs. Bare make must build report.txt. 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
  • wildcard
  • makefile variables
  • target dependencies

Topic breakdown

The $(wildcard pattern) function expands a mask into the list of files that actually exist at the moment the Makefile is read — a way to avoid hardcoding names and pick up everything matching the pattern. By storing the result in a variable and substituting it into a target's dependency list, you make make rebuild the target when any of those files changes. A variable is referenced as $(NAME), and the recipe (with its line on a TAB) then builds the required artifact from those sources.

What's checked

  1. make builds report.txt
  2. report.txt = sorted logs
  3. a new log is picked up automatically

What it looks like