【问题标题】:How do I use multiple spaces in a make variable/function?如何在 make 变量/函数中使用多个空格?
【发布时间】:2016-08-10 13:11:50
【问题描述】:

通常,make 会组合多个连续的空白字符。有时这种行为是不受欢迎的。以下是这种情况的示例

.PHONY: help

help:
    $(info Usage: make [command] [VARIABLES])
    $(info )
    $(info command1 ..... Do what command1 does and)
    $(info                let the sentence continue.)
    $(info command2 ..... Description of command2)

想要的输出:

Usage: make [command] [VARIABLES]

command1 ..... Do what command1 does and
               let the sentence continue.
command2 ..... Description of command2

实际输出:

Usage: make [command] [VARIABLES]

command1 ..... Do what command1 does and
 let the sentence continue.                 # Not aligned with "Do" in line above
command2 ..... Description of command2

换句话说,有没有办法在make中对齐或引用空格?

【问题讨论】:

    标签: string escaping gnu-make


    【解决方案1】:

    有个老套路:

    EMPTY=
    SPACE=$(EMPTY) $(EMPTY)
    

    所以,在你的情况下:

    .PHONY: help
    
    EMPTY=
    SPACE=$(EMPTY) $(EMPTY)
    
    help:
        $(info Usage: make [command] [VARIABLES])
        $(info )
        $(info command1 ..... Do what command1 does and)
        $(info $(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)$(SPACE)let the senten\
    ce continue.)
        $(info command2 ..... Description of command2)
    

    【讨论】:

      【解决方案2】:

      第一个想法:不要在配方中使用$(info ...)。而是使用 echo 等 shell 命令。然后您可以使用引用等来保留空格。

      第二个想法,使用define变量定义来包含完整的内容:

      define USAGE
      Usage: make [command] [VARIABLES]
      
      command1 ..... Do what command1 does and
                     let the sentence continue.
      command2 ..... Description of command2
      endef
      
      .PHONY: help
      help: ; $(info $(value USAGE))
      

      【讨论】:

      • 我喜欢define 的想法。至于echo:难道不应该尽量让内置插件更好地兼容不同的系统吗?
      • 好吧,make 的内置函数并不打算提供任何类型的兼容层来让您避免操作系统差异。它几乎 100% 保证您需要其他东西。如果使功能满足您的需求,那么可以肯定,使用它们并没有什么坏处。但是,如果它们不满足您的需求,那么显然使用它们是不可行的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多