NateT
NateT

Reputation: 142

What's the standard makefile idiom for trying different ways to make a target

I have a makefile that uses a source file from the internet. There are two locations where the file resides, neither of which I consider very dependable, so I also keep a local copy. So the relevant lines of my makefile look like:

src.c:
    wget -nv http://location.com/$@ || wget -nv http://otherplace.com/$@ || cp local/$@ .

src.o: src.c
    $(CC) -o $@ $<

Is this the "right way" to do this? What if there are multiple steps in each different way of creating the target - how do I tell make "Try A. If A fails, try B. If B fails, ..."?

Upvotes: 3

Views: 148

Answers (1)

Mark Galeck
Mark Galeck

Reputation: 6385

The right thing to do is this:

.PHONY: phony
src.c: phony
    if (wget -nv http://location.com/$@ -O [email protected]) && ! diff [email protected] $@ >/dev/null; then \
     mv [email protected] $@; \
    fi 

I shortened your command to a single wget but you can put whatever you want there, including a sequence of ||s to achieve "try this, if not, try that etc". Just make sure it outputs to a temporary file (and does not hang indefinitely !) .

It is in fact important to use phony here, and not only .PHONY. Can you see why?

Also, with this method, there is no longer a need to keep another "local" copy and/or use cp. Your target src.c is your "local copy" - the latest one you were able to successfully get from the Internet.

Upvotes: 1

Related Questions