在 makefile 中检索对 python 的 shell 调用的输出:是否缺少步骤?

Retrieve output from a shell call to python in a makefile: is there a missing step?

我有以下 makefiles:

# 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 命令的实际作用。

这个:

SHELL:=/usr/bin/env bash
H:=$(shell pwd)

导致相当于:

/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 中的字符串只是一个无操作语句,所以它什么都不做。

删除引号:

H := $(shell import os,sys; print(os.getcwd(),file=sys.stdout))