Search code examples
directorymakefiledependencies

Makefile rule depend on directory content changes


Using Make is there a nice way to depend on a directories contents.

Essentially I have some generated code which the application code depends on. The generated code only needs to change if the contents of a directory changes, not necessarily if the files within change their content. So if a file is removed or added or renamed I need the rule to run.

My first thought is generate a text file listing of the directory and diff that with the last listing. A change means rerun the build. I think I will have to pass off the generate and diff part to a bash script.

I am hoping somehow in their infinite intelligence might have an easier solution.


Solution

  • Kudos to gjulianm who got me on the right track. His solution works perfect for a single directory.

    To get it working recursively I did the following.

    ASSET_DIRS = $(shell find ../../assets/ -type d)
    ASSET_FILES = $(shell find ../../assets/ -type f -name '*')
    
    codegen: ../../assets/ $(ASSET_DIRS) $(ASSET_FILES)
         generate-my-code
    

    It appears now any changes to the directory or files (add, delete, rename, modify) will cause this rule to run. There is likely some issue with file names here (spaces might cause issues).