具有here-document重定向的Makefile配方

Jul*_*rra 6 bash shell makefile heredoc gnu-make

有谁知道如何在食谱上使用here-document重定向?

test:
  sh <<EOF
  echo I Need This
  echo To Work
  ls
  EOF
Run Code Online (Sandbox Code Playgroud)

我找不到任何解决方案尝试通常的反斜杠方法(基本上以一行中的命令结束).

理由:

我有一组多行配方,我想通过另一个命令代理(例如,sh,docker).

onelinerecipe := echo l1
define twolinerecipe :=
echo l1
echo l2
endef
define threelinerecipe :=
echo l1
echo l2
echo l3
endef

# sh as proxy command and proof of concept
proxy := sh

test1:
  $(proxy) <<EOF
  $(onelinerecipe)
  EOF

test2:
  $(proxy) <<EOF
  $(twolinerecipe)
  EOF

test3:
  $(proxy) <<EOF
  $(threelinerecipe)
  EOF
Run Code Online (Sandbox Code Playgroud)

我希望避免的解决方案:将多行宏转换为单行.

define threelinerecipe :=
echo l1;
echo l2;
echo l3
endef

test3:
  $(proxy) <<< "$(strip $(threelinerecipe))"
Run Code Online (Sandbox Code Playgroud)

这有效(我使用gmake 4.0和bash作为make的shell),但它需要更改我的食谱,我有很多.Strip从宏中移除换行符,然后所有内容都写在一行中.

我的最终目标是: proxy := docker run ...

use*_*267 6

使用.ONESHELL:Makefile中某处的行将所有配方行发送到单个shell调用,您应该发现原始Makefile按预期工作.

  • 重要的缺失注释:它的效果是全局的,而不是单个规则和配方的本地效果.如果它出现在makefile中的任何位置,那么每个**目标的**所有**配方行**将被提供给shell的单个调用.它不会在我的案例中引入问题.但是请注意,`.SHELLFLAGS = -ec`是保持默认离开的行为所必需的. (3认同)