Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aakbar5
Last active March 11, 2019 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aakbar5/78449586d451cc52c6308600d2f543ef to your computer and use it in GitHub Desktop.
Save aakbar5/78449586d451cc52c6308600d2f543ef to your computer and use it in GitHub Desktop.
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)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment