问题描述
我正在尝试使用一个继承hack (已在起作用),同时检查文件日期以创建一个一种可重用的,自动更新的基本makefile,多个项目可以从中继承;并使用 git pull
更新父Makefile,但是(这就是我遇到的问题)我只希望它每天尝试一次 git pull
,这样,在没有可用更新的机会几乎没有的情况下,等待git pull不会浪费很多时间.
I am attempting to use an inheritance hack (which is already working) along with checking a file date to create a sort of reusable, auto-updating base makefile from which multiple projects can inherit; and which will update the parent Makefile using a git pull
, but (and this is where I'm stuck) I only want it to try the git pull
once per day, so that it doesn't add up to a lot of wasted time waiting on a git pull when the chance of updates being available is almost nothing.
基本思想是:
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
我的理论是,通过使用 touch
更新 .check-makefile-update
文件上的修改时间戳,它应该每天仅运行一次git pull.
My theory is that by updating the modified timestamp on the .check-makefile-update
file using touch
it should only run the git pull once per day.
但是,我什至无法获得一个简单的 ifeq()
有条件的工作条件:
However, I can't even get a dirt simple ifeq()
conditional to work:
test:
ifeq (a, a)
@echo "a basic test works"
else
@echo "idunno"
endif
有了这个基本的Makefile(注意:这是我测试时Makefile的唯一内容),我得到此错误:
With this basic Makefile (note: this is the ONLY contents of the Makefile when I test it), I get this error:
$ 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,则会得到相同的结果:
I get the same result if I try to run the first 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参数的各种组合.
I think the stripped down example shows that there's something funky going on (or my understanding of ifeq is fundamentally flawed) but for what it's worth, I also tried quoting various combinations of the ifeq arguments, with both single and double quotes.
我没有主意,但我觉得我对工作的解决方案很了解!我在做什么错了?
I'm out of ideas, but I feel like I'm sooo close to a working solution! What am I doing wrong?
如果有关系,我使用的是OSX 10.14.5,我的主shell是 zsh
.这不是绝对要求,但是如果该解决方案也可以在Windows的现代版本(带有WSL)上运行,那就很好了.
If it matters, I'm on OSX 10.14.5, and my primary shell is zsh
. It is not an absolute requirement but would be good if the solution can also run on modern versions of Windows (with WSL), too.
推荐答案
您对 ifeq
的理解从根本上是有缺陷的:)
Your understanding of ifeq
is fundamentally flawed :)
makefile是两种不同语法的组合:makefile本身是用make语法编写的,而配方是用shell语法编写的.
A makefile is a combination of two different syntaxes: the makefile itself is written in make syntax, and the recipes are written in shell syntax.
这两种语法在任何方面都不兼容:编写配方时不能使用 make 语法,编写 makefile 时不能使用 shell 语法.
Those two syntaxes are not in any way compatible: you can't use make syntax when you are writing recipes and you can't use shell syntax when you are writing makefiles.
您怎么知道哪个?最简单的思考方法是,如果行中的第一个字符以TAB字符缩进,则它是shell语法,如果不是,则是make语法.现实更加微妙,但是规则永远是正确的.
How do you know which is which? The simplest way to think about it is that if the first character on your line is indented with a TAB character, it's shell syntax and if it's not, it's make syntax. The reality is more subtle but that rule is always true.
我确定您现在可以看到makefile出了什么问题以及为什么会出错:
I'm sure you can now see what's wrong with your makefile AND also why you get the errors you do:
test:
ifeq (a, a)
@echo "a basic test works"
else
@echo "idunno"
endif
您正在尝试将make语法传递给shell.
you are trying to pass make syntax to the shell.
这篇关于我无法使Makefile ifeq语法正常工作(检查文件的年龄)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!