Reputation: 25133
I have in my Makefile
rule which setup environment:
.ONESHELL:
set_db_env:
export DB_USER=XXX
export DB_PASS=YYY
May I reuse set_db_env
target?
another_rule: set_db_env
echo ${DB_USER}
I also have found .EXPORT_ALL_VARIABLES but do not understand how to use it.
UPD
I have found this works:
$(shell ${APP} db_env > ${CONF_DIR}/db_env.conf)
include ${CONF_DIR}/db_env.conf
But I do not think this is good approach
Upvotes: 9
Views: 7836
Reputation: 3286
An alternative solution could be to use pattern-specific variables:
My use case:
staging%: DB_HOST="127.0.0.1"
staging%: DB_NAME="yolo"
staging-shell:
@echo "My host is ${DB_HOST}"
staging-server:
@echo "My host is ${DB_HOST}"
Upvotes: 4
Reputation: 99094
In general, variables do not pass from one rule to another. But there is a way to do this with target-specific variables:
another_rule: DB_USER=XXX
another_rule: DB_PASS=YYY
another_rule:
@echo user is ${DB_USER}
@echo pass is $(DB_PASS)
If writing so many extra lines for every rule is too tedious, you can wrap them in a function:
define db_env
$(1): DB_USER=XXX
$(1): DB_PASS=YYY
endef
$(eval $(call db_env,another_rule))
another_rule:
@echo user is ${DB_USER}
@echo pass is $(DB_PASS)
Upvotes: 10