Makefile: value assignment to variable

Makefile: Exploring different ways to set value of a variable
# Ref: https://www.gnu.org/software/make/manual/html_node/Setting.html#Setting
########
# Setting variables:
# ?= Set variable if it is empty
# := Expand value variable before assignment,
# NOTE: (1) The behaviour of ::= is same as :=
# (2) Expansion of value variable will be only once at the of value assignment.
# = Simple variable assignment, value will evaluated/expanded on its usage
# += Append value to variable
# != Value variable will be evaluated before assignment.
# override directive
########
# UnComment following line NOT_SET_VAR will have VAL_0
# otherwise VAL_1
# NOT_SET_VAR = VAL_0
# Only set value to VAL_1 if NOT_SET_VAR hasn't got any value
NOT_SET_VAR ?= VAL_1
########
########
# EXPAND_VAR_ASSIGNMENT should be set to ZERO
VAR_0 := 10
VAR_1 := $(VAR_0)
ifeq ($(VAR_1),10)
EXPAND_VAR_ASSIGNMENT := ZERO
else
EXPAND_VAR_ASSIGNMENT := ONE
endif
########
# Simple assignment
VAR_X = 100
VAR_Y = 200
VAR_Z = $(VAR_X),$(VAR_Y),300 # On usage of VAR_Z, it will be expanded
# and it will end up with 100,200,300
# UnComment following line, the VAR_Z will show 200,200,300
# VAR_X = 200
########
# Appending
VAR_APPEND := 1
VAR_APPEND += 2 # VAR_APPEND will have 1 2
########
# Override: If a var has been set with command argument then
# simple assignment will not change its value.
# make -f variables.mk VAR_OVERRIDE_0=300
override VAR_OVERRIDE_0 = 200
override VAR_OVERRIDE_1 := 200
override VAR_OVERRIDE_APPEND += 200
all:
@echo "NOT_SET_VAR is having $(NOT_SET_VAR)"
@echo "EXPAND_VAR_ASSIGNMENT is having $(EXPAND_VAR_ASSIGNMENT)"
@echo "VAR_Z is having $(VAR_Z)"
@echo "VAR_APPEND is having $(VAR_APPEND)"
@echo "VAR_OVERRIDE_0 is having $(VAR_OVERRIDE_0)"
@echo "VAR_OVERRIDE_1 is having $(VAR_OVERRIDE_1)"
@echo "VAR_OVERRIDE_APPEND is having $(VAR_OVERRIDE_APPEND)"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s