Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
keep up with lastest diff computation changes
[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 unit 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 <stdio.h>\n";
78 print OUT "#include \"xbt.h\"\n";
79 print OUT $GENERATED;
80 print OUT "# $beginline \"$infile\" \n";
81 print OUT "$unit_source";
82 print OUT $GENERATED;
83 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
84
85 # write the main skeleton if needed
86 if (! -e "simgrid_units_main.c") {
87   open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
88   print OUT $GENERATED;
89   print OUT "#include <stdio.h>\n\n";
90   print OUT "#include \"xbt.h\"\n\n";
91   print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
92   print OUT "/* SGU: BEGIN PROTOTYPES */\n";
93   print OUT "/* SGU: END PROTOTYPES */\n\n";
94   print OUT $GENERATED;
95 #  print OUT "# 93 \"sg_unit_extractor.pl\"\n";
96   print OUT <<EOF;
97 int main(int argc, char *argv[]) {
98   xbt_test_suite_t suite; 
99   char selection[1024];
100   int i;\n
101   /* SGU: BEGIN SUITES DECLARATION */
102   /* SGU: END SUITES DECLARATION */
103       
104   /* Search for the tests to do */
105     selection[0]='\\0';
106     for (i=1;i<argc;i++) {
107       if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
108         char *p=strchr(argv[i],'=')+1;
109         if (selection[0] == '\\0') {
110           strcpy(selection, p);
111         } else {
112           strcat(selection, \",\");
113           strcat(selection, p);
114         }
115       } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
116                  !strncmp(argv[i],\"--dump\",     strlen(\"--dump\"))) {
117         xbt_test_dump(selection);
118         return 0;
119       } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
120           printf(
121               "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
122               "--help: display this help\\n"
123               "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
124               "--tests=selection: Use argument to select which suites/units/tests to run\\n"
125               "                   --tests can be used more than once, and selection may be a comma\\n"
126               "                   separated list of directives.\\n\\n"
127               "Directives are of the form:\\n"
128               "   [-]suitename[:unitname]\\n\\n"
129               "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
130               "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
131               "it applies on any unit.\\n\\n"
132               "By default, everything is enabled.\\n\\n"
133               "'all' as suite name apply to all suites.\\n\\n"
134               "Example 1: \\"-toto,+toto:tutu\\"\\n"
135               "  disables the whole toto testsuite (any unit in it),\\n"
136               "  then reenables the tutu unit of the toto test suite.\\n\\n"
137               "Example 2: \\"-all,+toto\\"\\n"
138               "  Run nothing but the toto suite.\\n");
139           return 0;
140       } else {
141         printf("testall: Unknown option: %s\\n",argv[i]);
142         return 1;
143       }
144     }
145   /* Got all my tests to do */
146       
147   return xbt_test_run(selection);
148 }
149 EOF
150   print OUT $GENERATED;
151   close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
152 }
153
154 print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
155 map {
156   my ($name,$func,$title) = @{$_};
157   print "    unit $name: func=$func; title=$title\n";
158 } @tests;
159
160 #while (my $t = shift @tests) {
161
162 # add this suite to the main
163 my $newmain="";
164 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
165   # search prototypes
166   while (<IN>) {
167     $newmain .= $_;
168 #    print "Look for proto: $_";
169     last if /SGU: BEGIN PROTOTYPES/;
170   }
171
172   # search my prototype
173   while (<IN>) {
174 #    print "Seek protos: $_";
175     last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
176     $newmain .= $_;
177   }
178   if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
179     while (<IN>) {
180       last if /SGU: END FILE/;
181     }
182     $_ = <IN>; # pass extra blank line
183     chomp;
184     die "this line should be blank ($_). Did you edit the file?" if /\W/;
185   }
186   my ($old_)=($_);
187   # add my section
188   $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
189   map {
190     my ($name,$func,$title) = @{$_};
191     $newmain .=  "    void $func(void);\n"
192   } @tests;
193
194   $newmain .= "  /* SGU: END FILE */\n\n";
195   if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
196     $newmain .= $old_;
197   }
198
199   # pass remaining prototypes, search declarations
200   while (<IN>) {
201     $newmain .= $_ unless /SGU: END PROTOTYPES/;
202     last if /SGU: BEGIN SUITES DECLARATION/;
203   }
204
205   ### Done with prototypes. And now, the actual code
206   
207   # search my prototype
208   while (<IN>) {
209     last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
210     $newmain .= $_;
211   }
212   if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
213     while (<IN>) {
214       last if /SGU: END FILE/;
215     }
216     $_ = <IN>; # pass extra blank line
217     chomp;
218     die "this line should be blank ($_). Did you edit the file?" if /\W/;
219   }
220   my ($old_)=($_);
221   # add my section
222   $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
223   $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
224   map {
225     my ($name,$func,$title) = @{$_};
226     $newmain .=  "      xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
227   } @tests;
228
229   $newmain .= "    /* SGU: END FILE */\n\n";
230   if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
231     $newmain .= $old_;
232   }
233
234   # pass the remaining 
235   while (<IN>) {
236     $newmain .= $_;
237   }
238 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
239
240 # write it back to main
241 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
242 print OUT $newmain;
243 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
244
245 0;