Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
postprocess everything, reduce verbosity
[simgrid.git] / tools / sg_unit_extractor.pl
1 #! /usr/bin/perl
2
3 use strict;
4
5 my $progname="sg_unit_extractor";
6 # Get the args 
7 die "USAGE: $progname infile [outfile]\n"
8   if (scalar @ARGV == 0 || scalar @ARGV > 2);
9 my ($infile,$outfile) = @ARGV;
10
11 if (not defined($outfile)) {
12   $outfile =  $infile;
13   $outfile =~ s/\.c$/_unit.c/;
14   $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
15 }
16
17 # Get the unit data
18 my ($unit_source,$suite_name,$suite_title)=("","","");
19 my (%tests); # to detect multiple definition
20 my (@tests); # actual content
21
22 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
23
24 my $takeit=0;
25 my $line=0;
26 my $beginline=0;
27 while (<IN>) {
28   $line++;
29   if (m/ifdef +SIMGRID_TEST/) {
30     $beginline = $line;
31     $takeit = 1;
32     next;
33   }
34   if (m/endif.*SIMGRID_TEST/) {
35     $takeit = 0;
36     next
37   }
38
39   if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*,(.*?)\);/) { #"
40     die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" 
41       if length($suite_name);
42     ($suite_name,$suite_title)=($1,$2);
43     next;
44   } 
45   
46   if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"
47     die "$progname: multiply defined test in file $infile: $1\n"
48       if (defined($tests{$1}));
49       
50     my @t=($1,$2,$3);
51     push @tests,\@t;
52     $tests{$1} = 1;
53   }
54   $unit_source .= $_ if $takeit;
55 }
56 close IN || die "$progname: cannot close input file '$infile': $!\n";
57
58
59 if ($takeit) {
60   die "$progname: end of file reached in SIMGRID_TEST block.\n".
61      "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
62      "Example:\n".
63      "#endif /* SIMGRID_TEST */\n"
64 }
65
66 die "$progname: no suite defined in $infile\n"
67   unless (length($suite_name));
68   
69 # Write the test
70
71 my ($GENERATED)=("/*******************************/\n".
72                  "/* GENERATED FILE, DO NOT EDIT */\n".
73                  "/*******************************/\n\n");
74 $beginline+=2;
75 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
76 print OUT $GENERATED;
77 print OUT "#include \"xbt.h\"\n";
78 print OUT $GENERATED;
79 print OUT "# $beginline \"$infile\" \n";
80 print OUT "$unit_source";
81 print OUT $GENERATED;
82 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
83
84 # write the main skeleton if needed
85 if (! -e "simgrid_units_main.c") {
86   open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
87   print OUT $GENERATED;
88   print OUT "#include \"xbt.h\"\n\n";
89   print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
90   print OUT "/* SGU: BEGIN PROTOTYPES */\n";
91   print OUT "/* SGU: END PROTOTYPES */\n\n";
92   print OUT $GENERATED;
93   print OUT "int main(int argc, char *argv[]) {\n";
94   print OUT "  xbt_test_suite_t suite;\n\n";
95   print OUT "  /* SGU: BEGIN SUITES DECLARATION */\n";
96   print OUT "  /* SGU: END SUITES DECLARATION */\n\n";  
97   print OUT "  return xbt_test_run();\n";
98   print OUT "}\n";
99   print OUT $GENERATED;
100   close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
101 }
102
103 print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
104 map {
105   my ($name,$func,$title) = @{$_};
106   print "    test $name: func=$func; title=$title\n";
107 } @tests;
108
109 #while (my $t = shift @tests) {
110
111 # add this suite to the main
112 my $newmain="";
113 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
114   # search prototypes
115   while (<IN>) {
116     $newmain .= $_;
117 #    print "Look for proto: $_";
118     last if /SGU: BEGIN PROTOTYPES/;
119   }
120
121   # search my prototype
122   while (<IN>) {
123 #    print "Seek protos: $_";
124     last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
125     $newmain .= $_;
126   }
127   if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
128     while (<IN>) {
129       last if /SGU: END FILE/;
130     }
131     $_ = <IN>; # pass extra blank line
132     chomp;
133     die "this line should be blank ($_). Did you edit the file?" if /\W/;
134   }
135   my ($old_)=($_);
136   # add my section
137   $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
138   map {
139     my ($name,$func,$title) = @{$_};
140     $newmain .=  "    void $func(void);\n"
141   } @tests;
142
143   $newmain .= "  /* SGU: END FILE */\n\n";
144   if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
145     $newmain .= $old_;
146   }
147
148   # pass remaining prototypes, search declarations
149   while (<IN>) {
150     $newmain .= $_ unless /SGU: END PROTOTYPES/;
151     last if /SGU: BEGIN SUITES DECLARATION/;
152   }
153
154   ### Done with prototypes. And now, the actual code
155   
156   # search my prototype
157   while (<IN>) {
158     last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
159     $newmain .= $_;
160   }
161   if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
162     while (<IN>) {
163       last if /SGU: END FILE/;
164     }
165     $_ = <IN>; # pass extra blank line
166     chomp;
167     die "this line should be blank ($_). Did you edit the file?" if /\W/;
168   }
169   my ($old_)=($_);
170   # add my section
171   $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
172   $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
173   map {
174     my ($name,$func,$title) = @{$_};
175     $newmain .=  "      xbt_test_suite_push(suite, $func, $title);\n";
176   } @tests;
177
178   $newmain .= "    /* SGU: END FILE */\n\n";
179   if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
180     $newmain .= $old_;
181   }
182
183   # pass the remaining 
184   while (<IN>) {
185     $newmain .= $_;
186   }
187 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
188
189 # write it back to main
190 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
191 print OUT $newmain;
192 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
193
194 0;