Project.pm

filter: Statementstmt 11.9% 29 / 242 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 25.7% 9 / 35 Podpod 0.0% 0 / 25 totaltotal 10.1% 38 / 376 CC 56 SCAR 43.3
CC
56
Cov
9%
compile
210.0
generate_all
182.0
dependency_sort
110.0
CRAP 75.7
« CLI.pm ↑ Summary
1 package Hardware::Vhdl::Automake::Project;
2 1 ▶use Hardware::Vhdl::Automake::SourceFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Hardware::Vhdl::Automake::DesignUnit;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Temp qw/ tempfile tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 1 our $VERSION = "1.00";
14
15 0 ▶sub new { # class or object method, returns a new project object
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
16 0 my $class=shift;
17 0 ▶ $class = ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
18 0 my $self={
19 sourcefiles => [],
20 dunits => {},
21 projectfile => undef,
22 hdldir => undef,
23 status_callback => undef,
24 };
25 0 bless $self, $class;
26 }
27
28 0 ▶sub set_status_callback {
Subroutine set_status_callback: not called CC=1 SCAR=6.9
Pod undocumented
29 0 my $self = shift;
30 0 $self->{status_callback} = shift;
31 }
32
33 0 ▶sub report_status {
Subroutine report_status: not called CC=2 SCAR=17.9
Pod undocumented
34 0 my $self = shift;
35 0 ▶ &{$self->{status_callback}}(@_) if defined $self->{status_callback};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"status_callback"} 0 0 0
36 }
37
38 0 ▶sub save {
Subroutine save: not called CC=4 SCAR=30.0
Pod undocumented
39 0 my $self=shift;
40 0 my $saveas = shift;
41 0 ▶ $self->{projectfile} = $saveas if defined $saveas;
Branch: 1 branch
BranchTrueFalseTotal
if defined $saveas 0 0 0
42 0 ▶ croak "Project filename not given" unless defined $self->{projectfile};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"projectfile"} 0 0 0
43 0 ▶ if (-f $self->{projectfile}) { rename $self->{projectfile}, $self->{projectfile}.".backup" }
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"projectfile"}) 0 0 0
44 0 YAML::DumpFile($self->{projectfile}, $self);
45 }
46
47 0 ▶sub load {
Subroutine load: not called CC=4 SCAR=30.0
Pod undocumented
48 0 my $self=shift;
49 0 my $loadfrom = shift;
50 0 ▶ croak "Project filename not given" unless defined $loadfrom;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loadfrom 0 0 0
51 0 for my $k (keys %$self) {
52 0 delete $self->{$k};
53 }
54 0 my ($data) = YAML::LoadFile($loadfrom);
55 #TODO! check loaded data is valid
56 0 for my $k (keys %$data) {
57 0 $self->{$k} = $data->{$k};
58 0 delete $data->{$k};
59 }
60 0 $self->{projectfile} = $loadfrom;
61 }
62
63 0 ▶sub hdl_dir { # getter/setter method. default hdl dir is {save filename}.'_files'
Subroutine hdl_dir: not called CC=2 SCAR=17.9
Pod undocumented
64 0 my $self = shift;
65 0 ▶ $self->{hdl_dir} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
66 0 $self->{hdl_dir};
67 }
68
69 0 ▶sub project_file { # getter/setter method. file is also changed by load/save with parameter
Subroutine project_file: not called CC=2 SCAR=17.9
Pod undocumented
70 0 my $self = shift;
71 0 ▶ $self->{projectfile} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
72 0 $self->{projectfile};
73 }
74
75 0 ▶sub add_sourcefiles {
Subroutine add_sourcefiles: not called CC=2 SCAR=17.9
Pod undocumented
76 0 my $self=shift;
77 0 for my $sourcespec (@_) {
78 0 push @{$self->{sourcefiles}}, new Hardware::Vhdl::Automake::SourceFile($sourcespec);
79 #!TODO: check for source file duplication?
80 }
81 }
82
83 0 ▶sub remove_sourcefiles {
Subroutine remove_sourcefiles: not called CC=3 SCAR=24.8
Pod undocumented
84 0 my $self=shift;
85 0 for my $sourceobj (@_) {
86 0 my $srch = "$sourceobj";
87 0 my $n = @{$self->{sourcefiles}};
88 0 @{$self->{sourcefiles}} = grep { "$_" ne $srch } @{$self->{sourcefiles}};
89 0 ▶ croak "Source file '".$sourceobj->file."' was not removed from the project because it was not found." if $n == @{$self->{sourcefiles}};
Branch: 1 branch
BranchTrueFalseTotal
if $n == @{$self->{"sourcefiles"};} 0 0 0
90 }
91 }
92
93 0 ▶sub sourcefiles {
Subroutine sourcefiles: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my $self=shift;
95 0 @{$self->{sourcefiles}};
96 }
97
98 0 ▶sub designunits {
Subroutine designunits: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my $self=shift;
100 0 values %{$self->{dunits}};
101 }
102
103 0 ▶sub packages {
Subroutine packages: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my $self=shift;
105 0 grep { $_->type eq 'package' } $self->designunits;
106 }
107
108 0 ▶sub package_bodies {
Subroutine package_bodies: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $self=shift;
110 0 grep { $_->type eq 'package body' } $self->designunits;
111 }
112
113 0 ▶sub entities {
Subroutine entities: not called CC=1 SCAR=6.9
Pod undocumented
114 0 my $self=shift;
115 0 grep { $_->type eq 'entity' } $self->designunits;
116 }
117
118 0 ▶sub architectures {
Subroutine architectures: not called CC=1 SCAR=6.9
Pod undocumented
119 0 my $self=shift;
120 0 grep { $_->type eq 'architecture' } $self->designunits;
121 }
122
123 0 ▶sub configurations {
Subroutine configurations: not called CC=1 SCAR=6.9
Pod undocumented
124 0 my $self=shift;
125 0 grep { $_->type eq 'configuration' } $self->designunits;
126 }
127
128 0 ▶sub generate_all {
Subroutine generate_all: not called CC=13 SCAR=52.0
Pod undocumented
129 0 my $self = shift;
130 0 ▶ croak "project's hdl_dir needs to be set before attempting generate_all" unless defined $self->hdl_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->hdl_dir 0 0 0
131
132 0 $self->{dunits_prev} = $self->{dunits};
133 0 $self->{dunits} = {};
134
135 0 ▶ unless (defined $self->{gen1_tempdir} && -d $self->{gen1_tempdir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"gen1_tempdir"} and -d $self->{"gen1_tempdir"}) 0 0 0
Condition0%0/3
defined $self->{"gen1_tempdir"} and -d $self->{"gen1_tempdir"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"gen1_tempdir"} and -d $self->{"gen1_tempdir"}
defined $self->{"gen1_tempdir"} -d $self->{"gen1_tempdir"}
Truth table0%0/3
A and B
A= defined $self->{"gen1_tempdir"}B= -d $self->{"gen1_tempdir"}
ABresult
0X0
100
111
136 0 $self->{gen1_tempdir} = tempdir("temp_XXXXXXXX", DIR => $self->hdl_dir, CLEANUP => 1);
137 }
138
139 0 for my $sourcefile (@{$self->{sourcefiles}}) {
140 0 eval {
141 0 $self->report_status({
142 type => 'generate1poss',
143 text => 'Considering generating (pass 1)',
144 file => $sourcefile->file,
145 });
146 0 $sourcefile->generate_phase1($self->{status_callback}, $self->{gen1_tempdir});
147 };
148 0 my $err = $@;
149 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
150 0 $self->report_status({
151 type => 'generate1_error',
152 text => "Error while generating (pass 1): $err",
153 file => $sourcefile->file,
154 });
155 0 $self->{dunits} = $self->{dunits_prev};
156 0 delete $sourcefile->{outfh};
157 0 delete $self->{dunits_prev};
158 0 return 0;
159 }
160 0 $self->add_dunits($sourcefile->designunits);
161 }
162
163 {
164 # insert component declarations into the arch or package body hdl, taken from the entity declarations
165 0 ▶ my $ent_dunit_finder = sub { $self->find_dunit_by_short_name(Hardware::Vhdl::Automake::UnitName->new('entity', $_[0], $_[1])->short_string) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 for my $dunit ($self->architectures, $self->package_bodies) {
167 0 $self->report_status({
168 type => 'generate2poss',
169 text => 'Considering generating (pass 2)',
170 duname => $dunit->name,
171 });
172 0 $dunit->do_component_inserts($self->{status_callback}, $ent_dunit_finder);
173 }
174 }
175
176 0 for my $dunit ($self->designunits) {
177 #my $file = name2file($dunit->type).(defined $dunit->pname ? ' '.name2file($dunit->pname) : '').' '.name2file($dunit->sname);
178 0 my $file = $dunit->name->filename_string;
179 {
180 0 my $lang = $dunit->sourcefile->language;
181 0 ▶ if ($lang =~ m/^VHDL/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lang =~ /^VHDL/i) { } 0 0 0
elsif ($lang =~ /^Verilog/i) { } 0 0 0
182 0 $file .= '.vhd'
183 } elsif ($lang =~ m/^Verilog/i) {
184 0 $file .= '.v'
185 }
186 }
187 0 my $path = $self->hdl_dir; #catdir($self->hdl_dir, name2file($dunit->name->library_dirname_string));
188 0 $file = catfile($path, $file);
189 0 ▶ if ($dunit->file ne $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dunit->file ne $file) 0 0 0
190 0 $self->report_status({
191 type => 'hdl_copy',
192 text => 'New design unit code copied to HDL dir',
193 duname => $dunit->name,
194 file => $file,
195 });
196 0 mkpath $path;
197 0 $dunit->move_file($file);
198 }
199 }
200
201 0 for my $old_dunit (values %{$self->{dunits_prev}}) {
202 0 my $new_dunit = $self->find_matching_dunit($old_dunit);
203 0 ▶ if (defined $new_dunit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_dunit) { } 0 0 0
204 0 $new_dunit->copy_compiler_info_from($old_dunit);
205 } else {
206 # delete stuff relating to old dunit, no longer present
207 0 unlink $old_dunit->file;
208 }
209 }
210 0 delete $self->{dunits_prev};
211 0 1;
212 }
213
214 0 ▶sub add_dunits {
Subroutine add_dunits: not called CC=3 SCAR=24.8
Pod undocumented
215 0 my $self = shift;
216 0 for my $du (@_) {
217 0 my $k = $du->name->short_string;
218 0 ▶ if (exists $self->{dunits}{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dunits"}{$k}) 0 0 0
219 #TODO: check for duplicate definitions of dunit?
220 0 carp "There seem to be duplicate definitions of $k"
221 }
222 0 $self->{dunits}{$k} = $du;
223 }
224 }
225
226 0 ▶sub find_matching_dunit {
Subroutine find_matching_dunit: not called CC=2 SCAR=17.9
Pod undocumented
227 0 my $self = shift;
228 0 my $search_dunit = shift;
229 0 my $k = $search_dunit->name->short_string;
230 0 ▶ exists $self->{dunits}{$k} ? $self->{dunits}{$k} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'dunits'}{$k} ? : 0 0 0
231 }
232
233 0 ▶sub find_dunit_by_name {
Subroutine find_dunit_by_name: not called CC=2 SCAR=17.9
Pod undocumented
234 0 my $self = shift;
235 0 my $duname = shift;
236 0 my $k = $duname->short_string;
237 0 ▶ exists $self->{dunits}{$k} ? $self->{dunits}{$k} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'dunits'}{$k} ? : 0 0 0
238 }
239
240 0 ▶sub find_dunit_by_short_name {
Subroutine find_dunit_by_short_name: not called CC=2 SCAR=17.9
Pod undocumented
241 0 my $self = shift;
242 0 my $k = shift;
243 0 ▶ exists $self->{dunits}{$k} ? $self->{dunits}{$k} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'dunits'}{$k} ? : 0 0 0
244 }
245
246 0 ▶sub libraries {
Subroutine libraries: not called CC=2 SCAR=17.9
Pod undocumented
247 0 my $self = shift;
248 0 my %libs;
249 0 for my $dunit (values %{$self->{dunits}}) {
250 0 $libs{$dunit->name->library} = undef;
251 }
252 0 keys %libs;
253 }
254
255 0 ▶sub find_parents {
Subroutine find_parents: not called CC=3 SCAR=24.8
Pod undocumented
256 0 my $self = shift;
257 # for design units of type package_body or architecture, set the 'parent' attribute to be the matching primary design unit
258 0 for my $dunit ( $self->architectures ) {
259 0 $dunit->parent($self->find_dunit_by_name(Hardware::Vhdl::Automake::UnitName->new('entity', $dunit->library, $dunit->pname)));
260 }
261 0 for my $dunit ($self->package_bodies ) {
262 0 $dunit->parent($self->find_dunit_by_name(Hardware::Vhdl::Automake::UnitName->new('package', $dunit->library, $dunit->pname)));
263 }
264 }
265
266 =for notes
267 New Compile algorithm.
268 Create a mapping of hdl files to design-unit objects (or duname objects)
269 from _info files get a mapping from each hdl file to a binary file, and a list of dunit names of dependencies
270 convert this into a mapping from each DUnit object to a binary file and a list of references to dependency DUnit objects
271 recompile a dunit if its hdl has changed, or if its binary file is older than the binary file for any of its dependencies.
272 =cut
273
274 0 ▶sub compile {
Subroutine compile: not called CC=14 SCAR=53.5
Pod undocumented
275 0 my $self = shift;
276 0 my $tool = shift;
277 0 $self->find_parents;
278 0 $tool->compile_start;
279 0 eval {
280 0 for my $lib ($self->libraries) {
281 0 $tool->ensure_library($lib);
282 }
283
284 0 my ($hdl2bin, $hdl2deps) = $tool->get_deps;
285
286 0 for my $dunit ($self->designunits) {
287 0 my $hdlfile = canonpath($dunit->file);
288 0 ▶ $hdlfile = lc $hdlfile if File::Spec->case_tolerant();
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->case_tolerant 0 0 0
289
290 # tell the dunit what its compiled name is and what its dependencies are
291 0 my $info = $dunit->get_compile_info($tool->toolid);
292
293 0 ▶ $info->{compiled_name} = defined $hdl2bin->{$hdlfile} ? $hdl2bin->{$hdlfile} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $hdl2bin->{$hdlfile} ? : 0 0 0
294
295 0 $info->{dependencies} = [];
296 0 ▶ if (defined $hdl2deps->{$hdlfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hdl2deps->{$hdlfile}) 0 0 0
297 0 for my $depname (@{$hdl2deps->{$hdlfile}}) {
298 0 my $depdunit = $self->find_dunit_by_name($depname);
299 0 ▶ push @{$info->{dependencies}}, $depdunit if defined $depdunit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $depdunit 0 0 0
300 }
301 }
302
303 0 for my $depname ($dunit->compile_after) {
304 0 my $depdunit = $self->find_dunit_by_name($depname);
305 0 ▶ push @{$info->{dependencies}}, $depdunit if defined $depdunit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $depdunit 0 0 0
306 }
307
308 # need to add the package header to the dependency list of a package body
309 0 ▶ if ($dunit->type eq 'package body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dunit->type eq "package body") 0 0 0
310 0 my $depdunit = $dunit->parent;
311 0 ▶ push @{$info->{dependencies}}, $depdunit if defined $depdunit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $depdunit 0 0 0
312 }
313
314 0 $dunit->set_compile_info($tool->toolid, $info);
315 }
316
317 0 for my $dunit (
318 $self->dependency_sort($self->packages),
319 $self->package_bodies,
320 $self->entities,
321 $self->architectures,
322 $self->configurations,
323 ) {
324 0 $tool->compile($dunit);
325 }
326
327 };
328 0 my $err = $@;
329 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 0 0 0
330 0 $tool->compile_abort;
331 #croak $err;
332 0 warn $err;
333 } else {
334 0 $tool->compile_finish;
335 }
336 }
337
338 0 ▶sub dependency_sort {
Subroutine dependency_sort: not called CC=10 SCAR=47.0
Pod undocumented
339 0 my $self = shift;
340 0 my @dunits = @_;
341 0 my @sorted;
342
343 # We will work with du short-names for the sort. make a lookup of short-name to dunit object
344 # to simplify conversion back at the end. Also useful for the graph build.
345 my %name2du;
346 0 for my $dunit (@dunits) { $name2du{$dunit->name->short_string} = $dunit }
347
348 # build the dependency graph: each du short-name maps to a list of other du short-names.
349 # only include dus that are in the list that was passed in.
350 0 my $tdeps = {};
351 0 for my $dunit (@dunits) {
352 0 @{$tdeps->{$dunit->name->short_string}} = grep { exists $name2du{$_} } map { $_->short_string } $dunit->compile_after;
353 }
354 #print "Dependency graph = ".Dump($tdeps);
355
356 # Topological sort!
357 # Make list of nodes with no dependencies
358 0 my @q;
359 0 for my $m (keys %$tdeps) {
360 0 ▶ if (@{$tdeps->{$m}} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$tdeps->{$m};} == 0) 0 0 0
361 0 push @q, $m;
362 0 delete $tdeps->{$m};
363 }
364 }
365
366 0 ▶ while (keys %$tdeps > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$tdeps > 0) 0 0 0
367 0 ▶ die "Topological sort can't find any starting points: there must be a dependency loop\n" if @q==0;
Branch: 1 branch
BranchTrueFalseTotal
if @q == 0 0 0 0
368 0 my $n = pop(@q);
369 0 push @sorted, $n;
370 #~ for each node m with an edge e from n to m do
371 0 for my $m (keys %$tdeps) {
372 0 ▶ if (grep {$_ eq $n} @{$tdeps->{$m}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $n;} @{$$tdeps{$m};}) 0 0 0
373 #~ remove edge e from the graph
374 0 @{$tdeps->{$m}} = grep {$_ ne $n} @{$tdeps->{$m}};
375 #~ if m has no other incoming edges then insert m into Q
376 0 ▶ if (@{$tdeps->{$m}} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$tdeps->{$m};} == 0) 0 0 0
377 0 push @q, $m;
378 0 delete $tdeps->{$m};
379 }
380 }
381 }
382 }
383 #print "Topo sort output = ".Dump([@sorted, reverse @q]);
384 0 map { $name2du{$_} } (@sorted, reverse @q);
385 }
386
387 1 1;
« CLI.pm ↑ Summary