I’m using a makefile to automate some document generation. I have several documents in a directory, and one of my makefile rules will generate an index page of those files. The list of files itself is loaded on the fly using list := $(shell ls documents/*.txt) so I don’t have to bother manually editing the makefile every time I add, delete, or rename a document. Naturally, I want the index-generation rule to trigger when number/title of files in the documents directory changes, but I don’t know how to set up the prerequisites to work in this way.
I could use .PHONY or something similar to force the index-generation to run all the time, but I’d rather not waste the cycles. I tried piping ls to a file list.txt and using that as a prerequisite for my index-generation rule, but that would require either editing list.txt manually (trying to avoid it), or auto-generating it in the makefile (this changes the creation time, so I can’t use list.txt in the prerequisite because it would trigger the rule every time).
If you need a dependency on the number of files, then… why not just depend on the number itself? The number will be represented as a dummy file that is created when the specified nubmer of files is in the
documentsdirectory.While number of files is one property to track, instead you may depend on a hash of a directory listing. It’s very unlikely that hash function will be the same for two listings that occur in your workflow. Here’s an example:
Note using
-l— this way you’ll depend on full listing of files, which includes modification time, sizes and file names. Bu if you don’t need it, you may drop the option.Note:
sedwas needed because on my systemmd5sumyields some stuff after the hash-sum itself.