Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using `date` in Makefile variable

Using GNU Make 4.1, in my Makefile I can create a timestamp log file with this::

flush_log:
        @echo "==> flush logs"
        cat file > file_`date +%FT%T%Z`.log

But who to put:

file_`date +%FT%T%Z`.log

in a var inside the Makefile e.g to make a wc on it ?

I've try (with no success):

flush_log:
    @echo "==> flush logs"
    logfile:=file_$(shell date +%FT%T%Z).log
    cat my_log > $(logfile)
    echo `wc -l $(logfile)`

I get this error:

$ make flush_log
==> flush logs
logfile:=file_2016-12-24T20:09:52CET.log
/bin/sh: 1: logfile:=file_2016-12-24T20:09:52CET.log: not found
Makefile:7: recipe for target 'flush_log' failed
make: *** [flush_log] Error 127
$

I was following recommendation from https://stackoverflow.com/a/14939434/3313834 and Simply expanded variables https://www.gnu.org/software/make/manual/html_node/Flavors.html#Flavors

like image 615
user3313834 Avatar asked Dec 24 '16 19:12

user3313834


1 Answers

Every line in a makefile (that appears after a rule statement) that is indented with a TAB character is part of the rule's recipe. Lines in the rule's recipe are passed to the shell for handling, they're not parsed by make (except to expand variable/function references).

So your line logfile:=file... is being passed to the shell and being interpreted by the shell... and there is no valid := operator in the shell, so the shell thinks that entire line is a single word and tries to run a program with that name, which obviously doesn't exist.

You probably want to create a make variable, which must be done outside of the recipe, like this:

logfile := file_$(shell date +%FT%T%Z).log
flush_log:
        @echo "==> flush logs"
        cat my_log > $(logfile)
        echo `wc -l $(logfile)`
like image 186
MadScientist Avatar answered Oct 03 '22 05:10

MadScientist