Search code examples
code-generationmakefile

How to write a Makefile rule to download a file only if it is missing?


I'm trying to write a Makefile which should download some sources if and only if they are missing.

Something like:

hello: hello.c
    gcc -o hello hello.c

hello.c:
    wget -O hello.c http://example.org/hello.c

But of course this causes hello.c to be downloaded every time make command is run. I would like hello.c to be downloaded by this Makefile only if it is missing. Is this possible with GNU make and how to do this if it is?


Solution

  • My guess is that wget doesn't update the timestamp on hello.c, but retains the remote timestamp. This causes make to believe that hello.c is old and attempts to download it again. Try

    hello.c:
            wget ...
            touch $@
    

    EDIT: The -N option to wget will prevent wget from downloading anything unless the remote file is newer (but it'll still check the timestamp of the remote file, of course.)