2

How do I write a Makefile that is able to store a dynamic value inside a static variable that can be reused throughout the make command? On Mac OS X, when I run make I have a problem where I can't set variables following instructions in the GNU docs.

Setting vars doesn't work as expected

A simple Makefile looks like this.

announce:
        starttime := `date`
        echo The time is now $(starttime)

When I run make announce I get the following error.

/bin/sh: starttime: command not found
make: *** [announce] Error 127

eval workaround tried

I've tried the solution offered in a previous thread, to use $(eval myvar = "some value or expr"). However, what I found is that this approach makes the variable dynamically calculated every time it's used. So, given the Makefile below, I expect the same time to be printed twice.

announce:
        $(eval starttime = `date`)
        echo The time is now $(starttime)
        sleep 3
        echo The time is now $(starttime)

But in reality, two different times will be printed instead of one consistent time.

Martys-MacBook-Air:express-babel-eb marty$ make announce
echo The time is now `date`
The time is now Thu Nov 3 18:27:39 EDT 2016
sleep 3
echo The time is now `date`
The time is now Thu Nov 3 18:27:43 EDT 2016
Community
  • 1
  • 1
Marty Chang
  • 6,269
  • 5
  • 16
  • 25

1 Answers1

1

Okay, reading more into the docs and experimenting, I found that shell does what I want it to do. The following Makefile can be used to echo the same timestamped value twice, as desired.

githash := $(shell git rev-parse --short HEAD)
timestamp := $(shell date +%s)
envname := markable-$(githash)-$(timestamp)

announce:
        echo $(envname)
        sleep 3
        echo $(envname)
Marty Chang
  • 6,269
  • 5
  • 16
  • 25