GNU make 用户定义函数内的条件函数 $(if ...) 始终计算为 true

发布于 2024-12-28 23:59:12 字数 2279 浏览 2 评论 0原文

我正在尝试编写一个 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., the ifeq 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难得心□动 2025-01-04 23:59:12

$(if ...) 条件当传递给函数的第一个参数非空时,函数 的计算结果为true。在您的情况下,条件是文字文本:ifeq "foo" "bar",显然,它是非空的。

ifeq/ifneq 条件实际上是指令,而不是函数。它们不能在变量定义内部和函数中使用。

回到您的示例,要测试条件内的字符串是否相等,请使用 filterfindstring

$(if $(filter foo,bar),@echo match is broken,@echo match works)
$(if $(filter-out foo,bar),@echo match works,@echo match is broken)

顺便说一句,这也可以变成内联形式以获得更好的可读性:

@echo match $(if $(filter foo,bar),is broken,works)
@echo match $(if $(filter-out foo,bar),works,is broken)

$(if ...) conditional function evaluates to true 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 and findstring:

$(if $(filter foo,bar),@echo match is broken,@echo match works)
$(if $(filter-out foo,bar),@echo match works,@echo match is broken)

BTW this could be also turned into an inline form for better readability:

@echo match $(if $(filter foo,bar),is broken,works)
@echo match $(if $(filter-out foo,bar),works,is broken)
孤寂小茶 2025-01-04 23:59:12

我遇到了这个问题,我发现您可以在“if”函数的“条件”部分中使用“filter”函数的结果。这是一个在 Linux 中(使用“evince”)或在 OSX 中使用“open”打开 pdf 的有用示例

uname     :=$(shell uname -s)
is_darwin :=$(filter Darwin,$(uname))
viewpdf   :=$(if $(is_darwin), open, evince)

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")

uname     :=$(shell uname -s)
is_darwin :=$(filter Darwin,$(uname))
viewpdf   :=$(if $(is_darwin), open, evince)
_畞蕅 2025-01-04 23:59:12

您似乎误解了 $(if 的工作方式。来自 make info 文档:

$(如果条件,则部分[,其他部分])'
“if”函数提供对条件扩展的支持
功能上下文

第一个参数 CONDITION,first 包含所有前面的和
尾随空白被删除,然后被扩展。如果它扩展到
任何非空字符串,则条件被视为 true。
如果它扩展为空字符串,则条件被认为是
是假的。

在您的所有示例中,您的条件类似于 ifeq SOMETHING OTHERTHING - 这是一个非空字符串(来自 ifeq,无论其他内容是什么),并且所以被视为真实。

You seem to be misunderstanding the way $(if works. From the make info docs:

$(if CONDITION,THEN-PART[,ELSE-PART])'
The `if' function provides support for conditional expansion in a
functional context

The first argument, CONDITION, first has all preceding and
trailing whitespace stripped, then is expanded. If it expands to
any non-empty string, then the condition is considered to be true.
If it expands to an empty string, the condition is considered to
be false.

In all your examples, your condition is something like ifeq SOMETHING OTHERTHING -- which is a non-empty string (from the ifeq irrespective of what the other things are), and so is treated as true.

提笔书几行 2025-01-04 23:59:12

如果你想检查你的操作系统版本是 Linux 还是其他版本,如果是,则打印一条消息,你可以这样做:

ifneq ($(TARGETOS), Linux)
    target: server
    @echo
    @echo $(MSG) $(TARGETOS)
else
target: server
    @echo $(MSG) $(TARGET)
endif

之前,我尝试使用 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:

ifneq ($(TARGETOS), Linux)
    target: server
    @echo
    @echo $(MSG) $(TARGETOS)
else
target: server
    @echo $(MSG) $(TARGET)
endif

Before, I tried with ifeq, but it printed the else part only (while I was running Linux, Ubuntu, to be more precise).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文