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