Print timestamp in makefile

前提是你 提交于 2020-01-17 01:30:10

问题


I want to measure the time for each of build item in make file, i just try below, why does it not work?

mytest:
    $(info 'Now time is $(date --iso=seconds)')

The date is not printed, just printed

'Now time is '
. What can be wrong?

make --version GNU Make 3.81


回答1:


There is no make function named date. If you want to invoke a shell command, the syntax is $(shell date).

Using $(info) in a recipe is not particularly elegant; the function doesn't produce anything which is useful to pass to a shell. You are probably looking simply for

mytest:
    date +"Now time is +%FT%T%z"

(Could not find --iso-seconds documented properly; grabbed the defintion from this blog: http://nixscripts.blogspot.com/2010/07/hidden-arguments-easter-egg-or-what.html)

... or perhaps somewhat less efficiently

mytest:
    printf 'Now time is %s\n' "$$(date --iso-seconds)"

where the double dollar sign escapes the dollar sign from Make so that you pass through a command substitution to the shell.

Just to point out the obvious, it is unfortunate that Make uses a syntax which is so similar to the shell's command substitution syntax. Inside Make, you use $(shell command) or $$(command) to pass command to the shell. The former makes sense in pure Make snippets (variable definitions in the Makefile proper, etc); the latter is only available in recipes.



来源:https://stackoverflow.com/questions/37722572/print-timestamp-in-makefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!