'Download list of files from list of URLs as prerequisites in makefile
I'd like to have a makefile for compiling a project, and I need some files (binaries) to be downloaded if they are missing, as it's not a good idea to track them with git.
I have a list of URLs and a list of given names for them (and I'd like to be able to choose the name to save them, as I cannot control the URL name and mostly names are quite awful and non-descriptive). Let's say that the files will always be the same or that we don't care if they change in the server (we do not need to download them again).
So my idea was to write something like this in my makefile:
files = one two three four
main : $(files)
command to do when files are present
but I want each of them to be downloaded if they are not already present, and each one from its own URL, so I'd need something like an iteration over the elements of $(files).
I'll write the idea I have in my mind mixing python code and what I know from makefiles. I know it's rubbish, but I don't know any other way to do it (the question is how to write it well, basically) and I think it's quite easy to understand.
urls = url1 url2 url3 url4
for i in len(files):
$(files)[i] :
curl -Lo $(files)[i] $(urls)[i]
Therefore the question is: how should I do this?
I've been looking the make documentation for a while and I cannot find the proper way to do it without writing the filename several times (which I think should be avoided and variables should be used instead). Maybe canned recipes are the way, but I cannot see how.
Solution 1:[1]
FILES := file1 file2 file3
main : $(FILES)
command to do when files are present
file1: firstuglyurl
file2: seconduglyurl
file3: thirduglyurl
$(FILES):
curl -Lo $@ $<
[EDIT] THAT WAS A VERY STUPID SOLUTION, I DON'T KNOW WHAT I WAS THINKING. Try this:
FILES := file1 file2 file3
main : $(FILES)
command to do when files are present
file1: URL:=firstuglyurl
file2: URL:=seconduglyurl
file3: URL:=thirduglyurl
$(FILES):
curl -Lo $@ $(URL)
If your map (URL=>filename) is in a file and you don't want to maintain it in the makefile by hand, you can have Make import it without too much trouble, just tell us the format.
Note that this does not check for files that are present but outdated, i.e. a more recent version of the file exists at the URL (which can perhaps be done, but it's tricky...).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
