Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export variables from makefile rule?

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

like image 250
Eugen Konkov Avatar asked May 11 '18 13:05

Eugen Konkov


2 Answers

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)
like image 190
Beta Avatar answered Oct 12 '22 23:10

Beta


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}"
like image 37
Drachenfels Avatar answered Oct 13 '22 00:10

Drachenfels