N13*_*N13 12 makefile gnu-make
我正在尝试编写一个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
Run Code Online (Sandbox Code Playgroud)
运行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)
Run Code Online (Sandbox Code Playgroud)
我考虑过/尝试了以下内容:
ifeq "foo" "bar"也会产生无效结果.另外,$(info ...)正确评估$(fileName)"编译时".$(if ifeq...),我也试过$(ifeq ...),这似乎被忽略.if(即ifeq无$(if...)功能)给出/bin/sh: ifeq: command not found.有人可以帮助确定为什么我的条件不符合我的预期(或者为什么我期待错误的事情)?
警告:我知道如果文件不存在,还有一些问题需要解决,但与此障碍相比,这应该是微不足道的.
Eld*_*mov 45
$(if ...)条件函数计算true传递给它的第一个参数是非空的.在你的情况下,条件是文字文本:ifeq "foo" "bar",显然,非空.
ifeq/ ifneqconditionals实际上是指令,而不是函数.它们不能在变量定义和函数内使用.
回到你的例子,在条件内测试字符串是否相等,使用filter和的findstring函数:
$(if $(filter foo,bar),@echo match is broken,@echo match works)
$(if $(filter-out foo,bar),@echo match works,@echo match is broken)
Run Code Online (Sandbox Code Playgroud)
顺便说一下,这也可以变成一个内联形式,以提高可读性:
@echo match $(if $(filter foo,bar),is broken,works)
@echo match $(if $(filter-out foo,bar),works,is broken)
Run Code Online (Sandbox Code Playgroud)
小智 7
我遇到了这个问题,我发现你可以在"if"功能的"条件"部分使用"过滤"功能的结果.这是一个用于在Linux中打开pdf(使用"evince")或在OSX中使用"open"打开pdf的示例
uname :=$(shell uname -s)
is_darwin :=$(filter Darwin,$(uname))
viewpdf :=$(if $(is_darwin), open, evince)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45304 次 |
| 最近记录: |