Logo AND Algorithmique Numérique Distribuée

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