Makefile将文件名更改为隐藏文件

时间:2015-08-25 19:32:14

标签: string makefile gnu-make

好吧,我的makefile中有2个可用的变量。我想迭代变量中的文件并应用如下所示的转换。

cmain.cpp         ->   .cmain.depend
src/source.cpp    ->   src/.source.depend

我如何将.添加到文件名的开头并将扩展名更改为依赖make?我要转换的数组如下所述。

ALLSRC=$(shell find . -name '*.cpp')

1 个答案:

答案 0 :(得分:1)

ALLSRC := $(shell find . -name '*.cpp')

#Get the filenames without the directory so we prepend the . to the right thing
FNAME := $(notdir $(ALLSRC))

#Add the .
FNAME := $(FNAME:%=.%)

#Put the directory names back
HIDDEN := $(join $(dir $(ALLSRC)),$(FNAME))
HIDDEN := $(HIDDEN:%=%.depend)

#Equivalent one liner
HIDDEN2 := $(patsubst %,%.depend,$(join $(dir $(ALLSRC)),$(patsubst %,.%,$(notdir $(ALLSRC)))))
.PHONY: all

all: $(HIDDEN)

# Copies the file in this example, but $< and $@ contain the before and after names, so you can do whatever you want to.
.%.depend : %
    cp "$<" "$@