【问题标题】:How to write inline files in a Makefile如何在 Makefile 中写入内联文件
【发布时间】:2012-06-21 00:31:25
【问题描述】:

我见过一些 shell 脚本,它们通过在同一个 shell 脚本中写入文件的内容来传递文件。例如:

if [[ $uponly -eq 1 ]]; then
    mysql $tabular -h database1 -u usertrack -pabulafia usertrack << END                                                                                           

    select host from host_status where host like 'ld%' and status = 'up';                                                                                          

END
     exit 0
fi

我已经能够做类似的事情:

python << END
print 'hello world'
END

如果脚本的名称是myscript.sh,那么我可以通过执行sh myscript.sh 来运行它。我得到了我想要的输出。

问题是,我们可以在 Makefile 中做类似的事情吗?我一直在环顾四周,他们都说我做了类似的事情:

target:
        python @<<
print 'hello world'
<<

但这不起作用。

这是我一直在寻找的链接:

http://www.opussoftware.com/tutorial/TutMakefile.htm#Response%20Files

http://www.scribd.com/doc/2369245/Makefile-Memo

【问题讨论】:

    标签: python makefile response inline


    【解决方案1】:

    你可以这样做:

    define TMP_PYTHON_PROG
    print 'hello'
    print 'world'
    endef
    
    export TMP_PYTHON_PROG
    target:
        @python -c "$$TMP_PYTHON_PROG"
    

    首先,您使用defineendef 定义一个多行变量。然后你需要将export 发送到 shell,否则它会将每个新行视为一个新命令。然后使用 $$ 重新插入 shell 变量。

    【讨论】:

      【解决方案2】:

      您的 @&lt;&lt; 不起作用的原因是它似乎是非标准 make 变体的功能。同样,mVChr 提到的define 命令是特定于 GNU Make 的(据我所知)。虽然 GNU Make 分布非常广泛,但这个技巧在 BSD make 和 POSIX-only make 中都行不通。

      作为一般原则,我觉得尽可能保持 makefile 的可移植性是很好的;如果您在自动配置系统的上下文中编写 Makefile,它仍然更重要。

      一种完全可移植的技术可以满足您的需求:

      target:
          { echo "print 'First line'"; echo "print 'second'"; } | python
      

      或者等价的,如果你想把东西布置得更整齐一点:

      target:
          { echo "print 'First line'"; \
            echo "print 'second'"; \
          } | python
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多