54

For example: I want:

if file1 exists:

CLEAN_SRC = *.h file3

else

CLEAN_SRC = 
Sam Liao
  • 43,637
  • 15
  • 53
  • 61

1 Answers1

75

If file1 does not exist then $(wildcard file1) will evaluate to an empty string.

ifeq ($(wildcard file1),) 
    CLEAN_SRC =
else 
    CLEAN_SRC = *.h file3
endif 
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Be careful about indentation and TAB/space characters. E.g. CLEAN_SRC needed to be separated by a TAB char otherwise it would throw `no separator` error. – Arun M Aug 18 '11 at 02:24
  • 2
    @ArunM: I don't think that's true about the TAB/space characters. Only the *rule definitions* require Tab indentation. Other parts of the makefile can be indented by spaces, or not at all -- make doesn't care there. – Colin D Bennett Nov 08 '13 at 21:21
  • $(realpath file1) would be an alternative to $(wildcard file1) in this context, since it would also evaluate to an empty string if the file is missing. – Alan Nov 25 '13 at 21:25
  • However, I've been told that $(wildcard file1) is favored over $(realpath file1) because it doesn't print a message that could mistakenly be thought to indicate an error. – Alan Nov 25 '13 at 21:44
  • 2
    I'd write the positive case first with `ifneq ($(wildcard file1),)` and then the "file does not exist" would be the `else` case. – Bulletmagnet Aug 17 '17 at 08:18