Skip to content
Advertisement

conditional check within makefile

I am updating makefile of the project where I need to perform different steps based on the customer or manufacturing build,
I have written simple makefile as follows and with that I am seeing unexpected output, could someone help figure out issue with 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

I am getting following output of the makefile

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

This is unexpected output, as build type is clearly MANUFACTURING. I remember that makefile works in two phases, I tried fixing it as follows but that too doesn’t work

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

Basically TARGET_DEVICE=myboard_mf it will be manufacturing build and when TARGET_DEVICE=myboard it will be customer build, so instead of searching _mf in all different conditions I want to set to some flag which will indicate build type, and I can use it in all other places.

Any suggestion/pointers to fix it ?

Advertisement

Answer

I’m too tired to explain in great detail why your attempts are not working :). However the answer to your question is, don’t try to mix make preprocessor commands and functions, with recipe scripts. Just don’t mix them together at all, and you won’t run into this confusion about the two phases. I really don’t understand why so many people are gung-ho to write eval statements inside of recipes… it’s weird that it’s so very very common. Really, that construct is only useful in the most esoteric situations.

In any event, if you want these variables set for use “in all other places”, then why are you trying to set them inside a recipe at all?

I would throw away the “all” target completely and write this as:

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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement