Search code examples
makefilegnu-make

exporting env variables from a common target to all targets in Makefile


I have a common target with setting a bunch of env variables something like this in Makefile

# file common.mk
setup :
    export LD_LIBRARY_PATH=${MY_HOME}/LIBS:${LD_LIBRARY_PATH}
    export MY_VERSION=2.2
    ...

Now I want to use these variables in all make targets

#file Makefile

include ${MY_RUN_DIR}/common.mk
run : setup
    # export all the variables 
    # do domething

Now when I do make run I cannot see the variables being set. It is because all the variables are set only in "setup" target. I have like 100s of Makefile where I will have to add all the export variables and I would prefer to add only one line instead to export all variables.

How can I export variables from one target to all targets wherever I want?


Solution

  • Just don't put the assignments in a rule:

    # file common.mk
    
    export LD_LIBRARY_PATH=${MY_HOME}/LIBS:${LD_LIBRARY_PATH}
    export MY_VERSION=2.2
    ...
    

    and don't bother with the prerequisite setup:

    #file Makefile
    
    include ${MY_RUN_DIR}/common.mk
    run:
    #  do something, such as
        @echo my version is $$MY_VERSION