Makefile 食谱中的日期
Date in Makefile recipes
我想创建带有时间戳的日志文件。在我的 Makefile 中,我得到如下日期:
DATE = $(shell date '+%Y-%m-%d-%H:%M:%S')
在我的食谱中,我对日志文件做了几个 grep:
my_long_run:
cd $(MYDIR); Running a tool for a long time | tee -i ./Log/my_long_run_log.$(DATE)
@sleep 2
@grep -iq "Status of the Job:0" Log/my_long_run_log.$(DATE)
@if [ ! -z "$(SOME_ENV_VAR)" ]; then ln -fs $(ANOTHER_ENV_VAR)/my_generate_file; fi; \
echo "$$dependencyTree" > $@; \
echo -e "\nWarning Summary:" >> $@; \
if grep -iq "warn.*total" Log/my_long_run_log.$(DATE) ; then grep -i "warn.*total" Log/my_long_run_log.$(DATE) >> $@; fi; \
if grep -iq "warn.*check" Log/my_long_run_log.$(DATE) ; then grep -i "warn.*check" Log/my_long_run_log.$(DATE) >> $@; fi; \
echo -e "\nError Summary:" >> $@; \
if grep -iq "error.*total" Log/my_long_run_log.$(DATE) ; then grep -i "error.*total" Log/my_long_run_log.$(DATE) >> $@; fi; \
if grep -iq "error.*check" Log/my_long_run_log.$(DATE) ; then grep -i "error.*check" Log/my_long_run_log.$(DATE) >> $@; fi;
问题是:
在第一行,日期扩展为:2020-02-26-09:14:09。
但是 grep 命令查找带有时间戳的文件:2020-02-26-09:14:10
grep: Log/my_long_run_log.2020-02-26-09:14:10: 没有那个文件或目录
gmake: *** [my_long_run] 错误 2。
然后我当然需要重复此 运行,因为这个 grep 失败 运行 时间为 6-7 小时。
sleep 2
是故意的。由于我的服务器有些滞后,文件需要一段时间才能显示。但是,据我所知,Makefile 在执行命令之前会扩展所有的配方。为什么 $(DATE) 对一个食谱有两个值?
抱歉,没有提供实际的 MWE,而是抽象的。感谢任何帮助。
您需要使用 :=
来定义您的变量,而不是 =
。使用 =
时,每次引用时都会对其求值,而使用 :=
时,仅在您定义它时才求值,稍后会使用它的值。
这在 GNU make's manual: The Two Flavors of Variables 中有解释,其中用 :=
定义的变量称为 simply expanded variables
,用 =
定义的变量称为 recursively expanded variables
。
The value of a simply expanded variable is scanned once and for all, expanding any references to other variables and functions, when the variable is defined.
您的 Makefile 将如下所示:
DATE := $(shell date '+%Y-%m-%d-%H:%M:%S')
我想创建带有时间戳的日志文件。在我的 Makefile 中,我得到如下日期:
DATE = $(shell date '+%Y-%m-%d-%H:%M:%S')
在我的食谱中,我对日志文件做了几个 grep:
my_long_run:
cd $(MYDIR); Running a tool for a long time | tee -i ./Log/my_long_run_log.$(DATE)
@sleep 2
@grep -iq "Status of the Job:0" Log/my_long_run_log.$(DATE)
@if [ ! -z "$(SOME_ENV_VAR)" ]; then ln -fs $(ANOTHER_ENV_VAR)/my_generate_file; fi; \
echo "$$dependencyTree" > $@; \
echo -e "\nWarning Summary:" >> $@; \
if grep -iq "warn.*total" Log/my_long_run_log.$(DATE) ; then grep -i "warn.*total" Log/my_long_run_log.$(DATE) >> $@; fi; \
if grep -iq "warn.*check" Log/my_long_run_log.$(DATE) ; then grep -i "warn.*check" Log/my_long_run_log.$(DATE) >> $@; fi; \
echo -e "\nError Summary:" >> $@; \
if grep -iq "error.*total" Log/my_long_run_log.$(DATE) ; then grep -i "error.*total" Log/my_long_run_log.$(DATE) >> $@; fi; \
if grep -iq "error.*check" Log/my_long_run_log.$(DATE) ; then grep -i "error.*check" Log/my_long_run_log.$(DATE) >> $@; fi;
问题是: 在第一行,日期扩展为:2020-02-26-09:14:09。 但是 grep 命令查找带有时间戳的文件:2020-02-26-09:14:10 grep: Log/my_long_run_log.2020-02-26-09:14:10: 没有那个文件或目录 gmake: *** [my_long_run] 错误 2。 然后我当然需要重复此 运行,因为这个 grep 失败 运行 时间为 6-7 小时。
sleep 2
是故意的。由于我的服务器有些滞后,文件需要一段时间才能显示。但是,据我所知,Makefile 在执行命令之前会扩展所有的配方。为什么 $(DATE) 对一个食谱有两个值? 抱歉,没有提供实际的 MWE,而是抽象的。感谢任何帮助。
您需要使用 :=
来定义您的变量,而不是 =
。使用 =
时,每次引用时都会对其求值,而使用 :=
时,仅在您定义它时才求值,稍后会使用它的值。
这在 GNU make's manual: The Two Flavors of Variables 中有解释,其中用 :=
定义的变量称为 simply expanded variables
,用 =
定义的变量称为 recursively expanded variables
。
The value of a simply expanded variable is scanned once and for all, expanding any references to other variables and functions, when the variable is defined.
您的 Makefile 将如下所示:
DATE := $(shell date '+%Y-%m-%d-%H:%M:%S')