Sharing variables across multiple files

View: New views
6 Messages — Rating Filter:   Alert me  

Sharing variables across multiple files

by Fredrik Zetterman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I've been trying to figure out how to share variables across multiple  
Jamfiles, and so far I haven't found any info on how to do so. I however  
found the following statement on the boost build wiki:

"8. Variables, their usage; local, global variables; accessing variables  
cross Jamfiles (again from the above example, I'm unable to access  
BOOST_ROOT variable from anywhere else except the Jamfile that declared  
it, even with the 'local' keyword dropped from variale declaration)."

(http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Build_V2/Documentation_Todo)

A small example from what I'm trying to do:

./Jamfile
----------------------
import os ;
switch [ os.name ]
{
   case "NT" :
     PLATFORM_EXCLUDE = linux ;
   case "*" :
     PLATFORM_EXCLUDE = win32 ;
}

# This works fine
Echo $(PLATFORM_EXCLUDE) ;

build-project source/app ;
----------------------


./source/Jamfile
----------------------
#This doesn't work
Echo $(PLATFORM_EXCLUDE) ;

#Intended usage, should remove all *linux.cpp-files when compiling on NT  
or vice versa
exe app
   : [ glob-tree *.cpp : *$(PLATFORM_EXCLUDE).cpp ]
;
----------------------

I read that each Jamfile has it's own namespace, and that would explain  
why above usage doesn't work.
But is it possible with some tricks (i.e. exporting it somehow)? If so,  
what am I missing? And if it's not possible, then what would you recommend  
me to do?

Best Regards,
Fredrik Zetterman
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost-build

Parent Message unknown Re: Sharing variables across multiple files

by Vladimir Prus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sunday 10 February 2008 20:36:27 Fredrik Zetterman wrote:

> Hi,
>
> I've been trying to figure out how to share variables across multiple  
> Jamfiles, and so far I haven't found any info on how to do so. I however  
> found the following statement on the boost build wiki:
>
> "8. Variables, their usage; local, global variables; accessing variables  
> cross Jamfiles (again from the above example, I'm unable to access  
> BOOST_ROOT variable from anywhere else except the Jamfile that declared  
> it, even with the 'local' keyword dropped from variale declaration)."
>
> (http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Boost.Build_V2/Documentation_Todo)
>
> A small example from what I'm trying to do:
>
> ./Jamfile
> ----------------------
> import os ;
> switch [ os.name ]
> {
>    case "NT" :
>      PLATFORM_EXCLUDE = linux ;
>    case "*" :
>      PLATFORM_EXCLUDE = win32 ;
> }
>
> # This works fine
> Echo $(PLATFORM_EXCLUDE) ;
>
> build-project source/app ;
> ----------------------
>
>
> ./source/Jamfile
> ----------------------
> #This doesn't work
> Echo $(PLATFORM_EXCLUDE) ;
>
> #Intended usage, should remove all *linux.cpp-files when compiling on NT  
> or vice versa
> exe app
>    : [ glob-tree *.cpp : *$(PLATFORM_EXCLUDE).cpp ]
> ;
> ----------------------
>
> I read that each Jamfile has it's own namespace, and that would explain  
> why above usage doesn't work.
> But is it possible with some tricks (i.e. exporting it somehow)? If so,  
> what am I missing? And if it's not possible, then what would you recommend  
> me to do?

One way is to use the 'constant' and 'path-constant' rules to define
a variable in Jamroot or Jamfile. The variable will be available for
all child Jamfiles.

A dirty way is to use modules.peek and modules.poke to set an read
a variable in a global module:

        modules.poke : FOO : value ;
        local x = [ modules.peek : FOO ] ;

- Volodya

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost-build

Parent Message unknown Re: Sharing variables across multiple files

by Jurko Gospodnetić :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

   Hi Fredrik.

> I've been trying to figure out how to share variables across multiple  
> Jamfiles, and so far I haven't found any info on how to do so. I however  
> found the following statement on the boost build wiki:
>
[...many lines snipped...]
> I read that each Jamfile has it's own namespace, and that would explain  
> why above usage doesn't work.
> But is it possible with some tricks (i.e. exporting it somehow)? If so,  
> what am I missing? And if it's not possible, then what would you recommend  
> me to do?

   I believe what you're looking for are the path-constant & constant
rules defined by Boost Build.

Example:
   path-constant SOURCE_BASE  : "./../../../../Source" ;
   constant      BOOSTVERSION : "boost_1_34_1"         ;

   It is true that all Jamfiles use a separate namespace (module in
Boost Build lingo) and that is why variables from one Jamfile are not
directly accessible from other Jamfiles.


   There is also another way to achieve this which would also allow you
to make non-constant shareable variables but it takes a bit more work:
   1. Place your values in a separate module
   2. Read them from other modules using the modules.peek rule.
   3. Write to them from other modules using the modules.poke rule.

   Example:

module AAA
{
   xxx = "Here I am!" ;
}

# Reading...
yyy = [ modules.peek AAA : xxx ] ;

# Writing...
modules.poke AAA : xxx : new-value ;


   constant rules work around the problem of you not knowing what a
Jamfile module is called and so not being able to access it directly as
shown above, at least not without using some internal implementation
details. What they do is ensure that a copy of their constant values is
made in each new Jamfile module. That should also explain why those
variables are 'constants' - there is actually no single variable but one
copy per Jamfile.

   path-constant rule helps with automatically rooting specified
relative paths based to your project's folder.

   Hope this helps.

   Best regards,
     Jurko Gospodnetić

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost-build

Re: Sharing variables across multiple files

by Jurko Gospodnetić :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

   Hi Volodya.

   Should we add this as a FAQ with a bit more elaborate explanation?
This is one of the questions my people asked me several times when
learning to use Boost Build...

   Best regards,
     Jurko Gospodnetić

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost-build

Re: Sharing variables across multiple files

by Vladimir Prus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sunday 10 February 2008 22:04:52 Jurko Gospodnetić wrote:
>    Hi Volodya.
>
>    Should we add this as a FAQ with a bit more elaborate explanation?
> This is one of the questions my people asked me several times when
> learning to use Boost Build...

Good idea. Can you add an issue an assigned to the upcoming "docs" milestone?

- Volodya
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost-build

Re: Sharing variables across multiple files

by Jurko Gospodnetić :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

   Hi Volodya.

> Good idea. Can you add an issue an assigned to the upcoming "docs" milestone?

   Added as a task ticket #168:

   https://zigzag.cs.msu.su:7813/boost.build/ticket/168

   Best regards,
     Jurko Gospodnetić

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost-build