Gnu make 检索makefile中对python的shell调用的输出:是否缺少步骤?
我有以下Gnu make 检索makefile中对python的shell调用的输出:是否缺少步骤?,gnu-make,Gnu Make,我有以下makefile: # bash version SHELL:=/usr/bin/env bash H:=$(shell pwd) install: @echo "$H::make.$@: not implemented!" 和python3版本: SHELL:=/usr/bin/env python3 H:=$(shell "import os,sys; print(os.getcwd(),file=sys.stdout)")
makefile
:
# bash version
SHELL:=/usr/bin/env bash
H:=$(shell pwd)
install:
@echo "$H::make.$@: not implemented!"
和python3版本:
SHELL:=/usr/bin/env python3
H:=$(shell "import os,sys; print(os.getcwd(),file=sys.stdout)")
install:
@print("$H::make.$@: not implemented!")
bash
版本工作得很好,而python3
版本工作得很好,persay。但是,路径变量$H
为空
检查shell调用中的python表达式可以确认它正在运行python代码:
python3 -c "import os,sys; print(os.getcwd(),file=sys.stdout)"
//works
如何在
shell
调用中遵守预期的行为,以便make拾取输出?您编写的shell命令实际上并不是这样做的
这:
原因相当于:
/usr/bin/env bash -c 'pwd'
待运行
那么这个,
SHELL:=/usr/bin/env python3
H:=$(shell "import os,sys; print(os.getcwd(),file=sys.stdout)")
导致运行与此等效的程序:
/usr/bin/env python3 -c '"import os,sys; print(os.getcwd(),file=sys.stdout)"'
待运行。Python中的字符串只是一个no-op语句,因此它不起任何作用
删除引号:
H := $(shell import os,sys; print(os.getcwd(),file=sys.stdout))
明白了——用bash而不是python检查引号。我想括号是个问题,但听起来好像
$
是make的神奇角色;应该不断尝试。只有当你有非偶数个paren时,括号才是一个问题:GNU make做匹配。如果您这样做,那么您应该使用带大括号的替代形式${shell…}
。如果有不匹配的括号和括号,则需要将脚本放入变量中,并使用$(shell$(SCRIPTVAR))
。
H := $(shell import os,sys; print(os.getcwd(),file=sys.stdout))