How to include path prefix in GNU Make pattern rule

Consider the following:

%.foo: %.bar echo $< > $@ 

Assuming we have one 1.bar file, the command just executed echo 1.bar > 1.foo . However, when % contains a path, and not just a file name, it starts to become awkward. My problem is that I want to add another path to %.bar , the template becomes completely distorted. Ie, when %.bar nice/path/1.bar , this becomes impossible:

 %.foo: /project/something/%.bar echo $< > $@ 

This will execute, but it does echo nice/path//project/something/1.bar > 1.foo instead of echo /project/something/nice/path1.bar > 1.foo

The reason for this is how make makes its rule pattern. From the docs:

If the target pattern does not contain a slash (and this is usually not the case), the directory names in the file names are removed from the file name before matching it with the target prefix and suffix. [...] Directories are ignored only to find an implicit rule to use, and not to apply this rule. Thus, 'e% t matches the file name src / eat, and src / a as the base. When prerequisites turn into file names, directories from the stem are added in front, and the rest of the stem is replaced with "%". The string 'src / a with the required pattern' c% r gives the file name src / car

Is it possible to disable this for a specific rule?

+5
source share
1 answer

You may like to read How Pattern Matching :

If the target pattern does not contain a slash (and this is usually not the case), the directory names in the file names are removed from the file name before matching it with the target prefix and suffix. After matching the file name with the target template, the directory names along with the slash that ends them are added to the names of the necessary preliminary files generated from the rule template and file name templates. Directories are ignored only to find an implicit rule to use, and not to apply this rule. Thus, 'e% t matches the file name src / eat, and src / a as the base. When prerequisites turn into file names, directories from the stem are added in front, and the rest of the stem is replaced with "%". The string 'src / a with the required pattern' c% r gives the name of the src / car file.

The above explains why nice/path/ added to the background /project/something/1.bar .


One fix would be to use full file names in the rules, for example:

 ${CURDIR}/nice/path/%.foo: /project/something/%.bar 
+3
source

All Articles