首页 > 解决方案 > 我无法使 Makefile ifeq 语法正常工作(检查文件的年龄)

问题描述

我正在尝试使用继承黑客(它已经在工作)以及检查文件日期来创建一种可重用的、自动更新的基础makefile,多个项目可以从中继承;这将使用 更新父 Makefile git pull,但是(这是我卡住的地方)我只希望它git pull每天尝试一次,这样就不会浪费很多时间等待 git当更新可用的机会几乎没有时拉。

基本思想是这样的:

baseMakefilePath=../../baseMakefile

do_some_work: .check-for-update
    @echo "working..."

.check-for-update:
    # is the file > 24 hours old?
    ifeq ( $(find . -mtime +24h -name '.check-makefile-update'), ./.check-makefile-update )
        @make .update
    else
        # if the file doesn't exist at all yet, pull the update
        ifeq ( $(find . -name '.check-makefile-update'), '' )
            @make .update
        else
            @echo "last update was recent, not updating..."
        endif
    endif

.update:
    cd $(baseMakefilePath) && git pull
    touch .check-makefile-update

我的理论是,通过.check-makefile-update使用touch它更新文件上修改后的时间戳,应该每天只运行一次 git pull 。

但是,我什至无法获得一个简单的ifeq()条件来工作:

test:
    ifeq (a, a)
        @echo "a basic test works"
    else
        @echo "idunno"
    endif

使用这个基本的 Makefile(注意:这是我测试时 Makefile 的唯一内容),我得到这个错误:

$ make test
ifeq (a, a)
/bin/sh: -c: line 0: syntax error near unexpected token `a,'
/bin/sh: -c: line 0: `ifeq (a, a)'
make: *** [test] Error 2

如果我尝试运行第一个 Makefile,我会得到相同的结果:

$ make do_some_work
# is the file > 24 hours old?
ifeq ( , ./.check-makefile-update )
/bin/sh: -c: line 0: syntax error near unexpected token `,'
/bin/sh: -c: line 0: `ifeq ( , ./.check-makefile-update )'
make: *** [.check-for-update] Error 2

我认为精简的示例表明发生了一些奇怪的事情(或者我对 ifeq 的理解存在根本缺陷),但为了它的价值,我还尝试引用 ifeq 参数的各种组合,包括单引号和双引号。

我没有想法,但我觉得我非常接近一个可行的解决方案!我究竟做错了什么?

如果重要的话,我在 OSX 10.14.5 上,我的主要 shell 是zsh. 这不是绝对要求,但如果该解决方案也可以在现代版本的 Windows(使用 WSL)上运行,那就太好了。

标签: makefile

解决方案


请记住,有Make条件和shell条件。

这里:

test:
    ifeq (a, a)
        @echo "a basic test works"
    else
        @echo "idunno"
    endif

我假设您正在尝试使用 Make 条件,但如果这些空白边距是 TAB,那么您无意中告诉 Make 这些行是它应该按原样传递给 shell 的 shell 命令。shell 尝试解释ifeq (a, a)并抱怨语法错误。

删除一些选项卡(将这些选项卡留在实际的 shell 命令前面):

test:
ifeq (a, a)
    @echo "a basic test works"
else
    @echo "idunno"
endif

它有效。


推荐阅读