GNU make 用户定义函数内的条件函数 $(if ...) 始终计算为 true
我正在尝试编写一个 make 函数来触摸/创建一个空文件和/或在可能的情况下设置权限、用户和组,或者如果没有则发出警告。然而,我的函数中的每个条件检查似乎都评估为 true。
我的 Makefile 的要点是
INSTALL_USER := fileUser
INSTALL_GROUP := fileGroup
.PHONY: test
test:
$(call touchFile,~/test.ini)
define touchFile
$(eval fileName := $(strip $(1)))
-touch $(fileName)
-chmod -c 664 $(fileName)
$(info filename info $(fileName))
$(info $(shell stat -c "%a %U:%G" $(fileName)))
$(if ifeq "foo" "bar", @echo match is broken, @echo match works)
$(if ifneq "foo" "bar", @echo match works, @echo match is broken)
$(if ifneq ($(shell stat -c %a $(fileName)),664), $(warning Error - $(fileName) does not have expected permissions of 664))
-chgrp -c $(INSTALL_GROUP) $(fileName)
$(if ifneq ($(shell stat -c %G $(fileName)),$(INSTALL_GROUP)), $(warning Error - $(fileName) does not belong to $(INSTALL_GROUP) group))
-chown -c $(INSTALL_USER) $(fileName)
$(if ifneq ($(shell stat -c %U $(fileName)),$(INSTALL_USER)), $(warning Error - $(fileName) does not belong to $(INSTALL_USER) user))
endef
运行 make test
输出
filename info ~/test.ini
664 myUserName:myGroup
Makefile:7: Error - ~/test.ini does not have expected permissions of 664
Makefile:7: Error - ~/test.ini does not belong to common group
Makefile:7: Error - ~/test.ini does not belong to netserve user
touch ~/test.ini
chmod -c 664 ~/test.ini
match is broken
match works
chgrp -c fileGroup ~/test.ini
changed group of `/home/myUserName/test.ini' to fileGroup
chown -c fileUser ~/test.ini
chown: changing ownership of `/home/myUserName/test.ini': Operation not permitted
make: [test] Error 1 (ignored)
我已经考虑/尝试了以下操作:
- $(if ...) 在使用参数调用函数之前在“编译时”评估。但是,硬编码的
ifeq "foo" "bar"
也会给出无效结果。此外,$(info ...)
在“编译时”正确计算$(fileName)
。 - 文档实际上并没有给出示例,所以在除了
$(if ifeq...)
之外,我还尝试了$(ifeq ...)
,似乎被忽略了。 - 函数内的“非功能性”
if
(即没有$(if...)
的ifeq
)给出/ bin/sh: ifeq: 未找到命令
。
有人可以帮助确定为什么我的条件没有按我的预期运行(或者为什么我期待错误的事情)?
警告:我知道如果文件不存在,仍然有一些错误需要解决,但与这个障碍相比,这应该是微不足道的。
I am trying to write a make function to touch/create an empty file and/or set the permissions, user and group, where possible, or warn if not. However, every conditional check within my function seems to evaluate to true.
The essentials of my Makefile are
INSTALL_USER := fileUser
INSTALL_GROUP := fileGroup
.PHONY: test
test:
$(call touchFile,~/test.ini)
define touchFile
$(eval fileName := $(strip $(1)))
-touch $(fileName)
-chmod -c 664 $(fileName)
$(info filename info $(fileName))
$(info $(shell stat -c "%a %U:%G" $(fileName)))
$(if ifeq "foo" "bar", @echo match is broken, @echo match works)
$(if ifneq "foo" "bar", @echo match works, @echo match is broken)
$(if ifneq ($(shell stat -c %a $(fileName)),664), $(warning Error - $(fileName) does not have expected permissions of 664))
-chgrp -c $(INSTALL_GROUP) $(fileName)
$(if ifneq ($(shell stat -c %G $(fileName)),$(INSTALL_GROUP)), $(warning Error - $(fileName) does not belong to $(INSTALL_GROUP) group))
-chown -c $(INSTALL_USER) $(fileName)
$(if ifneq ($(shell stat -c %U $(fileName)),$(INSTALL_USER)), $(warning Error - $(fileName) does not belong to $(INSTALL_USER) user))
endef
Running make test
outputs
filename info ~/test.ini
664 myUserName:myGroup
Makefile:7: Error - ~/test.ini does not have expected permissions of 664
Makefile:7: Error - ~/test.ini does not belong to common group
Makefile:7: Error - ~/test.ini does not belong to netserve user
touch ~/test.ini
chmod -c 664 ~/test.ini
match is broken
match works
chgrp -c fileGroup ~/test.ini
changed group of `/home/myUserName/test.ini' to fileGroup
chown -c fileUser ~/test.ini
chown: changing ownership of `/home/myUserName/test.ini': Operation not permitted
make: [test] Error 1 (ignored)
I've considered/tried the following:
- $(if ...) is evaluated at "compile-time", before the function is called with a parameter. But, the hard-coded
ifeq "foo" "bar"
also gives an invalid result. Additionally,$(info ...)
correctly evaluates$(fileName)
at "compile-time". - The documentation doesn't actually give examples, so in addition to
$(if ifeq...)
, I also tried$(ifeq ...)
, which seemed to be ignored. - "Non-functional"
if
(i.e., theifeq
without the$(if...)
) inside a function gives/bin/sh: ifeq: command not found
.
Can someone help identify why my conditionals aren't behaving as I expect (or why I'm expecting the wrong thing)?
Caveat: I know there are still bugs to be worked out if the file doesn't exist, but that should be trivial compared to this hurdle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
$(if ...)
条件当传递给函数的第一个参数非空时,函数 的计算结果为true
。在您的情况下,条件是文字文本:ifeq "foo" "bar"
,显然,它是非空的。ifeq
/ifneq 条件
实际上是指令,而不是函数。它们不能在变量定义内部和函数中使用。
回到您的示例,要测试条件内的字符串是否相等,请使用
filter
和findstring
:顺便说一句,这也可以变成内联形式以获得更好的可读性:
$(if ...)
conditional function evaluates totrue
when the first argument passed to it is non-empty. In you case the condition is literal text:ifeq "foo" "bar"
, which is, obviously, non-empty.ifeq
/ifneq
conditionals are in fact directives, not functions. They can't be used inside variable definition and in functions.Back to your example, to test string for equality inside the condition use functions like
filter
andfindstring
:BTW this could be also turned into an inline form for better readability:
我遇到了这个问题,我发现您可以在“if”函数的“条件”部分中使用“filter”函数的结果。这是一个在 Linux 中(使用“evince”)或在 OSX 中使用“open”打开 pdf 的有用示例
I faced this problem and I found you can use the result of "filter" function in the "condition" part of "if" function. Here is an example useful for opening a pdf either in Linux (with "evince"), or in OSX with "open")
您似乎误解了
$(if
的工作方式。来自 make info 文档:在您的所有示例中,您的条件类似于
ifeq SOMETHING OTHERTHING
- 这是一个非空字符串(来自ifeq
,无论其他内容是什么),并且所以被视为真实。You seem to be misunderstanding the way
$(if
works. From the make info docs:In all your examples, your condition is something like
ifeq SOMETHING OTHERTHING
-- which is a non-empty string (from theifeq
irrespective of what the other things are), and so is treated as true.如果你想检查你的操作系统版本是 Linux 还是其他版本,如果是,则打印一条消息,你可以这样做:
之前,我尝试使用
ifeq
,但它只打印了 else 部分(当我运行 Linux(更准确地说是 Ubuntu)时)。If you want to check that your OS version is Linux or something else, and print a message if it is, you can do this:
Before, I tried with
ifeq
, but it printed the else part only (while I was running Linux, Ubuntu, to be more precise).