最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Makefile中判断一个文件是否存在

makefile crifan 12069浏览 0评论

折腾:

【已解决】更新gitbook发布脚本Makefile忽略某些book

期间,需要去判断判断文件

deploy_ignore_book_crifan_com.txt

是否存在

然后再去执行不同动作。

makefile check file exist

How do I check if file exists in Makefile so I can delete it? – Stack Overflow

gnu – Testing if a file exists in makefile target, and quitting if not present – Stack Overflow

好像是这类:

clean:
    if [ -d "${OUTDIR}" ]; then \
        rm -r ${OUTDIR}; \
    fi \

Makefile – Check if a file exists using wildcard function – Humbug

然后去试试:

ifneq [ -f $(DEPLOY_IGNORE_BOOK_CRIFAN_COM) ]
ifneq [ -f "$(DEPLOY_IGNORE_BOOK_CRIFAN_COM)" ]

都语法报错:

261: *** missing separator.  Stop.

if [ -f "$(DEPLOY_IGNORE_BOOK_CRIFAN_COM)" ]
SHOULD_IGNORE = false
endif

if [ -f “$(DEPLOY_IGNORE_BOOK_CRIFAN_COM)” ]

SHOULD_IGNORE = false

fi

也都报语法错误

-》这个是shell的语法,不是Makefile的语法

去试试

DEPLOY_IGNORE_BOOK_CRIFAN_COM = deploy_ignore_book_crifan_com.txt
ifneq ("$(wildcard $(DEPLOY_IGNORE_BOOK_CRIFAN_COM))", "")
$(info $(DEPLOY_IGNORE_BOOK_CRIFAN_COM) is not empty)
endif

但是没有输出打印

How to check if a file exists in Makefile – Stack Overflow

试了半天只有文件的全路径,才可以:

# DEPLOY_IGNORE_BOOK_CRIFAN_COM = deploy_ignore_book_crifan_com.txt
# DEPLOY_IGNORE_BOOK_CRIFAN_COM = ./deploy_ignore_book_crifan_com.txt
DEPLOY_IGNORE_BOOK_CRIFAN_COM = /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/deploy_ignore_book_crifan_com.txt

ifneq ("$(wildcard $(DEPLOY_IGNORE_BOOK_CRIFAN_COM))", "")
$(info $(DEPLOY_IGNORE_BOOK_CRIFAN_COM) is not empty)
endif

才能输出:

/Users/crifan/dev/dev_root/gitbook/gitbook_src_root/deploy_ignore_book_crifan_com.txt is not empty

【总结】

DEPLOY_IGNORE_BOOK_CRIFAN_COM = $(GITBOOK_ROOT)/deploy_ignore_book_crifan_com.txt
$(info DEPLOY_IGNORE_BOOK_CRIFAN_COM=$(DEPLOY_IGNORE_BOOK_CRIFAN_COM))
ifneq ("$(wildcard $(DEPLOY_IGNORE_BOOK_CRIFAN_COM))", "")
$(info $(DEPLOY_IGNORE_BOOK_CRIFAN_COM) is not empty)
endif

可以检测出,对应文件(注意要绝对路径)的存在,然后输出:

/Users/crifan/dev/dev_root/gitbook/gitbook_src_root/deploy_ignore_book_crifan_com.txt is not empty

后记:

折腾了:

【已解决】Makefile中判断变量是否为空无效

后,其实发现也可以把

ifneq ("$(wildcard $(DEPLOY_IGNORE_BOOK_CRIFAN_COM))", "")

写成:

ifneq ($(wildcard $(DEPLOY_IGNORE_BOOK_CRIFAN_COM)), )

即:

要么都有””,要么都不用””

转载请注明:在路上 » 【已解决】Makefile中判断一个文件是否存在

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.167 seconds, using 22.03MB memory