CLI.pm untested

filter: Statementstmt 0.0% 0 / 273 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 483 CC 50 SCAR 44.3
CC
50
Cov
0%
status_report
240.0
_scan_for_filehandle
110.0
be_server
90.0
CRAP 84.1
1
2 package Hardware::Vhdl::Automake::CLI;
3
4 use Hardware::Vhdl::Automake::UnitName;
5 use Hardware::Vhdl::Automake::Project;
6 use Hardware::Vhdl::Automake::Compiler::ModelSim;
7 use File::Basename;
8 use File::Spec::Functions;
9 use Win32::Shortcut;
10 use YAML;
11 #use Data::Dumper;
12 use Cwd;
13 use IO::Socket;
14 use Net::hostent; # for OO version of gethostbyaddr
15 #use Win32::GuiTest qw(FindWindowLike GetWindowText SetForegroundWindow SendKeys);
16
17 use strict;
18 use warnings;
19
20 #&be_server;
21
22 sub new {
23 my $class = shift;
24 my $self = {
25 std_out => \*STDOUT,
26 };
27 bless $self, $class;
28 }
29
30 sub be_server {
31 local $|=1;
32
33 my $PORT = 8119; # pick something not in use
34
35 my $server = IO::Socket::INET->new(
36 Proto => 'tcp',
37 LocalPort => $PORT,
38 Listen => 2, #SOMAXCONN,
39 Reuse => 1
40 );
41
42 die "Can't setup server" unless $server;
43 print "[Server $0 accepting clients on port $PORT]\n";
44
45 my $project = Hardware::Vhdl::Automake::CLI->new();
46 my %dir2proj;
47 my $client;
48 while ($client = $server->accept()) {
49 $client->autoflush(1);
50 $project->{std_out} = $client;
51 #print $client "Welcome to $0; type help for command list.\n";
52 my $hostinfo = gethostbyaddr($client->peeraddr);
53 printf "\n[Connect from %s]\n", $hostinfo->name || $client->peerhost;
54 #print $client "Command? ";
55 $_=<$client>; { # while (defined($_=<$client>)) {
56 chomp;
57 print "client said '$_'\n";
58 #print $client "you said '$_'\n";
59 if ($_ =~ /^generate_compile (.*)$/) {
60 my $dir = $1;
61 eval {
62 if (exists $dir2proj{$dir} && -f $dir2proj{$dir}) {
63 my $ppf = $dir2proj{$dir};
64 unless ($project->{projfile} eq $ppf && $project->{projfile_mtime} == -M($ppf)) {
65 $project->{projfile} = $ppf;
66 $project->load();
67 }
68 } else {
69 my $ppf = $project->{projfile};
70 $project->find_project($dir);
71 unless (defined $ppf && $project->{projfile} eq $ppf && $project->{projfile_mtime} == -M($ppf)) {
72 $project->load();
73 }
74 $dir2proj{$dir} = $project->{projfile};
75 }
76 $project->{compiled_ok} = 0;
77 $project->generate() || die "Generate failed\n";
78 $project->compile();
79 #$project->scan_for_filehandle();
80 $project->save();
81 if (0 && $project->{compiled_ok}) {
82 my @windows = FindWindowLike(0, "^ModelSim PE ", "");
83 if (@windows==1) {
84 SetForegroundWindow($windows[0]);
85 #SendKeys("{HOME}warmstart; {#}{ENTER}{PAUSE 1000}{UP}{HOME}{DELETE 12}{END}",10);
86 SendKeys("{HOME}warmstart; {#}{ENTER}",10);
87 print $client "ModelSim restart requested\n";
88 } else {
89 print $client scalar(@windows)." ModelSim instances found - no restart requested\n";
90 }
91 }
92 };
93 my $err = $@;
94 if ($err) {
95 print $client "ERROR: $err\n";
96 }
97 } else {
98 print $client "ERROR: command not recognised\n";
99 }
100 }
101 $project->{std_out} = \*STDOUT;
102 close $client;
103 print "[disconnect]\n";
104 }
105 }
106
107 sub parse_cli {
108 #print "Hardware::Vhdl::Automake::CLI args are ",join(' ', @ARGV),"\n";
109 my $job = shift @ARGV;
110 if (lc $job eq 'build') {
111 my $project = Hardware::Vhdl::Automake::CLI->new();
112 $project->find_and_load_project(getcwd());
113 $project->generate();
114 $project->compile();
115 $project->save();
116 } elsif (lc $job eq 'instance_template') {
117 my $entname = <STDIN>;
118 if ($entname !~ m/^ \s* ((?:[A-Za-z][A-Za-z0-9_]*)|(?:\\.*?\\)) \s* \. \s* ((?:[A-Za-z][A-Za-z0-9_]*)|(?:\\.*?\\)) \s* $/x) {
119 die "Selected text does not look like a library.entity_name\n";
120 }
121 my $libname = $1;
122 $entname = $2;
123 my $project = Hardware::Vhdl::Automake::CLI->new();
124 $project->{std_out} = undef;
125 $project->find_and_load_project(getcwd());
126 my $ent_dunit = $project->{project}->find_dunit_by_short_name(Hardware::Vhdl::Automake::UnitName->new('entity', $libname, $entname)->short_string);
127 die "Component insertion error; entity $libname.$entname not found in project" unless defined $ent_dunit;
128 $ent_dunit->entity_instance(sub { print shift });
129 exit 0;
130 } else {
131 die "command '$job' was not recognised.";
132 }
133 print "Finished.\n";
134 }
135
136 if (0) {
137 my $filelist = &file_list("E:/VHDL projects/serswitch/");
138 #print Dump($filelist);
139 my $project = &load_or_construct_project("E:/VHDL projects/serswitch/", 'serswitch', $filelist);
140 &generate($project);
141 &compile($project);
142 &save($project);
143 print "Finished.\n";
144 }
145
146 sub find_and_load_project {
147 my $self = shift;
148 my $dir = shift;
149 $self->find_project($dir);
150 $self->load();
151 }
152
153 sub find_project {
154 my $self = shift;
155 my $dir = shift;
156 my @projfiles;
157 my $stdout = $self->{std_out};
158 while (1) {
159 print $stdout "looking for project in $dir...\n" if defined $stdout;
160 my @allfiles = &files_in_dir($dir);
161 # find any .hdl_proj files in this directory
162 @projfiles = grep { m/.hdl_proj$/i } @allfiles;
163 # find any .hdl_proj files linked to from this directory
164 for my $linkfile (grep { m/.lnk$/i } @allfiles) {
165 my $link = Win32::Shortcut->new();
166 if ($link->Load($linkfile)) {
167 my $linksto = $link->{Path};
168 if ($linksto =~ m/.hdl_proj$/i && -f $linksto) { push @projfiles, $linksto }
169 }
170 }
171 last if (@projfiles);
172 # nothing found here - try the parent dir...
173 my $pdir = dirname($dir);
174 last if ($pdir eq $dir);
175 $dir = $pdir;
176 }
177 die "No project file was found.\n" unless @projfiles;
178 die "More than one possible project file was found.\n" unless @projfiles==1;
179 $self->{projfile} = $projfiles[0];
180 }
181
182 sub load {
183 my $self = shift;
184 my $stdout = $self->{std_out};
185
186 print $stdout "Loading project file: $self->{projfile}\n" if defined $stdout;
187 ($self->{project}, $self->{compiler}) = YAML::LoadFile($self->{projfile});
188 $self->{projfile_mtime} = -M($self->{projfile});
189
190 $self->{project}->set_status_callback( sub { $self->status_report(@_) } );
191 $self->{compiler}->set_status_callback( sub { $self->status_report(@_) } );
192
193 $self;
194 }
195
196 sub generate {
197 my $self = shift;
198 $self->{project}->generate_all;
199 }
200
201 sub compile {
202 my $self = shift;
203 $self->{project}->compile($self->{compiler});
204 }
205
206 sub scan_for_filehandle {
207 my $self = shift;
208 &_scan_for_filehandle($self->{project}, '$project->{project}');
209 &_scan_for_filehandle($self->{compiler}, '$project->{compiler}');
210 }
211
212 sub _scan_for_filehandle {
213 my ($ref, $name, @path) = @_;
214 my $r2 = reftype $ref;
215 #print STDERR "scanning $name:\n";
216 if ($r2) {
217 if ($r2 eq 'GLOB') {
218 print "$name is a GLOB reference!\n";
219 } elsif ($r2 =~ /^(SCALAR|CODE|REF|LVALUE)$/) {
220 # go no further
221 } elsif ($r2 eq 'ARRAY') {
222 # scan array elements
223 for my $i (0..$#{$ref}) {
224 if (ref $ref->[$i]) {
225 my $nref = $ref->[$i];
226 my $sifref = "$nref"; # stringified version
227 &_scan_for_filehandle($nref, $name."[$i]", @path, $sifref) unless grep({$_ eq $sifref} @path);
228 }
229 }
230 } elsif ($r2 eq 'HASH') {
231 # HASH - scan key-value pairs
232 for my $k (keys %$ref) {
233 if (ref $ref->{$k}) {
234 my $nref = $ref->{$k};
235 my $sifref = "$nref"; # stringified version
236 &_scan_for_filehandle($nref, $name."{$k}", @path, $sifref) unless grep({$_ eq $sifref} @path);
237 }
238 }
239 }
240 }
241 }
242
243 sub save {
244 my $self = shift;
245 if (-f $self->{projfile}) { rename $self->{projfile}, $self->{projfile}.".backup" }
246 YAML::DumpFile($self->{projfile}, $self->{project}, $self->{compiler});
247 $self->{projfile_mtime} = -M($self->{projfile});
248 if (0) {
249 # save in Data::Dumper format
250 my $fh;
251 open $fh, '>', $self->{projfile}.".ddump" || die "Can't write Data::Dumper format project file";
252 local $Data::Dumper::Purity = 1;
253 print $fh Dumper($self->{project}, $self->{compiler});
254 }
255 }
256
257 sub load_or_construct_project {
258 my $self = shift;
259 my ($projdir, $projname, $filelist) = @_;
260
261 my $stdout = $self->{std_out};
262 $self->{projfile} = catfile($projdir, $projname.".hdl_proj");
263
264 if (-f $self->{projfile}) {
265 print $stdout "Loading existing project...\n";
266 ($self->{project}, $self->{compiler}) = YAML::LoadFile($self->{projfile});
267 } else {
268 print $stdout "Creating a new project...\n";
269 # create a new project
270 $self->{project} = new Hardware::Vhdl::Automake::Project;
271 $self->{project}->hdl_dir($projdir."hdl");
272
273 # create a compiler instance
274 $self->{compiler} = new Hardware::Vhdl::Automake::Compiler::ModelSim({basedir => $projdir."sim"});
275 }
276
277 # ensure that the project has the right files, compiled into the right libraries
278 # first find out what files are in the project
279 my %projfiles;
280 for my $sourcefileobj ($self->{project}->sourcefiles) {
281 #print $stdout "Project contains file ".$sourcefileobj->file."\n";
282 $projfiles{$sourcefileobj->file} = { library => $sourcefileobj->library, object => $sourcefileobj }
283 }
284 # remove any that we don't want, or where the library is wrong
285 for my $sourcefile (keys %projfiles) {
286 my @flmatches = grep { $_->{file} eq $sourcefile } @$filelist;
287 if (@flmatches>1) { die "file '$sourcefile' exists more than once in file list!\n" }
288 if (@flmatches == 0 || $flmatches[0]->{library} ne $projfiles{$sourcefile}->{library}) {
289 print $stdout "Removing '$sourcefile' from project.\n";
290 $self->{project}->remove_sourcefiles($projfiles{$sourcefile}->{object});
291 delete $projfiles{$sourcefile};
292 }
293 }
294 # add any files that are in @$filelist but not in the project
295 for my $file (@$filelist) {
296 unless (exists $projfiles{$file->{file}}) {
297 print $stdout "Adding '$file->{file}' to project.\n";
298 $self->{project}->add_sourcefiles($file)
299 }
300 }
301
302 &save($self);
303
304 $self->{project}->set_status_callback( \&status_report );
305 $self->{compiler}->set_status_callback( \&status_report );
306
307 $self;
308 }
309
310 sub file_list {
311 my ($basedir) = @_;
312 my $list = [];
313 for my $libdir (&directories_in_dir($basedir)) {
314 my $library = basename($libdir);
315 for my $sourcefile (grep { m/\.vhd$/i } &files_in_dir($libdir)) {
316 push @$list, { file => $sourcefile, library => $library};
317 }
318 }
319 $list;
320 }
321
322 sub status_report {
323 my $self = shift;
324 my $report = shift;
325 local $| = 1;
326 my $type = $report->{type};
327 my $stdout = $self->{std_out};
328 if ($type eq 'error' || $type eq 'warning') {
329 print {$stdout} uc($type).": ".$report->{text}."\n";
330 print $stdout " line: ".$report->{sourceline}."\n" if defined $report->{sourceline};
331 for my $st (qw/ src gen /) {
332 if (defined $report->{$st.'file'}) {
333 print $stdout " $st: at ";
334 my $fn = $report->{$st.'file'};
335 $fn =~ s!/!\\!g;
336 print $stdout $fn.' line '.$report->{$st.'linenum'};
337 print $stdout "\n";
338 }
339 }
340 } elsif ($type eq 'assert_fail') {
341 print $stdout "ASSERTION FAIL: ".$report->{text}."\n";
342 print $stdout " expected: ".$report->{expected}."\n";
343 print $stdout " got: ".$report->{got}."\n";
344 } elsif ($type eq 'generate1') {
345 print $stdout "Generating HDL from source file ".$report->{file}."\n";
346 } elsif ($type eq 'generated') {
347 print $stdout " generated ".$report->{duname}->short_string."\n";
348 } elsif ($type eq 'generate2') {
349 print $stdout "Generating (pass 2) ".$report->{duname}->short_string."\n";
350 } elsif ($type eq 'hdl_copy') {
351 print $stdout "copying new HDL for ".$report->{duname}->short_string." at ".$report->{file}." line 1\n";
352 } elsif ($type eq 'compile_start') {
353 print $stdout "\n".$report->{text}."\n";
354 } elsif ($type =~ m/^(mklib|config|compile_finish|compile_abort)$/) {
355 print $stdout $report->{text}."\n";
356 $self->{compiled_ok}=1 if $type eq 'compile_finish';
357 } elsif ($type eq 'compile') {
358 print $stdout " compiling ".$report->{duname}->short_string."\t(because $report->{reason})\n";
359 } elsif ($type eq 'compile_skip') {
360 #print $stdout " not compiling ".$report->{duname}->short_string."\n";
361 } elsif ($type =~ m/^(generate1poss|generate2poss)$/) {
362 # suppress output
363 } else {
364 print $stdout "\n".Dump($report);
365 }
366 }
367
368 sub directories_in_dir {
369 my $dir = shift;
370 my $dh;
371 opendir $dh, $dir || return ();
372 map { catdir($dir,$_) } grep { !m/^\.{1,2}$/ && -d $dir . '/' . $_ } readdir $dh;
373 }
374
375 sub files_in_dir {
376 my $dir = shift;
377 my $dh;
378 opendir $dh, $dir || return ();
379 map { catfile($dir,$_) } grep { !m/^\.{1,2}$/ && -f $dir . '/' . $_ } readdir $dh;
380 }
381
382 1;