Makefile:在目标命令行中分配函数变量
仅当决定执行更新目标的命令时,我才需要分配 xpi_hash 变量。然后我使用这个变量作为环境、导出等。
如果我把它放在规则之外,它会在调用 $(xpi)
目标之前首先扩展,因此不会发现文件。
substitute := perl -p -e 's/@([^@]+)@/$$ENV{$$1} bla bla...
export xpi_hash
.PHONY: dirs substitute update
update: $(xpi) $(target_update_rdf)
xpi_hash := $(shell sha1sum $(xpi) | grep -Eow '^[^ ]+')
@echo "Updating..."
$(target_update_rdf): $(update_rdf)
$(substitute) $< > $@
上面的当然是不正确的,因为对于命令部分来说,shell 是代表的。所以也许提出这个问题的另一种方式是 - 如何将变量作为命令输出?
I need the xpi_hash
variable to be assigned only when update target's command is decided to execute. Then I'm using this variable as environment, exporting, etc..
If I put it outside of rule, it will be expanded firstly, before $(xpi)
target is called, hence will not find that file.
substitute := perl -p -e 's/@([^@]+)@/$ENV{$1} bla bla...
export xpi_hash
.PHONY: dirs substitute update
update: $(xpi) $(target_update_rdf)
xpi_hash := $(shell sha1sum $(xpi) | grep -Eow '^[^ ]+')
@echo "Updating..."
$(target_update_rdf): $(update_rdf)
$(substitute) lt; > $@
and above of course is not correct, because for command part the shell is represented. So maybe another way to put this question is - how to bring variable as command output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您到底在寻找什么,您打算如何使用
xpi_hash
?如果您想在每次使用变量时获取当前哈希值,请使用=
来分配变量而不是:=
,例如将打印
xpi< 的哈希值/code> 更新后。
对于
make
中的变量,请参见第 6.2 节<手册的/a>。简而言之,':=' 将扩展右侧的变量,'=' 将使它们稍后扩展。我的评论中更改的命令 (
substitute = xpi_hash="$(xpi_hash)" perl -p -e 's/@([^@]+)@/$$ENV{$$1}...'< /code>) 将扩展为等效于
xpi_hash="..."
语法是在 bash 子 shell 中定义变量,而不是在 make 中使用该变量。I'm not sure exactly what you're looking for here, how are you planning to use
xpi_hash
? If you want to get the current hash every time you use the variable use=
to assign the variable instead of:=
, e.g.will print the hash of
xpi
after it has been updated.For variables in
make
, see section 6.2 of the manual. Briefly ':=' will expand variables on the right hand side, '=' will leave them to be expanded later.The altered command in my comment (
substitute = xpi_hash="$(xpi_hash)" perl -p -e 's/@([^@]+)@/$$ENV{$$1}...'
) will expand to be equivalent toThe
xpi_hash="..."
syntax is defining a variable in the bash subshell, rather than using the variable in make.如果只有
substitute
必须使用xpi_hash
,请将 xpi_hash 设为特定于目标的变量:如果其他 Perl 脚本需要
xpi_hash
,并且您想要导出它,你有一个问题,因为在规则的子 shell 中分配的变量无法(轻松)传递给 Make。但您可以将其存储在文件中并包含
它:If only
substitute
must usexpi_hash
, make xpi_hash a target-specific variable:If other Perl scripts will need
xpi_hash
, and you want to export it, you have a problem, because the variables assigned in the subshells of a rule cannot (easily) be communicated to Make. But you can store it in a file andinclude
it: