【问题标题】:How to set Makefile variable and make on Mac OS X?如何在 Mac OS X 上设置 Makefile 变量和 make?
【发布时间】:2016-11-03 22:35:14
【问题描述】:

如何编写一个能够将动态值存储在静态变量中的 Makefile,该静态变量可以在整个make 命令中重复使用?在 Mac OS X 上,当我运行 make 时,我遇到了一个问题,我无法按照 the GNU docs 中的说明设置变量。

设置变量不能按预期工作

一个简单的 Makefile 如下所示。

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

当我运行 make announce 时,出现以下错误。

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

eval 尝试了解决方法

我尝试了a previous thread 中提供的解决方案,以使用$(eval myvar = "some value or expr")。但是,我发现这种方法使变量在每次使用时都动态计算。因此,鉴于下面的 Makefile,我希望将同一时间打印两次。

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

但实际上,将打印两个不同的时间,而不是一个一致的时间。

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

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    好的,reading more into the docs 和实验,我发现 shell 做了我想做的事。以下 Makefile 可用于根据需要将相同的时间戳值回显两次。

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

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 2017-07-08
      • 2013-11-27
      • 2011-11-22
      • 2014-05-15
      • 2012-03-15
      • 2015-11-28
      相关资源
      最近更新 更多