Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

In a GNU makefile, I am wondering if it is possible, with an file list input, to make a file list output with new extensions.

In input, I get this list:

FILES_IN=file1.doc file2.xls

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

FILES_OUT=file1.docx file2.xlsx

Is it possible ? How ?

It's quite difficult because I have to parse file list, and detect each extension (.doc, .xls) to replace it to correct extension.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...