Saar Drimer
Saar Drimer

Reputation: 1191

Makefile: creating a function that checks if files within a list exist

make: I'd like to create a function that I can call from a phony, and that accepts a variable containing a list of files. The function should check for the existence of each file and act based on the result (print a message, etc.)

I know how to check for the existence of files, but in the context of a function call, I see several ways to go about it. I'm asking here for the right way to do it.

EDIT: I'm not necessarily looking for complete working code... helpful pointers are fine.

Upvotes: 2

Views: 1402

Answers (1)

user2100815
user2100815

Reputation:

Here's an example of a makefile that creates the non-existent files in the QUERY variable when you say make create - I don't know if this is the kind of thing you are thinking of:

QUERY := f1.txt no.txt no2.txt
FILES := $(wildcard *.txt)
EXIST_IN_QUERY := $(filter $(QUERY),$(FILES) )
EXIST_NOT_IN_QUERY := $(filter-out $(QUERY),$(FILES) )
NOTEXIST := $(filter-out $(FILES),$(QUERY) )

info:
    @echo existing files $(FILES)
    @echo existing files in QUERY $(EXIST_IN_QUERY)
    @echo existing files not in QUERY $(EXIST_NOT_IN_QUERY)
    @echo non-existent files in QUERY $(NOTEXIST)

create: $(NOTEXIST)

%.txt:
    echo "something" >  $@

When run on a directory containing f1.txt and f2.txt, the info target produces:

existing files f1.txt f2.txt
existing files in QUERY f1.txt
existing files not in QUERY f2.txt
non-existent files in QUERY no.txt no2.txt

And if run in the same directory, the create target will create the files no.txt and no2.txt.

Upvotes: 2

Related Questions