Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Isolate generation of the NATIVE path for the jar file.
[simgrid.git] / buildtools / Cmake / Scripts / tesh.pl
1 #! /usr/bin/perl
2
3 # Copyright (c) 2012-2014. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 eval 'exec perl -S $0 ${1+"$@"}'
10   if $running_under_some_shell;
11
12 =encoding UTF-8
13
14 =head1 NAME
15
16 tesh -- testing shell
17
18 =head1 SYNOPSIS
19
20 B<tesh> [I<options>] I<tesh_file>
21
22 =cut
23 my($bindir)=".";
24 my($srcdir)=".";
25 my($timeout)=0;
26 my($time_to_wait)=0;
27 my $path = $0;
28 my $OS;
29 my $enable_coverage=0;
30 my $sort_prefix = 19;
31 my $tesh_file;
32 my $tesh_name;
33 my $error=0;
34 my $exitcode=0;
35 my @bg_cmds;
36
37 $path =~ s|[^/]*$||;
38 push @INC,$path;
39
40 use Getopt::Long qw(GetOptions);
41 use strict;
42 use Term::ANSIColor;
43 use IPC::Open3;
44 use IO::File;
45
46 if($^O eq "linux"){
47     $OS = "UNIX";
48 }
49 else{
50     $OS = "WIN";
51     $ENV{"PRINTF_EXPONENT_DIGITS"} = "2"; 
52 }
53
54 #Add current directory to path
55 $ENV{PATH} = "$ENV{PATH}:.";
56
57 ##
58 ## Command line option handling
59 ##
60
61 # option handling helper subs
62 sub cd_cmd {
63   my $directory=$_[1];
64   my $failure=1;
65   if (-e $directory && -d $directory) {
66     chdir("$directory");
67     print "[Tesh/INFO] change directory to $directory\n";
68   $failure=0;
69   } elsif (-e $directory) {
70     print "Cannot change directory to '$directory': it is not a directory\n";
71   } else {
72     print "Chdir to $directory failed: No such file or directory\n";
73   }
74   if($failure==1){
75   $error=1;
76   $exitcode=4;
77   print "Test suite `$tesh_file': NOK (system error)\n";
78   exit 4;
79   }
80 }
81
82 sub setenv_cmd {
83   my($var,$ctn);
84   if ($_[0] =~ /^(.*)=(.*)$/) {
85     ($var,$ctn)=($1,$2);
86   }elsif ($_[1] =~ /^(.*)=(.*)$/) {
87     ($var,$ctn)=($1,$2);
88   } else { 
89       die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
90   }
91     
92     if($var =~ /bindir/){
93         print "[Tesh/INFO] setenv $var=$ctn\n";
94         $bindir = $ctn;
95     }
96     else
97     {
98         if($var =~ /srcdir/){
99             $srcdir = $ctn;
100         }
101         else{
102             $ENV{$var} = $ctn;
103             print "[Tesh/INFO] setenv $var=$ctn\n";
104         }
105     }    
106 }
107
108 # Main option parsing sub
109
110 sub get_options {
111   # remove the tesh file from the ARGV used
112   my @ARGV = @_;
113   $tesh_file = pop @ARGV;
114
115   # temporary arrays for GetOption
116   my @verbose = ();
117   my @cfg;
118   my $log; # ignored
119
120
121   my %opt = (
122     "help"  => 0,
123     "debug"   => 0,
124     "verbose" => 0
125     );
126
127   Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
128   
129   GetOptions(
130     'help|h'   => \$opt{'help'},
131
132     'verbose|v'  => \@verbose,
133     'debug|d'  => \$opt{"debug"},
134
135     'cd=s'     => \&cd_cmd,
136     'timeout=s'  => \$opt{'timeout'},    
137     'setenv=s'   => \&setenv_cmd,
138     'cfg=s'    => \@cfg,
139     'log=s'    => \$log,
140     'enable-coverage+'  => \$enable_coverage,    
141     );
142
143   if($enable_coverage){
144     print "Enable coverage\n";
145   }
146
147   unless($tesh_file=~/(.*)\.tesh/){
148     $tesh_file="(stdin)";
149     $tesh_name="(stdin)";
150     print "Test suite from stdin\n";
151   }else{
152     $tesh_name=$1;
153     print "Test suite `$tesh_name'\n";
154   }
155
156   $opt{'verbose'} = scalar @verbose;
157   foreach (@cfg) {
158     $opt{'cfg'} .= " --cfg=$_";
159   }
160   return %opt;
161 }
162
163 my %opts = get_options(@ARGV);
164
165 ##
166 ## File parsing
167 ##
168 my($nb_arg)=0;
169 my($old_buffer);
170 my($linebis);
171 my($SIGABRT)=0;
172 my($verbose)=0;
173 my($return)=-1;
174 my($pid);
175 my($result);
176 my($result_err);
177 my($forked);
178 my($config)="";
179 my($tesh_command)=0;
180 my(@buffer_tesh)=();
181
182 #eval {
183   use POSIX;
184
185   sub exit_status {
186     my $status = shift;
187     if (POSIX::WIFEXITED($status)) {
188       $exitcode=POSIX::WEXITSTATUS($status)+40;
189       return "returned code ".POSIX::WEXITSTATUS($status);
190     } elsif (POSIX::WIFSIGNALED($status)) {
191       my $code;
192       if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
193       elsif  (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
194       elsif  (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
195       elsif  (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
196       elsif  (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
197       $exitcode=POSIX::WTERMSIG($status)+4;
198       return "got signal $code";
199     }
200     return "Unparsable status. Is the process stopped?";
201   }
202 #};
203 #if ($@) { # no POSIX available?
204 #  warn "POSIX not usable to parse the return value of forked child: $@\n";
205 #  sub exit_status {
206 #    return "returned code -1 $@ ";
207 #  }
208 #}
209
210 sub exec_cmd { 
211   my %cmd = %{$_[0]};
212   if ($opts{'debug'}) {
213     print "IN BEGIN\n";
214     map {print "  $_"} @{$cmd{'in'}};
215     print "IN END\n";
216     print "OUT BEGIN\n";
217     map {print "  $_"} @{$cmd{'out'}};
218     print "OUT END\n";
219     print "CMD: $cmd{'cmd'}\n";
220   }
221
222   # cleanup the command line
223   if($OS eq "WIN"){
224         $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
225         $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
226         $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
227     }
228     else{
229         $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
230     }
231   $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
232   $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
233   $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
234   $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
235   $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
236   $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
237 # $cmd{'cmd'} =~ s|^\./||g;
238 #  $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
239   $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
240   $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
241
242   print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
243
244   ###
245   # exec the command line
246   ###  $line =~ s/\r//g;
247
248   $cmd{'got'} = IO::File->new_tmpfile;
249   $cmd{'got'}->autoflush(1);
250   local *E = $cmd{'got'}; 
251   $cmd{'pid'} = open3(\*CHILD_IN,  ">&E",  ">&E", $cmd{'cmd'} );
252
253   # push all provided input to executing child
254   map { print CHILD_IN "$_\n"; }  @{$cmd{'in'}};
255   close CHILD_IN;
256
257   # if timeout specified, fork and kill executing child at the end of timeout
258   if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
259     $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
260     $forked = fork();
261     $timeout=-1;
262     die "fork() failed: $!" unless defined $forked;
263     if ( $forked == 0 ) { # child
264       sleep $time_to_wait;
265       kill(SIGKILL, $cmd{'pid'});
266       exit $time_to_wait;
267     }
268   }
269
270   
271   # Cleanup the executing child, and kill the timeouter brother on need
272   $cmd{'return'} = 0 unless defined($cmd{'return'});
273   if($cmd{'background'} != 1){
274     waitpid ($cmd{'pid'}, 0);
275     $cmd{'gotret'} = exit_status($?);
276     parse_out(\%cmd);
277   }else{
278     # & commands, which will be handled at the end
279     push @bg_cmds, \%cmd;
280     # no timeout for background commands
281     if($forked){
282        kill(SIGKILL, $forked);
283        $timeout=0;
284        $forked=0;
285     }
286   }
287 }
288
289
290 sub parse_out { 
291   my %cmd = %{$_[0]};
292   my $gotret=$cmd{'gotret'};
293
294   my $wantret;
295
296   if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
297     $wantret = "got signal $cmd{'expect'}";
298   }else{
299     $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
300   }
301
302   local *got = $cmd{'got'};
303   seek(got,0,0);
304   # pop all output from executing child
305   my @got;
306   while(defined(my $got=<got>)) {
307     $got =~ s/\r//g;
308     chomp $got;
309     if (!($enable_coverage and $got=~ /^profiling:/)){
310       push @got, $got;
311     }
312   }    
313
314   if ($cmd{'sort'}){   
315     sub mysort{
316         substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
317     }
318     use sort 'stable';
319     @got = sort mysort @got;
320     while (@got and $got[0] eq "") {
321       shift @got;
322     }
323
324     #also resort the other one, as perl sort is not the same as the C one used to generate teshes
325     if(defined($cmd{'out'})){
326       @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
327       while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
328         shift @{$cmd{'out'}};
329       }
330     }
331   }
332
333   #Did we timeout ? If yes, handle it. If not, kill the forked process.
334
335   if($timeout==-1 and $gotret eq "got signal SIGKILL"){
336     $gotret="return code 0";
337     $timeout=1;
338     $gotret= "timeout after $time_to_wait sec";
339     $error=1;
340     $exitcode=3;
341     print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
342   }else{
343     $timeout=0;  
344   }
345   if($gotret ne $wantret) {
346     $error=1;
347     my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
348     if ($timeout!=1) {
349         $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";    
350     }
351     map {$msg .=  "|| $_\n"} @got;
352     if(!@got) {
353         if($timeout==1){
354         print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
355         }else{
356         $msg .= "||\n";
357         }
358     }
359     $timeout = 0;
360     print STDERR "$msg";
361   }
362
363       
364   ###
365   # Check the result of execution 
366   ###
367   my $diff;
368   if (!defined($cmd{'output ignore'})){
369     $diff = build_diff(\@{$cmd{'out'}}, \@got);
370   }else{
371   print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
372   }
373   if (length $diff) {
374     print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
375     map { print "$_\n" } split(/\n/,$diff);
376
377     print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
378     $error=1;
379     $exitcode=2;
380   }
381 }
382
383 sub mkfile_cmd {
384   my %cmd = %{$_[0]};
385   my $file = $cmd{'arg'};
386   print "[Tesh/INFO] mkfile $file\n";
387
388   unlink($file);
389   open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
390   print FILE join("\n", @{$cmd{'in'}});
391   print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
392   close(FILE);
393 }
394
395 # parse tesh file
396 #my $teshfile=$tesh_file;
397 #$teshfile=~ s{\.[^.]+$}{};
398
399 unless($tesh_file eq "(stdin)"){
400   open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
401 }
402
403 my %cmd; # everything about the next command to run
404 my $line_num=0;
405 my $finished =0;
406 LINE: while (not $finished and not $error) {
407   my $line;
408
409
410   if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
411     $finished=1;
412     next LINE;
413   }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
414     $finished=1;
415     next LINE;
416   }
417
418
419   $line_num++;
420   chomp $line;
421   $line =~ s/\r//g;
422   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
423   my $next;
424   # deal with line continuations
425   while ($line =~ /^(.*?)\\$/) {
426     $next=<TESH_FILE>;
427     die "[TESH/CRITICAL] Continued line at end of file\n"
428       unless defined($next);
429     $line_num++;
430     chomp $next;
431     print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
432     $line = $1.$next;
433   }
434
435   # Push delayed commands on empty lines
436   unless ($line =~ m/^(.)(.*)$/) {
437     if (defined($cmd{'cmd'}))  {
438       exec_cmd(\%cmd);
439       %cmd = ();
440     }
441     next LINE;
442   }     
443  
444   my ($cmd,$arg) = ($1,$2);
445   $arg =~ s/^ //g;
446   $arg =~ s/\r//g;
447   $arg =~ s/\\\\/\\/g;
448   # handle the commands
449   if ($cmd =~ /^#/) {    #comment
450   } elsif ($cmd eq '>'){    #expected result line
451     print "[TESH/debug] push expected result\n" if $opts{'debug'};
452     push @{$cmd{'out'}}, $arg;
453
454   } elsif ($cmd eq '<') {    # provided input
455     print "[TESH/debug] push provided input\n" if $opts{'debug'};
456     push @{$cmd{'in'}}, $arg;
457
458   } elsif ($cmd eq 'p') {    # comment
459     print "[$tesh_name:$line_num] $arg\n";
460
461   } elsif ($cmd eq '$') {  # Command
462     # if we have something buffered, run it now
463     if (defined($cmd{'cmd'})) {
464       exec_cmd(\%cmd);
465       %cmd = ();
466     }
467     if ($arg =~ /^\s*mkfile /){      # "mkfile" command line
468       die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
469
470       $cmd{'arg'} = $arg;
471       $cmd{'arg'} =~ s/\s*mkfile //;
472       mkfile_cmd(\%cmd);
473       %cmd = ();
474
475     } elsif ($arg =~ /^\s*cd /) {
476       die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
477       die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
478
479       $arg =~ s/^ *cd //;
480       cd_cmd("",$arg);
481       %cmd = ();
482
483     } else { # regular command
484       $cmd{'cmd'} = $arg;
485       $cmd{'file'} = $tesh_file;
486       $cmd{'line'} = $line_num;
487     }
488   }
489   elsif($cmd eq '&'){      # parallel command line
490
491     if (defined($cmd{'cmd'})) {
492       exec_cmd(\%cmd);
493       %cmd = ();
494     }
495     $cmd{'background'} = 1;
496     $cmd{'cmd'} = $arg;
497     $cmd{'file'} = $tesh_file;
498     $cmd{'line'} = $line_num;
499   }    
500   elsif($line =~ /^!\s*output sort/){    #output sort
501     if (defined($cmd{'cmd'})) {
502       exec_cmd(\%cmd);
503       %cmd = ();
504     }
505     $cmd{'sort'} = 1;
506     if ($line =~ /^!\s*output sort\s+(\d+)/) {
507         $sort_prefix = $1;
508     }
509   }
510   elsif($line =~ /^!\s*output ignore/){    #output ignore
511     if (defined($cmd{'cmd'})) {
512       exec_cmd(\%cmd);
513       %cmd = ();
514     }
515     $cmd{'output ignore'} = 1;
516   }
517   elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
518     if (defined($cmd{'cmd'})) {
519       exec_cmd(\%cmd);
520       %cmd = ();
521     }
522 print "hey\n";
523     $cmd{'expect'} = "$1";
524   }
525   elsif($line =~ /^!\s*expect return/){    #expect return
526     if (defined($cmd{'cmd'})) {
527       exec_cmd(\%cmd);
528       %cmd = ();
529     }
530     $line =~ s/^! expect return //g;
531     $line =~ s/\r//g;
532     $cmd{'return'} = $line;
533   }
534   elsif($line =~ /^!\s*setenv/){    #setenv
535     if (defined($cmd{'cmd'})) {
536       exec_cmd(\%cmd);
537       %cmd = ();
538     }
539     $line =~ s/^! setenv //g;
540     $line =~ s/\r//g;
541     setenv_cmd($line);
542   }
543   elsif($line =~ /^!\s*include/){    #include
544     if (defined($cmd{'cmd'})) {
545       exec_cmd(\%cmd);
546       %cmd = ();
547     }
548     print color("red"), "[Tesh/CRITICAL] need include";
549     print color("reset"), "\n";
550     die;
551   }
552   elsif($line =~ /^!\s*timeout/){    #timeout
553     if (defined($cmd{'cmd'})) {
554       exec_cmd(\%cmd);
555       %cmd = ();
556     }
557     $line =~ s/^! timeout //;
558     $line =~ s/\r//g;
559     $cmd{'timeout'} = $line;
560   } else {
561     die "[TESH/CRITICAL] parse error: $line\n";
562   }
563   if($forked){
564    kill(SIGKILL, $forked);
565    $timeout=0;
566   }
567
568 }
569
570
571
572 # Deal with last command
573 if (defined($cmd{'cmd'})) {
574   exec_cmd(\%cmd);
575   %cmd = ();
576 }
577
578
579 if($forked){
580    kill(SIGKILL, $forked);
581    $timeout=0;
582 }
583
584 foreach(@bg_cmds){
585   my %test=%{$_};
586   waitpid ($test{'pid'}, 0);
587   $test{'gotret'} = exit_status($?);
588   parse_out(\%test);
589 }
590
591 @bg_cmds=();
592
593
594 if($error !=0){
595     exit $exitcode;
596 }elsif($tesh_file eq "(stdin)"){
597     print "Test suite from stdin OK\n";
598 }else{
599     print "Test suite `$tesh_name' OK\n";
600 }
601
602 #my (@a,@b);
603 #push @a,"bl1";   push @b,"bl1";
604 #push @a,"bl2";   push @b,"bl2";
605 #push @a,"bl3";   push @b,"bl3";
606 #push @a,"bl4";   push @b,"bl4";
607 #push @a,"bl5";   push @b,"bl5";
608 #push @a,"bl6";   push @b,"bl6";
609 #push @a,"bl7";   push @b,"bl7";
610 ##push @a,"Perl";  push @b,"ruby";
611 #push @a,"END1";   push @b,"END1";
612 #push @a,"END2";   push @b,"END2";
613 #push @a,"END3";   push @b,"END3";
614 #push @a,"END4";   push @b,"END4";
615 #push @a,"END5";   push @b,"END5";
616 #push @a,"END6";   push @b,"END6";
617 #push @a,"END7";   push @b,"END7";
618 #print "Identical:\n". build_diff(\@a,\@b);
619
620 #@a = (); @b =();
621 #push @a,"AZE"; push @b,"EZA";
622 #print "Different:\n".build_diff(\@a,\@b);
623
624 use lib "@CMAKE_BINARY_DIR@/bin" ;
625
626 use Diff qw(diff); # postpone a bit to have time to change INC
627
628 sub build_diff {
629   my $res;
630   my $diff = Diff->new(@_);
631   
632   $diff->Base( 1 );   # Return line numbers, not indices
633   my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
634   return ""   if ($chunk_count == 1 && $diff->Same());
635   $diff->Reset();
636   while(  $diff->Next()  ) {
637     my @same = $diff->Same();
638     if ($diff->Same() ) {
639       if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
640         $res .= '  '.$same[0]."\n" ;
641         $res .= '  '.$same[1]."\n" if (scalar @same>1);
642       }     
643       $res .= "...\n"  if (scalar @same>2);
644 #    $res .= $diff->Next(0)."/$chunk_count\n";
645       if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
646         $res .= '  '.$same[scalar @same -2]."\n" if (scalar @same>1);
647         $res .= '  '.$same[scalar @same -1]."\n";
648       } 
649     } 
650     next if  $diff->Same();
651     map { $res .= "- $_\n" } $diff->Items(1);
652     map { $res .= "+ $_\n" } $diff->Items(2);
653   }
654   return $res;
655 }
656
657