How to change the extension of each file in a list with multiple extensions in GNU make?

In the GNU makefile, I wonder if it is possible to list files with new extensions by entering a list of files.

On the tab, I get this list:

FILES_IN=file1.doc file2.xls 

And I would like to build this variable in my makefile from the FILES_IN variable:

 FILES_OUT=file1.docx file2.xlsx 

Is it possible? How?

This is quite difficult because I have to parse the list of files and detect each extension (.doc, .xls) to replace it with the correct extension.

+53
makefile gnu-make
Aug 22 '12 at 8:50
source share
1 answer

Substitution of extensions in the list of file names separated by spaces is a general requirement, and there are built-in functions for this. If you want to add x at the end of each name in the list:

 FILES_OUT = $(FILES_IN:=x) 

General view of $(VARIABLE:OLD_SUFFIX=NEW_SUFFIX) . This takes the value VARIABLE and replaces OLD_SUFFIX at the end of each word that ends with this suffix with NEW_SUFFIX (inconsistent words remain unchanged). GNU calls this function (which exists in every make implementation) of the replacement reference .

If you just want to change .doc to .docx and .xls to .xlsx using this function, you need to use an intermediate variable.

 FILES_OUT_1 = $(FILES_IN:.doc=.docx) FILES_OUT = $(FILES_OUT_1:.xls=.xlsx) 

You can also use a slightly more general $(VARIABLE:OLD_PREFIX%OLD_SUFFIX=NEW_PREFIX%NEW_SUFFIX) syntax $(VARIABLE:OLD_PREFIX%OLD_SUFFIX=NEW_PREFIX%NEW_SUFFIX) . This feature is not unique to GNU make, but not as portable as a simple suffix replacement.

There is also a GNU make function that allows you to chain multiple substitutions on a single line: the patsubst function .

 FILES_OUT = $(patsubst %.xls,%.xlsx,$(patsubst %.doc,%.docx,$(FILES_IN))) 
+102
Aug 22 '12 at 11:18
source share



All Articles