How to export variables from makefile rule?

Solution 1:

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)