I am currently working on a project that requires translating a list of files in a directory into a c++ header to store info about the resources. For example:
/
update.py
resources.h
Makefile
/resources
resource1.png
/sub1
/sub2
resource2.png
What I want is for Makefile to run update.py only when one of the resources anywhere in the directory tree is changed. No other makefiles are going to touch the resources, only resources.h. What kind of makefile do I need to do this?
PS From what I have read, recursively searching with make is a bad idea, but if nothing else is touching the same file, would it be a problem?
I'm not sure what you've read, but there's no problem writing a rule like this in make:
RESOURCES := $(shell find . -name resource\*.png -print)
resources.h: update.py $(RESOURCES)
./update.py $(RESOURCES)
or whatever.
Notice that you can also use the $(shell find)
command for the dependencies without a variable:
resources.h: $(shell find . -name 'resource*.png')
./update.py $^
As such, it's like the answer by @MadScientist, but you get the list of dependencies in the $^
automatic variable. (Can't include the update.py
in the dependencies in this case so easily though.)
What makes this powerful is that you can use patterns with it, for example if you need to compile to multiple target files (resourcesN.h
) from multiple matching directories (resourcesN/
):
targets: resources1.h resources2.h
%.h: $(shell find $*/ -name 'resource*.png')
./update.py $^
Here $*
gets the values resources1
and resources2
, and then the $^
holds the list of dependencies found in the respective directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With