How can I avoid the Makefile error if the file does not exist?

I have a dependency between several files where they are generated from another. For example, for generation foo-temp.xml

it is needed bar.txt

for generation foo-temp.xml

. My Makefile expresses it like this:

foo-temp.xml: bar.txt
    do-magic -o foo-temp.xml bar.txt

foo.xml: foo-temp.xml
    do-more-magic -o foo.xml foo-temp.xml

      

As long as all files exist, everything works smoothly. But there are times when I provide foo-temp.xml

manually instead of generating from bar.txt

; in fact, bar.txt

in this case it doesn't even exist.

How can I express this "nonexistence" in my Makefile without getting it because it bar.txt

doesn't exist?

+3


source to share


1 answer


I'm not sure if this is the best way, but it should work (assuming bar.txt itself should never be created with another target).

foo-temp.xml: $(wildcard bar.txt)

      

The function wildcard

returns a list of files that match the patterns and removes those that don't. This also works for filenames without wildcard / wildcard characters in them.

Alternatively (and in a little more detail)



foo-temp.xml: $(and $(realpath bar.txt),bar.txt)

      

Same basic idea, but realpath

returns a "canonical absolute name", so you cannot directly use its result (unless you want an absolute path there), so instead we use the fact that and

the short-circuiting function to generate output only if when realpath

empty.

If bar.txt

you need to generate it yourself, none of these solutions will work as it will prevent it from bar.txt

being a prerequisite if it doesn't already exist (although once the normal make process is running).

If so, you can try using make -o foo-temp.xml foo.xml

to say what foo-temp.xml

is infinitely old, which should short-circuit, to try to rewrite it (and handle its prerequisites), and should allow the rule to foo.xml: foo-temp.xml

work (assuming it foo.xml

doesn't exist or is older than foo-temp.xml

, which you created manually).

0


source







All Articles