This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDidbsDependencyEngine.pm
250 lines (205 loc) · 5.88 KB
/
DidbsDependencyEngine.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package DidbsDependencyEngine;
use DidbsUtils;
sub new
{
my $self = bless{}, shift;
my $verbose = shift;
$self->{v} = $verbose;
my $scriptLocation = shift;
my $packageDefsDir = shift;
my $didbsCompiler = shift;
$self->{scriptLocation} = $scriptLocation;
$self->{packageDefsDir} = $packageDefsDir;
$self->findPackages($didbsCompiler);
return $self;
}
sub findPackages
{
my $self = shift;
my $didbsCompiler = shift;
my $packageLocation = "$self->{packageDefsDir}";
if( ! -e $packageLocation )
{
didbsprint "Unable to find packages at $packageLocation\n";
exit 1;
}
didbsprint "Looking for packages in $packageLocation\n";
# Glob is _slightly_ quicker, and saves a "fork/exec", too
# my @FOUNDPKGS = `ls $packageLocation/*/*.packagedef`;
my @FOUNDPKGS = glob "$packageLocation/*/*.packagedef";
didbsprint "Completed package search of $packageLocation\n";
chomp(@FOUNDPKGS);
if( length(@FOUNDPKGS) == 0 )
{
didbsprint "Unable to find packages at $packageLocation\n";
exit 1;
}
# didbsprint "Have @FOUNDPKGS\n";
my @knownPackages = ();
didbsprint "Reading package definitions...\n";
foreach $foundpkg (@FOUNDPKGS)
{
(my $pkgname = $foundpkg) =~ s/.*\/.+\/(\S+)\.packagedef.*/$1/;
my $dpkg = DidbsPackage->new($self->{v},$pkgname);
$dpkg->readPackageDef($self->{scriptLocation},
$packageLocation);
$dpkg->debug();
if( (!$dpkg->{disabled}) && index($dpkg->{compilers}, $didbsCompiler) != -1 )
{
push(@knownPackages, \$dpkg);
}
else
{
my $pkgid = $dpkg->{packageId};
$self->{v} && didbsprint "Skipping disabled package '$pkgid'\n";
}
}
$self->{knownPackages} = \@knownPackages;
my %pidToPackage = ();
foreach $pkg (@{$self->{knownPackages}})
{
my $pkgid = ${$pkg}->{packageId};
# didbsprint "DE - Have a package '$pkgid'\n";
$pidToPackage{$pkgid} = $pkg;
}
$self->{pidToPackage} = \%pidToPackage;
my %donePackages;
didbsprint "Computing package dependencies and order...\n";
my $orderedRef = flattenAndSortDeps( \@knownPackages, \%pidToPackage, \%donePackages );
if( $self->{v} )
{
didbsprint "Package order as follows:\n";
foreach $pkg (@{$orderedRef})
{
my $curpkg = ${$pkg};
my $pkgid = $curpkg->{packageId};
my $sequenceNo = $curpkg->{sequenceNo};
my $dependencies = $curpkg->{dependenciesList};
didbsprint "Package: $pkgid => $sequenceNo\t\t($dependencies)\n";
}
}
$self->{knownPackages} = $orderedRef;
didbsprint "Completed package dependencies and order...\n";
}
sub listPackages
{
my $self = shift;
return $self->{knownPackages};
}
sub getPackageMap
{
my $self = shift;
return $self->{pidToPackage};
}
sub packageInResolutionStack
{
my $pkgResolutionStackRef = shift;
my $pkgId = shift;
my %params = map { $_ => 1 } @{$pkgResolutionStackRef};
return exists($params{$pkgId});
}
sub flattenAndSortDeps
{
my $knownPkgsRef = shift;
my $p2pRef = shift;
my $donepRef = shift;
# Set up deps
for $pkgref (@{$knownPkgsRef})
{
my @pkgResolutionStack=();
recursiveFlattenDeps( $pkgref,
$knownPkgsRef,
$p2pRef,
$donepRef,
$pkgref,
\@pkgResolutionStack );
# didbsprint "One done.\n";
# foreach $pkgid (keys %{$donepRef})
# {
# didbsprint "Done package: $pkgid\n";
# }
}
# Sort (first on seq no, then on package ID)
my @ordered = sort {
${$a}->{sequenceNo} <=> ${$b}->{sequenceNo}
||
${$a}->{packageId} cmp ${$b}->{packageId}
} @{$knownPkgsRef};
# If having problem with dependency ordering
# Uncomment this stuff to see what order is being used.
# didbsprint "Sorted packages:\n";
# foreach $sortedpkgref (@ordered)
# {
# my $pkg = ${$sortedpkgref};
# didbsprint "Seq: $pkg->{sequenceNo}\t:\t $pkg->{packageId} \n";
# }
# exit;
return \@ordered;
}
sub recursiveFlattenDeps
{
my $drivingPkgRef = shift;
my $knownPkgsRef = shift;
my $p2pRef = shift;
my $donepRef = shift;
my $curpkgRef = shift;
my $pkgResolutionStackRef = shift;
my @knownPackages = @{$knownPkgsRef};
my %pidToPackage = %{$p2pRef};
my %donePackages = %{$donepRef};
my $curPkg = ${$curpkgRef};
my $curPkgId = $curPkg->{packageId};
# didbsprint "RecursiveFlattenDeps of $curPkgId\n";
# Check if already handled
if( ${$donepRef}{$curPkgId} ne "" )
{
# didbsprint "Package $curPkgId is already complete.\n";
return $curPkg->{sequenceNo};
}
# Check if package is already in the resolution stack
# which means circular dependencies somewhere
if( packageInResolutionStack( $pkgResolutionStackRef,
$curPkgId ) )
{
didbsprint "Dependency cycle detected with driving pkg: ".
${$drivingPkgRef}->{packageId} . "!\n";
foreach $deppkgid (@{$pkgResolutionStackRef})
{
didbsprint "Pkgid: $deppkgid\n";
}
exit 1;
}
push @{$pkgResolutionStackRef}, $curPkgId;
# For each dependency, find and add their dependencies
my $deps = $curPkg->{dependenciesList};
# didbsprint "For package $curPkgId dependencies are $deps\n";
my @depIds = split(',',$deps);
my $sequenceNo = 0;
foreach $depId (@depIds)
{
my $depRef = ${$p2pRef}{$depId};
if( !defined($depRef) )
{
didbsprint "Missing dependency: $depId for $curPkgId\n";
exit 1;
}
my $childSeqNo = recursiveFlattenDeps( $drivingPkgRef,
$knownPkgsRef,
$p2pRef,
$donepRef,
$depRef,
$pkgResolutionStackRef );
my $tmpSeqNo = $childSeqNo + 1;
if( $tmpSeqNo > $sequenceNo )
{
$sequenceNo = $tmpSeqNo;
}
}
$curPkg->{sequenceNo} = $sequenceNo;
${$donepRef}{$curPkgId} = "done";
${$pkgsInCurrentResolveRef}{$curPkgId} = "done";
# didbsprint "Setting package $curPkgId as done\n";
pop @{$pkgResolutionStackRef};
return $curPkg->{sequenceNo};
}
1;