使用 make 自动从其他目录复制依赖项

发布于 2024-12-13 01:29:10 字数 805 浏览 3 评论 0原文

我有一个包含多个目标的 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 like make: 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 copying cp $< $@ to every rule, I'd define a cp-dep sequence and copy cp-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

变身佩奇 2024-12-20 01:29:10

我还没有测试过它,但是你不能只使用没有先决条件的模式规则,并在单独的行上指定每个目标的先决条件吗?

a.tex: $(wildcard /foo/work1/a.tex)
b.tex: $(wildcard /foo/work2/b.tex)

%.tex:
    cp -p 
lt; $@

顺便提一句。当通配符函数找不到匹配项时,它不是返回空字符串,因此 $< 也是空的吗?这不会给 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?

a.tex: $(wildcard /foo/work1/a.tex)
b.tex: $(wildcard /foo/work2/b.tex)

%.tex:
    cp -p 
lt; $@

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 with cp?

天涯离梦残月幽梦 2024-12-20 01:29:10

我认为您的 copyrule 太过分了(而且不灵活)。如果您对 @eriktous 的解决方案的反对意见是它会将规则应用于您尚未明确定义依赖项的目标,则可以使用 静态模式规则:(

a.tex: $(wildcard /foo/work1/a.tex)
b.tex: $(wildcard /foo/work2/b.tex)
blue.tex: $(wildcard /some/other/path/green.tex)

TEXES = a.tex b.tex

$(TEXES): %.tex:
    cp -p 
lt; $@

如果这解决了您的问题,您应该接受 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:

a.tex: $(wildcard /foo/work1/a.tex)
b.tex: $(wildcard /foo/work2/b.tex)
blue.tex: $(wildcard /some/other/path/green.tex)

TEXES = a.tex b.tex

$(TEXES): %.tex:
    cp -p 
lt; $@

(If this solves your problem you should accept eriktous's answer-- this is just a variation on it.)

别闹i 2024-12-20 01:29:10

我最终这样做了:

COPYFILES = /foo/work1/a.tex /foo/work2/b.tex

define copyrule
$(notdir $(1)): $(wildcard $(1))
    cp -p $< $@
endef
$(foreach file,$(COPYFILES),$(eval $(call copyrule,$(file))))

这种方法的优点是我可以轻松地使用最少的样板文本添加新文件,并且可以轻松地将其规则部分复制到新的 Makefile 中。缺点是我无法再更改目标文件名,并且对于 makefile 经验较少的人来说,实现相当不透明。

I ended up doing this:

COPYFILES = /foo/work1/a.tex /foo/work2/b.tex

define copyrule
$(notdir $(1)): $(wildcard $(1))
    cp -p $< $@
endef
$(foreach file,$(COPYFILES),$(eval $(call copyrule,$(file))))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文