Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile rule that depends on all files under a directory (including within subdirectories)

One rule in my Makefile zips an entire directory (res/) into a ZIP file. Obviously, this rule needs to execute when any file under the res/ directory changes. Thus, I want the rule to have as a prerequisite all files underneath that directory. How can I implement this rule?

In Bash with the globstar option enabled, you can obtain a list of all the files in that directory using the wildcard pattern res/**/*. However, it doesn't seem to work if you specify it as a prerequisite in the Makefile:

filename.jar: res/**/* 

Even after touching a file in res/, Make still reports

make: `filename.jar' is up to date. 

so clearly it is not recognizing the pattern.

If I declare the directory itself as a prerequisite:

filename.jar: res 

then Make will not re-execute when a file is modified (I think make only looks at the modified date of the directory itself, which only changes when immediate children are added, removed, or renamed).

like image 325
Mechanical snail Avatar asked Jan 12 '13 02:01

Mechanical snail


People also ask

What is all rule in makefile?

This means that when you do a "make all", make always thinks that it needs to build it, and so executes all the commands for that target. Those commands will typically be ones that build all the end-products that the makefile knows about, but it could do anything.

What is a makefile depend?

A Makefile rule that typically scans all C/C++ source files in a directory, and generates rules that indicate that an object file depends on certain header files, and must be recompiled if they are recompiled.

Can makefile target be a directory?

Yes, a Makefile can have a directory as target. Your problem could be that the cd doesn't do what you want: it does cd and the git clone is carried out in the original directory (the one you cd ed from, not the one you cd ed to). This is because for every command in the Makefile an extra shell is created.

What is makefile target?

A simple makefile consists of “rules” with the following shape: target … : prerequisites … recipe … … A target is usually the name of a file that is generated by a program; examples of targets are executable or object files. A target can also be the name of an action to carry out, such as ' clean ' (see Phony Targets).


1 Answers

This:

filename.jar: $(wildcard res/**/*) 

seems to work, at least on some platforms.

EDIT:

Or better, just cut the knot:

filename.jar: $(shell find res -type f) 
like image 93
Beta Avatar answered Sep 24 '22 07:09

Beta