Logo AND Algorithmique Numérique Distribuée

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