Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / tools / sg_unit_extractor.pl
1 #! /usr/bin/env perl
2
3 # Copyright (c) 2005-2012, 2014-2017. 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 "#define STRLEN 1024\n";
130         print OUT "/* SGU: BEGIN PROTOTYPES */\n";
131         print OUT "/* SGU: END PROTOTYPES */\n\n";
132         print OUT $GENERATED;
133         #  print OUT "# 93 \"sg_unit_extractor.pl\"\n";
134         print OUT <<EOF;
135 int main(int argc, char *argv[]) {
136   xbt_test_suite_t suite; 
137   char selection[STRLEN];
138   int verbosity = 0;
139   int i;
140   int res;
141
142   /* SGU: BEGIN SUITES DECLARATION */
143   /* SGU: END SUITES DECLARATION */
144       
145   xbt_init(&argc,argv);
146     
147   /* Search for the tests to do */
148     selection[0]='\\0';
149     for (i=1;i<argc;i++) {
150       if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
151         char *p=strchr(argv[i],'=')+1;
152         if (selection[0] != '\\0')
153           strncat(selection, \",\", STRLEN - 1 - strlen(selection));
154         strncat(selection, p, STRLEN - 1 - strlen(selection));
155       } else if (!strcmp(argv[i], \"--verbose\")) {
156         verbosity++;
157       } else if (!strcmp(argv[i], \"--dump-only\")||
158                  !strcmp(argv[i], \"--dump\")) {
159         xbt_test_dump(selection);
160         return 0;
161       } else if (!strcmp(argv[i], \"--help\")) {
162           printf(
163               "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
164               "--help: display this help\\n"
165               "--verbose: print the name for each running test\\n"
166               "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
167               "--tests=selection: Use argument to select which suites/units/tests to run\\n"
168               "                   --tests can be used more than once, and selection may be a comma\\n"
169               "                   separated list of directives.\\n\\n"
170               "Directives are of the form:\\n"
171               "   [-]suitename[:unitname]\\n\\n"
172               "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
173               "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
174               "it applies on any unit.\\n\\n"
175               "By default, everything is enabled.\\n\\n"
176               "'all' as suite name apply to all suites.\\n\\n"
177               "Example 1: \\"-toto,+toto:tutu\\"\\n"
178               "  disables the whole toto testsuite (any unit in it),\\n"
179               "  then reenables the tutu unit of the toto test suite.\\n\\n"
180               "Example 2: \\"-all,+toto\\"\\n"
181               "  Run nothing but the toto suite.\\n");
182           return 0;
183       } else {
184         printf("testall: Unknown option: %s\\n",argv[i]);
185         return 1;
186       }
187     }
188   /* Got all my tests to do */
189       
190   res = xbt_test_run(selection, verbosity);
191   xbt_test_exit();
192   return res;
193 }
194 EOF
195         print OUT $GENERATED;
196         close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
197     }
198
199    print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
200    map {
201        my ($name,$func,$title) = @{$_};
202        print "    unit $name: func=$func; title=$title\n";
203    } @tests;
204
205    #while (my $t = shift @tests) {
206
207    # add this suite to the main
208    my $newmain="";
209    open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
210     # search prototypes
211        while (<IN>) {
212            $newmain .= $_;
213            #    print "Look for proto: $_";
214            last if /SGU: BEGIN PROTOTYPES/;
215        }
216
217        # search my prototype
218        while (<IN>) {
219            #    print "Seek protos: $_";
220            last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
221            $newmain .= $_;
222        }
223        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
224            while (<IN>) {
225                last if /SGU: END FILE/;
226            }
227            $_ = <IN>; # pass extra blank line
228            chomp;
229            die "this line should be blank ($_). Did you edit the file?" if /\W/;
230        }
231        my ($old_)=($_);
232        # add my section
233        $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
234        map {
235            my ($name,$func,$title) = @{$_};
236            $newmain .=  "    void $func(void);\n"
237        } @tests;
238        
239        $newmain .= "  /* SGU: END FILE */\n\n";
240        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
241            $newmain .= $old_;
242        }
243        
244        # pass remaining prototypes, search declarations
245        while (<IN>) {
246            $newmain .= $_ unless /SGU: END PROTOTYPES/;
247            last if /SGU: BEGIN SUITES DECLARATION/;
248        }
249        
250        ### Done with prototypes. And now, the actual code
251        
252        # search my prototype
253        while (<IN>) {
254            last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
255            $newmain .= $_;
256        }
257        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
258            while (<IN>) {
259                last if /SGU: END FILE/;
260            }
261            $_ = <IN>; # pass extra blank line
262            chomp;
263            die "this line should be blank ($_). Did you edit the file?" if /\W/;
264        }
265        my ($old_)=($_);
266        # add my section
267        $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
268        $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
269        map {
270            my ($name,$func,$title) = @{$_};
271            $newmain .=  "      xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
272        } @tests;
273        
274        $newmain .= "    /* SGU: END FILE */\n\n";
275        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
276            $newmain .= $old_;
277        }
278        
279        # pass the remaining 
280        while (<IN>) {
281            $newmain .= $_;
282        }
283        close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
284        
285        # write it back to main
286        open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
287        print OUT $newmain;
288        close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
289 } # end if process_one($)
290
291 0;