>_ IzzyLab

Hands-on task

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

A default value with override

medium~7 min

In /root/proj there is src.txt. Write a Makefile where the name of the result is set by a variable NAME with a default value app, but in such a way that it can be overridden both from the command line and from the environment. The build target copies src.txt into the file $(NAME). A bare make build creates app, while make NAME=prog build and NAME=prog make build create prog.

What you'll practise:
  • make
  • make variables
  • conditional assignment
  • variable priority

Topic breakdown

make variables form a priority hierarchy: a value passed in the command's arguments (make VAR=value) overrides the one assigned inside the Makefile. To set a value "by default, if nothing came from outside", the conditional-assignment operator is used, which triggers only for a variable that isn't set yet. In the recipe the name is substituted via $(...), so the same target builds a file with a different name depending on the invocation.

What's checked

  1. by default app is created
  2. make NAME=prog creates prog
  3. NAME=prog from the environment creates prog

What it looks like