开发者

How to log time stamp in Makefile before every line executes under target

My make file target has several steps. Example below

do-something:
    cat Dockerfile
    ls

I want to display time stamp before invoking a command. I could like below. Is there a better and concise way?

do-something:
    echo $(shell date -u +'%d-%m-%Y %H:%M:%S:%N') calling step1
    cat Dockerfile
    echo $(shell date -u +'%d-%m-%Y %H:%M:%S:%N') calling step2
    ls
    echo $(shell date -u +'%d-%m-%Y %H:%M:%S:%N') Done

Answer:

I could build my solution using the answers开发者_高级运维 below.

logLine = @date -u +'%d-%m-%Y %H:%M:%S:%N $1'

do-something:
    $(call logLine, calling step1)
    cat Dockerfile
    @date -u +'%d-%m-%Y %H:%M:%S:%N calling step2'
    ls
    $(call logLine, Done)


Using $(shell ...) in make recipes is almost always wrong because make recipes are already shell scripts and because of the time at which $(shell ...) is expanded by make.

Instead you could simply use:

do-something:
    date -u +'%d-%m-%Y %H:%M:%S:%N calling step1'
    cat Dockerfile
    date -u +'%d-%m-%Y %H:%M:%S:%N calling step2'
    ls
    date -u +'%d-%m-%Y %H:%M:%S:%N Done'

And to simplify a bit you could define a macro:

date = date -u +'%d-%m-%Y %H:%M:%S:%N $1'

do-something:
    $(call date,calling step1)
    cat Dockerfile
    $(call date,calling step2)
    ls
    $(call date,Done)


Is there a better and concise way?

Yes, you don't need (and really shouldn't) call the shell when parsing the makefile (which is what $(shell ...) does):

do-something:
    date -u +'%d-%m-%Y %H:%M:%S:%N'
    cat Dockerfile
    date -u +'%d-%m-%Y %H:%M:%S:%N'
    ls
    date -u +'%d-%m-%Y %H:%M:%S:%N'

Moreover you can have the date command in a variable:

ECHO_TIMESTAMP := date -u +'%d-%m-%Y %H:%M:%S:%N'

do-something:
    $(ECHO_TIMESTAMP)
    cat Dockerfile
    $(ECHO_TIMESTAMP)
    ls
    $(ECHO_TIMESTAMP)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜