Home > OS >  Why is my Makefile variable not assigned a default value here?
Why is my Makefile variable not assigned a default value here?

Time:10-21

Whenever I mention any version along with the execution command of this makefile that version should be stored in the variable VERSION, if no version is mentioned the default value should be taken.

This is my Makefile

export BUILDVERSION=$1
$(info    BUILDVERSION is "$(BUILDVERSION)")
ifneq ($(BUILDVERSION),undefined)
    override VERSION := $(BUILDVERSION)
else
    VERSION=VABCD.00.00A001
endif
$(info    VERSION is $(VERSION))

CASE 1: I'm getting this output when I include argument for execution

BUILDVERSION is "VABCD.00.00A001"
VERSION is "VABCD.00.00A001"

CASE 2: I'm getting this output when I do not include argument for execution

BUILDVERSION is ""
VERSION is ""

But, the expected output is

BUILDVERSION is ""
VERSION is "VABCD.00.00A001"

Since, if an argument is not given else case should run. In my code else case is not taken for execution.

Could anyone please help me find what's wrong with my code!

CodePudding user response:

Variable assignments on the command line normally override assignments in the makefile, so unless I am misunderstanding what you want to do, you could simply say

VERSION=VABCD.00.00A002

in the makefile, and then run make with

make VERSION=0.9beta1

to override the version number.

CodePudding user response:

ifneq ($(BUILDVERSION),undefined) means "if the value of BUILDVERSION equals 'undefined' string".

I guess the intent was ifneq ($(origin BUILDVERSION),undefined) (see origin function).

Note that an empty value IS a value, so you may want ifneq ($(BUILDVERSION),) sometimes.

No need for override directive here.

  • Related