使用 make 自动从其他目录复制依赖项
我有一个包含多个目标的 makefile,这些目标是通过从工作目录外部复制文件生成的。
a.tex : $(wildcard /foo/work1/a.tex)
cp -p $< $@
b.tex : $(wildcard /foo/work2/b.tex)
cp -p $< $@
我使用 $(wildcard)
因为有时我在无法访问 /foo
的系统上运行 Make。
避免重复 cp -p $的最佳方法是什么?每个规则的 $@
命令?一些选项:
- 设置
%.tex : %.tex
规则。 因此我收到很多警告,例如make: Circular a.tex <- a.tex dependency drop.
- 这有效,但它也适用于未明确指示的目标,
定义
。这似乎毫无意义,因为命令只有一行。因此,不要复制 cp $< $@ 对于每个规则,我都会定义一个cp-dep
序列并将cp-dep
复制到每个规则。 - 将命令定义为变量,以便我可以执行
a.tex : $(wildcard /foo/work1/a.tex); $(CP-DEP)
- 复制目标名称作为附加规则。
a.tex b.tex : ; cp -p $< $@
。容易出错。 - 只是复制和粘贴。笨重但有效且易于理解。
I have a makefile with multiple targets that are generated by copying a file from outside the working directory.
a.tex : $(wildcard /foo/work1/a.tex)
cp -p lt; $@
b.tex : $(wildcard /foo/work2/b.tex)
cp -p lt; $@
I use $(wildcard)
because sometimes I run Make on systems that do not have access to /foo
.
What is the best way to avoid repeating the cp -p $< $@
commands for every rule? Some options:
- Setting up a
%.tex : %.tex
rule. This works, but it also applies to targets that aren't specifically indicated so I get lots of warnings likemake: Circular a.tex <- a.tex dependency dropped.
- Defining a sequence of commands with
define
. This seems pointless since the command is only one line. So instead of copyingcp $< $@
to every rule, I'd define acp-dep
sequence and copycp-dep
to every rule. - Defining the command as a variable so that I could do
a.tex : $(wildcard /foo/work1/a.tex); $(CP-DEP)
- Duplicating the target names as an additional rule.
a.tex b.tex : ; cp -p $< $@
. Error-prone. - Just copying and pasting. Clunky but effective and easy to understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有测试过它,但是你不能只使用没有先决条件的模式规则,并在单独的行上指定每个目标的先决条件吗?
顺便提一句。当通配符函数找不到匹配项时,它不是返回空字符串,因此
$<
也是空的吗?这不会给 cp 带来问题吗?I haven't tested it, but can't you just use a pattern rule without prerequisites, and specify the prerequisite for each target on a separate line?
Btw. doesn't the wildcard function return the empty string when it doesn't find a match, so that
$<
is empty as well? Wouldn't that give a problem withcp
?我认为您的
copyrule
太过分了(而且不灵活)。如果您对 @eriktous 的解决方案的反对意见是它会将规则应用于您尚未明确定义依赖项的目标,则可以使用 静态模式规则:(如果这解决了您的问题,您应该接受 eriktous 的答案 - 这只是它的一个变体。)
I think your
copyrule
is overkill (and inflexible). If your objection to @eriktous's solution is that it will apply the rule to targets for which you haven't explicitly defined dependencies, that's easy to fix with a static pattern rule:(If this solves your problem you should accept eriktous's answer-- this is just a variation on it.)
我最终这样做了:
这种方法的优点是我可以轻松地使用最少的样板文本添加新文件,并且可以轻松地将其规则部分复制到新的 Makefile 中。缺点是我无法再更改目标文件名,并且对于 makefile 经验较少的人来说,实现相当不透明。
I ended up doing this:
The advantage of this method is that I can easily add new files with a minimum of boilerplate text and I can easily copy the rule part of this to a new Makefile. The disadvantages are that I can no longer change the destination filename, and the implementation is rather opaque for people with less makefile experience.