[英]Makefile recipe with a here-document redirection
有誰知道如何在食譜上使用here-document重定向?
test:
sh <<EOF
echo I Need This
echo To Work
ls
EOF
我找不到任何解決方案嘗試通常的反斜杠方法(基本上以一行中的命令結束)。
理由:
我有一組多行配方,我想通過另一個命令代理(例如,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
我希望避免的解決方案:將多行宏轉換為單行。
define threelinerecipe :=
echo l1;
echo l2;
echo l3
endef
test3:
$(proxy) <<< "$(strip $(threelinerecipe))"
這有效(我使用gmake 4.0和bash作為make的shell),但它需要更改我的食譜,我有很多。 Strip從宏中移除換行符,然后所有內容都寫在一行中。
我的最終目標是: proxy := docker run ...
使用.ONESHELL:
行.ONESHELL:
Makefile中的某個地方將所有配方行發送到單個shell調用,您應該發現原始Makefile按預期工作。
當make在一個配方中看到一個多行塊時(即一行所有以\\
結尾的行,除了最后一行),它會將未經修改的塊傳遞給shell。 這通常適用於bash ,除了這里的文檔。
解決此的一種方式是剝去任何尾隨\\
s,則通過所產生的串打壞的eval
。 你可以通過玩${.SHELLFLAGS}
和${SHELL}
做到這一點。 如果您只希望它為一些目標啟動,您可以使用特定於目標的兩種形式。
.PHONY: heredoc
heredoc: .SHELLFLAGS = -c eval
heredoc: SHELL = bash -c 'eval "$${@//\\\\/}"'
heredoc:
@echo First
@cat <<-there \
here line1 \
here anotherline \
there
@echo Last
給
$ make
First
here line1
here anotherline
Last
小心引用,尤金。 請注意這里的作弊:我正在刪除所有反斜杠,而不僅僅是行末端的反斜杠。 因人而異。
使用GNU make,您可以將多行變量與export
指令組合使用多行命令,而無需全局打開.ONESHELL
:
export define script
cat <<'EOF'
here document in multi-line shell snippet
called from the "$@" target
EOF
endef
run:; @ eval "$$script"
會給
here document in multi-line shell snippet
called from the "run" target
您還可以將它與value
函數結合使用,以防止其值被make擴展:
define _script
cat <<EOF
SHELL var expanded by the shell to $SHELL, pid is $$
EOF
endef
export script = $(value _script)
run:; @ eval "$$script"
會給
SHELL var expanded by the shell to /bin/sh, pid is 12712
不是這里的文檔,但這可能是一個有用的解決方法。 它不需要任何GNU Make'isms。 將行放在帶有parens的子shell中,在每行前面加上echo。 在適當的情況下,你需要拖尾晃動和分叉和晃動。
test:
( \
echo echo I Need This ;\
echo echo To Work ;\
echo ls \
) \
| sh
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.