gnu make将许多文件复制到一个位置 [英] gnu make copy many files to a single location

查看:46
本文介绍了gnu make将许多文件复制到一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题在本质上与问题 2543127 相似. /p>

我有一个带有头文件列表的gnu makefile.每个标头文件可能位于不同的目录中,例如

HEADERS = $(wildcard *.h) $(wildcard foo/*.h) $(wildcard bar/*.h)

我想让makefile将所有标头复制到一个包含目录

INCDIR = ../include

,并且当调用虚拟目标(例如ALL)时,它将适当地更新include目录中的头文件,即

.PHONY: ALL
ALL : $(addprefix $(INCDIR)/,$(notdir $(HEADERS)))

很显然,如果我知道目录列表是什么,我可以很轻松地完成我想要的事情.如果我这样做了,那么我可以编写一些规则(这样的东西)(虽然不完全正确,但是您掌握了原理):

$(addprefix $(INCDIR)/,$(notdir $(filter foo/%.h,$(HEADERS)))) : %.h : foo/%.h
     @cp -f $< $@

$(addprefix $(INCDIR)/,$(notdir $(filter bar/%.h,$(HEADERS)))) : %.h : bar/%.h
     @cp -f $< $@

$(addprefix $(INCDIR)/,$(notdir $(filter-out bar/%.h,$(filter-out foo/%.h,$(HEADERS))))) : %.h : %.h
     @cp -f $< $@

此方法存在两个问题,(1)随着目录数量的增加,它变得乏味;(2)我正在makefile包含文件中编写此文件,该文件不知道目录,它所知道的只是变量INCDIR和HEADERS;除了通过$(sort $ {dir $ {HEADERS)))

之外,它不直接知道目录foo/,bar/和./.

问题:如何在仅提供INCDIR和HEADERS变量的约束下编写规则以实现所需的效果.

解决方案

这应该做到:

HFILES = $(notdir $(HEADERS))
DIRS = $(dir $(HEADERS))

TARGETS = $(addprefix $(INCDIR)/, $(HFILES))

all: $(TARGETS)

$(INCDIR)/%.h: %.h
  cp $< $@

vpath %.h $(DIRS)

This question is similar in spirit to question 2543127.

I have a gnu makefile with a list of header files. Each header file may be located in a different directory, e.g.,

HEADERS = $(wildcard *.h) $(wildcard foo/*.h) $(wildcard bar/*.h)

and I want to have the makefile copy all headers to an include directory

INCDIR = ../include

and when a dummy target, e.g., ALL is invoked, it will update the header files in the include directory appropriately, i.e.,

.PHONY: ALL
ALL : $(addprefix $(INCDIR)/,$(notdir $(HEADERS)))

Obviously, I could accomplish what I want quite easily if I knew what the lists of directories were. If I did, then I could write some rules (something) like so (not entirely correct, but you get the jist):

$(addprefix $(INCDIR)/,$(notdir $(filter foo/%.h,$(HEADERS)))) : %.h : foo/%.h
     @cp -f $< $@

$(addprefix $(INCDIR)/,$(notdir $(filter bar/%.h,$(HEADERS)))) : %.h : bar/%.h
     @cp -f $< $@

$(addprefix $(INCDIR)/,$(notdir $(filter-out bar/%.h,$(filter-out foo/%.h,$(HEADERS))))) : %.h : %.h
     @cp -f $< $@

There are two problems with this approach, (1) It becomes tedious as the number of directories increases and (2) I am writing this in a makefile include, which doesn't know directories, all it knows are the variables INCDIR and HEADERS; it does not directly know the directories foo/, bar/, and ./ other than through $(sort $(dir $(HEADERS)))

Question: How can I write a rule to achieve the desired effect under the constraints of only being provided the INCDIR and HEADERS variables.

解决方案

This should do it:

HFILES = $(notdir $(HEADERS))
DIRS = $(dir $(HEADERS))

TARGETS = $(addprefix $(INCDIR)/, $(HFILES))

all: $(TARGETS)

$(INCDIR)/%.h: %.h
  cp $< $@

vpath %.h $(DIRS)

这篇关于gnu make将许多文件复制到一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆