如何在 GNU make 中更改具有多个扩展名的列表中每个文件的扩展名?

在 GNU makefile 中,我想知道是否可以使用文件列表输入来生成带有新扩展名的文件列表输出。

在输入中,我得到这个列表:

FILES_IN=file1.doc file2.xls

我想用 档案输入变量在 makefile 中构建这个变量:

FILES_OUT=file1.docx file2.xlsx

有可能吗,怎么可能?

这非常困难,因为我必须解析文件列表,并检测每个扩展名(。医生。将其替换为正确的扩展名。

69015 次浏览

Substituting extensions in a list of whitespace-separated file names is a common requirement, and there are built-in features for this. If you want to add an x at the end of every name in the list:

FILES_OUT = $(FILES_IN:=x)

The general form is $(VARIABLE:OLD_SUFFIX=NEW_SUFFIX). This takes the value of VARIABLE and replaces OLD_SUFFIX at the end of each word that ends with this suffix by NEW_SUFFIX (non-matching words are left unchanged). GNU make calls this feature (which exists in every make implementation) substitution references.

If you just want to change .doc into .docx and .xls into .xlsx using this feature, 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 the slightly more general syntax $(VARIABLE:OLD_PREFIX%OLD_SUFFIX=NEW_PREFIX%NEW_SUFFIX). This feature is not unique to GNU make, but it is not as portable as the plain suffix-changing substitution.

There is also a GNU make feature that lets you chain multiple substitutions on the same line: the patsubst function.

FILES_OUT = $(patsubst %.xls,%.xlsx,$(patsubst %.doc,%.docx,$(FILES_IN)))