# Copyright 2002-2017 Rene Rivera
# Copyright 2002-2017 Vladimir Prus
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

import "class" : new ;
import generators ;

class searched-lib-generator : generator
{
    import property-set ;

    rule __init__ ( )
    {
        # The requirements cause the generators to be tried *only* when we are
        # building a lib target with a 'search' feature. This seems ugly --- all
        # we want is to make sure searched-lib-generator is not invoked deep
        # inside transformation search to produce intermediate targets.
        generator.__init__ searched-lib-generator : : SEARCHED_LIB ;
    }

    rule run ( project name ? : property-set : sources * )
    {
        if $(name)
        {
            # If 'name' is empty, it means we have not been called to build a
            # top-level target. In this case, we just fail immediately, because
            # searched-lib-generator cannot be used to produce intermediate
            # targets.

            local properties = [ $(property-set).raw ] ;
            local shared ;
            if <link>shared in $(properties)
            {
                shared = true ;
            }

            local search = [ feature.get-values <search> : $(properties) ] ;

            local a = [ new null-action [ $(property-set).add-raw <relevant>link <relevant>search ] ] ;
            local lib-name = [ feature.get-values <name> : $(properties) ] ;
            lib-name ?= $(name) ;
            local t = [ new searched-lib-target $(lib-name) : $(project)
                : $(shared) : $(search) : $(a) ] ;
            # We return sources for a simple reason. If there is
            #    lib png : z : <name>png ;
            # the 'z' target should be returned, so that apps linking to 'png'
            # will link to 'z', too.
            return [ property-set.create <xdll-path>$(search) <relevant>link <relevant>search <relevant>name ]
                   [ virtual-target.register $(t) ] $(sources) ;
        }
    }
}

generators.register [ new searched-lib-generator ] ;

class searched-lib-target : abstract-file-target
{
    rule __init__ ( name
        : project
        : shared ?
        : search *
        : action
    )
    {
        abstract-file-target.__init__ $(name) : SEARCHED_LIB : $(project)
          : $(action) : ;

        self.shared = $(shared) ;
        self.search = $(search) ;
    }

    rule shared ( )
    {
        return $(self.shared) ;
    }

    rule search ( )
    {
        return $(self.search) ;
    }

    rule actualize-location ( target )
    {
        NOTFILE $(target) ;
    }

    rule relevant ( )
    {
        return [ property-set.create <relevant>link <relevant>search ] ;
    }

    rule path ( )
    {
    }
}
