我需要有条件地执行一些make规则,只有当安装的Python大于某个版本(比如2.5)。

我想我可以做一些类似执行的事情:

python -c 'import sys; print int(sys.version_info >= (2,5))'

然后在ifeq make语句中使用输出(ok时为'1',否则为'0')。

在一个简单的bash shell脚本中,它只是:

MY_VAR=`python -c 'import sys; print int(sys.version_info >= (2,5))'`

但这在Makefile中不起作用。

有什么建议吗?我可以使用任何其他明智的解决办法来实现这一点。


当前回答

在eval中包装任务对我来说是有效的。

# dependency on .PHONY prevents Make from 
# thinking there's `nothing to be done`
set_opts: .PHONY
  $(eval DOCKER_OPTS = -v $(shell mktemp -d -p /scratch):/output)

其他回答

下面是一个稍微复杂一点的例子,在recipe中使用管道和变量赋值:

getpodname:
    # Getting pod name
    @eval $$(minikube docker-env) ;\
    $(eval PODNAME=$(shell sh -c "kubectl get pods | grep profile-posts-api | grep Running" | awk '{print $$1}'))
    echo $(PODNAME)

使用内置的Make shell,比如MY_VAR=$(shell echo whatever)

me@Zack:~$make
MY_VAR IS whatever

me@Zack:~$ cat Makefile MY_VAR:= $(shell echo whatever) 全部: MY_VAR是$(MY_VAR)

从制作手册

shell赋值操作符` != '可用于执行shell脚本并将>变量设置为其输出。这个运算符首先计算右边的值,然后将结果>传递给shell执行。如果执行结果以>换行符结束,则删除该换行符;所有其他换行符都替换为空格。然后将>结果字符串放入命名的递归展开变量中。>的例子: = printf '\043' File_list != find。- name ' * . c '

在下例中,我将Makefile文件夹路径存储到LOCAL_PKG_DIR,然后在目标中使用LOCAL_PKG_DIR变量。

Makefile:

LOCAL_PKG_DIR := $(shell eval pwd)

.PHONY: print
print:
    @echo $(LOCAL_PKG_DIR)

终端输出:

$ make print
/home/amrit/folder

With GNU Make, you can use shell and eval to store, run, and assign output from arbitrary command line invocations. The difference between the example below and those which use := is the := assignment happens once (when it is encountered) and for all. Recursively expanded variables set with = are a bit more "lazy"; references to other variables remain until the variable itself is referenced, and the subsequent recursive expansion takes place each time the variable is referenced, which is desirable for making "consistent, callable, snippets". See the manual on setting variables for more info.

# Generate a random number.
# This is not run initially.
GENERATE_ID = $(shell od -vAn -N2 -tu2 < /dev/urandom)

# Generate a random number, and assign it to MY_ID
# This is not run initially.
SET_ID = $(eval MY_ID=$(GENERATE_ID))

# You can use .PHONY to tell make that we aren't building a target output file
.PHONY: mytarget
mytarget:
# This is empty when we begin
    @echo $(MY_ID)
# This recursively expands SET_ID, which calls the shell command and sets MY_ID
    $(SET_ID)
# This will now be a random number
    @echo $(MY_ID)
# Recursively expand SET_ID again, which calls the shell command (again) and sets MY_ID (again)
    $(SET_ID)
# This will now be a different random number
    @echo $(MY_ID)