[3c10176] | 1 | #!/bin/bash
|
---|
| 2 | #
|
---|
| 3 | # $Id$
|
---|
| 4 | #
|
---|
| 5 |
|
---|
[e576789] | 6 | # A string of spaces for indenting:
|
---|
[3c10176] | 7 | declare -a spaceSTR=" "
|
---|
[e576789] | 8 | declare -a exchange_triplet
|
---|
| 9 |
|
---|
| 10 | # In case we find a cirdular dependency, it has the form :
|
---|
| 11 | # parent->dependency_0->...->dependency_n->dependency_0
|
---|
| 12 | # If we want to build dependency_n before dependency_0,
|
---|
| 13 | # no problem: we just prune the tree at dependency_n.
|
---|
| 14 | # If we want to build first dependency_0, we need to
|
---|
| 15 | # put dependency_n as a dependency of parent. The triplet
|
---|
| 16 | # above shall contain (parent dependency_0 dependency_n)
|
---|
[3c10176] | 17 |
|
---|
| 18 | #----------------------------#
|
---|
| 19 | generate_dependency_tree() { #
|
---|
| 20 | #----------------------------#
|
---|
| 21 | : <<inline_doc
|
---|
[e576789] | 22 | function: Create a subtree of the dependency tree
|
---|
| 23 | (recursive function)
|
---|
| 24 | input vars: $1 : file with a list of targets
|
---|
| 25 | the first line of the file is an array
|
---|
| 26 | of links
|
---|
| 27 | externals: vars: BLFS_XML
|
---|
[3c10176] | 28 | DEP_LEVEL
|
---|
[e576789] | 29 | modifies: vars: none
|
---|
[3c10176] | 30 | returns: nothing
|
---|
[e576789] | 31 | output: files: for each pkg with dependencies in $1,
|
---|
| 32 | a file pkg.dep and its dependencies
|
---|
[3c10176] | 33 | on error: nothing
|
---|
| 34 | on success: nothing
|
---|
| 35 | inline_doc
|
---|
| 36 |
|
---|
[e576789] | 37 | local DepFile=$1
|
---|
| 38 | local -a rootlink
|
---|
| 39 | local -a otherlink
|
---|
| 40 | local -i depth
|
---|
| 41 | local -i count=0
|
---|
| 42 | local id_of_dep
|
---|
| 43 | local parent
|
---|
| 44 | local lines_to_remove=
|
---|
| 45 | local srootlink
|
---|
| 46 | local dep_level
|
---|
| 47 |
|
---|
| 48 | {
|
---|
| 49 | # BEWARE : the order of options matters : read -a -u6 rootlink hangs forever
|
---|
| 50 | # (actually, the doc says -a array -u fd)
|
---|
| 51 | # We use fd number 6 for input from DepFile, because we need 0 for user input
|
---|
| 52 | read -u6 -a rootlink
|
---|
| 53 | depth=${#rootlink[*]}
|
---|
| 54 | dep_level=$DEP_LEVEL
|
---|
| 55 | if (( $DEP_LEVEL > 2 )) && (( $depth > 1 )); then dep_level=2; fi
|
---|
| 56 | srootlink="${rootlink[*]} "
|
---|
| 57 | # start of Depfile
|
---|
| 58 | echo -en "\nNode: $depth${spaceSTR:0:$depth}${RED}$DepFile${OFF}"
|
---|
| 59 |
|
---|
| 60 | while read -u6 id_of_dep; do
|
---|
| 61 | # count entries in file
|
---|
| 62 | (( count++ ))
|
---|
| 63 | # Has this entry already been seen?
|
---|
| 64 | if [ -f ${id_of_dep}.dep ]; then # found ${id_of_dep}.dep already in tree
|
---|
| 65 | otherlink=($(head -n 1 ${id_of_dep}.dep))
|
---|
| 66 | if [ -z "${otherlink[*]}" ]
|
---|
| 67 | then echo otherlink empty for $id_of_dep.dep
|
---|
| 68 | echo This should not happen, but happens to happen...
|
---|
| 69 | exit 1
|
---|
| 70 | fi
|
---|
| 71 | # Do not use "${rootlink[*]}" =~ "${otherlink[*]}": case rootlink=(1 11)
|
---|
| 72 | # and otherlink=(1 1)
|
---|
| 73 | if [[ ${srootlink#"${otherlink[*]} "} != ${srootlink} ]]; then # circular dep
|
---|
| 74 | # First look for the other parent of this dependency.
|
---|
| 75 | # The parent has the same link without the last entry.
|
---|
| 76 | # We do not need otherlink anymore so just destroy the last element
|
---|
| 77 | unset otherlink[${#otherlink[*]}-1]
|
---|
| 78 | parent=$(grep ^"${otherlink[*]}"\$ -l *)
|
---|
| 79 | parent=${parent%.dep}
|
---|
| 80 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BLUE}Circular dependency detected:${OFF}"
|
---|
| 81 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${id_of_dep}${OFF} is a dependency \
|
---|
| 82 | of ${BOLD}${parent}${OFF}"
|
---|
| 83 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${DepFile%.dep}${OFF} is a dependency \
|
---|
| 84 | of ${BOLD}${id_of_dep}${OFF}"
|
---|
| 85 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${id_of_dep}${OFF} is a dependency \
|
---|
| 86 | of ${BOLD}${DepFile%.dep}${OFF}"
|
---|
| 87 | # If idofdep is the parent of DepFile, we can exchange them if required
|
---|
| 88 | # if grep -q ${DepFile%.dep} ${id_of_dep}.dep; then
|
---|
| 89 | # we propose exchange always
|
---|
| 90 | echo -en "\nCirc: $depth${spaceSTR:0:$depth}Do you want to build ${id_of_dep} first? yes/no (no):"
|
---|
| 91 | read ANSWER
|
---|
| 92 | if [ x$ANSWER = "xyes" ] ; then # exchange:
|
---|
| 93 | # set triplet and return 1
|
---|
| 94 | exchange_triplet=($parent $id_of_dep ${DepFile%.dep})
|
---|
| 95 | return 1
|
---|
| 96 | else # no exchange: prune
|
---|
| 97 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
| 98 | fi
|
---|
| 99 | # else
|
---|
| 100 | # lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
| 101 | # fi
|
---|
| 102 | else # not circular: prune
|
---|
| 103 | lines_to_remove="$lines_to_remove $id_of_dep"
|
---|
| 104 | fi
|
---|
| 105 | continue
|
---|
[3c10176] | 106 | fi
|
---|
[e576789] | 107 | flag=true
|
---|
| 108 | while [ $flag = true ]; do
|
---|
| 109 | flag=false
|
---|
| 110 | xsltproc --stringparam dependencies ${dep_level} \
|
---|
| 111 | --stringparam idofdep $id_of_dep \
|
---|
| 112 | -o ${id_of_dep}.dep \
|
---|
| 113 | ../xsl/dependencies.xsl ../packages.xml
|
---|
| 114 |
|
---|
| 115 | if [[ -f ${id_of_dep}.dep ]]; then
|
---|
| 116 | sed -i "1i${rootlink[*]} $count" ${id_of_dep}.dep
|
---|
| 117 | generate_dependency_tree ${id_of_dep}.dep
|
---|
| 118 | # Test return value, in case we exchange dependencies
|
---|
| 119 | case $? in
|
---|
| 120 | 0) # Normal return
|
---|
| 121 | ;;
|
---|
| 122 | 1) # We are backing up to parent
|
---|
| 123 | if [[ ${exchange_triplet} == ${DepFile%.dep} ]]
|
---|
| 124 | then tree_erase ${id_of_dep}.dep
|
---|
| 125 | # Just doing a sed -i "s@${id_of_dep}@${exchange_triplet[2]}@" $DepFile
|
---|
| 126 | # is not good if $DepFile contains several times the same line
|
---|
| 127 | # so first find the first line and then sed
|
---|
| 128 | lineno=$(sed -n /${id_of_dep}/= $DepFile | head -n1)
|
---|
| 129 | sed -i "${lineno}s@${id_of_dep}@${exchange_triplet[2]}@" $DepFile
|
---|
| 130 | id_of_dep=${exchange_triplet[2]}
|
---|
| 131 | flag=true
|
---|
| 132 | else
|
---|
| 133 | # echo backing up to ${exchange_triplet} at ${DepFile%.dep}
|
---|
| 134 | return 1
|
---|
| 135 | fi
|
---|
| 136 | ;;
|
---|
[3c10176] | 137 | esac
|
---|
[e576789] | 138 | else
|
---|
| 139 | echo "${rootlink[*]} $count" > ${id_of_dep}.dep
|
---|
[3c10176] | 140 | fi
|
---|
| 141 | done
|
---|
[e576789] | 142 | done
|
---|
| 143 | echo -en "\n End: $depth${spaceSTR:0:$depth}${GREEN}$DepFile${OFF}"
|
---|
| 144 | } 6<$DepFile
|
---|
| 145 | # It may happen that a file is created with several times
|
---|
| 146 | # the same line. Normally, all those lines but one
|
---|
| 147 | # would be flagged to be removed (or all of them if
|
---|
| 148 | # the dependency appeared before). a simple sed /$line/d
|
---|
| 149 | # destroys all the lines. We should instead remove
|
---|
| 150 | # only one for each appearance of it in lines_to_remove.
|
---|
| 151 | # so first get the number of first line and then delete
|
---|
| 152 | # that line
|
---|
| 153 | for line in $lines_to_remove
|
---|
[bd07ef5] | 154 | do lineno=$(sed -n /^$line\$/= $DepFile | tail -n1)
|
---|
[e576789] | 155 | sed -i ${lineno}d $DepFile
|
---|
| 156 | done
|
---|
| 157 | return 0
|
---|
| 158 | }
|
---|
[3c10176] | 159 |
|
---|
[e576789] | 160 | #---------------#
|
---|
| 161 | tree_browse() { #
|
---|
| 162 | #---------------#
|
---|
| 163 | local file=$1
|
---|
| 164 | local f
|
---|
| 165 |
|
---|
| 166 | #echo file=$file
|
---|
| 167 | for f in $(grep '[^0-9 ]' $file); do
|
---|
| 168 | # echo f=$f
|
---|
| 169 | if grep -q '[^0-9 ]' ${f}.dep ; then
|
---|
| 170 | tree_browse ${f}.dep
|
---|
[3c10176] | 171 | fi
|
---|
[e576789] | 172 | echo $f
|
---|
| 173 | done
|
---|
| 174 | }
|
---|
[3c10176] | 175 |
|
---|
[e576789] | 176 | #--------------#
|
---|
| 177 | tree_erase() { #
|
---|
| 178 | #--------------#
|
---|
| 179 | local file=$1
|
---|
| 180 | local f
|
---|
| 181 | local -a rootlink
|
---|
| 182 | local -a rootlink2
|
---|
| 183 |
|
---|
| 184 | #echo file=$file
|
---|
| 185 | rootlink=($(head -n1 $file))
|
---|
| 186 | for f in $(grep '[^0-9 ]' $file); do
|
---|
| 187 | # echo " f"=$f
|
---|
| 188 | if [ -f ${f}.dep ]; then
|
---|
| 189 | rootlink2=($(head -n1 ${f}.dep))
|
---|
| 190 | if [[ "${rootlink2[*]}" =~ "${rootlink[*]}" ]] ; then
|
---|
| 191 | tree_erase ${f}.dep
|
---|
| 192 | fi
|
---|
| 193 | fi
|
---|
| 194 | done
|
---|
| 195 | rm -f $file
|
---|
[3c10176] | 196 | }
|
---|