【问题标题】:Inconsistent handling of a recipe for the "--just-print" command-line option“--just-print”命令行选项的配方处理不一致
【发布时间】:2015-08-21 10:34:17
【问题描述】:

来自docs

-n-t-q 选项不会影响那些 以+ 字符开头或包含字符串$(MAKE)${MAKE}。 请注意,只有包含 + 字符的行或字符串 无论这些选项如何,都会运行 $(MAKE)${MAKE}。其他线路 除非它们也以 + 开头或包含 $(MAKE)${MAKE}



现在,给定一个 makefile:

# 'test -f foo' is "false".
$(shell rm -rf foo)
# 'test -f bar' is "true".
$(shell rm -rf bar; touch bar)

# 'test -f bar' will be executed, even for -[tnq] command-line options. NOT SO, for 'test -f foo'.
define cmd
test -f 'foo'
+test -f 'bar'
endef


1 2 ::
    $(cmd)



运行,我们得到:

$ make --just-print 1
test -f 'foo'
test -f 'bar'

#####################

$ make --just-print 2
test -f 'foo'
test -f 'bar'

#####################

$ make --just-print 1 2
test -f 'foo'
test -f 'bar'
test -f 'foo'
makefile:14: recipe for target '2' failed
make: *** [2] Error 1

#####################

$ make --just-print 2 1
test -f 'foo'
test -f 'bar'
test -f 'foo'
makefile:14: recipe for target '1' failed
make: *** [1] Error 1



显然,我们这里有 4 个案例:

  1. make --just-print 1
    • test -f foo,显然是一个“错误”命令,成功。这是因为 --just-print 命令行选项,所以 Make 并没有真正“运行”它。这只是一个回声
    • test -f bar,由于 + 前缀而实际运行的命令。但鉴于这个秘籍是一个“真正的”命令,它成功也就不足为奇了。
  2. make --just-print 2
    • 正是作为案例1。好吧,执行配方的目标在这里可能不同(21),但执行的配方是 100%相同。这包括他们成功背后的“逻辑”,在case 1 中进行了解释。
  3. make --just-print 1 2
    • 这更复杂。它以 case 1 开头,出于同样的原因,test -f footest -f bar 都成功了。
    • 但是,当 Make 必须通过执行 2 的配方来“重复”相同的“例程”时,会发生致命错误。为什么?
  4. make --just-print 2 1
    • case 3 相同,但21 目标在这里扮演某种“相反”的角色。也就是说,2 的执行成功(根据case 2)但随后在目标1 上失败。



现在,毫无疑问,这里出了点问题。

因为在--just-print 模式下执行时,test -f foo 的 Make 永远不会失败。

很简单,因为Make不允许执行命令。它应该只是回应它!

那么,这里出了什么问题?

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    这与多行变量的扩展有关,其中一行包含+ 而其他行不包含内容。如果您使用普通内容而不是多行变量来编写规则,那么它适用于所有情况:

    1 2 ::
            test -f 'foo'
            +test -f 'bar'
    

    您应该提交一个关于这种特殊情况的错误。包含多行的变量很棘手,因为它们在第一次被 make 解析时只显示为一行,但在执行时会扩展为多行。

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 2011-05-25
      • 2014-06-28
      • 1970-01-01
      • 2013-04-30
      • 2010-09-28
      • 1970-01-01
      相关资源
      最近更新 更多