我有一个存储项目资产的目录,比如说:
+-- assets
| +-- styles
| | +-- child
| | | +-- child.css
| | +-- main.css
| +-- font.ttf
| +-- image.png
如您所见,它们是几种文件类型(这只是一个示例,可能有更多类型),树深可能会有所不同。
如何将所有这些文件复制到单个(平面)位置?像这样:
+-- assets
| +-- child.css
| +-- font.ttf
| +-- image.png
| +-- main.css
我尝试过使用模式规则,但我在GNU Make中很新,所以似乎没什么用。
我使用Bash globstar功能获得了源文件的递归列表,并通过Make的字符串操作函数对其进行修改:
BR_ASSETS := /home/user/project/assets/
BR_APP_ASSETS := /home/user/project/build/assets/
SOURCES := $(shell ls $(BR_ASSETS)**/*.*)
TARGETS := $(addprefix $(BR_APP_ASSETS),$(notdir $(SOURCES)))
下一步是什么?
非常感谢提前。
答案 0 :(得分:3)
all:
.PHONY: all
BR_ASSETS := /home/user/project/assets/
BR_APP_ASSETS := /home/user/project/build/assets/
SOURCES := $(shell find $(BR_ASSETS) -type f)
$(info SOURCES=$(SOURCES))
TARGETS := $(addprefix $(BR_APP_ASSETS),$(notdir $(SOURCES)))
# Generate rules to map sources into targets
$(foreach s,$(SOURCES),$(foreach t,$(filter %$(notdir $s),$(TARGETS)),$(info New rule: $t: $s)$(eval $t: $s)))
# All targets have the same recipe.
$(TARGETS):; $(if $(wildcard $(@D)),,mkdir -p $(@D) &&) cp $^ $@
all: $(TARGETS)
clean:; rm -rf $(BR_APP_ASSETS)
.PHONY: clean
运行此Makefile之前的环境:
$ find /home/user/project
/home/user/project
/home/user/project/assets
/home/user/project/assets/font.ttf
/home/user/project/assets/img.png
/home/user/project/assets/styles
/home/user/project/assets/styles/child
/home/user/project/assets/styles/child/child.css
/home/user/project/assets/styles/main.css
/home/user/project/build
运行此Makefile:
$ make -f Makefile.sample
SOURCES=/home/user/project/assets/font.ttf /home/user/project/assets/img.png /home/user/project/assets/styles/child/child.css /home/user/project/asset
s/styles/main.css
New rule: /home/user/project/build/assets/font.ttf: /home/user/project/assets/font.ttf
New rule: /home/user/project/build/assets/img.png: /home/user/project/assets/img.png
New rule: /home/user/project/build/assets/child.css: /home/user/project/assets/styles/child/child.css
New rule: /home/user/project/build/assets/main.css: /home/user/project/assets/styles/main.css
mkdir -p /home/user/project/build/assets && cp /home/user/project/assets/font.ttf /home/user/project/build/assets/font.ttf
cp /home/user/project/assets/img.png /home/user/project/build/assets/img.png
cp /home/user/project/assets/styles/child/child.css /home/user/project/build/assets/child.css
cp /home/user/project/assets/styles/main.css /home/user/project/build/assets/main.css
运行此Makefile后的环境:
$ find /home/user/project
/home/user/project
/home/user/project/assets
/home/user/project/assets/font.ttf
/home/user/project/assets/img.png
/home/user/project/assets/styles
/home/user/project/assets/styles/child
/home/user/project/assets/styles/child/child.css
/home/user/project/assets/styles/main.css
/home/user/project/build
/home/user/project/build/assets
/home/user/project/build/assets/child.css
/home/user/project/build/assets/font.ttf
/home/user/project/build/assets/img.png
/home/user/project/build/assets/main.css