Skip to content
Advertisement

Traversing a directory using Makefile

I am making use of a makefile to traverse a directory , store the file names in a variable , extract the file number using regex and print the file number .

Below is the code block I am using :

JavaScript

LINE 7 is the command in makefile I want to include in the for loop. I want the actual file_num in the above < file_num > placeholder (line 7). How can I do the same. Is there any alternative approach for the same ?

Thanks in advance

Advertisement

Answer

I would make a native make approach instead of looping in bash, like so:

JavaScript

clause_files will keep all existing files matching the pattern. clause_numbers will process the file names by stripping extension and directory, then split on clause to get only the part between clause and extension.

execute-clause-% is a generic rule to run based on existence of a specific base_py_*.py script and a matching clause file. If one or the other does not exist, the rule will not be run.

Finally all rule executes all existing clauses. And since every processing is done by a separate rule, all of them might be executed in parallel by just running make -j.

Sample output:

JavaScript

Note that neither foo.py nor base_py_100.py did not cause running the rule.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement