问题描述
我有一个复杂的逻辑来检测Makefile变量的路径.我放弃了用make语言进行此操作,因此我用Python对其进行了编码,并希望将代码嵌入Makefile中以设置变量.但是,这不起作用:
I have a complex logic for detecting path for Makefile variable. I gave up on doing this in make language, so I coded it in Python, and want to embed the code into Makefile to set variable. However, this doesn't work:
define DETECT_SDK
import os
locations = [
"../google_appengine",
"/usr/local/google_appengine",
"../.locally/google_appengine",
]
for path in locations:
if os.path.exists(path):
print(path)
break
else:
print(".")
endef
SDK_PATH ?= $(shell python -c $(DETECT_SDK))
default:
python -c 'import sys; print(sys.argv)' $(SDK_PATH)
更新:更新了以前,它因Makefile:2: *** missing separator. Stop.
失败.现在它失败并出现另一个错误:
UPDATE: Updated multiline definition from Is it possible to create a multi-line string variable in a MakefilePreviously it was failing with Makefile:2: *** missing separator. Stop.
. Now it fails with another error:
/bin/sh: 1: Syntax error: "(" unexpected
python -c 'import sys; print(sys.argv)'
['-c']
推荐答案
您必须引用传递给Python的字符串:
You have to quote the string you pass to Python:
SDK_PATH ?= $(shell python -c '$(DETECT_SDK)')
否则,shell将在尝试解析Python脚本时会感到困惑.
Otherwise the shell will be confused trying to parse the Python script.
我不明白您的Python脚本.您的缩进是错误的,因此应该将else
附加到if(grrr ...)上,否则您将丢失break
语句...或者else
可能是无用的.在撰写本文时,它将生成一个以换行符分隔的现有路径列表以及.".如果您描述的是您真正想做的事情,而不仅仅是说您放弃了,我们可以为您提供帮助.
I don't understand your Python script though. Either your indentation is wrong, so the else
is supposed to be attached to the if (grrr...) or else you're missing a break
statement... or possibly the else
is just useless. As it's written, it will generate a newline-separated list of paths that exist, plus ".". If you describe what you're really trying to do, rather than just say you gave up, we can help.
例如,如果您要打印的是该列表或."中的第一个现有路径.如果不存在(即您的Python循环在print
之后缺少break
),那么您可以在GNU make中轻松地做到这一点:
For example, if what you want to do is print the first existing path in that list or "." if none exist (that is, your Python loop is missing a break
after the print
), then you can easily do this in GNU make:
SDK_PATH_LIST = ../google/appengine /usr/local/google_appengine ../.locally/google_appengine
SDK_PATH ?= $(firstword $(wildcard $(SDK_PATH_LIST:%=%/.)) .)
这篇关于将Python嵌入Makefile中以设置make变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!