【发布时间】:2012-01-25 19:07:08
【问题描述】:
我正在尝试编写一个 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)。 -
documentation实际上并没有给出例子,所以除了
$(if ifeq...),我还尝试了$(ifeq ...),似乎被忽略了。 - 函数内的“非功能性”
if(即,没有$(if...)的ifeq)给出/bin/sh: ifeq: command not found。
有人可以帮助确定为什么我的条件不符合我的预期(或者为什么我期待错误的事情)?
警告:我知道如果文件不存在,仍有一些错误需要解决,但与这个障碍相比,这应该是微不足道的。
【问题讨论】:
-
GNU 网站虽然看起来不错,但简洁得几乎毫无意义,不是吗。他们会告诉你有一个函数存在,然后不给你任何如何使用它的例子,以便用它来骚扰你。