解释 makefile % 以及 $<和$@

发布于 2024-08-30 19:18:09 字数 711 浏览 1 评论 0 原文

xpi_built := $(build_dir)/$(install_rdf) \
             $(build_dir)/$(chrome_manifest) \
             $(chrome_jar_file) \
             $(default_prefs)

xpi_built_no_dir := $(subst $(build_dir)/,,$(xpi_built))

$(xpi_file): $(build_dir) $(xpi_built)
 @echo "Creating XPI file."
 cd $(build_dir); $(ZIP) ../$(xpi_file) $(xpi_built_no_dir)
 @echo "Creating XPI file. Done!"

$(build_dir)/%: %
 cp -f $< $@

$(build_dir):
 @if [ ! -x $(build_dir) ]; \
  then \
    mkdir $(build_dir); \
  fi

谁能给我解释一下这个 makefile 部分吗?对

  • $(build_dir)/%: % 以及 $<$@ 指令
  • 两个标签 $(build_dir ) 存在,我猜两者都执行了,但是按什么顺序执行?
xpi_built := $(build_dir)/$(install_rdf) \
             $(build_dir)/$(chrome_manifest) \
             $(chrome_jar_file) \
             $(default_prefs)

xpi_built_no_dir := $(subst $(build_dir)/,,$(xpi_built))

$(xpi_file): $(build_dir) $(xpi_built)
 @echo "Creating XPI file."
 cd $(build_dir); $(ZIP) ../$(xpi_file) $(xpi_built_no_dir)
 @echo "Creating XPI file. Done!"

$(build_dir)/%: %
 cp -f 
lt; $@

$(build_dir):
 @if [ ! -x $(build_dir) ]; \
  then \
    mkdir $(build_dir); \
  fi

can anyone explain me this makefile part? particularly interested in

  • $(build_dir)/%: % as well as $< and $@ directives
  • two labels $(build_dir) exists, I guess both are executed, but in which order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

温暖的光 2024-09-06 19:18:09
$(build_dir)/%: %
    cp -f 
lt; $@

这是一个静态模式规则,在其命令中使用自动变量; $< 扩展到最左边的先决条件,$@ 扩展到目标。如果您尝试创建 $(build_dir)/foo (无论 $(build_dir) 是什么),Make 会将此规则视为

$(build_dir)/foo: foo
    cp -f foo $(build_dir)/foo

下一个规则,

$(build_dir):
    @if [ ! -x $(build_dir) ]; \
  then \
  mkdir $(build_dir); \
  fi

适用于 $ (build_dir) 本身,并且不必要地复杂。它说“如果 $(build_dir) 不存在,则 mkdir 它”,可以这样写:

$(build_dir):
    mkdir $@

看起来您的主要目标是 $(xpi_file)

$(xpi_file): $(build_dir) $(xpi_built)

因此,Make 将首先创建 $(build_dir) (如果需要),然后创建列表的成员 %(xpi_built),其中包括形式$(build_dir)/%。一旦完成这些,它将执行此规则的命令:它将 cd 到 $(build_dir) ,压缩一些内容,并回显一些消息。

$(build_dir)/%: %
    cp -f 
lt; $@

This is a static pattern rule which uses automatic variables in its command; $< expands to the leftmost prerequisite, $@ expands to the target. If you try to make $(build_dir)/foo (whatever $(build_dir) is), Make will treat this rule as

$(build_dir)/foo: foo
    cp -f foo $(build_dir)/foo

The next rule,

$(build_dir):
    @if [ ! -x $(build_dir) ]; \
  then \
  mkdir $(build_dir); \
  fi

is for $(build_dir) itself, and is unnecessarily complicated. It says "if $(build_dir) doesn't exist, then mkdir it", and it could be written this way:

$(build_dir):
    mkdir $@

It looks as if your primary target is $(xpi_file):

$(xpi_file): $(build_dir) $(xpi_built)

So Make will first make $(build_dir) (if necessary), then the members of the list %(xpi_built), which includes a couple of things of the form $(build_dir)/%. Once those are done, it will execute the commands of this rule: it will cd into $(build_dir), zip some things up, and echo a couple of messages.

ら栖息 2024-09-06 19:18:09

请参阅模式规则自动变量。第一条规则匹配 $(build_dir) 内部的文件,而不是 $(build_dir) 本身。 $< 扩展为当前规则的先决条件列表,$@ 是当前规则的目标。

See Pattern Rules and Automatic Variables in the GNU make documentation. The first rule matches files inside $(build_dir), not $(build_dir) itself. $< expands to the list of prerequisites of the current rule, $@ is the target for the current rule.

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