如何在Makefile中编写内联文件

时间:2012-06-21 00:31:26

标签: python makefile response inline

我见过一些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

2 个答案:

答案 0 :(得分:3)

您可以这样做:

define TMP_PYTHON_PROG
print 'hello'
print 'world'
endef

export TMP_PYTHON_PROG
target:
    @python -c "$$TMP_PYTHON_PROG"

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

答案 1 :(得分:3)

您的@<<内容不起作用的原因是它似乎是非标准品牌变体的一项功能。同样,mVChr提到的define命令对于GNU Make是特定的(据我所知)。虽然GNU Make的分布非常广泛,但这个技巧不适用于BSD make,也不适用于POSIX-make。

作为一般原则,我认为保持makefile尽可能便携是件好事;如果您在自动配置系统的上下文中编写Makefile,它仍然更重要。

完全可移植的技术,可以帮助您实现目标:

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

或等效地,如果你想更整洁地铺设东西:

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