Test whether a directory exists inside a makefile

前端 未结 7 1417
-上瘾入骨i
-上瘾入骨i 2020-12-23 09:11

In his answer @Grundlefleck explains how to check whether a directory exists or not. I tried some to use this inside a makefile as follow:

foo.b         


        
相关标签:
7条回答
  • 2020-12-23 09:25

    There is a very different answer that allows you to use your if statements as you envisioned them in one shell:

    .ONESHELL:
    foo.bak: foo.bar
        echo "foo"
        if [ -d "~/Dropbox" ]; then
            echo "Dir exists"
        fi
    

    Note that the only difference is the ONESHELL special target.

    0 讨论(0)
  • 2020-12-23 09:27

    This approach functions with minimal echos:

    .PHONY: all
    all:
    ifneq ($(wildcard ~/Dropbox/.*),)
            @echo "Found ~/Dropbox."
    else
            @echo "Did not find ~/Dropbox."
    endif
    
    0 讨论(0)
  • 2020-12-23 09:41

    Make commands, if a shell command, must be in one line, or be on multiple lines using a backslash for line extension. So, this approach will work:

    foo.bak: foo.bar
        echo "foo"
        if [ -d "~/Dropbox" ]; then echo "Dir exists"; fi
    

    Or

    foo.bak: foo.bar
        echo "foo"
        if [ -d "~/Dropbox" ]; then \
            echo "Dir exists"; \
        fi
    
    0 讨论(0)
  • 2020-12-23 09:41

    I had a case where I wanted to define a variable based on the test whether a directory exists or not at the top-most level of the Makefile where the approaches described above don't work. I found here a nice solution which can be used like this:

    MY_DIRNAME=../External
    ifneq "$(wildcard $(MY_DIRNAME) )" ""
      # if directory MY_DIRNAME exists:
      INCLUDES += -I../External
    else
      # if it doesn't:
      INCLUDES += -I$(HOME)/Code/External
    endif
    

    This will modify the variable INCLUDES based on whether the directory stored in MY_DIRNAME exists or not.

    (Motivation: In my case this variable would be used in another Makefile included later by the first:

    include $(SFRAME_DIR)/Makefile.common
    

    I wanted to have the same Makefile work in two different environments in a simple way.)

    0 讨论(0)
  • 2020-12-23 09:41

    I use the following to detect if a file or a directory exists and act upon it :

    $(if $(filter expected,$(wildcard *)), the expected file exists)
    

    With your request :

    .PHONY: ~/Dropbox
    
    ~/Dropbox:
        echo "Dir exists"
    
    foo.bak: foo.bar | $(if $(filter ~/Dropbox,$(wildcard ~/*)), the expected file exists)
    

    Which can further be simplify :

    foo.bak: foo.bar | $(wildcard ~/Dropbox)
    
    0 讨论(0)
  • 2020-12-23 09:45

    Try this:

    .PHONY: all
    something:
        echo "hi"
    all:
        test -d "Documents" && something
    

    This will execute the commands under something only if Documents exists.

    In order to address the problem noted in the comments, you can make a variable like this:

    PATH_TEST = ~/SomeDirectory
    
    test -d $(PATH_TEST) && something
    
    0 讨论(0)
提交回复
热议问题