1

This is a pseudo code:

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

all: 
    do_build...

I tried to search this, but they always show how to do this inside rules, as in:

$(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
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

2 Answers2

2

A one-liner:

GITIGNORE_PATH := $(if $(wildcard .gitignore),,../).gitignore
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
1

This worked:

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

all:
    echo GITIGNORE_PATH ${GITIGNORE_PATH}

From this answer: https://stackoverflow.com/a/17712774/4934640

Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
  • There is one catch about $wildcard. Make caches its results. So, if you don't find .gitignore and add it in your script, and after that test with $wildcard again, it still will report 'not found' – Alex Cohn Apr 12 '19 at 05:13
  • 1
    @AlexCohn No it doesn't! Not in 4.2.1 at any rate where `$(wildcard)` works as expected. – bobbogo Apr 17 '19 at 12:58
  • @bobbogo maybe you have a reference for that? I based my remark on this: https://stackoverflow.com/a/41984461/192373. – Alex Cohn Apr 17 '19 at 16:28
  • 1
    @AlexCohn Hmmm, nothing in the git log. Works in every release since 3.82, including 3.82 AFAICT (I did try them). Fails in 3.81. – bobbogo Apr 18 '19 at 15:57