source: BLFS/libs/func_dependencies@ 532ede6

2.4 ablfs-more legacy new_features trunk
Last change on this file since 532ede6 was e576789, checked in by Pierre Labastie <pierre@…>, 11 years ago

Merge ablfs branch. Normally, jhalfs should not perform differently
for building LFS

  • Property mode set to 100644
File size: 6.2 KB
Line 
1#!/bin/bash
2#
3# $Id$
4#
5
6# A string of spaces for indenting:
7declare -a spaceSTR=" "
8declare -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)
17
18#----------------------------#
19generate_dependency_tree() { #
20#----------------------------#
21: <<inline_doc
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
28 DEP_LEVEL
29 modifies: vars: none
30 returns: nothing
31 output: files: for each pkg with dependencies in $1,
32 a file pkg.dep and its dependencies
33 on error: nothing
34 on success: nothing
35inline_doc
36
37local DepFile=$1
38local -a rootlink
39local -a otherlink
40local -i depth
41local -i count=0
42local id_of_dep
43local parent
44local lines_to_remove=
45local srootlink
46local 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
52read -u6 -a rootlink
53depth=${#rootlink[*]}
54dep_level=$DEP_LEVEL
55if (( $DEP_LEVEL > 2 )) && (( $depth > 1 )); then dep_level=2; fi
56srootlink="${rootlink[*]} "
57# start of Depfile
58echo -en "\nNode: $depth${spaceSTR:0:$depth}${RED}$DepFile${OFF}"
59
60while 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 \
82of ${BOLD}${parent}${OFF}"
83 echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${DepFile%.dep}${OFF} is a dependency \
84of ${BOLD}${id_of_dep}${OFF}"
85 echo -en "\nCirc: $depth${spaceSTR:0:$depth}${BOLD}${id_of_dep}${OFF} is a dependency \
86of ${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
106 fi
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 ;;
137 esac
138 else
139 echo "${rootlink[*]} $count" > ${id_of_dep}.dep
140 fi
141 done
142done
143echo -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
153for line in $lines_to_remove
154 do lineno=$(sed -n /^$line\$/= $DepFile | head -n1)
155 sed -i ${lineno}d $DepFile
156done
157return 0
158}
159
160#---------------#
161tree_browse() { #
162#---------------#
163local file=$1
164local f
165
166#echo file=$file
167for 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
171 fi
172 echo $f
173done
174}
175
176#--------------#
177tree_erase() { #
178#--------------#
179local file=$1
180local f
181local -a rootlink
182local -a rootlink2
183
184#echo file=$file
185rootlink=($(head -n1 $file))
186for 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
194done
195rm -f $file
196}
Note: See TracBrowser for help on using the repository browser.