help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Export a function


From: Paul Smith
Subject: Re: Export a function
Date: Fri, 18 Jun 2021 09:00:40 -0400
User-agent: Evolution 3.36.5-0ubuntu1

On Fri, 2021-06-18 at 11:36 +0200, G wrote:
> Passed variables to "imported" func. submake are not passed at all or
> are empty.
> 
> How do I get around the problem?

The best answer is, "don't do that".

As the stackoverflow answer points out, make will EXPAND the value of
every variable it exports.  This is obviously correct behavior, because
exported variables were not intended to be used to send large complex
scripts between different instances of make.  They were intended to set
environment variables, like the shell does, to communicate with
programs that make invokes.

If make did not expand exported variables before putting them into the
environment, then things like this:

  BINDIR = bin

  export PATH := $(PATH):$(BINDIR)

would clearly not work correctly.

The right way to reuse the same complex content between multiple
instances of make is through included files; take the content you want
to share, put it into a separate makefile, and then add:

  include sharedstuff.mk

to your makefiles.


If you REALLY REALLY want to do this, there's no way to do it other
than creating a separate variable to be exported.  You can take
advantage of the $(value ...) function to prevent the content of the
variable from being expanded.

For example if you have this:

   $ cat Makefile

   BAR = bar
   export FOO = $(BAR)

   all: ; $(MAKE) -f submake.mk

   $ cat submake.mk

   BAR = notbar
   all: ; @echo FOO=$(FOO)


The output of running it will be "FOO=bar", not "FOO=notbar", because
FOO was expanded by the parent makefile.

On the other hand if you use this:

   $ cat Makefile

   BAR = bar
   FOO = $(BAR)
   export EXFOO = $(value FOO)

   all: ; $(MAKE) -f submake.mk

   $ cat submake.mk

   BAR = notbar
   FOO = $(EXFOO)
   all: ; @echo FOO=$(FOO)

the output will be "FOO=notbar" which is what you want, because EXFOO
was expanded but that puts the _value_ of the FOO variable into the
environment.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]