首页 > 解决方案 > 在 Makefile 中使用条件运算符比较两个变量

问题描述

下面是我的 Makefile。我只想使用 && 运算符(或其等价物)进行比较,如以下伪代码所示。我想在“all”目标中运行以下逻辑

#   if (CUR_PI_VERSION == LAST_PI_VERSION) && (CUR_GIT_VERSION == LAST_GIT_VERSION) 
#       print "everything matched. Nothing to do"
#   else
#       print "files not matched"
#       run python script.
#
#   How do I achieve this.

我已经查看了其他答案,但我无法得到我希望的结果。我附上了我的示例代码以供参考。

CUR_PI_VERSION:= "abc"
CUR_GIT_VERSION:= "cde"

LAST_PI_VERSION:= "abc"
LAST_GIT_VERSION:= "cde"

$(info $$CUR_GIT_VERSION is [${CUR_GIT_VERSION}])
$(info $$CUR_PI_VERSION is [${CUR_PI_VERSION}])

$(info $$LAST_GIT_VERSION is [${LAST_GIT_VERSION}])
$(info $$LAST_PI_VERSION is [${LAST_PI_VERSION}])

all:

# The pseudocode of what I want to do is as follows 
#   if (CUR_PI_VERSION == LAST_PI_VERSION) && (CUR_GIT_VERSION == LAST_GIT_VERSION) 
#       print "everything matched. Nothing to do"
#   else
#       print "files not matched"
#       run python script.
#
#   How do I achieve this.
#
    ifeq ($(CUR_GIT_VERSION),$(LAST_GIT_VERSION))
        ifeq ($(CUR_FPI_VERSION),$(LAST_FPI_VERSION))
            echo "Everything matched, so don't need the make top"
        endif 
    endif

非常感谢任何帮助。

标签: linuxmakefilecomparison-operators

解决方案


你可以试试这个:

all:
ifeq ($(CUR_PI_VERSION)@$(CUR_GIT_VERSION),$(LAST_PI_VERSION)@$(LAST_GIT_VERSION))
    echo "Everything matched, so don't need the make top"
endif

上面的测试比较了两个连接的字符串(由 连接@):

$(CUR_PI_VERSION)@$(CUR_GIT_VERSION)

$(LAST_PI_VERSION)@$(LAST_GIT_VERSION)

推荐阅读