#
# /+\
# +\    Copyright 1993, 2000 Christopher Seiwald.
# \+/
#
# This file is part of Jam - see jam.c for Copyright information.
#

# This file is ALSO:
# Copyright 2001-2004 David Abrahams.
# Copyright 2002-2004 Rene Rivera.
# Copyright 2015 Artur Shepilko.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)

if $(NT)
{
    SLASH ?= \\ ;
}
SLASH ?= / ;


# Glob for patterns in the directories starting from the given start directory,
# up to and including the root of the file-system. We stop globbing as soon as
# we find at least one match.
#
rule find-to-root ( dir : patterns + )
{
    local globs = [ GLOB $(dir) : $(patterns) ] ;
    while ! $(globs) && $(dir:P) != $(dir)
    {
        dir = $(dir:P) ;
        globs = [ GLOB $(dir) : $(patterns) ] ;
    }
    return $(globs) ;
}


# This global will hold the location of the user's boost-build.jam file.
.boost-build-file = ;

# This global will hold the location of the build system bootstrap file.
.bootstrap-file = ;

# Remember the value of $(BOOST_BUILD_PATH) supplied to us by the user.
BOOST_BUILD_PATH.user-value = $(BOOST_BUILD_PATH) ;

# On Unix only, when BOOST_BUILD_PATH is not supplied by the user, set it to a
# sensible default value. This allows Boost.Build to work without any
# environment variables, which is good in itself and also required by the Debian
# Policy.
if ! $(BOOST_BUILD_PATH) && $(UNIX)
{
    BOOST_BUILD_PATH = /usr/share/boost-build ;
}


rule _poke ( module-name ? : variables + : value * )
{
    module $(<)
    {
        $(>) = $(3) ;
    }
}


# This rule can be invoked from an optional user's boost-build.jam file to both
# indicate where to find the build system files, and to load them. The path
# indicated is relative to the location of the boost-build.jam file.
#
rule boost-build ( dir ? )
{
    if $(.bootstrap-file)
    {
        ECHO "Error: Illegal attempt to re-bootstrap the build system by invoking" ;
        ECHO ;
        ECHO "   'boost-build" $(dir) ";'" ;
        ECHO ;
        EXIT "Please consult the documentation at 'http://www.boost.org'." ;
    }

    # Add the given directory to the path so we can find the build system. If
    # dir is empty, has no effect.
    BOOST_BUILD_PATH = $(dir:R=$(.boost-build-file:D)) $(BOOST_BUILD_PATH) ;

    # We might have just modified the *global* value of BOOST_BUILD_PATH. The
    # code that loads the rest of Boost.Build, in particular the site-config.jam
    # and user-config.jam configuration files uses os.environ, so we need to
    # update the value there.
    _poke .ENVIRON : BOOST_BUILD_PATH : $(BOOST_BUILD_PATH) ;

    # Try to find the build system bootstrap file 'bootstrap.jam'.
    local bootstrap-file = [ GLOB $(BOOST_BUILD_PATH) : bootstrap.jam ] ;
    .bootstrap-file = $(bootstrap-file[1]) ;

    # There is no bootstrap.jam we can find, exit with an error.
    if ! $(.bootstrap-file)
    {
        ECHO "Unable to load Boost.Build: could not find build system." ;
        ECHO --------------------------------------------------------- ;
        ECHO "$(.boost-build-file) attempted to load the build system by invoking" ;
        ECHO ;
        ECHO "   'boost-build" $(dir) ";'" ;
        ECHO ;
        ECHO "but we were unable to find \"bootstrap.jam\" in the specified directory" ;
        ECHO "or in BOOST_BUILD_PATH (searching "$(BOOST_BUILD_PATH:J=", ")")." ;
        ECHO ;
        EXIT "Please consult the documentation at 'http://www.boost.org'." ;
    }

    if [ MATCH .*(--debug-configuration).* : $(ARGV) ]
    {
        ECHO "notice: loading Boost.Build from"
            [ NORMALIZE_PATH $(.bootstrap-file:D) ] ;
    }

    # Load the build system, now that we know where to start from.
    include $(.bootstrap-file) ;
}


{
    # We attempt to load "boost-build.jam" by searching from the current
    # invocation directory up to the root of the file-system.
    #
    # boost-build.jam is expected to invoke the "boost-build" rule to load the
    # Boost.Build files.

    local search-path = $(BOOST_BUILD_PATH) $(BOOST_ROOT) ;
    local self = [ SELF_PATH ] ;
    local boost-build-relative = ../../share/boost-build ;
    local self-based-path = [ NORMALIZE_PATH $(boost-build-relative:R=$(self)) ] ;

    local boost-build-files =
        [ find-to-root [ PWD ] : boost-build.jam ]
        [ GLOB $(self-based-path) : boost-build.jam ]
        # Another temporary measure so Jam works with Boost.Build v1.
        [ GLOB $(search-path) : boost-build.jam ] ;

    .boost-build-file = $(boost-build-files[1]) ;

    # There is no boost-build.jam we can find, exit with an error, and
    # information.
    if ! $(.boost-build-file)
    {
        ECHO "Unable to load Boost.Build: could not find \"boost-build.jam\"" ;
        ECHO --------------------------------------------------------------- ;

        if ! [ MATCH .*(bjam).* : $(ARGV[1]:BL) ]
        {
            ECHO "BOOST_ROOT must be set, either in the environment, or " ;
            ECHO "on the command-line with -sBOOST_ROOT=..., to the root" ;
            ECHO "of the boost installation." ;
            ECHO ;
        }

        ECHO "Attempted search from" [ PWD ] "up to the root" ;
        ECHO "at" $(self-based-path) ;
        ECHO "and in these directories from BOOST_BUILD_PATH and BOOST_ROOT: "$(search-path:J=", ")"." ;
        EXIT "Please consult the documentation at 'http://www.boost.org'." ;
    }

    if [ MATCH .*(--debug-configuration).* : $(ARGV) ]
    {
        ECHO "notice: found boost-build.jam at"
            [ NORMALIZE_PATH $(.boost-build-file) ] ;
    }

    # Now load the boost-build.jam to get the build system loaded. This
    # incidentaly loads the users jamfile and attempts to build targets.
    #
    # We also set it up so we can tell whether we are loading the new V2 system
    # or the the old V1 system.
    include $(.boost-build-file) ;

    # Check that, at minimum, the bootstrap file was found.
    if ! $(.bootstrap-file)
    {
        ECHO "Unable to load Boost.Build" ;
        ECHO -------------------------- ;
        ECHO "\"$(.boost-build-file)\" was found by searching from" [ PWD ] "up to the root" ;
        ECHO "and in these directories from BOOST_BUILD_PATH and BOOST_ROOT: "$(search-path:J=", ")"." ;
        ECHO ;
        ECHO "However, it failed to call the \"boost-build\" rule to indicate" ;
        ECHO "the location of the build system." ;
        ECHO ;
        EXIT "Please consult the documentation at 'http://www.boost.org'." ;
    }
}
