Logo AND Algorithmique Numérique Distribuée

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