代码之家  ›  专栏  ›  技术社区  ›  ART

makefile中的条件检查

  •  0
  • ART  · 技术社区  · 6 年前

    我正在更新项目的makefile,其中我需要根据客户或制造构建执行不同的步骤,
    我已经编写了如下简单的makefile,并且看到了意外的输出, 有人能帮我找出makefile的问题吗。

    PKG_VER             ?= 1.2
    TARGET_DEVICE       ?= myboard_mf
    BUILD_TYPE_CUSTOMER := CUSTOMER
    BUILD_TYPE_MFG      := MANUFACTURING
    BUILD_TYPE          := $(BUILD_TYPE_CUSTOMER)
    TARGET_COMMON       := $(subst _mf,,$(TARGET_DEVICE))
    
    all:
    
    #Check if it is mfg target or customer target
    ifneq (,$(findstring _mf,$(TARGET_DEVICE)))
        $(info common target name:$(TARGET_COMMON))
        $(eval BUILD_TYPE := $(BUILD_TYPE_MFG))
    endif
        $(info build type:$(BUILD_TYPE))
        $(info customer build type string:$(BUILD_TYPE_CUSTOMER))
    
    #If it is customer build check package version.
    ifneq ($(BUILD_TYPE), $(BUILD_TYPE_MFG))
        $(info "inside customer build")
    
    ifneq ($(PKG_VER),)
        $(error pkage version not passed)
    endif
    
    endif
    
    .PHONEY:all
    

    我得到了makefile的以下输出

        common target name:myboard
        build type:MANUFACTURING
        customer build type string:CUSTOMER
        "inside customer build"
        Makefile:12: *** pkage version not passed.  Stop.
    

    这是意外的输出,因为构建类型很明显 MANUFACTURING . 我记得makefile分两个阶段工作,我尝试按如下方式修复它,但也不起作用

    PKG_VER             ?= 1.2
    TARGET_DEVICE       ?= myboard
    BUILD_TYPE_CUSTOMER := CUSTOMER
    BUILD_TYPE_MFG      := MANUFACTURING
    BUILD_TYPE          := $(BUILD_TYPE_CUSTOMER)
    TARGET_COMMON       := $(subst _mf,,$(TARGET_DEVICE))
    
    all:
    
    #Check if it is mfg target or customer target
    ifneq (,$(findstring _mf,$(TARGET_DEVICE)))
        $(info common target name:$(TARGET_COMMON))
        $(eval BUILD_TYPE := $(BUILD_TYPE_MFG))
    endif
        $(info build type:$(BUILD_TYPE))
        $(info customer build type string:$(BUILD_TYPE_CUSTOMER))
    
    #If it is customer build check package version.
    if [ $(BUILD_TYPE) = $(BUILD_TYPE_CUSTOMER) ];then \
        $(info "inside customer build") \
    if [ $(PKG_VER) = "" ];then \
        $(error pkage version not passed) \
    fi \
    fi
    
    .PHONEY:all
    

    大体上 TARGET_DEVICE=myboard_mf 它将在何时进行制造建造 TARGET_DEVICE=myboard 这将是客户构建,所以不是搜索 _mf 在所有不同的情况下,我想设置一些标志来指示构建类型,我可以在所有其他地方使用它。

    有什么建议/指示来修复它吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   MadScientist    6 年前

    我太累了,无法详细解释为什么您的尝试无效:)。然而,您的问题的答案是,不要试图将make预处理器命令和函数与配方脚本混合使用。只要不要把它们混在一起,你就不会对这两个阶段感到困惑。我真不明白为什么这么多人都在拼命写作 eval 配方中的语句。。。奇怪的是,这太普遍了。实际上,这种构造只在最深奥的情况下有用。

    无论如何,如果您希望这些变量设置为“在所有其他地方”使用,那么您为什么要在配方中设置它们呢?

    我会完全抛弃“所有”目标,并将其写成:

    PKG_VER             ?= 1.2
    TARGET_DEVICE       ?= myboard
    BUILD_TYPE_CUSTOMER := CUSTOMER
    BUILD_TYPE_MFG      := MANUFACTURING
    BUILD_TYPE          := $(BUILD_TYPE_CUSTOMER)
    TARGET_COMMON       := $(subst _mf,,$(TARGET_DEVICE))
    
    #Check if it is mfg target or customer target
    ifneq (,$(findstring _mf,$(TARGET_DEVICE)))
        $(info common target name:$(TARGET_COMMON))
        BUILD_TYPE := $(BUILD_TYPE_MFG)
    endif
    $(info build type:$(BUILD_TYPE))
    $(info customer build type string:$(BUILD_TYPE_CUSTOMER))
    
    #If it is customer build check package version.
    ifneq ($(BUILD_TYPE), $(BUILD_TYPE_MFG))
        $(info "inside customer build")
    
        ifneq ($(PKG_VER),)
            $(error pkage version not passed)
        endif
    endif