src/mulle-bootstrap-dependency-resolve.sh
c81c8721
 #! /usr/bin/env bash
5c4e1033
 #
 #   Copyright (c) 2016 Nat! - Mulle kybernetiK
 #   All rights reserved.
 #
 #   Redistribution and use in source and binary forms, with or without
 #   modification, are permitted provided that the following conditions are met:
 #
 #   Redistributions of source code must retain the above copyright notice, this
 #   list of conditions and the following disclaimer.
 #
 #   Redistributions in binary form must reproduce the above copyright notice,
 #   this list of conditions and the following disclaimer in the documentation
 #   and/or other materials provided with the distribution.
 #
 #   Neither the name of Mulle kybernetiK nor the names of its contributors
 #   may be used to endorse or promote products derived from this software
 #   without specific prior written permission.
 #
 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 #   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 #   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 #   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 #   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 #   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 #   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 #   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 #   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 #   POSSIBILITY OF SUCH DAMAGE.
 #
9339a58a
 MULLE_BOOTSTRAP_DEPENDENCY_RESOLVE_SH="included"
5c4e1033
 
 
8f951e19
 _dependency_add()
5c4e1033
 {
394d4b93
    local map="$1"
    local name="$2"
    local sub_name="$3"
5c4e1033
 
 
    local escaped_dependencies
    local dependencies
 
    escaped_dependencies="`assoc_array_get "${map}" "${name}"`"
f36a470f
    dependencies="`unescape_linefeeds "${escaped_dependencies}"`"
5c4e1033
 
    if array_contains "${dependencies}" "${sub_name}"
    then
       if [ ! -z "${map}" ]
       then
          echo "${map}"
       fi
       return
    fi
 
    dependencies="`array_add "${dependencies}" "${sub_name}"`"
f36a470f
    escaped_dependencies="`escape_linefeeds "${dependencies}"`"
5c4e1033
 
    assoc_array_set "${map}" "${name}" "${escaped_dependencies}"
 }
 
 
8f951e19
 dependency_add()
 {
f67f6857
    if [ "${MULLE_TRACE_RESOLVER_FLIP_X}" = "YES" ]
8f951e19
    then
394d4b93
       set +x
8f951e19
    fi
 
    _dependency_add "$@"
 
f67f6857
    if [ "${MULLE_TRACE_RESOLVER_FLIP_X}" = "YES" ]
8f951e19
    then
394d4b93
       set -x
8f951e19
    fi
 }
 
 
 _dependency_add_array()
8e31b74a
 {
394d4b93
    local map="$1"
    local name="$2"
    local array="$3"
8e31b74a
 
5c06c0e8
    local sub_name
8e31b74a
 
    IFS="
 "
    for sub_name in ${array}
    do
5c06c0e8
       IFS="${DEFAULT_IFS}"
 
8f951e19
       map="`_dependency_add "${map}" "${name}" "${sub_name}"`"
8e31b74a
    done
 
5c06c0e8
    IFS="${DEFAULT_IFS}"
8e31b74a
 
    echo "${map}"
 }
 
 
8f951e19
 dependency_add_array()
 {
f67f6857
    if [ "${MULLE_TRACE_RESOLVER_FLIP_X}" = "YES" ]
8f951e19
    then
       set +x
    fi
 
    _dependency_add_array "$@"
 
f67f6857
    if [ "${MULLE_TRACE_RESOLVER_FLIP_X}" = "YES" ]
8f951e19
    then
       set -x
    fi
 }
 
 
fcf4306a
 _dependency_resolve()
 {
394d4b93
    local map="$1"
    local name="$2"
fcf4306a
 
0d78d4c5
    if [ "$MULLE_FLAG_LOG_SETTINGS" = "YES" -o "$MULLE_FLAG_LOG_MERGE" = "YES"  ]
fcf4306a
    then
       log_trace2 "resolve ${name}"
    fi
 
    local escaped_dependencies
    local dependencies
 
    escaped_dependencies="`assoc_array_get "${map}" "${name}"`"
    dependencies="`unescape_linefeeds "${escaped_dependencies}"`"
 
    UNRESOLVED_DEPENDENCIES="`array_add "${UNRESOLVED_DEPENDENCIES}" "${name}"`"
 
    local sub_name
    #local insert
 
    #insert="`array_count "${RESOLVED_DEPENDENCIES}"`"
 
    IFS="
 "
    for sub_name in ${dependencies}
    do
       IFS="${DEFAULT_IFS}"
 
       if array_contains "${RESOLVED_DEPENDENCIES}" "${sub_name}"
       then
          continue
       fi
 
       if array_contains "${UNRESOLVED_DEPENDENCIES}" "${sub_name}"
       then
          fail "circular dependency ${sub_name} and ${name}"
       fi
 
       _dependency_resolve "${map}" "${sub_name}"
    done
 
    IFS="${DEFAULT_IFS}"
 
    UNRESOLVED_DEPENDENCIES="`array_remove "${UNRESOLVED_DEPENDENCIES}" "${name}"`"
    RESOLVED_DEPENDENCIES="`array_add "${RESOLVED_DEPENDENCIES}" "${name}"`"
8f951e19
 
    :
fcf4306a
 }
 
 
5c4e1033
 dependency_resolve()
 {
394d4b93
    local map="$1"
    local name="$2"
5c4e1033
 
d87a5262
    log_debug ":dependency_resolve:"
 
5c4e1033
    RESOLVED_DEPENDENCIES=
    UNRESOLVED_DEPENDENCIES=
 
f67f6857
    if [ "${MULLE_TRACE_RESOLVER_FLIP_X}" = "YES" ]
8f951e19
    then
       set +x
    fi
 
8e31b74a
    #
    # _dependency resolve tries to preserve order, but its sorted in reverse
    #
5c4e1033
    _dependency_resolve "${map}" "${name}"
 
f67f6857
    if [ "${MULLE_TRACE_RESOLVER_FLIP_X}" = "YES" ]
8f951e19
    then
       set -x
    fi
 
8e31b74a
    if [ ! -z "${UNRESOLVED_DEPENDENCIES}" ]
5c4e1033
    then
8e31b74a
       log_error "unresolved dependencies ${UNRESOLVED_DEPENDENCIES}"
       return 1
    else
       echo "${RESOLVED_DEPENDENCIES}" # | sed -n '1!G;h;$p'
5c4e1033
    fi
8f951e19
 
    :
5c4e1033
 }
 
 
8f77be37
 dependency_resolve_initialize()
 {
f1f83418
    [ -z "${MULLE_BOOTSTRAP_LOGGING_SH}" ] && . mulle-bootstrap-logging.sh
 
9339a58a
    log_debug ":dependency_resolve_initialize:"
f1f83418
 
ec2b9bc2
    [ -z "${MULLE_BOOTSTRAP_ARRAY_SH}" ] && . mulle-bootstrap-array.sh
8f951e19
    :
8f77be37
 }
 
ec2b9bc2
 dependency_resolve_initialize