【问题标题】:How to check whether a file exists, outside a Makefile rule?如何在 Makefile 规则之外检查文件是否存在?
【发布时间】:2019-09-02 16:18:32
【问题描述】:

这是一个伪代码:

if .gitignore exists 
    GITIGNORE_PATH := .gitignore
else
    GITIGNORE_PATH := ../.gitignore
fi

all: 
    do_build...

我试图搜索这个,但他们总是在规则中显示如何做到这一点,如:

$(UBIN)/%:
    @if [ -f '$@' ]; then \
        $(CC) $(CFLAGS) -o '$@' $(OBJS) -L $(ORAHOME) $(ORALIBS) \
        $(LNKPATH) $(DSTN_LIBS); \
        echo ""; \
    fi
  1. Testing if a file exists in a make file
  2. Testing if a file exists in makefile target, and quitting if not present
  3. How to check if a file exists in a makefile

【问题讨论】:

    标签: file if-statement makefile gnu-make


    【解决方案1】:

    单行:

    GITIGNORE_PATH := $(if $(wildcard .gitignore),,../).gitignore
    

    【讨论】:

      【解决方案2】:

      这行得通:

      # Read it as `if .gitignore file exists`
      ifneq (,$(wildcard .gitignore))
          GITIGNORE_PATH := .gitignore
      else
          GITIGNORE_PATH := ../.gitignore
      endif
      
      all:
          echo GITIGNORE_PATH ${GITIGNORE_PATH}
      

      来自这个答案:https://stackoverflow.com/a/17712774/4934640

      【讨论】:

      • $wildcard 有一个问题。使缓存其结果。因此,如果您没有找到 .gitignore 并将其添加到您的脚本中,并且在再次使用 $wildcard 进行测试之后,它仍然会报告“未找到”
      • @AlexCohn 不,它没有!无论如何,$(wildcard) 不会在 4.2.1 中按预期工作。
      • @bobbogo 也许您对此有参考?我的评论基于此:stackoverflow.com/a/41984461/192373.
      • @AlexCohn 嗯,git 日志中没有任何内容。在 3.82 之后的每个版本中都可以使用,包括 3.82 AFAICT(我确实尝试过)。在 3.81 中失败。
      猜你喜欢
      • 1970-01-01
      • 2010-09-19
      • 2016-02-10
      • 2011-12-19
      • 2011-11-27
      • 2019-08-19
      • 2012-01-10
      • 2016-09-10
      • 2017-04-01
      相关资源
      最近更新 更多