Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Write error messages on stderr.
[simgrid.git] / tools / sg_unit_extractor.pl
1 #! /usr/bin/perl
2
3 use strict;
4
5 use strict;
6 use Getopt::Long qw(GetOptions);
7
8 my $progname="sg_unit_extractor";
9 # Get the args 
10
11 sub usage($) {
12     my $ret;
13     print "USAGE: $progname [--root=part/to/cut] [--path=where/to/search NOT WORKING] [--outdir=where/to/generate/files] infile [infile+]\n";
14     exit $ret;
15 }
16
17 my $path=undef;
18 my $outdir=undef;
19 my $root;
20 my $help;
21
22 Getopt::Long::config('permute','no_getopt_compat', 'no_auto_abbrev');
23 GetOptions(
24         'help|h'                => sub {usage(0)},
25         'root=s' =>\$root,
26         'path=s' =>\$path,
27         'outdir=s' =>\$outdir) or usage(1);
28
29 usage(1) if (scalar @ARGV == 0);
30
31 map {process_one($_)} @ARGV;
32
33 sub process_one($) {
34     my $infile = shift;
35     my $outfile;
36     
37     $infile =~ s|src/|| unless (-e $infile);
38     
39     $outfile =  $infile;
40     $outfile =~ s/\.c$/_unit.c/;
41     $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
42     $outfile = "$outdir$outfile";
43     
44     print "$progname: processing $infile (generating $outfile)...\n";    
45     
46     # Get the unit data
47     my ($unit_source,$suite_name,$suite_title)=("","","");
48     my (%tests); # to detect multiple definition
49     my (@tests); # actual content
50     
51     open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
52     $infile =~ s|$root|| if defined($root);
53     
54     my $takeit=0;
55     my $line=0;
56     my $beginline=0;
57     while (<IN>) {
58         $line++;
59         if (m/ifdef +SIMGRID_TEST/) {
60             $beginline = $line;
61             $takeit = 1;
62             next;
63         }
64         if (m/endif.*SIMGRID_TEST/) {
65             $takeit = 0;
66             next
67         }
68         
69         if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
70             die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
71             ($suite_name,$suite_title)=($1,$2);
72             die "$progname: Empty suite name in $infile" unless length($suite_name);
73             die "$progname: Empty suite title in $infile" unless length($suite_title);
74             next;
75         } elsif (m/XBT_TEST_SUITE/) {
76             die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
77         }
78
79         if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
80             die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
81             
82             my @t=($1,$2,$3);
83             push @tests,\@t;
84             $tests{$1} = 1;
85         } elsif (m/XBT_TEST_UNIT/) {
86             die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
87         }
88         $unit_source .= $_ if $takeit;
89     }
90     close IN || die "$progname: cannot close input file '$infile': $!\n";
91
92
93     if ($takeit) {
94         die "$progname: end of file reached in SIMGRID_TEST block.\n".
95           "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
96           "Example:\n".
97           "#endif /* SIMGRID_TEST */\n"
98     }
99
100     die "$progname: no suite defined in $infile\n" unless (length($suite_name));
101   
102     # Write the test
103
104     my ($GENERATED)=("/*******************************/\n".
105                      "/* GENERATED FILE, DO NOT EDIT */\n".
106                      "/*******************************/\n\n");
107     $beginline+=2;
108     open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
109     print OUT $GENERATED;
110     print OUT "#include <stdio.h>\n";
111     print OUT "#include \"xbt.h\"\n";
112     print OUT $GENERATED;
113     print OUT "#line $beginline \"$infile\" \n";
114     print OUT "$unit_source";
115     print OUT $GENERATED;
116     close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
117
118     # write the main skeleton if needed
119     if (! -e "${outdir}simgrid_units_main.c") {
120         open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
121         print OUT $GENERATED;
122         print OUT "#include <stdio.h>\n\n";
123         print OUT "#include \"xbt.h\"\n\n";
124         print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
125         print OUT "/* SGU: BEGIN PROTOTYPES */\n";
126         print OUT "/* SGU: END PROTOTYPES */\n\n";
127         print OUT $GENERATED;
128         #  print OUT "# 93 \"sg_unit_extractor.pl\"\n";
129         print OUT <<EOF;
130 int main(int argc, char *argv[]) {
131   xbt_test_suite_t suite; 
132   char selection[1024];
133   int verbosity = 0;
134   int i;
135   int res;
136
137   /* SGU: BEGIN SUITES DECLARATION */
138   /* SGU: END SUITES DECLARATION */
139       
140   xbt_init(&argc,argv);
141     
142   /* Search for the tests to do */
143     selection[0]='\\0';
144     for (i=1;i<argc;i++) {
145       if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
146         char *p=strchr(argv[i],'=')+1;
147         if (selection[0] == '\\0') {
148           strcpy(selection, p);
149         } else {
150           strcat(selection, \",\");
151           strcat(selection, p);
152         }
153       } else if (!strcmp(argv[i], \"--verbose\")) {
154         verbosity++;
155       } else if (!strcmp(argv[i], \"--dump-only\")||
156                  !strcmp(argv[i], \"--dump\")) {
157         xbt_test_dump(selection);
158         return 0;
159       } else if (!strcmp(argv[i], \"--help\")) {
160           printf(
161               "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
162               "--help: display this help\\n"
163               "--verbose: print the name for each running test\\n"
164               "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
165               "--tests=selection: Use argument to select which suites/units/tests to run\\n"
166               "                   --tests can be used more than once, and selection may be a comma\\n"
167               "                   separated list of directives.\\n\\n"
168               "Directives are of the form:\\n"
169               "   [-]suitename[:unitname]\\n\\n"
170               "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
171               "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
172               "it applies on any unit.\\n\\n"
173               "By default, everything is enabled.\\n\\n"
174               "'all' as suite name apply to all suites.\\n\\n"
175               "Example 1: \\"-toto,+toto:tutu\\"\\n"
176               "  disables the whole toto testsuite (any unit in it),\\n"
177               "  then reenables the tutu unit of the toto test suite.\\n\\n"
178               "Example 2: \\"-all,+toto\\"\\n"
179               "  Run nothing but the toto suite.\\n");
180           return 0;
181       } else {
182         printf("testall: Unknown option: %s\\n",argv[i]);
183         return 1;
184       }
185     }
186   /* Got all my tests to do */
187       
188   res = xbt_test_run(selection, verbosity);
189   xbt_test_exit();
190   return res;
191 }
192 EOF
193         print OUT $GENERATED;
194         close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
195     }
196
197    print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
198    map {
199        my ($name,$func,$title) = @{$_};
200        print "    unit $name: func=$func; title=$title\n";
201    } @tests;
202
203    #while (my $t = shift @tests) {
204
205    # add this suite to the main
206    my $newmain="";
207    open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
208     # search prototypes
209        while (<IN>) {
210            $newmain .= $_;
211            #    print "Look for proto: $_";
212            last if /SGU: BEGIN PROTOTYPES/;
213        }
214
215        # search my prototype
216        while (<IN>) {
217            #    print "Seek protos: $_";
218            last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
219            $newmain .= $_;
220        }
221        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
222            while (<IN>) {
223                last if /SGU: END FILE/;
224            }
225            $_ = <IN>; # pass extra blank line
226            chomp;
227            die "this line should be blank ($_). Did you edit the file?" if /\W/;
228        }
229        my ($old_)=($_);
230        # add my section
231        $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
232        map {
233            my ($name,$func,$title) = @{$_};
234            $newmain .=  "    void $func(void);\n"
235        } @tests;
236        
237        $newmain .= "  /* SGU: END FILE */\n\n";
238        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
239            $newmain .= $old_;
240        }
241        
242        # pass remaining prototypes, search declarations
243        while (<IN>) {
244            $newmain .= $_ unless /SGU: END PROTOTYPES/;
245            last if /SGU: BEGIN SUITES DECLARATION/;
246        }
247        
248        ### Done with prototypes. And now, the actual code
249        
250        # search my prototype
251        while (<IN>) {
252            last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
253            $newmain .= $_;
254        }
255        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
256            while (<IN>) {
257                last if /SGU: END FILE/;
258            }
259            $_ = <IN>; # pass extra blank line
260            chomp;
261            die "this line should be blank ($_). Did you edit the file?" if /\W/;
262        }
263        my ($old_)=($_);
264        # add my section
265        $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
266        $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
267        map {
268            my ($name,$func,$title) = @{$_};
269            $newmain .=  "      xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
270        } @tests;
271        
272        $newmain .= "    /* SGU: END FILE */\n\n";
273        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
274            $newmain .= $old_;
275        }
276        
277        # pass the remaining 
278        while (<IN>) {
279            $newmain .= $_;
280        }
281        close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
282        
283        # write it back to main
284        open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
285        print OUT $newmain;
286        close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
287 } # end if process_one($)
288
289 0;