[ACCEPTED]-How to write a Makefile rule to download a file only if it is missing?-makefile

Accepted answer
Score: 16

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

hello.c:
        wget ...
        touch $@

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

Score: 7

The Makefile you wrote downloads hello.c only if it's missing. Perhaps 3 you are doing something else wrong? See 2 for example:

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

hello.c:
        echo 'int main() {}' > hello.c

And:

% make
echo 'int main() {}' > hello.c
gcc -o hello hello.c
% rm hello
% make
gcc -o hello hello.c
% rm hello*
% make
echo 'int main() {}' > hello.c
gcc -o hello hello.c

(the echo command was not executed 1 the second time)

Score: 7

Since the Makefile should be working as 7 you want, you need to check a few unlikely 6 cases:

1) Check that you don't have any .PHONY rules 5 mentioning the source file.

2) Check that 4 the source target name matches the file 3 path you are downloading.

You could also 2 try running make -d to see why make thinks it needs 1 to 're-build' the source file.

Score: 2

If the prerequisite for hello.c has changed or 7 is empty and Make continues to download 6 the file when it exists, then one option 5 to prevent Make from re-downloading the 4 file is to use a flag in the body of the 3 target to detect if the file exists:

hello.c:
    test -f $@ || wget -O hello.c http://example.org/hello.c

The 2 test command will return true if the hello.c file exists, otherwise 1 it will return false and the wget command will run.

More Related questions