Setting env variables in makefile

Makefile: Setting up environment variables globally as well as from a phony target.

GLOBAL_VAR=GOLBAL_VAR_VALUE # This value will be set as it is global
export GLOBAL_VAR_EXPORT=GLOBAL_VAR_EXPORT_VALUE # This value will be set as it is global
# @$(foreach var, $(filter GB_ENV_%, $(.VARIABLES)), $(eval export $(subst GB_ENV_,,$(var))=$($(var))))
all:
# Following two vars remain unset as Makefile doesn't set var under phony target
@LOCAL_VAR=LOCAL_VAR_VALUE
@export LOCAL_VAR_EXPORT=LOCAL_VAR_EXPORT_VALUE
# Following two vars will be set using eval
@$(eval LOCAL_VAR_EVAL=LOCAL_VAR_EVAL_VALUE)
@$(eval export LOCAL_VAR_EXPORT_EVAL=LOCAL_VAR_EXPORT_EVAL_VALUE)
@echo "GLOBAL_VAR : $(GLOBAL_VAR)"
@echo "GLOBAL_VAR_EXPORT : $(GLOBAL_VAR_EXPORT)"
@echo "LOCAL_VAR : $(LOCAL_VAR)"
@echo "LOCAL_VAR_EXPORT : $(LOCAL_VAR_EXPORT)"
@echo "LOCAL_VAR_EVAL : $(LOCAL_VAR_EVAL)"
@echo "LOCAL_VAR_EXPORT_EVAL : $(LOCAL_VAR_EXPORT_EVAL)"

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