#  (C) Copyright David Abrahams 2001. Permission to copy, use, modify, sell and
#  distribute this software is granted provided this copyright notice appears in
#  all copies. This software is provided "as is" without express or implied
#  warranty, and with no claim as to its suitability for any purpose.

# Essentially an include guard; ensures that no module is loaded multiple times
.loaded ?= ;

# A list of modules currently being loaded for error reporting of circular dependencies
.loading ?= ;

# A list of modules needing to be tested via __test__ rule
.untested ?= ;

# A list of modules which have been tested via __test__
.tested ?= ;

# meant to be invoked from import when no __test__ rule is defined in a given
# module
local rule no_test_defined
{
    if ! ( --quiet in [ peek : ARGV ] )
    {
        ECHO warning: no __test__ rule defined in module [ CALLER_MODULE ] ;
    }
}

# return the binding of the given module
rule binding ( module )
{
    return $($(module).__binding__) ;
}

# Sets the module-local value of a variable.  This is the most
# reliable way to set a module-local variable in a different module;
# it eliminates issues of name shadowing due to dynamic scoping.
rule poke ( module-name ? : variables + : value * )
{
    module $(<)
    {
        $(>) = $(3) ;
    }
}

# Returns the module-local value of a variable.  This is the most
# reliable way to examine a module-local variable in a different
# module; it eliminates issues of name shadowing due to dynamic
# scoping.
rule peek ( module-name ? : variables + )
{
    module $(<)
    {
        return $($(>)) ;
    }
}

# Call the given rule locally in the given module. Use this for rules
# which accept rule names as arguments, so that the passed rule may be
# invoked in the context of the rule's caller (for example, if the
# rule accesses module globals or is a local rule).
rule call-in ( module-name ? : rule-name args * : * )
{
    module $(module-name)
    {
        return [ $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ] ;
    }
}

# load the indicated module if it is not already loaded.
rule load (
  module-name      # name of module to load. Rules will be defined in this module
    : filename ?   # (partial) path to file; Defaults to $(module-name).jam
    : search *     # Directories in which to search for filename. Defaults to $(BOOST_BUILD_PATH)
)
{
    # Avoid loading modules twice
    if ! ( $(module-name) in $(.loaded) )
    {
        filename ?= $(module-name).jam ;
        
        # Mark the module loaded so we don't try to load it recursively
        .loaded += $(module-name) ;
        
        # suppress tests if any module loads are already in progress. 
        local suppress-test = $(.loading[1]) ;
        
        # Push this module on the loading stack
        .loading += $(module-name) ;
        
        # Remember that it's untested
        .untested += $(module-name) ; 
        
        # Insert the new module's __name__ and __file__ globals
        poke $(module-name) : __name__ : $(module-name) ;
        poke $(module-name) : __file__ : $(filename) ;
        
        module $(module-name)
        {
            # Prepare a default behavior, in case no __test__ is defined.
            IMPORT modules : no_test_defined : $(__name__) : __test__ ;

            # Add some grist so that the module will have a unique target name
            local module-target = $(__file__:G=module@) ;
            
            local search = $(3) ;
            search ?= [ modules.peek : BOOST_BUILD_PATH ] ;
            SEARCH on $(module-target) = $(search) ;
            BINDRULE on $(module-target) = modules.record-binding ;
            include $(module-target) ;
        }
        
        # Pop the loading stack. Must happen before testing or we'll find a circular loading dependency
        .loading = $(.loading[1--2]) ;
        
        # Run any pending tests if this is an outer load
        if ! $(suppress-test)
        {
            local argv = [ peek : ARGV ] ;
            for local m in $(.untested)
            {
                if ( ! $(m) in $(.tested) )    # avoid recursive test invocations
                   && ( ( --debug in $(argv) ) || ( --debug-module=$(m) in $(argv) ) )
                {
                    .tested += $(m) ;
                    if ! ( --quiet in $(argv) )
                    {
                        ECHO testing module $(m)... ;
                    }
                    
                    module $(m)
                    {
                        __test__ ;
                    }
                }
            }
            .untested = ;
        }
    }
    else if $(module-name) in $(.loading)
    {
        ECHO loading \"$(module-name)\" ;
        ECHO circular module loading dependency: ;
        EXIT $(.loading)" ->" $(module-name) ;
    }
}

# This helper is used by load (above) to record the binding (path) of
# each loaded module.
rule record-binding ( module-target : binding )
{
    $(.loading[-1]).__binding__ = $(binding) ;
}

# load the indicated module and import rule names into the current
# module. Any members of rules-opt will be available without
# qualification in the caller's module. Any members of rename-opt will
# be taken as the names of the rules in the caller's module, in place
# of the names they have in the imported module. If rules-opt = '*',
# all rules from the indicated module are imported into the caller's
# module. If rename-opt is supplied, it must have the same number of
# elements as rules-opt.
rule import ( module-name : rules-opt * : rename-opt * )
{
    load $(module-name) ;
    
    local source-names = $(rules-opt) ;
    if $(rules-opt) = *
    {
        source-names = [ RULENAMES $(module-name) ] ;
    }
    
    local target-names = $(rename-opt) ;
    target-names ?= $(source-names) ;
    IMPORT $(module-name) : $(source-names) : [ CALLER_MODULE ] : $(target-names) ;
}

# Define exported copies in $(target-module) of all rules exported
# from $(source-module).  Also make them available in the global
# module with qualification, so that it is just as though the rules
# were defined originally in $(target-module).
rule clone-rules (
    source-module
    target-module 
    )
{
    local rules = [ RULENAMES $(source-module) ] ;
    
    IMPORT $(source-module) : $(rules) : $(target-module) : $(rules) : LOCALIZE ;
    EXPORT $(target-module) : $(rules) ;
    IMPORT $(target-module) : $(rules) : : $(target-module).$(rules) ;
}

local rule __test__ ( )
{
    import assert ;
    
    module modules.__test__
    {
        foo = bar ;
    }
    
    assert.result bar : peek modules.__test__ : foo ;
    poke modules.__test__ : foo : bar baz ;
    assert.result bar baz : peek modules.__test__ : foo ;
}


