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
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)
Alternative solution could be to use pattern specific variables:
My use case:
staging%: export DB_HOST="127.0.0.1"
staging%: export DB_NAME="yolo"
staging-shell:
@echo "My host is ${DB_HOST}"
staging-server:
@echo "My host is ${DB_HOST}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With