Makefile:依赖于目录中的每个文件
我想做一个使用 gnumake 或 makepp 运行的 Makefile,它打包给定目录下的所有文件:
DIRS:=$(shell find . -mindepth 2 -maxdepth 2 -not -name mp3 -not -name ".*" -type d)
PACKAGES = $(DIRS:%=%.npk)
all: packages
packages: $(PACKAGES)
%.npk: %/*
npack c $@ @^
.PHONY: all packages
问题是依赖项中没有 %/* 这样的东西。 我需要目标(X.npk)依赖于目录 X 中的每个文件,但当我编写 Makefile 时我不知道这些文件是什么,因为它们是稍后生成的。
一个例子:
./dirA/x
./dirA/y
./dirB/e
./dirB/f
我想创建./dirA.npk(取决于x,y),./dirB.npk(e,f) 除了第一行中使用的 find 找到所有目录之外,我事先对目录或文件一无所知。
I'd like to do a Makefile that runs either with gnumake or makepp that packs all the files under given directiories:
DIRS:=$(shell find . -mindepth 2 -maxdepth 2 -not -name mp3 -not -name ".*" -type d)
PACKAGES = $(DIRS:%=%.npk)
all: packages
packages: $(PACKAGES)
%.npk: %/*
npack c $@ @^
.PHONY: all packages
the problem is that there's no such thing as %/* in the dependencies.
I need the targets (X.npk) to depend on every file in directory X, but I don't know what the files are when I write the Makefile, 'cause they're generated later.
An example:
./dirA/x
./dirA/y
./dirB/e
./dirB/f
I'd like to create ./dirA.npk (depending on x,y), ./dirB.npk (e,f)
There's nothing I know about the dirs or the files in advance except that the find used in the 1st line finds all the dirs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
通配符
指令:编辑:
上面只是使用通配符的示例,它使每个 .npk 文件依赖于所有其他文件夹中的文件。您的用法会略有不同。
我认为可能有一种更简单的方法来解决这个问题。为什么要依赖文件夹中的所有文件?仅仅是使用
$^
运算符吗?或者如果任何文件发生更改,您是否需要重建 .npk?一种替代(可能更干净)的解决方案是在配方中使用
find
实用程序而不是$^
并使用.FORCE
指令始终强制重建 .npk 文件。缺点是 .npk 文件可能会不必要地重建。编辑2:
如果没有办法使用
make
命令干净地完成此操作,您可以使用.FORCE
来解决这个问题,以确保配方始终运行并移动“我应该吗?重建此文件”检查到配方的正文:其中
check_for_rebuild.sh
是一个 shell 脚本,它执行以下操作:我不太喜欢该解决方案,因为它解决问题而不是解决问题直接,但它可能可以让你同时继续。如果您打算走这条路,那么在 shell 脚本中完成所有操作可能会更干净、更容易,并且让 makefile 简单地调用脚本或完全删除 makefile。
Try using the
wildcard
directive:EDIT:
The above is just an example of using
wildcard
and makes each .npk file dependent on the files in all of the other folders. Your usage would be slightly different.I think there may be an easier way to go about this. Why are you wanting to have a dependency on all of the files in the folder? Is it just to use the
$^
operator? Or do you need to rebuild the .npk if any of the files changed?One alternate (and possibly cleaner) solution would be to use the
find
utility in your recipe instead of$^
and use the.FORCE
directive to always force the .npk file to be rebuilt. The downside is that .npk files may be rebuilt unnecessarily.EDIT 2:
If there's not a way to do this cleanly with
make
commands, you can work around it by using.FORCE
to ensure that the recipe is always run and move the "should I rebuild this file" check into the body of the recipe:where
check_for_rebuild.sh
is a shell script that does something like this:I don't really like that solution because it works around the problem instead of solving it directly, but it may be able to get you going in the meantime. If you are going to go that route, it's probably cleaner and easier to do everything in the shell script and either have the makefile simply invoke the script or get rid of the makefile entirely.
这是我找到的解决方案:
它基于 makedepend 想法,并带有一些“元”脚本。不太好,但是有效。
This is the solution I found:
it is based on the makedepend idea, with some "meta" scripting. Not very nice, but works.
使用 makepp,您可以通过 :foreach 规则修饰符分两步完成此操作:
这为每个子目录提供了一个规则,只要其中的文件列表发生更改,该规则就会重新执行。
With makepp you can do this in 2 steps, via the :foreach rule modifier:
This provides a rule for every subdirectory, which reexecutes whenever there is a change in the list of files therein.