Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Recursive Dependencies

Tags:

makefile

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?

like image 856
user1539179 Avatar asked Jun 24 '13 15:06

user1539179


2 Answers

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.

like image 183
MadScientist Avatar answered Oct 12 '22 23:10

MadScientist


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.

like image 23
Magi Avatar answered Oct 12 '22 22:10

Magi